On 02/24/2016 11:30 PM, Alex Hultman wrote: > Hi. > > I'm doing a wrapper which needs to take ownership of a socket (uv_tcp_t) > passed from Node.js to the native code. The native code uses uv_poll_t > to represent sockets and I first tried just getting the fd > (socket._handle.fd) and creating a new uv_poll_t but this would not work > since there would be two handles on the same fd. > > So I'm wondering, what would be the best way to move a uv_tcp_t into a > uv_poll_t? Is it possible to take the fd, close the handle and create a > new using uv_poll_t or can I simply use uv_tcp_t with the uv_poll_t > functions? > > Otherwise I'm planning to port the native library from using uv_poll_t > into using uv_tcp_t but that would not really be very optimal since the > library already handles polling and writing and such very nicely. Also, > the library exposes an API that resembles the underlying polling so it > would probably not be a good performance choice to use uv_tcp_t. > > In more detail, I'm being called from the http.Server 'upgrade' callback > and being passed an net.Socket and this socket needs to be shoved into > the native library to handle the upgrade, etc. >
Hi, Once you get fd belonging to a uv_tcp_t handle, you can duplicate it using dup(), close the net.Socket object, and use the duplicated fd with a uv_poll_t handle. You need to duplicate the fd because libuv doesn't support operating on the same fd with 2 different handles, and when the net.Socket is closed the underlying fd would be closed. Cheers, -- Saúl Ibarra Corretgé bettercallsaghul.com -- You received this message because you are subscribed to the Google Groups "libuv" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/libuv. For more options, visit https://groups.google.com/d/optout.
signature.asc
Description: OpenPGP digital signature
