-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
poller.cpp
166 lines (144 loc) · 4.93 KB
/
poller.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <napi.h>
#include <uv.h>
#include "./poller.h"
Poller::Poller (const Napi::CallbackInfo &info) : Napi::ObjectWrap<Poller>(info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "First argument must be an int").ThrowAsJavaScriptException();
return;
}
this->fd = info[0].As<Napi::Number>().Int32Value();
// callback
if (!info[1].IsFunction()) {
Napi::TypeError::New(env, "Second argument must be a function").ThrowAsJavaScriptException();
return;
}
this->callback = Napi::Persistent(info[1].As<Napi::Function>());
this->poll_handle = new uv_poll_t();
memset(this->poll_handle, 0, sizeof(uv_poll_t));
poll_handle->data = this;
int status = uv_poll_init(uv_default_loop(), poll_handle, fd);
if (0 != status) {
Napi::Error::New(env, uv_strerror(status)).ThrowAsJavaScriptException();
return;
}
uv_poll_init_success = true;
}
Poller::~Poller() {
// if we call uv_poll_stop after uv_poll_init failed we segfault
if (uv_poll_init_success) {
uv_poll_stop(poll_handle);
uv_unref(reinterpret_cast<uv_handle_t*> (poll_handle));
uv_close(reinterpret_cast<uv_handle_t*> (poll_handle), Poller::onClose);
} else {
delete poll_handle;
}
return;
}
void Poller::onClose(uv_handle_t* poll_handle) {
// fprintf(stdout, "~Poller is closed\n");
delete poll_handle;
}
// Events can be UV_READABLE | UV_WRITABLE | UV_DISCONNECT
void Poller::poll(Napi::Env env, int events) {
Napi::HandleScope scope(env);
// fprintf(stdout, "Poller:poll for %d\n", events);
this->events = this->events | events;
int status = uv_poll_start(this->poll_handle, events, Poller::onData);
if (0 != status) {
Napi::Error::New(env, uv_strerror(status)).ThrowAsJavaScriptException();
}
return;
}
void Poller::stop(Napi::Env env) {
Napi::HandleScope scope(env);
int status = uv_poll_stop(this->poll_handle);
if (0 != status) {
Napi::Error::New(env, uv_strerror(status)).ThrowAsJavaScriptException();
}
return;
}
int Poller::_stop() {
return uv_poll_stop(poll_handle);
}
void Poller::onData(uv_poll_t* handle, int status, int events) {
Poller* obj = static_cast<Poller*>(handle->data);
Napi::Env env = obj->Env();
Napi::HandleScope scope(env);
// if Error
if (0 != status) {
// fprintf(stdout, "OnData Error status=%s events=%d\n", uv_strerror(status), events);
obj->_stop(); // doesn't matter if this errors
obj->callback.Call({Napi::Error::New(env, uv_strerror(status)).Value(), env.Undefined()});
} else {
// fprintf(stdout, "OnData status=%d events=%d subscribed=%d\n", status, events, obj->events);
// remove triggered events from the poll
int newEvents = obj->events & ~events;
obj->poll(env, newEvents);
obj->callback.Call({env.Null(), Napi::Number::New(env, events)});
}
}
Napi::Object Poller::Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "Poller", {
StaticMethod<&Poller::New>("New"),
InstanceMethod<&Poller::poll>("poll"),
InstanceMethod<&Poller::stop>("stop"),
InstanceMethod<&Poller::destroy>("destroy"),
});
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
exports.Set("Poller", func);
env.SetInstanceData<Napi::FunctionReference>(constructor);
return exports;
}
Napi::Value Poller::New(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "fd must be an int").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Value fd = info[0];
if (!info[1].IsFunction()) {
Napi::TypeError::New(env, "cb must be a function").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Function callback = info[1].As<Napi::Function>();
Napi::FunctionReference* constructor = info.Env().GetInstanceData<Napi::FunctionReference>();
return constructor->New({fd, callback});
}
Napi::Value Poller::poll(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Poller* obj = this;
if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "events must be an int").ThrowAsJavaScriptException();
return env.Null();
}
int events = info[0].As<Napi::Number>().Int32Value();
obj->poll(env, events);
return env.Undefined();
}
Napi::Value Poller::stop(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
this->stop(env);
return env.Undefined();
}
Napi::Value Poller::destroy(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Poller* obj = this;
// TODO Fix destruction Segfault
obj->Reset();
// delete obj;
return env.Undefined();
}
inline Napi::FunctionReference & Poller::constructor() {
static Napi::FunctionReference my_constructor;
// TODO Check if required
// my_constructor.SuppressDestruct();
return my_constructor;
}