-
Notifications
You must be signed in to change notification settings - Fork 17
/
socket_watcher.cpp
144 lines (113 loc) · 3.86 KB
/
socket_watcher.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2012 Toby Ealden.
// Copyright (c) 2014 Martin Man.
// vim: ts=2 sw=2 et
#include "socket_watcher.hpp"
#include <string.h>
using namespace v8;
Nan::Persistent<Function> constructor;
SocketWatcher::SocketWatcher() : poll_(NULL), fd_(0), events_(0)
{
}
void SocketWatcher::Initialize(Local<Object> exports)
{
Nan::HandleScope scope;
Local<FunctionTemplate> t = Nan::New<FunctionTemplate>(New);
t->SetClassName(Nan::New("SocketWatcher").ToLocalChecked());
t->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(t, "set", SocketWatcher::Set);
Nan::SetPrototypeMethod(t, "start", SocketWatcher::Start);
Nan::SetPrototypeMethod(t, "stop", SocketWatcher::Stop);
constructor.Reset(t->GetFunction(Nan::GetCurrentContext()).ToLocalChecked());
Nan::Set(exports, Nan::New("SocketWatcher").ToLocalChecked(), t->GetFunction(Nan::GetCurrentContext()).ToLocalChecked());
}
void SocketWatcher::Start(const Nan::FunctionCallbackInfo<Value>& info)
{
SocketWatcher *watcher = Nan::ObjectWrap::Unwrap<SocketWatcher>(info.Holder());
watcher->StartInternal();
}
void SocketWatcher::StartInternal()
{
if (poll_ == NULL) {
poll_ = new uv_poll_t;
memset(poll_,0,sizeof(uv_poll_t));
poll_->data = this;
uv_poll_init_socket(uv_default_loop(), poll_, fd_);
Ref();
}
if (!uv_is_active((uv_handle_t*)poll_)) {
uv_poll_start(poll_, events_, &SocketWatcher::Callback);
}
}
void SocketWatcher::Callback(uv_poll_t *w, int status, int revents)
{
Nan::HandleScope scope;
SocketWatcher *watcher = static_cast<SocketWatcher*>(w->data);
assert(w == watcher->poll_);
Local<String> callback_symbol = Nan::New("callback").ToLocalChecked();
Local<Value> callback_v = Nan::Get(watcher->handle(), callback_symbol).ToLocalChecked();
if(!callback_v->IsFunction()) {
watcher->StopInternal();
return;
}
Local<Function> callback = Local<Function>::Cast(callback_v);
const unsigned argc = 2;
Local<Value> argv[argc] = {
revents & UV_READABLE ? Nan::True() : Nan::False(),
revents & UV_WRITABLE ? Nan::True() : Nan::False(),
};
Nan::AsyncResource ar("SocketWatcher:callback");
ar.runInAsyncScope(watcher->handle(), callback, argc, argv);
}
void SocketWatcher::Stop(const Nan::FunctionCallbackInfo<Value>& info)
{
SocketWatcher *watcher = Nan::ObjectWrap::Unwrap<SocketWatcher>(info.Holder());
watcher->StopInternal();
}
void SocketWatcher::StopInternal() {
if (poll_ != NULL && uv_is_active((uv_handle_t*)poll_)) {
uv_poll_stop(poll_);
Unref();
}
}
void SocketWatcher::New(const Nan::FunctionCallbackInfo<Value>& info)
{
Nan::HandleScope scope;
if (info.IsConstructCall()) {
// Invoked as constructor: `new SocketWatcher(...)`
SocketWatcher *s = new SocketWatcher();
s->Wrap(info.This());
info.GetReturnValue().Set(info.This());
} else {
// Invoked as plain function `SocketWatcher(...)`, turn into construct call.
Local<Function> cons = Nan::New<Function>(constructor);
info.GetReturnValue().Set(Nan::NewInstance(cons).ToLocalChecked());
}
}
void SocketWatcher::Set(const Nan::FunctionCallbackInfo<Value>& info)
{
SocketWatcher *watcher = Nan::ObjectWrap::Unwrap<SocketWatcher>(info.Holder());
if(!info[0]->IsInt32()) {
Nan::ThrowTypeError("First arg should be a file descriptor.");
return;
}
int fd = Nan::To<int32_t>(info[0]).FromJust();
if(!info[1]->IsBoolean()) {
Nan::ThrowTypeError("Second arg should a boolean (readable).");
return;
}
int events = 0;
if(info[1]->IsTrue()) events |= UV_READABLE;
if(!info[2]->IsBoolean()) {
Nan::ThrowTypeError("Third arg should a boolean (writable).");
return;
}
if (info[2]->IsTrue()) events |= UV_WRITABLE;
assert(watcher->poll_ == NULL);
watcher->fd_ = fd;
watcher->events_ = events;
}
void Init(Local<Object> exports)
{
SocketWatcher::Initialize(exports);
}
NODE_MODULE(socketwatcher, Init)