Detaching a native socket from the Boost.ASIO socket class

Is it possible to detach a native socket from the Boost.ASIO socket class? If so, how can this be done? I can not find anything obvious in the documentation.

As a brief overview of what I'm trying to accomplish: I have a class that makes a connection and performs some negotiations using Boost.ASIO, and then passes its own Windows SOCKET to success or 0 on failure.

If I am mistaken, the native socket will be closed and released when my boost :: asio :: basic_socket is destroyed.

+6
c ++ boost sockets boost-asio
source share
2 answers

Answering my own question.

Windows has the WSADuplicateSocket function, which can be used to duplicate the native socket. The main socket will remain open until all descriptors of this socket are released.

http://msdn.microsoft.com/en-us/library/ms741565(VS.85).aspx

+2
source share

For Mac OS X, follow these steps (for Linux, it’s easy to change, just pay attention to the idea):

  • Expand the socket in shared_ptr so that it does not close when switching to different routines and supports it (at least one link must always exist);
  • Get your own descriptor with socket.native();
  • Remove it from kqueue:

     struct kevent event; EV_SET(&event, descriptor, EVFILT_READ, EV_DELETE, 0, 0, 0); //or EVFILT_WRITE 
  • And make it block if necessary:

     fcntl(descriptor, F_SETFL, fcntl(descriptor, F_GETFL, 0) & ~O_NONBLOCK); 
0
source share

All Articles