-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathbinding.cc
749 lines (587 loc) · 19.3 KB
/
binding.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#include <node_api.h>
#include <napi-macros.h>
#include <uv.h>
#include <stdlib.h>
#include <string.h>
#include "./deps/libutp/utp.h"
#define UTP_NAPI_TIMEOUT_INTERVAL 20
#define UTP_NAPI_THROW(err) \
{ \
napi_throw_error(env, uv_err_name(err), uv_strerror(err)); \
return NULL; \
}
#define NAPI_MAKE_CALLBACK_AND_ALLOC(env, nil, ctx, cb, n, argv, res, nread) \
if (napi_make_callback(env, nil, ctx, cb, n, argv, &res) == napi_pending_exception) { \
napi_value fatal_exception; \
napi_get_and_clear_last_exception(env, &fatal_exception); \
napi_fatal_exception(env, fatal_exception); \
{ \
UTP_NAPI_CALLBACK(self->realloc, { \
NAPI_MAKE_CALLBACK(env, nil, ctx, callback, 0, NULL, &res); \
UTP_NAPI_BUFFER_ALLOC(self, res, 0) \
}) \
} \
} else { \
UTP_NAPI_BUFFER_ALLOC(self, res, nread) \
}
#define UTP_NAPI_CALLBACK(fn, src) \
napi_env env = self->env; \
napi_handle_scope scope; \
napi_open_handle_scope(env, &scope); \
napi_value ctx; \
napi_get_reference_value(env, self->ctx, &ctx); \
napi_value callback; \
napi_get_reference_value(env, fn, &callback); \
src \
napi_close_handle_scope(env, scope);
#define UTP_NAPI_BUFFER_ALLOC(self, ret, nread) \
char *buf; \
size_t buf_len; \
napi_get_buffer_info(env, ret, (void **) &buf, &buf_len); \
if (buf_len == 0) { \
size_t size = nread <= 0 ? 0 : nread; \
self->buf.base += size; \
self->buf.len -= size; \
} else { \
self->buf.base = buf; \
self->buf.len = buf_len; \
}
typedef struct {
uint32_t min_recv_packet_size;
uint32_t recv_packet_size;
struct utp_iovec send_buffer[256];
struct utp_iovec *send_buffer_next;
uint32_t send_buffer_missing;
utp_socket *socket;
napi_env env;
napi_ref ctx;
uv_buf_t buf;
napi_ref on_read;
napi_ref on_drain;
napi_ref on_end;
napi_ref on_error;
napi_ref on_close;
napi_ref on_connect;
napi_ref realloc;
} utp_napi_connection_t;
typedef struct {
uv_udp_t handle;
utp_context *utp;
uint32_t accept_connections;
utp_napi_connection_t *next_connection;
uv_timer_t timer;
napi_env env;
napi_ref ctx;
uv_buf_t buf;
napi_ref on_message;
napi_ref on_send;
napi_ref on_connection;
napi_ref on_close;
napi_ref realloc;
int pending_close;
int closing;
} utp_napi_t;
typedef struct {
uv_udp_send_t req;
napi_ref ctx;
} utp_napi_send_request_t;
static void
on_sendto_free (uv_udp_send_t *req, int status) {
free(req);
}
static int
utp_napi_connection_drain (utp_napi_connection_t *self) {
struct utp_iovec *next = self->send_buffer_next;
uint32_t missing = self->send_buffer_missing;
if (!missing) return 1;
int sent_bytes = utp_writev(self->socket, next, missing);
if (sent_bytes < 0) {
UTP_NAPI_CALLBACK(self->on_error, {
napi_value argv[1];
napi_create_int32(env, sent_bytes, &(argv[0]));
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 1, argv, NULL)
})
return 0;
}
size_t bytes = sent_bytes;
while (bytes > 0) {
if (next->iov_len <= bytes) {
bytes -= next->iov_len;
next++;
missing--;
} else {
next->iov_len -= bytes;
next->iov_base = ((char *) next->iov_base) + bytes;
break;
}
}
self->send_buffer_missing = missing;
self->send_buffer_next = next;
return missing ? 0 : 1;
}
inline static void
utp_napi_parse_address (struct sockaddr *name, char *ip, int *port) {
struct sockaddr_in *name_in = (struct sockaddr_in *) name;
*port = ntohs(name_in->sin_port);
uv_ip4_name(name_in, ip, 17);
}
static void
on_uv_interval (uv_timer_t *req) {
utp_napi_t *self = (utp_napi_t *) req->data;
utp_issue_deferred_acks(self->utp);
utp_check_timeouts(self->utp);
}
static void
on_uv_alloc (uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
utp_napi_t *self = (utp_napi_t *) handle->data;
*buf = self->buf;
}
static void
on_uv_read (uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags) {
utp_napi_t *self = (utp_napi_t *) handle->data;
// TODO: is this overkill to call here?
// we do it because ucat.c does it
utp_check_timeouts(self->utp);
if (self->closing) return;
if (nread == 0) {
utp_issue_deferred_acks(self->utp);
return;
}
if (nread > 0) {
const unsigned char *base = (const unsigned char *) buf->base;
if (utp_process_udp(self->utp, base, nread, addr, sizeof(struct sockaddr))) return;
}
int port;
char ip[17];
utp_napi_parse_address((struct sockaddr *) addr, ip, &port);
UTP_NAPI_CALLBACK(self->on_message, {
napi_value ret;
napi_value argv[3];
napi_create_int32(env, nread, &(argv[0]));
napi_create_uint32(env, port, &(argv[1]));
napi_create_string_utf8(env, ip, NAPI_AUTO_LENGTH, &(argv[2]));
NAPI_MAKE_CALLBACK_AND_ALLOC(env, NULL, ctx, callback, 3, argv, ret, nread)
})
}
static void
on_uv_close (uv_handle_t *handle) {
utp_napi_t *self = (utp_napi_t *) handle->data;
self->pending_close--;
if (self->pending_close > 0) return;
UTP_NAPI_CALLBACK(self->on_close, {
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 0, NULL, NULL);
})
}
static void
on_uv_send (uv_udp_send_t *req, int status) {
uv_udp_t *handle = req->handle;
utp_napi_t *self = (utp_napi_t *) handle->data;
utp_napi_send_request_t *send = (utp_napi_send_request_t *) req->data;
UTP_NAPI_CALLBACK(self->on_send, {
napi_value argv[2];
napi_get_reference_value(env, send->ctx, &(argv[0]));
napi_create_int32(env, status, &(argv[1]));
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 2, argv, NULL);
})
}
static uint64
on_utp_firewall (utp_callback_arguments *a) {
utp_napi_t *self = (utp_napi_t *) utp_context_get_userdata(a->context);
return self->accept_connections ? 0 : 1;
}
inline static void
utp_napi_connection_destroy (utp_napi_connection_t *self) {
UTP_NAPI_CALLBACK(self->on_close, {
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 0, NULL, NULL)
})
self->env = env;
self->buf.base = NULL;
self->buf.len = 0;
napi_delete_reference(self->env, self->ctx);
napi_delete_reference(self->env, self->on_read);
napi_delete_reference(self->env, self->on_drain);
napi_delete_reference(self->env, self->on_end);
napi_delete_reference(self->env, self->on_error);
napi_delete_reference(self->env, self->on_close);
napi_delete_reference(self->env, self->realloc);
}
static uint64
on_utp_state_change (utp_callback_arguments *a) {
utp_napi_connection_t *self = (utp_napi_connection_t *) utp_get_userdata(a->socket);
switch (a->state) {
case UTP_STATE_CONNECT: {
UTP_NAPI_CALLBACK(self->on_connect, {
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 0, NULL, NULL)
})
break;
}
case UTP_STATE_WRITABLE: {
if (utp_napi_connection_drain(self)) {
UTP_NAPI_CALLBACK(self->on_drain, {
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 0, NULL, NULL)
})
}
break;
}
case UTP_STATE_EOF: {
if (self->recv_packet_size) {
UTP_NAPI_CALLBACK(self->on_read, {
napi_value ret;
napi_value argv[1];
napi_create_uint32(env, self->recv_packet_size, &(argv[0]));
NAPI_MAKE_CALLBACK_AND_ALLOC(env, NULL, ctx, callback, 1, argv, ret, self->recv_packet_size)
self->recv_packet_size = 0;
})
}
UTP_NAPI_CALLBACK(self->on_end, {
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 0, NULL, NULL)
})
break;
}
case UTP_STATE_DESTROYING: {
utp_napi_connection_destroy(self);
break;
}
default: {
printf("on_utp_statechange (unkown state: %i)\n", a->state);
break;
}
}
return 0;
}
static uint64
on_utp_accept (utp_callback_arguments *a) {
utp_napi_t *self = (utp_napi_t *) utp_context_get_userdata(a->context);
struct sockaddr addr;
socklen_t addr_len = sizeof(addr);
utp_getpeername(a->socket, &addr, &addr_len);
int port;
char ip[17];
utp_napi_parse_address(&addr, ip, &port);
self->next_connection->socket = a->socket;
utp_set_userdata(a->socket, self->next_connection);
UTP_NAPI_CALLBACK(self->on_connection, {
napi_value argv[2];
napi_create_uint32(env, port, &(argv[0]));
napi_create_string_utf8(env, ip, NAPI_AUTO_LENGTH, &(argv[1]));
napi_value next;
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 2, argv, &next) // will never throw due to the event being NTed in js
utp_napi_connection_t *connection;
size_t connection_size;
napi_get_buffer_info(env, next, (void **) &connection, &connection_size);
self->next_connection = connection;
})
return 0;
}
static uint64
on_utp_error (utp_callback_arguments *a) {
utp_napi_connection_t *self = (utp_napi_connection_t *) utp_get_userdata(a->socket);
UTP_NAPI_CALLBACK(self->on_error, {
napi_value argv[1];
napi_create_int32(env, a->error_code, &(argv[0]));
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 1, argv, NULL)
})
return 0;
}
static uint64
on_utp_read (utp_callback_arguments *a) {
utp_napi_connection_t *self = (utp_napi_connection_t *) utp_get_userdata(a->socket);
memcpy(self->buf.base + self->recv_packet_size, a->buf, a->len);
self->recv_packet_size += a->len;
if (self->recv_packet_size < self->min_recv_packet_size) {
return 0;
}
UTP_NAPI_CALLBACK(self->on_read, {
napi_value ret;
napi_value argv[1];
napi_create_uint32(env, self->recv_packet_size, &(argv[0]));
NAPI_MAKE_CALLBACK_AND_ALLOC(env, NULL, ctx, callback, 1, argv, ret, self->recv_packet_size)
self->recv_packet_size = 0;
})
return 0;
}
static uint64
on_utp_sendto (utp_callback_arguments *a) {
utp_napi_t *self = (utp_napi_t *) utp_context_get_userdata(a->context);
uv_buf_t buf = uv_buf_init((char *) a->buf, a->len);
if (uv_udp_try_send(&(self->handle), &buf, 1, a->address) >= 0) return 0;
char *cpy = (char *) malloc(sizeof(uv_udp_send_t) + a->len);
buf.base = cpy + sizeof(uv_udp_send_t);
memcpy(buf.base, a->buf, a->len);
uv_udp_send((uv_udp_send_t *) cpy, &(self->handle), &buf, 1, a->address, on_sendto_free);
return 0;
}
NAPI_METHOD(utp_napi_init) {
NAPI_ARGV(9)
NAPI_ARGV_BUFFER_CAST(utp_napi_t *, self, 0)
self->closing = 0;
self->pending_close = 2;
self->env = env;
napi_create_reference(env, argv[1], 1, &(self->ctx));
NAPI_ARGV_BUFFER_CAST(utp_napi_connection_t *, next, 2)
self->next_connection = next;
uv_timer_t *timer = &(self->timer);
timer->data = self;
struct uv_loop_s *loop;
napi_get_uv_event_loop(env, &loop);
int err = uv_timer_init(loop, timer);
if (err < 0) UTP_NAPI_THROW(err)
NAPI_ARGV_BUFFER(buf, 3)
self->buf.base = buf;
self->buf.len = buf_len;
uv_udp_t *handle = &(self->handle);
handle->data = self;
err = uv_udp_init(loop, handle);
if (err < 0) UTP_NAPI_THROW(err)
napi_create_reference(env, argv[4], 1, &(self->on_message));
napi_create_reference(env, argv[5], 1, &(self->on_send));
napi_create_reference(env, argv[6], 1, &(self->on_connection));
napi_create_reference(env, argv[7], 1, &(self->on_close));
napi_create_reference(env, argv[8], 1, &(self->realloc));
self->utp = utp_init(2);
utp_context_set_userdata(self->utp, self);
utp_set_callback(self->utp, UTP_ON_STATE_CHANGE, &on_utp_state_change);
utp_set_callback(self->utp, UTP_ON_READ, &on_utp_read);
utp_set_callback(self->utp, UTP_ON_FIREWALL, &on_utp_firewall);
utp_set_callback(self->utp, UTP_ON_ACCEPT, &on_utp_accept);
utp_set_callback(self->utp, UTP_SENDTO, &on_utp_sendto);
utp_set_callback(self->utp, UTP_ON_ERROR, &on_utp_error);
self->accept_connections = 0;
return NULL;
}
NAPI_METHOD(utp_napi_close) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(utp_napi_t *, self, 0)
self->closing = 1;
int err;
err = uv_timer_stop(&(self->timer));
if (err < 0) UTP_NAPI_THROW(err)
err = uv_udp_recv_stop(&(self->handle));
if (err < 0) UTP_NAPI_THROW(err)
uv_close((uv_handle_t *) &(self->handle), on_uv_close);
uv_close((uv_handle_t *) &(self->timer), on_uv_close);
return NULL;
}
NAPI_METHOD(utp_napi_destroy) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(utp_napi_t *, self, 0)
napi_value send_reqs = argv[1];
self->buf.base = NULL;
self->buf.len = 0;
napi_delete_reference(env, self->ctx);
napi_delete_reference(env, self->on_message);
napi_delete_reference(env, self->on_send);
napi_delete_reference(env, self->on_connection);
napi_delete_reference(env, self->on_close);
napi_delete_reference(env, self->realloc);
NAPI_FOR_EACH(send_reqs, el) {
NAPI_BUFFER_CAST(utp_napi_send_request_t *, send_req, el)
napi_delete_reference(env, send_req->ctx);
}
utp_destroy(self->utp);
self->utp = NULL;
return NULL;
}
NAPI_METHOD(utp_napi_bind) {
NAPI_ARGV(3)
NAPI_ARGV_BUFFER_CAST(utp_napi_t *, self, 0)
NAPI_ARGV_UINT32(port, 1)
NAPI_ARGV_UTF8(ip, 17, 2)
uv_udp_t *handle = &(self->handle);
int err;
struct sockaddr_in addr;
err = uv_ip4_addr((char *) &ip, port, &addr);
if (err < 0) UTP_NAPI_THROW(err)
err = uv_udp_bind(handle, (const struct sockaddr*) &addr, 0);
if (err < 0) UTP_NAPI_THROW(err)
// TODO: We should close the handle here also if this fails
err = uv_udp_recv_start(handle, on_uv_alloc, on_uv_read);
if (err < 0) UTP_NAPI_THROW(err)
// TODO: same as above
err = uv_timer_start(&(self->timer), on_uv_interval, UTP_NAPI_TIMEOUT_INTERVAL, UTP_NAPI_TIMEOUT_INTERVAL);
if (err < 0) UTP_NAPI_THROW(err)
uv_unref((uv_handle_t *) &(self->timer));
return NULL;
}
NAPI_METHOD(utp_napi_local_port) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(utp_napi_t *, self, 0)
int err;
struct sockaddr name;
int name_len = sizeof(name);
err = uv_udp_getsockname(&(self->handle), &name, &name_len);
if (err < 0) UTP_NAPI_THROW(err)
struct sockaddr_in *name_in = (struct sockaddr_in *) &name;
int port = ntohs(name_in->sin_port);
NAPI_RETURN_UINT32(port)
}
NAPI_METHOD(utp_napi_send_request_init) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(utp_napi_send_request_t *, send_req, 0)
uv_udp_send_t *req = &(send_req->req);
req->data = send_req;
napi_create_reference(env, argv[1], 1, &(send_req->ctx));
return NULL;
}
NAPI_METHOD(utp_napi_send) {
NAPI_ARGV(7)
NAPI_ARGV_BUFFER_CAST(utp_napi_t *, self, 0)
NAPI_ARGV_BUFFER_CAST(utp_napi_send_request_t *, send_req, 1)
NAPI_ARGV_BUFFER(buf, 2)
NAPI_ARGV_UINT32(offset, 3)
NAPI_ARGV_UINT32(len, 4)
NAPI_ARGV_UINT32(port, 5)
NAPI_ARGV_UTF8(ip, 17, 6)
uv_udp_send_t *req = &(send_req->req);
uv_buf_t bufs = {};
bufs.base = buf + offset;
bufs.len = len;
struct sockaddr_in addr;
int err;
err = uv_ip4_addr((char *) &ip, port, &addr);
if (err) UTP_NAPI_THROW(err)
err = uv_udp_send(req, &(self->handle), &bufs, 1, (const struct sockaddr *) &addr, on_uv_send);
if (err) UTP_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(utp_napi_ref) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(utp_napi_t *, self, 0)
uv_ref((uv_handle_t *) &(self->handle));
return NULL;
}
NAPI_METHOD(utp_napi_unref) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(utp_napi_t *, self, 0)
uv_unref((uv_handle_t *) &(self->handle));
return NULL;
}
NAPI_METHOD(utp_napi_recv_buffer) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(utp_napi_t *, self, 0)
NAPI_ARGV_INT32(size, 1)
int err;
NAPI_UV_THROWS(err, uv_recv_buffer_size((uv_handle_t *) &(self->handle), &size))
NAPI_RETURN_INT32(size)
}
NAPI_METHOD(utp_napi_send_buffer) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(utp_napi_t *, self, 0)
NAPI_ARGV_INT32(size, 1)
int err;
NAPI_UV_THROWS(err, uv_send_buffer_size((uv_handle_t *) &(self->handle), &size))
NAPI_RETURN_INT32(size)
}
NAPI_METHOD(utp_napi_set_ttl) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(utp_napi_t *, self, 0)
NAPI_ARGV_UINT32(ttl, 1)
int err;
NAPI_UV_THROWS(err, uv_udp_set_ttl(&(self->handle), ttl))
return NULL;
}
NAPI_METHOD(utp_napi_connection_init) {
NAPI_ARGV(10)
NAPI_ARGV_BUFFER_CAST(utp_napi_connection_t *, self, 0)
self->env = env;
napi_create_reference(env, argv[1], 1, &(self->ctx));
NAPI_ARGV_BUFFER(buf, 2)
self->buf.base = buf;
self->buf.len = buf_len;
napi_create_reference(env, argv[3], 1, &(self->on_read));
napi_create_reference(env, argv[4], 1, &(self->on_drain));
napi_create_reference(env, argv[5], 1, &(self->on_end));
napi_create_reference(env, argv[6], 1, &(self->on_error));
napi_create_reference(env, argv[7], 1, &(self->on_close));
napi_create_reference(env, argv[8], 1, &(self->on_connect));
napi_create_reference(env, argv[9], 1, &(self->realloc));
return NULL;
}
NAPI_METHOD(utp_napi_connection_on_close) {
// To trigger a manual teardown if connect was never called
// on a client connection
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(utp_napi_connection_t *, self, 0)
utp_napi_connection_destroy(self);
return NULL;
}
NAPI_METHOD(utp_napi_connection_write) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(utp_napi_connection_t *, self, 0)
NAPI_ARGV_BUFFER(buf, 1)
self->send_buffer_next = self->send_buffer;
self->send_buffer_next->iov_base = buf;
self->send_buffer_next->iov_len = buf_len;
self->send_buffer_missing = 1;
int drained = utp_napi_connection_drain(self);
NAPI_RETURN_UINT32(drained)
}
NAPI_METHOD(utp_napi_connection_writev) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(utp_napi_connection_t *, self, 0)
napi_value bufs = argv[1];
struct utp_iovec *next = self->send_buffer_next = self->send_buffer;
NAPI_FOR_EACH(bufs, el) {
NAPI_BUFFER(buf, el)
next->iov_base = buf;
next->iov_len = buf_len;
next++;
}
self->send_buffer_missing = bufs_len;
int drained = utp_napi_connection_drain(self);
NAPI_RETURN_UINT32(drained)
}
NAPI_METHOD(utp_napi_connection_shutdown) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(utp_napi_connection_t *, self, 0)
utp_shutdown(self->socket, SHUT_WR);
return NULL;
}
NAPI_METHOD(utp_napi_connection_close) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(utp_napi_connection_t *, self, 0)
utp_close(self->socket);
return NULL;
}
NAPI_METHOD(utp_napi_connect) {
NAPI_ARGV(4)
NAPI_ARGV_BUFFER_CAST(utp_napi_t *, self, 0)
NAPI_ARGV_BUFFER_CAST(utp_napi_connection_t *, conn, 1)
NAPI_ARGV_UINT32(port, 2)
NAPI_ARGV_UTF8(ip, 17, 3)
int err;
struct sockaddr_in addr;
// TODO: error handle
conn->socket = utp_create_socket(self->utp);
utp_set_userdata(conn->socket, conn);
err = uv_ip4_addr((char *) &ip, port, &addr);
if (err) UTP_NAPI_THROW(err)
// TODO: error handle
utp_connect(conn->socket, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
return NULL;
}
NAPI_INIT() {
NAPI_EXPORT_SIZEOF(utp_napi_t)
NAPI_EXPORT_SIZEOF(utp_napi_send_request_t)
NAPI_EXPORT_SIZEOF(utp_napi_connection_t)
NAPI_EXPORT_OFFSETOF(utp_napi_t, accept_connections)
NAPI_EXPORT_FUNCTION(utp_napi_init)
NAPI_EXPORT_FUNCTION(utp_napi_bind)
NAPI_EXPORT_FUNCTION(utp_napi_local_port)
NAPI_EXPORT_FUNCTION(utp_napi_send_request_init)
NAPI_EXPORT_FUNCTION(utp_napi_send)
NAPI_EXPORT_FUNCTION(utp_napi_close)
NAPI_EXPORT_FUNCTION(utp_napi_destroy)
NAPI_EXPORT_FUNCTION(utp_napi_ref)
NAPI_EXPORT_FUNCTION(utp_napi_unref)
NAPI_EXPORT_FUNCTION(utp_napi_set_ttl)
NAPI_EXPORT_FUNCTION(utp_napi_send_buffer)
NAPI_EXPORT_FUNCTION(utp_napi_recv_buffer)
NAPI_EXPORT_FUNCTION(utp_napi_connection_init)
NAPI_EXPORT_FUNCTION(utp_napi_connection_write)
NAPI_EXPORT_FUNCTION(utp_napi_connection_writev)
NAPI_EXPORT_FUNCTION(utp_napi_connection_close)
NAPI_EXPORT_FUNCTION(utp_napi_connection_shutdown)
NAPI_EXPORT_FUNCTION(utp_napi_connection_on_close)
NAPI_EXPORT_FUNCTION(utp_napi_connect)
}