-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcl.cpp
433 lines (341 loc) · 13 KB
/
dcl.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "dcl.h"
#include <arpa/inet.h>
#include <nan.h>
#include <unordered_map>
const char* const kInvalidArgumentCountErr = "Wrong number of arguments";
const char* const kInvalidArguments = "Invalid arguments";
struct Address {
Address(uint32_t addr, uint16_t port) : address(addr), port(port) {
snprintf(textAddress, sizeof(textAddress), "%u.%u.%u.%u", (addr & 0xFF000000) >> 24,
(addr & 0x00FF0000) >> 16, (addr & 0x0000FF00) >> 8, (addr & 0x000000FF));
}
uint32_t address;
uint16_t port;
char textAddress[16];
};
struct WuHost {
Wu* wu = nullptr;
};
class WuHostWrap : public Nan::ObjectWrap {
public:
static NAN_MODULE_INIT(Init);
explicit WuHostWrap(Wu* wu);
~WuHostWrap();
static NAN_METHOD(New);
static NAN_METHOD(ExchangeSDP);
static NAN_METHOD(SetUDPWriteFunction);
static NAN_METHOD(HandleUDP);
static NAN_METHOD(Serve);
static NAN_METHOD(SetClientJoinFunction);
static NAN_METHOD(SetClientLeaveFunction);
static NAN_METHOD(SetTextDataReceivedFunction);
static NAN_METHOD(SetBinaryDataReceivedFunction);
static NAN_METHOD(RemoveClient);
static NAN_METHOD(SendTextData);
static NAN_METHOD(SendBinaryData);
static Nan::Persistent<v8::Function> constructor;
std::unordered_map<uint32_t, WuClient*> clients;
uint32_t idCounter = 1;
WuHost host;
Nan::Callback udpWriteCallback;
Nan::Callback clientJoinCallback;
Nan::Callback clientLeaveCallback;
Nan::Callback textDataCallback;
Nan::Callback binaryDataCallback;
void HandleClientJoin(WuClient* client);
void HandleClientLeave(WuClient* client);
void HandleContent(const WuEvent* evt);
void RemoveClient(uint32_t id);
};
void WuHostWrap::HandleClientJoin(WuClient* client) {
uint32_t id = (uint32_t)(uintptr_t) WuClientGetUserData(client);
if (!id) {
id = idCounter++;
WuClientSetUserData(client, (void*) (uintptr_t) id);
clients[id] = client;
}
WuAddress address = WuClientGetAddress(client);
Address remote(address.host, address.port);
auto args = Nan::New<v8::Object>();
Nan::Set(args, Nan::New("address").ToLocalChecked(),
Nan::New(remote.textAddress).ToLocalChecked());
Nan::Set(args, Nan::New("port").ToLocalChecked(), Nan::New(remote.port));
Nan::Set(args, Nan::New("clientId").ToLocalChecked(), Nan::New(id));
const int argc = 1;
v8::Local<v8::Value> argv[argc] = {args};
Nan::AsyncResource ar("ClientJoin");
clientJoinCallback.Call(argc, argv, &ar);
}
void WuHostWrap::HandleClientLeave(WuClient* client) {
uintptr_t id = (uintptr_t) WuClientGetUserData(client);
WuAddress address = WuClientGetAddress(client);
Address remote(address.host, address.port);
auto args = Nan::New<v8::Object>();
Nan::Set(args, Nan::New("address").ToLocalChecked(),
Nan::New(remote.textAddress).ToLocalChecked());
Nan::Set(args, Nan::New("port").ToLocalChecked(), Nan::New(remote.port));
Nan::Set(args, Nan::New("clientId").ToLocalChecked(), Nan::New((uint32_t) id));
const int argc = 1;
v8::Local<v8::Value> argv[argc] = {args};
Nan::AsyncResource ar("ClientLeave");
clientLeaveCallback.Call(argc, argv, &ar);
WuRemoveClient(host.wu, client);
}
void WuHostWrap::HandleContent(const WuEvent* evt) {
uintptr_t id = (uintptr_t) WuClientGetUserData(evt->client);
WuAddress address = WuClientGetAddress(evt->client);
Address remote(address.host, address.port);
auto args = Nan::New<v8::Object>();
Nan::Set(args, Nan::New("address").ToLocalChecked(),
Nan::New(remote.textAddress).ToLocalChecked());
Nan::Set(args, Nan::New("port").ToLocalChecked(), Nan::New(remote.port));
Nan::Set(args, Nan::New("clientId").ToLocalChecked(), Nan::New((uint32_t) id));
const int argc = 1;
if (evt->type == WuEvent_TextData) {
Nan::Set(args, Nan::New("text").ToLocalChecked(),
Nan::New((const char*) evt->data, evt->length).ToLocalChecked());
v8::Local<v8::Value> argv[argc] = {args};
Nan::AsyncResource ar("TextData");
textDataCallback.Call(argc, argv, &ar);
} else if (evt->type == WuEvent_BinaryData) {
auto buf = Nan::CopyBuffer((const char*) evt->data, (uint32_t) evt->length).ToLocalChecked();
Nan::Set(args, Nan::New("data").ToLocalChecked(), buf);
v8::Local<v8::Value> argv[argc] = {args};
Nan::AsyncResource ar("BinaryData");
binaryDataCallback.Call(argc, argv, &ar);
}
}
void WriteUDPData(const uint8_t* data, size_t length, const WuClient* client, void* userData) {
WuHostWrap* wrap = (WuHostWrap*) userData;
WuAddress address = WuClientGetAddress(client);
Address remote(address.host, address.port);
auto buf = Nan::CopyBuffer((const char*) data, (uint32_t) length).ToLocalChecked();
auto addr = Nan::New<v8::Object>();
Nan::Set(addr, Nan::New("address").ToLocalChecked(),
Nan::New(remote.textAddress).ToLocalChecked());
Nan::Set(addr, Nan::New("port").ToLocalChecked(), Nan::New(remote.port));
const int argc = 2;
v8::Local<v8::Value> argv[argc] = {buf, addr};
Nan::AsyncResource ar("UDPWrite");
wrap->udpWriteCallback.Call(argc, argv, &ar);
}
void WuHostWrap::RemoveClient(uint32_t id) {
if (clients.count(id) > 0) {
WuClient* client = clients[id];
clients.erase(id);
WuRemoveClient(host.wu, client);
}
}
Nan::Persistent<v8::Function> WuHostWrap::constructor;
NAN_MODULE_INIT(WuHostWrap::Init) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Host").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "exchangeSDP", ExchangeSDP);
Nan::SetPrototypeMethod(tpl, "setUDPWriteFunction", SetUDPWriteFunction);
Nan::SetPrototypeMethod(tpl, "handleUDP", HandleUDP);
Nan::SetPrototypeMethod(tpl, "serve", Serve);
Nan::SetPrototypeMethod(tpl, "onClientJoin", SetClientJoinFunction);
Nan::SetPrototypeMethod(tpl, "onClientLeave", SetClientLeaveFunction);
Nan::SetPrototypeMethod(tpl, "onTextData", SetTextDataReceivedFunction);
Nan::SetPrototypeMethod(tpl, "onBinaryData", SetBinaryDataReceivedFunction);
Nan::SetPrototypeMethod(tpl, "removeClient", RemoveClient);
Nan::SetPrototypeMethod(tpl, "sendText", SendTextData);
Nan::SetPrototypeMethod(tpl, "sendBinary", SendBinaryData);
constructor.Reset(Nan::GetFunction(tpl).ToLocalChecked());
Nan::Set(target, Nan::New("Host").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
}
WuHostWrap::WuHostWrap(Wu* wu) { host.wu = wu; }
WuHostWrap::~WuHostWrap() { free(host.wu); }
NAN_METHOD(WuHostWrap::New) {
if (info.Length() < 2) {
Nan::ThrowError(kInvalidArgumentCountErr);
return;
}
if (info.IsConstructCall()) {
int maxClients = 512;
if (info.Length() >= 3 && info[2]->IsObject()) {
Nan::MaybeLocal<v8::Object> maybeExtraArgs = Nan::To<v8::Object>(info[2]);
if (!maybeExtraArgs.IsEmpty()) {
v8::Local<v8::Object> extraArgs = maybeExtraArgs.ToLocalChecked();
v8::MaybeLocal<v8::Value> maybeMaxClientsVal =
Nan::Get(extraArgs, Nan::New("maxClients").ToLocalChecked());
if (!maybeMaxClientsVal.IsEmpty() && maybeMaxClientsVal.ToLocalChecked()->IsNumber()) {
Nan::Maybe<int32_t> maybeMaxClients =
Nan::To<int32_t>(maybeMaxClientsVal.ToLocalChecked());
if (maybeMaxClients.IsJust()) {
maxClients = maybeMaxClients.FromJust();
if (maxClients <= 0) {
maxClients = 1;
}
}
}
}
}
Nan::Utf8String host(info[0]);
Nan::Utf8String port(info[1]);
Wu* wu = nullptr;
int32_t status = WuCreate(*host, *port, maxClients, &wu);
if (status != WU_OK) {
Nan::ThrowError("Initialization error");
return;
}
WuHostWrap* obj = new WuHostWrap(wu);
WuSetUserData(wu, obj);
WuSetUDPWriteFunction(wu, WriteUDPData);
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
} else {
const int argc = 2;
v8::Local<v8::Value> argv[argc] = {info[0], info[1]};
v8::Local<v8::Function> cons = Nan::New(constructor);
info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
}
}
NAN_METHOD(WuHostWrap::ExchangeSDP) {
if (info.Length() < 1) {
Nan::ThrowError(kInvalidArgumentCountErr);
return;
}
WuHostWrap* obj = Nan::ObjectWrap::Unwrap<WuHostWrap>(info.This());
Wu* wu = obj->host.wu;
Nan::Utf8String sdp(info[0]);
const SDPResult res = WuExchangeSDP(wu, *sdp, sdp.length());
if (res.status == WuSDPStatus_Success) {
Nan::MaybeLocal<v8::String> responseSdp = Nan::New<v8::String>(res.sdp, res.sdpLength);
info.GetReturnValue().Set(responseSdp.ToLocalChecked());
return;
}
info.GetReturnValue().Set(Nan::Null());
}
NAN_METHOD(WuHostWrap::SetUDPWriteFunction) {
if (info.Length() < 1 || !info[0]->IsFunction()) {
Nan::ThrowError(kInvalidArguments);
return;
}
WuHostWrap* obj = Nan::ObjectWrap::Unwrap<WuHostWrap>(info.This());
obj->udpWriteCallback.Reset(Nan::To<v8::Function>(info[0]).ToLocalChecked());
}
NAN_METHOD(WuHostWrap::HandleUDP) {
if (info.Length() < 2) {
Nan::ThrowError(kInvalidArgumentCountErr);
return;
}
WuHostWrap* obj = Nan::ObjectWrap::Unwrap<WuHostWrap>(info.This());
Wu* wu = obj->host.wu;
auto ctx = info.GetIsolate()->GetCurrentContext();
auto buf = info[0]->ToObject(ctx).ToLocalChecked();
char* data = node::Buffer::Data(buf);
size_t length = node::Buffer::Length(buf);
auto ipObj = info[1]->ToObject(ctx).ToLocalChecked();
Nan::Utf8String ipVal(Nan::Get(ipObj, Nan::New("address").ToLocalChecked()).ToLocalChecked());
struct in_addr address;
inet_pton(AF_INET, *ipVal, &address);
uint32_t ip = ntohl(address.s_addr);
uint32_t port =
Nan::To<uint32_t>(Nan::Get(ipObj, Nan::New("port").ToLocalChecked()).ToLocalChecked())
.ToChecked();
WuAddress remote;
remote.host = ip;
remote.port = port;
WuHandleUDP(wu, &remote, (const uint8_t*) data, length);
WuHostWrap::Serve(info);
}
NAN_METHOD(WuHostWrap::Serve) {
WuHostWrap* obj = Nan::ObjectWrap::Unwrap<WuHostWrap>(info.This());
Wu* wu = obj->host.wu;
WuEvent evt;
while (WuUpdate(wu, &evt)) {
switch (evt.type) {
case WuEvent_ClientJoin: {
obj->HandleClientJoin(evt.client);
break;
}
case WuEvent_TextData:
case WuEvent_BinaryData: {
obj->HandleContent(&evt);
break;
}
case WuEvent_ClientLeave: {
obj->HandleClientLeave(evt.client);
break;
}
default:
break;
}
}
}
NAN_METHOD(WuHostWrap::SetClientJoinFunction) {
if (info.Length() < 1 || !info[0]->IsFunction()) {
Nan::ThrowError(kInvalidArguments);
return;
}
WuHostWrap* obj = Nan::ObjectWrap::Unwrap<WuHostWrap>(info.This());
obj->clientJoinCallback.Reset(Nan::To<v8::Function>(info[0]).ToLocalChecked());
}
NAN_METHOD(WuHostWrap::SetClientLeaveFunction) {
if (info.Length() < 1 || !info[0]->IsFunction()) {
Nan::ThrowError(kInvalidArguments);
return;
}
WuHostWrap* obj = Nan::ObjectWrap::Unwrap<WuHostWrap>(info.This());
obj->clientLeaveCallback.Reset(Nan::To<v8::Function>(info[0]).ToLocalChecked());
}
NAN_METHOD(WuHostWrap::SetBinaryDataReceivedFunction) {
if (info.Length() < 1 || !info[0]->IsFunction()) {
Nan::ThrowError(kInvalidArguments);
return;
}
WuHostWrap* obj = Nan::ObjectWrap::Unwrap<WuHostWrap>(info.This());
obj->binaryDataCallback.Reset(Nan::To<v8::Function>(info[0]).ToLocalChecked());
}
NAN_METHOD(WuHostWrap::SetTextDataReceivedFunction) {
if (info.Length() < 1 || !info[0]->IsFunction()) {
Nan::ThrowError(kInvalidArguments);
return;
}
WuHostWrap* obj = Nan::ObjectWrap::Unwrap<WuHostWrap>(info.This());
obj->textDataCallback.Reset(Nan::To<v8::Function>(info[0]).ToLocalChecked());
}
NAN_METHOD(WuHostWrap::RemoveClient) {
if (info.Length() < 1 || !info[0]->IsNumber()) {
Nan::ThrowError(kInvalidArguments);
return;
}
WuHostWrap* obj = Nan::ObjectWrap::Unwrap<WuHostWrap>(info.This());
uint32_t id = Nan::To<uint32_t>(info[0]).ToChecked();
obj->RemoveClient(id);
}
NAN_METHOD(WuHostWrap::SendTextData) {
if (info.Length() < 2 || !info[1]->IsString()) {
Nan::ThrowError(kInvalidArguments);
return;
}
uint32_t clientId = Nan::To<uint32_t>(info[0]).ToChecked();
WuHostWrap* obj = Nan::ObjectWrap::Unwrap<WuHostWrap>(info.This());
if (obj->clients.count(clientId) == 0) {
return;
}
WuClient* client = obj->clients[clientId];
Nan::Utf8String text(info[1]);
WuSendText(obj->host.wu, client, *text, text.length());
}
NAN_METHOD(WuHostWrap::SendBinaryData) {
if (info.Length() < 2) {
Nan::ThrowError(kInvalidArguments);
return;
}
uint32_t clientId = Nan::To<uint32_t>(info[0]).ToChecked();
WuHostWrap* obj = Nan::ObjectWrap::Unwrap<WuHostWrap>(info.This());
if (obj->clients.count(clientId) == 0) {
return;
}
WuClient* client = obj->clients[clientId];
auto ctx = info.GetIsolate()->GetCurrentContext();
auto buf = info[1]->ToObject(ctx).ToLocalChecked();
char* data = node::Buffer::Data(buf);
size_t length = node::Buffer::Length(buf);
WuSendBinary(obj->host.wu, client, (uint8_t*) data, length);
}
NAN_MODULE_INIT(Init) { WuHostWrap::Init(target); }
NODE_MODULE(dcl, Init);