Posted to tcl by colin at Mon Apr 23 07:40:20 GMT 2012view raw

  1. An ensemble command [udp] which provides the following subcommands:
  2.  
  3. set udp [udp create $port ?$addr? $command-prefix]
  4.  
  5. [udp create] creates a udp socket and return a unique udp identifier that may be used in future [udp] subcommand invocations.
  6.  
  7. port is the (mandatory) local port from which datagrams will be sent, and upon which datagrams will be received.
  8.  
  9. addr is the (optional) address to which this datagram will be bound. If not specified, the datagram will be bound to all addresses on the local host. The udp socket created will accept datagrams addressed to any address to which it is bound.
  10.  
  11. command-prefix is a (mandatory) list which will be evaluated in global scope as a proper command prefix.
  12.  
  13. Before evaluation, the following elements will be appended to command-prefix:
  14.  
  15. datagram - the received datagram
  16. remote-address - the remote address from which the datagram was sent
  17. remote-port - the remote port from which the datagram was sent
  18.  
  19. unless an error occurs in the reception of the datagram, in which case the command-prefix will have one or two elements appended to it, the last such element being a string describing the error.
  20.  
  21. udp configure $udp [-mcastadd group] [-mcastdrop group] [-broadcast flag] [-ttl ttl]
  22.  
  23. [udp configure] configures the udp socket as follows:
  24.  
  25. -mcastadd group - adds the udp socket to the named multicast group
  26. -mcastdrop group - removes the udp socket from the named multicast group
  27. -broadcast flag - set the broadcast flag on this udp socket to the value given
  28. -ttl ttl - sets the TTL value for packets sent from this udp socket
  29.  
  30. udp send $udp $datagram $remote-host $remote-port - send the specified datagram to the remote host and port
  31.  
  32. udp close $udp - close the udp socket and destroy any resources associated with it

Comments

Posted by jenglish at Mon Apr 23 16:43:36 GMT 2012 [text] [code]

Looks reasonable. A few suggestions: The API is (almost) suitable for any datagram protocol, not just UDP. However, not all protocols use ($host, $port) pairs for addressing. Consider passing a single $endpoint parameter instead of separate $host and $port parameters. For UDP, $endpoint would be [list $host $port]; for other protocols it could be something different. On names: I submit that "-join" and "-leave" are better names than "-mcastadd" and "-mcastdrop". Constructor syntax: the description of the $port and $addr seems off to me; I don't think that's right.