www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - RAII implementation for Socket and Selector

reply Jose Armando Garcia <jsancio gmail.com> writes:
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
Jun 20 2011
next sibling parent jdrewsen <jdrewsen nospam.com> writes:
Den 20-06-2011 22:38, Jose Armando Garcia skrev:
 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

Isn't there a lot of overlap with std.socket? Could it be based on std.socket instead? Maybe by extending std.socket itself with the any missing features. /Jonas
Jun 23 2011
prev sibling parent Jose Armando Garcia <jsancio gmail.com> writes:
On Thu, Jun 23, 2011 at 5:13 PM, jdrewsen <jdrewsen nospam.com> wrote:
 Den 20-06-2011 22:38, Jose Armando Garcia skrev:
 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

Isn't there a lot of overlap with std.socket? Could it be based on std.socket instead? Maybe by extending std.socket itself with the any missing features. /Jonas

Yes, it is possible to fix some of the issues with std.socket without deprecating the API but you can't make it RAII without deprecation. Reference counting is the main point of my change. Actually, I don't currently have an intention to propose this as a replacement for std.socket. My current intention is to build an event driven framework for D similar to Twisted in Python. In the process we can move some of the basic functionality to Phobos.
Jun 23 2011