Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
June 20, 2011 [phobos] RAII implementation for Socket and Selector | ||||
---|---|---|---|---|
| ||||
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 |
June 21, 2011 [phobos] RAII implementation for Socket and Selector | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jose Armando Garcia | Hi Jose. Skim the code. - Your Address struct seems to be a Resolver. I think std.net.dns or std.net.resolver is better. - Will you provide Address representaion? -- e.g. https://bitbucket.org/repeatedly/scrap/src/11fbb4a95602/socket.d#cl-598 --- This is half implementation. Lacks some functions such as isBroadcast, etc... - How to define SSLSocket? - I think 'self' is bad name. It's a not clear. Later, I read Selector. Masahiro On Tue, 21 Jun 2011 05:38:10 +0900, Jose Armando Garcia <jsancio at gmail.com> wrote: > 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 > _______________________________________________ > phobos mailing list > phobos at puremagic.com > http://lists.puremagic.com/mailman/listinfo/phobos |
June 20, 2011 [phobos] RAII implementation for Socket and Selector | ||||
---|---|---|---|---|
| ||||
Posted in reply to Masahiro Nakagawa | On Mon, Jun 20, 2011 at 7:53 PM, Masahiro Nakagawa <repeatedly at gmail.com> wrote: > Hi Jose. > > Skim the code. > > - Your Address struct seems to be a Resolver. I think std.net.dns or > ?std.net.resolver is better. > - Will you provide Address representaion? > -- e.g. > https://bitbucket.org/repeatedly/scrap/src/11fbb4a95602/socket.d#cl-598 > --- This is half implementation. Lacks some functions such as isBroadcast, It is my understanding that getaddrinfo should do that without contacting a DNS. For example: Address.lookup("111.111.111.111", "3000") should give you an IPv4 address without talking to a DNS. Or do you want to turn the binary representation of the address into an Address? > etc... > - How to define SSLSocket? If we can bundle openssl, then we should probably provide that. Need to think about the design. > - I think 'self' is bad name. It's a not clear. Any Suggestions? > > Later, I read Selector. Thanks. |
June 22, 2011 [phobos] RAII implementation for Socket and Selector | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jose Armando Garcia | On 6/21/11 1:24 AM, Jose Armando Garcia wrote:
>> - How to define SSLSocket?
>
> If we can bundle openssl, then we should probably provide that. Need to think about the design.
Just a short note, more to follow soon: Be sure to provide some way of accessing the underlying socket handle ? you need it when interfacing with some C libraries, like OpenSSL.
David
|
June 22, 2011 [phobos] RAII implementation for Socket and Selector | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | On Tue, Jun 21, 2011 at 10:07 PM, David Nadlinger <code at klickverbot.at> wrote:
> On 6/21/11 1:24 AM, Jose Armando Garcia wrote:
>>>
>>> - How to define SSLSocket?
>>
>> If we can bundle openssl, then we should probably provide that. Need to think about the design.
>
> Just a short note, more to follow soon: Be sure to provide some way of accessing the underlying socket handle ? you need it when interfacing with some C libraries, like OpenSSL.
>
You are right that we will probably wont have a choice but to expose the handle. I would like to avoid that because if we expose the handle it is not safe to close the handle once the ref count goes to zero. I am thinking of instead returning a duplicate handle. I think that should work really nicely.
I am hopping to also implement an SSLSocket (not sure exactly how the API will work). It wont remove the need to expose the handle but it should reduce the need for the user to have to manage the exposed duplicate handle.
Thanks,
-Jose
|
June 22, 2011 [phobos] RAII implementation for Socket and Selector | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | Just a note. Different algorithms bundled with OpenSSL have different licenses. Not all may be compatible with Phobos' Boost license.
Sent from my iPhone
On Jun 21, 2011, at 6:07 PM, David Nadlinger <code at klickverbot.at> wrote:
> On 6/21/11 1:24 AM, Jose Armando Garcia wrote:
>>> - How to define SSLSocket?
>>
>> If we can bundle openssl, then we should probably provide that. Need to think about the design.
>
> Just a short note, more to follow soon: Be sure to provide some way of accessing the underlying socket handle ? you need it when interfacing with some C libraries, like OpenSSL.
>
> David
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
|
Copyright © 1999-2021 by the D Language Foundation