Detaching a native socket from Boost.ASIO's socket class

后端 未结 2 1342
别跟我提以往
别跟我提以往 2021-01-18 07:23

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

As

2条回答
  •  爱一瞬间的悲伤
    2021-01-18 07:43

    For Mac OS X do the following (for Linux it isn't hard to modify, just notice the very idea):

    1. Wrap socket in a shared_ptr, so that it won't close when passing into different routines and keep it alive (at least one reference should always exist);
    2. Get a native descriptor with socket.native();
    3. Remove it from kqueue:

      struct kevent event;
      EV_SET(&event, descriptor, EVFILT_READ, EV_DELETE, 0, 0, 0);  //or EVFILT_WRITE
      
    4. And make it blocking if needed:

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

提交回复
热议问题