-
Notifications
You must be signed in to change notification settings - Fork 2
/
getSocketHandleAddress.cc
35 lines (31 loc) · 1.09 KB
/
getSocketHandleAddress.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// hello.cc
#include <node.h>
#include "uv.h"
namespace getHandleAddress {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Number;
using v8::Value;
uv_handle_t* getTcpHandle(void *handleWrap) {
volatile char *memory = (volatile char *) handleWrap;
for (volatile uv_handle_t *tcpHandle = (volatile uv_handle_t *) memory; tcpHandle->type != UV_TCP
|| tcpHandle->data != handleWrap || tcpHandle->loop != uv_default_loop(); tcpHandle = (volatile uv_handle_t *) memory) {
memory++;
}
return (uv_handle_t *) memory;
}
void getAddress(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
void *handleWrap = args[0]->ToObject()->GetAlignedPointerFromInternalField(0);
uv_handle_t* handle = getTcpHandle(handleWrap);
uv_tcp_t* tcpHandle = (uv_tcp_t*)handle;
args.GetReturnValue().Set(Number::New(isolate, tcpHandle->socket));
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "getAddress", getAddress);
}
NODE_MODULE(addon, init)
}