Friday, May 04, 2007

UDP support

About six months ago, Ivan Tikhonov contributed some code for UDP sockets. Unfortunately he lost interest in Factor and never finished his code, so it has been sitting in Lisppaste since then. I decided to dust it off, fix the bitrot, clean it up a bit, and add it to the core. This code implements UDP for all Unices except Solaris, where a definition of the SOCK_DGRAM constant must be provided in core/io/unix/syscalls-solaris.factor -- perhaps somebody can submit a patch for this one? Doug Coleman will deal with the Windows side, as usual.

It is worth noting that with this change, I have moved all networking words to a network vocabulary. I updated all contributed libraries and made sure they load.

The UDP words are very simple to use.

<datagram> ( port -- datagram ) creates a datagram socket bound to a specific port. Passing 0 binds to the first available port.

send ( packet host addr datagram -- ) sends a UDP packet. The packet must be given as a byte array.

receive ( datagram -- packet host addr ) waits to receive a UDP packet.

That is all. I've only tested them lightly, however Ivan's original code was known to work so I don't expect any major problems here.

Another contributor is currently working on support for Unix domain sockets and IPv6. This will round out Factor's networking capabilities quite nicely and put them on par with other languages.

No comments: