[phobos] RAII implementation for Socket and Selector

Jose Armando Garcia jsancio at gmail.com
Mon Jun 20 13:38:10 PDT 2011


Hi everyone,

For the past few of days I have been working on a RAII implementation
for Socket and Selector. Sockets are a ref counted wrapper around the
socket handle which closes the handle once the ref count goes to zero.
It provides safe methods for bind, connect, listen, accept, recv, send
and close. The module also provides helper methods for creating
sockets for a tcp server, tcp client, udp server and udp client. The
helper method used the Address struct which is basically wrapper
around getaddrinfo. As of right now the module provides support for
ipv4 and ipv6.

On top of Socket we have Selector which can be used to safely wait on
more than one socket. To register on a selector call the register
method in Socket. Use Socket.unregister to unregister the socket.
Sockets are automatically unregistered when closed. The current
implementation for selector only support epoll (sorry Windows and BSD
users) but I am highly confident that it can be ported to other
platforms. I plan to it at a future date but there are currently some
serious issues with DMD and druntime that invalidate the strong/weak
ref counting design.

It works well enough to pass the unittests but I had to do a lot of
hacks which I hope I can remove once DMD and druntime are fixed.

I should also mention that the design was influenced by Java's NIO and
Ruby's socket implementation.

Here is the code:
https://github.com/jsancio/phobos/blob/socket/std/net/socket.d. It
doesn't have any documentation right now.

I wont be able to work on it for the next couple of weeks but comments
are welcome.

Thanks!
-Jose


More information about the phobos mailing list