Get fd or handle from socket object

I want to create a custom (C ++) module for node.js that can send sockets to another node process that is completely unrelated to the current process. For this I had to use a helper library, which is actually a very simple API. The problem I have to solve now is how can I get the fd or handle to the node.js socket object.

There is a TCPWrap class in

and tcp_wrap.h

, which has a property handle_

that contains an object uv_tcp_t

from libuv, but this property is private. Also I can't # include it because it's just a node.js module not directly in node.js. I don't know if it's worth copying the source files to my module to get this class ...

Do you have any ways how I could do this?

I don't need to run on winoline, hardcoded.



source to share

1 answer

I finally found a way to do it. The node module can be found here:

I just took the headers "tcp_wrap.h", "stream_wrap.h" and "handle_wrap.h" and then included "tcp_wrap.h".

I could get the object like this:

TCPWrap* wrap = static_cast<TCPWrap*>(args[0]->ToObject()->GetPointerFromInternalField(0));
StreamWrap* s = (StreamWrap*)wrap;


The following code then gives access to the file descriptor





All Articles