www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14471] New: std.socket: add method for detach socket handle


          Issue ID: 14471
           Summary: std.socket: add method for detach socket handle
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: kozzi11 gmail.com

Current Socket implementation has method handle which return underlying socket
and this can be use to recreate Socket with this(socket_t sock, AddressFamily
af) constructor.

This is quiet usefull when you need accept connection in one thread and
process(send data) in another.

However there is a problem with GC, which close the underlying socket.

while(true) {
    if(Socket.select(...) {
        auto oClientSocket = oSocket.accept();
        auto tid = spawn(&processData, oClientSocket.handle());
            send(tid, ...);

void processData(socket_t handle) {
    auto oSocket = new Socket(handle, AddressFamily.INET);
    /// processing some data
    oSocket.send(aMsg); // this sometimes does not work, becase socket is close
from main thread

One workaround is put oClientSocket to some array, or disable GC. But this will
lead to bigger memory consumption. Another way is extend Socket class and
override close method to do nothing (this is what I use now).

But it would be fine if there will be some detach method on std.socket.Socket,
which will return underlying socket (same as handle method) and unset socket
hande in Socket object, so collection will not close original socket handle.

Apr 20 2015