[Issue 14471] New: std.socket: add method for detach socket handle

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Apr 20 01:30:55 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=14471

          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 at puremagic.com
          Reporter: kozzi11 at 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.

--


More information about the Digitalmars-d-bugs mailing list