-
-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathsocket.cc
1069 lines (876 loc) · 30.9 KB
/
socket.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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "./socket.h"
#include <array>
#include <cmath>
#include <cstdint>
#include <limits>
#include <unordered_set>
#include "./context.h"
#include "./incoming_msg.h"
#include "./module.h"
#include "./observer.h"
#include "util/arguments.h"
#include "util/async_scope.h"
#include "util/error.h"
#include "util/object.h"
#include "util/string_or_buffer.h"
#include "util/take.h"
#include "util/uvdelayed.h"
#include "util/uvwork.h"
namespace zmq {
/* The maximum number of sync I/O operations that are allowed before the I/O
methods will force the returned promise to be resolved in the next tick. */
[[maybe_unused]] auto constexpr max_sync_operations = 1U << 9U;
/* Ordinary static cast for all available numeric types. */
template <typename T>
T NumberCast(const Napi::Number& num) {
return static_cast<T>(num);
}
/* Specialization for uint64_t; check for out of bounds and warn on values
that cannot be represented accurately. TODO: Use native JS BigInt. */
template <>
uint64_t NumberCast<uint64_t>(const Napi::Number& num) {
auto value = num.DoubleValue();
if (std::nextafter(value, -0.0) < 0) {
return 0;
}
static constexpr auto max_safe_integer = static_cast<double>((1ULL << 53U) - 1);
if (value > max_safe_integer) {
Warn(num.Env(),
"Value is larger than Number.MAX_SAFE_INTEGER and may have been rounded "
"inaccurately.");
}
/* If the next representable value of the double is beyond the maximum
integer, then assume the maximum integer. */
static constexpr auto max_uint64_as_double =
static_cast<double>(std::numeric_limits<uint64_t>::max());
if (std::nextafter(value, std::numeric_limits<double>::infinity())
> max_uint64_as_double) {
return std::numeric_limits<uint64_t>::max();
}
return static_cast<uint64_t>(value);
}
struct AddressContext {
std::string address;
uint32_t error = 0;
explicit AddressContext(std::string&& address) : address(std::move(address)) {}
};
Socket::Socket(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<Socket>(info), async_context(Env(), "Socket"), poller(*this),
module(*static_cast<Module*>(info.Data())) {
Arg::Validator const args{
Arg::Required<Arg::Number>("Socket type must be a number"),
Arg::Optional<Arg::Object>("Options must be an object"),
};
if (args.ThrowIfInvalid(info)) {
return;
}
type = info[0].As<Napi::Number>().Int32Value();
if (info[1].IsObject()) {
auto options = info[1].As<Napi::Object>();
if (options.Has("context")) {
context_ref.Reset(options.Get("context").As<Napi::Object>(), 1);
options.Delete("context");
} else {
context_ref.Reset(module.GlobalContext.Value(), 1);
}
} else {
context_ref.Reset(module.GlobalContext.Value(), 1);
}
auto* context = Context::Unwrap(context_ref.Value());
if (Env().IsExceptionPending()) {
return;
}
socket = zmq_socket(context->context, type);
if (socket == nullptr) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return;
}
auto file_descriptor = uv_os_sock_t{};
const auto error = [this]() {
[[maybe_unused]] auto err = zmq_close(socket);
assert(err == 0);
socket = nullptr;
};
#ifdef ZMQ_THREAD_SAFE
{
int value = 0;
size_t length = sizeof(value);
if (zmq_getsockopt(socket, ZMQ_THREAD_SAFE, &value, &length) < 0) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
error();
}
thread_safe = (value != 0);
}
#endif
std::function<void()> finalize = nullptr;
/* Currently only some DRAFT sockets are threadsafe. */
if (thread_safe) {
#ifdef ZMQ_HAS_POLLABLE_THREAD_SAFE
/* Threadsafe sockets do not expose an FD we can integrate into the
event loop, so we have to construct one by creating a zmq_poller. */
auto* poll = zmq_poller_new();
if (poll == nullptr) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
error();
}
/* Callback to free the underlying poller. Move the poller to transfer
ownership after the constructor has completed. */
finalize = [poll]() mutable {
if (poll != nullptr) {
[[maybe_unused]] auto err = zmq_poller_destroy(&poll);
assert(err == 0);
poll = nullptr;
}
};
if (zmq_poller_add(poll, socket, nullptr, ZMQ_POLLIN | ZMQ_POLLOUT) < 0) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
finalize();
error();
}
if (zmq_poller_fd(poll, &file_descriptor) < 0) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
finalize();
error();
}
#else
/* A thread safe socket was requested, but there is no support for
retrieving a poller FD, so we cannot construct them. */
ErrnoException(Env(), EINVAL).ThrowAsJavaScriptException();
error();
#endif
} else {
size_t length = sizeof(file_descriptor);
if (zmq_getsockopt(socket, ZMQ_FD, &file_descriptor, &length) < 0) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
error();
}
}
if (poller.Initialize(Env(), file_descriptor, finalize) < 0) {
ErrnoException(Env(), errno).ThrowAsJavaScriptException();
error();
}
/* Initialization was successful, register the socket for cleanup. */
module.ObjectReaper.Add(this);
/* Sealing causes setting/getting invalid options to throw an error.
Otherwise they would fail silently, which is very confusing. */
Seal(info.This().As<Napi::Object>());
/* Set any options after the socket has been successfully created. */
if (info[1].IsObject()) {
Assign(info.This().As<Napi::Object>(), info[1].As<Napi::Object>());
}
}
Socket::~Socket() {
Close();
}
/* Define all socket options that should not trigger a warning when set on
a socket that is already bound/connected. */
void Socket::WarnUnlessImmediateOption(int32_t option) const {
static const std::unordered_set<int32_t> immediate = {
ZMQ_SUBSCRIBE,
ZMQ_UNSUBSCRIBE,
ZMQ_LINGER,
ZMQ_ROUTER_MANDATORY,
ZMQ_PROBE_ROUTER,
ZMQ_XPUB_VERBOSE,
ZMQ_REQ_CORRELATE,
ZMQ_REQ_RELAXED,
#ifdef ZMQ_ROUTER_HANDOVER
ZMQ_ROUTER_HANDOVER,
#endif
#ifdef ZMQ_XPUB_VERBOSER
ZMQ_XPUB_VERBOSER,
#endif
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
/* As of 4.2.0 these options can take effect after bind/connect. */
ZMQ_SNDHWM,
ZMQ_RCVHWM,
#endif
/* These take effect immediately due to our Node.js implementation. */
ZMQ_SNDTIMEO,
ZMQ_RCVTIMEO,
};
if (immediate.contains(option)) {
return;
}
if (endpoints == 0 && state == State::Open) {
return;
}
Warn(Env(), "Socket option will not take effect until next connect/bind.");
}
bool Socket::ValidateOpen() const {
switch (state) {
case State::Blocked:
ErrnoException(Env(), EBUSY, "Socket is blocked by a bind or unbind operation")
.ThrowAsJavaScriptException();
return false;
case State::Closed:
ErrnoException(Env(), EBADF).ThrowAsJavaScriptException();
return false;
default:
return true;
}
}
bool Socket::HasEvents(uint32_t requested_events) const {
uint32_t events = 0;
size_t events_size = sizeof(events);
while (zmq_getsockopt(socket, ZMQ_EVENTS, &events, &events_size) < 0) {
/* Ignore errors. */
if (zmq_errno() != EINTR) {
return false;
}
}
return (events & requested_events) != 0;
}
void Socket::Close() {
if (socket != nullptr) {
module.ObjectReaper.Remove(this);
Napi::HandleScope const scope(Env());
/* Clear endpoint count. */
endpoints = 0;
/* Stop all polling and release event handlers. */
poller.Close();
/* Close succeeds unless socket is invalid. */
[[maybe_unused]] auto err = zmq_close(socket);
assert(err == 0);
/* Release reference to context and observer. */
observer_ref.Reset();
context_ref.Reset();
state = State::Closed;
/* Reset pointer to avoid double close. */
socket = nullptr;
}
}
void Socket::Send(const Napi::Promise::Deferred& res, OutgoingMsg::Parts& parts) {
auto iter = parts.begin();
auto end = parts.end();
while (iter != end) {
auto& part = *iter;
iter++;
auto const flags = iter == end ? ZMQ_DONTWAIT : ZMQ_DONTWAIT | ZMQ_SNDMORE;
while (zmq_msg_send(part.get(), socket, flags) < 0) {
if (zmq_errno() != EINTR) {
res.Reject(ErrnoException(Env(), zmq_errno()).Value());
return;
}
}
}
res.Resolve(Env().Undefined());
}
void Socket::Receive(const Napi::Promise::Deferred& res) {
/* Return an array of message parts, or an array with a single message
followed by a metadata object. */
auto list = Napi::Array::New(Env(), 1);
uint32_t i_part = 0;
while (true) {
IncomingMsg part;
while (zmq_msg_recv(part.get(), socket, ZMQ_DONTWAIT) < 0) {
if (zmq_errno() != EINTR) {
res.Reject(ErrnoException(Env(), zmq_errno()).Value());
return;
}
}
list[i_part++] = part.IntoBuffer(Env());
#ifdef ZMQ_HAS_POLLABLE_THREAD_SAFE
switch (type) {
case ZMQ_SERVER: {
auto meta = Napi::Object::New(Env());
meta.Set("routingId", zmq_msg_routing_id(part.get()));
list[i_part++] = meta;
break;
}
case ZMQ_DISH: {
auto meta = Napi::Object::New(Env());
const auto* data = zmq_msg_group(part.get());
auto length = strnlen(data, ZMQ_GROUP_MAX_LENGTH);
meta.Set("group", Napi::Buffer<char>::Copy(Env(), data, length));
list[i_part++] = meta;
break;
}
}
#endif
if (zmq_msg_more(part.get()) == 0) {
break;
}
}
res.Resolve(list);
}
Napi::Value Socket::Bind(const Napi::CallbackInfo& info) {
Arg::Validator const args{
Arg::Required<Arg::String>("Address must be a string"),
};
if (args.ThrowIfInvalid(info)) {
return Env().Undefined();
}
if (!ValidateOpen()) {
return Env().Undefined();
}
state = Socket::State::Blocked;
auto res = Napi::Promise::Deferred::New(Env());
auto run_ctx = std::make_shared<AddressContext>(info[0].As<Napi::String>());
auto status = UvQueue(
Env(),
[this, run_ctx]() {
/* Don't access V8 internals here! Executed in worker thread. */
while (zmq_bind(socket, run_ctx->address.c_str()) < 0) {
if (zmq_errno() != EINTR) {
run_ctx->error = static_cast<uint32_t>(zmq_errno());
return;
}
}
},
[this, run_ctx, res]() {
AsyncScope const scope(Env(), async_context);
state = Socket::State::Open;
endpoints++;
if (request_close) {
Close();
}
if (run_ctx->error != 0) {
res.Reject(ErrnoException(
Env(), static_cast<int32_t>(run_ctx->error), run_ctx->address)
.Value());
return;
}
res.Resolve(Env().Undefined());
});
if (status < 0) {
ErrnoException(Env(), EBADF).ThrowAsJavaScriptException();
return Env().Undefined();
}
return res.Promise();
}
Napi::Value Socket::BindSync(const Napi::CallbackInfo& info) {
Arg::Validator const args{
Arg::Required<Arg::String>("Address must be a string"),
};
if (args.ThrowIfInvalid(info)) {
return Env().Undefined();
}
if (!ValidateOpen()) {
return Env().Undefined();
}
state = Socket::State::Blocked;
auto run_ctx = std::make_shared<AddressContext>(info[0].As<Napi::String>());
while (zmq_bind(socket, run_ctx->address.c_str()) < 0) {
if (zmq_errno() != EINTR) {
run_ctx->error = static_cast<uint32_t>(zmq_errno());
break;
}
}
state = Socket::State::Open;
endpoints++;
if (request_close) {
Close();
}
if (run_ctx->error != 0) {
ErrnoException(Env(), static_cast<int32_t>(run_ctx->error), run_ctx->address)
.ThrowAsJavaScriptException();
}
return Env().Undefined();
}
Napi::Value Socket::Unbind(const Napi::CallbackInfo& info) {
Arg::Validator const args{
Arg::Required<Arg::String>("Address must be a string"),
};
if (args.ThrowIfInvalid(info)) {
return Env().Undefined();
}
if (!ValidateOpen()) {
return Env().Undefined();
}
state = Socket::State::Blocked;
auto res = Napi::Promise::Deferred::New(Env());
auto run_ctx = std::make_shared<AddressContext>(info[0].As<Napi::String>());
auto status = UvQueue(
Env(),
[this, run_ctx]() {
/* Don't access V8 internals here! Executed in worker thread. */
while (zmq_unbind(socket, run_ctx->address.c_str()) < 0) {
if (zmq_errno() != EINTR) {
run_ctx->error = static_cast<uint32_t>(zmq_errno());
return;
}
}
},
[this, run_ctx, res]() {
AsyncScope const scope(Env(), async_context);
state = Socket::State::Open;
--endpoints;
if (request_close) {
Close();
}
if (run_ctx->error != 0) {
res.Reject(ErrnoException(
Env(), static_cast<int32_t>(run_ctx->error), run_ctx->address)
.Value());
return;
}
res.Resolve(Env().Undefined());
});
if (status < 0) {
ErrnoException(Env(), EBADF).ThrowAsJavaScriptException();
return Env().Undefined();
}
return res.Promise();
}
Napi::Value Socket::UnbindSync(const Napi::CallbackInfo& info) {
Arg::Validator const args{
Arg::Required<Arg::String>("Address must be a string"),
};
if (args.ThrowIfInvalid(info)) {
return Env().Undefined();
}
if (!ValidateOpen()) {
return Env().Undefined();
}
state = Socket::State::Blocked;
auto run_ctx = std::make_shared<AddressContext>(info[0].As<Napi::String>());
while (zmq_unbind(socket, run_ctx->address.c_str()) < 0) {
if (zmq_errno() != EINTR) {
run_ctx->error = static_cast<uint32_t>(zmq_errno());
break;
}
}
state = Socket::State::Open;
--endpoints;
if (request_close) {
Close();
}
if (run_ctx->error != 0) {
ErrnoException(Env(), static_cast<int32_t>(run_ctx->error), run_ctx->address)
.ThrowAsJavaScriptException();
}
return Env().Undefined();
}
void Socket::Connect(const Napi::CallbackInfo& info) {
Arg::Validator const args{
Arg::Required<Arg::String>("Address must be a string"),
};
if (args.ThrowIfInvalid(info)) {
return;
}
if (!ValidateOpen()) {
return;
}
std::string const address = info[0].As<Napi::String>();
if (zmq_connect(socket, address.c_str()) < 0) {
ErrnoException(Env(), zmq_errno(), address).ThrowAsJavaScriptException();
return;
}
endpoints++;
}
void Socket::Disconnect(const Napi::CallbackInfo& info) {
Arg::Validator const args{
Arg::Required<Arg::String>("Address must be a string"),
};
if (args.ThrowIfInvalid(info)) {
return;
}
if (!ValidateOpen()) {
return;
}
std::string const address = info[0].As<Napi::String>();
if (zmq_disconnect(socket, address.c_str()) < 0) {
ErrnoException(Env(), zmq_errno(), address).ThrowAsJavaScriptException();
return;
}
--endpoints;
}
void Socket::Close(const Napi::CallbackInfo& info) {
if (Arg::Validator{}.ThrowIfInvalid(info)) {
return;
}
/* We can't access the socket when it is blocked, delay closing. */
if (state == State::Blocked) {
request_close = true;
} else {
request_close = false;
Close();
}
}
Napi::Value Socket::Send(const Napi::CallbackInfo& info) {
#ifdef _MSC_VER
#pragma warning(disable : 4065)
#endif
switch (type) {
#ifdef ZMQ_HAS_POLLABLE_THREAD_SAFE
case ZMQ_SERVER:
case ZMQ_RADIO: {
Arg::Validator args{
Arg::Required<Arg::NotUndefined>("Message must be present"),
Arg::Required<Arg::Object>("Options must be an object"),
};
if (args.ThrowIfInvalid(info)) {
return Env().Undefined();
}
break;
}
#endif
default: {
Arg::Validator const args{
Arg::Required<Arg::NotUndefined>("Message must be present"),
};
if (args.ThrowIfInvalid(info)) {
return Env().Undefined();
}
}
}
if (!ValidateOpen()) {
return Env().Undefined();
}
if (poller.Writing()) {
ErrnoException(Env(), EBUSY,
"Socket is busy writing; only one send operation may be in progress "
"at any time")
.ThrowAsJavaScriptException();
return Env().Undefined();
}
OutgoingMsg::Parts parts(info[0], module);
#ifdef ZMQ_HAS_POLLABLE_THREAD_SAFE
switch (type) {
case ZMQ_SERVER: {
if (!parts.SetRoutingId(info[1].As<Napi::Object>().Get("routingId"))) {
return Env().Undefined();
}
break;
}
case ZMQ_RADIO: {
if (!parts.SetGroup(info[1].As<Napi::Object>().Get("group"))) {
return Env().Undefined();
}
break;
}
}
#endif
if (send_timeout == 0 || HasEvents(ZMQ_POLLOUT)) {
/* We can send on the socket immediately. This is a fast path. NOTE: We
must make sure to not keep returning synchronously resolved promises,
or we will starve the event loop. This can happen because ZMQ uses a
background I/O thread, which could mean that the Node.js process is
busy sending data to the I/O thread but is no longer able to respond
to other events. */
#ifdef ZMQ_NO_SYNC_RESOLVE
Warn(Env(), "Promise resolution by send() is delayed (ZMQ_NO_SYNC_RESOLVE).");
#else
if (send_timeout == 0 || sync_operations++ < max_sync_operations) {
auto res = Napi::Promise::Deferred::New(Env());
Send(res, parts);
/* This operation may have caused a state change, so we must also
update the poller state manually! */
poller.TriggerReadable();
return res.Promise();
}
#endif
/* We can send on the socket immediately, but we don't, in order to
avoid starving the event loop. Writes will be delayed. */
UvScheduleDelayed(Env(), [&]() {
poller.WritableCallback();
if (socket == nullptr) {
return;
}
poller.TriggerReadable();
});
} else {
poller.PollWritable(send_timeout);
}
return poller.WritePromise(std::move(parts));
}
Napi::Value Socket::Receive(const Napi::CallbackInfo& info) {
if (Arg::Validator{}.ThrowIfInvalid(info)) {
return Env().Undefined();
}
if (!ValidateOpen()) {
return Env().Undefined();
}
if (poller.Reading()) {
ErrnoException(Env(), EBUSY,
"Socket is busy reading; only one receive operation may be in "
"progress at any time")
.ThrowAsJavaScriptException();
return Env().Undefined();
}
if (receive_timeout == 0 || HasEvents(ZMQ_POLLIN)) {
/* We can read from the socket immediately. This is a fast path.
Also see the related comments in Send(). */
#ifdef ZMQ_NO_SYNC_RESOLVE
Warn(Env(), "Promise resolution by receive() is delayed (ZMQ_NO_SYNC_RESOLVE).");
#else
if (receive_timeout == 0 || sync_operations++ < max_sync_operations) {
auto res = Napi::Promise::Deferred::New(Env());
Receive(res);
/* This operation may have caused a state change, so we must also
update the poller state manually! */
poller.TriggerWritable();
return res.Promise();
}
#endif
/* We can read from the socket immediately, but we don't, in order to
avoid starving the event loop. Reads will be delayed. */
UvScheduleDelayed(Env(), [&]() {
poller.ReadableCallback();
if (socket == nullptr) {
return;
}
poller.TriggerWritable();
});
} else {
poller.PollReadable(receive_timeout);
}
return poller.ReadPromise();
}
void Socket::Join([[maybe_unused]] const Napi::CallbackInfo& info) {
#ifdef ZMQ_HAS_POLLABLE_THREAD_SAFE
for (size_t i_value = 0; i_value < info.Length(); ++i_value) {
const auto& value = info[i_value];
this->JoinElement(value);
}
#endif
}
void Socket::JoinElement([[maybe_unused]] const Napi::Value& value) {
#ifdef ZMQ_HAS_POLLABLE_THREAD_SAFE
if (!ValidateOpen()) {
return;
}
const auto str = convert_string_or_buffer(value);
if (zmq_join(socket, str.c_str()) < 0) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return;
}
#endif
}
void Socket::Leave([[maybe_unused]] const Napi::CallbackInfo& info) {
#ifdef ZMQ_HAS_POLLABLE_THREAD_SAFE
for (size_t i_value = 0; i_value < info.Length(); ++i_value) {
const auto& value = info[i_value];
this->LeaveElement(value);
}
#endif
}
void Socket::LeaveElement([[maybe_unused]] const Napi::Value& value) {
#ifdef ZMQ_HAS_POLLABLE_THREAD_SAFE
if (!ValidateOpen()) {
return;
}
const auto str = convert_string_or_buffer(value);
if (zmq_leave(socket, str.c_str()) < 0) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return;
}
#endif
}
template <>
Napi::Value Socket::GetSockOpt<bool>(const Napi::CallbackInfo& info) {
Arg::Validator const args{
Arg::Required<Arg::Number>("Identifier must be a number"),
};
if (args.ThrowIfInvalid(info)) {
return Env().Undefined();
}
auto const option = info[0].As<Napi::Number>();
int32_t value = 0;
size_t length = sizeof(value);
if (zmq_getsockopt(socket, option, &value, &length) < 0) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return Env().Undefined();
}
return Napi::Boolean::New(Env(), value != 0);
}
template <>
void Socket::SetSockOpt<bool>(const Napi::CallbackInfo& info) {
Arg::Validator const args{
Arg::Required<Arg::Number>("Identifier must be a number"),
Arg::Required<Arg::Boolean>("Option value must be a boolean"),
};
if (args.ThrowIfInvalid(info)) {
return;
}
int32_t const option = info[0].As<Napi::Number>();
WarnUnlessImmediateOption(option);
int32_t value = static_cast<int32_t>(info[1].As<Napi::Boolean>());
if (zmq_setsockopt(socket, option, &value, sizeof(value)) < 0) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return;
}
}
template <>
Napi::Value Socket::GetSockOpt<char*>(const Napi::CallbackInfo& info) {
Arg::Validator const args{
Arg::Required<Arg::Number>("Identifier must be a number"),
};
if (args.ThrowIfInvalid(info)) {
return Env().Undefined();
}
auto const option = info[0].As<Napi::Number>();
static constexpr auto max_value_length = 1024; //+
auto value = std::array<char, max_value_length>(); //+
size_t length = value.size(); //+
if (zmq_getsockopt(socket, option, value.data(), &length) < 0) { //+
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return Env().Undefined();
}
if (length == 0 || (length == 1 && value[0] == 0)) {
return Env().Null();
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
value[length] = '\0';
return Napi::String::New(Env(), value.data());
}
template <>
void Socket::SetSockOpt<char*>(const Napi::CallbackInfo& info) {
Arg::Validator const args{
Arg::Required<Arg::Number>("Identifier must be a number"),
Arg::Required<Arg::String, Arg::Buffer, Arg::Null>(
"Option value must be a string or buffer"),
};
if (args.ThrowIfInvalid(info)) {
return;
}
int32_t const option = info[0].As<Napi::Number>();
WarnUnlessImmediateOption(option);
int32_t err = 0;
if (info[1].IsBuffer()) {
auto const buf = info[1].As<Napi::Object>();
auto length = buf.As<Napi::Buffer<char>>().Length();
auto* value = buf.As<Napi::Buffer<char>>().Data();
err = zmq_setsockopt(socket, option, value, length);
} else if (info[1].IsString()) {
std::string str = info[1].As<Napi::String>();
auto length = str.length();
auto* value = str.data();
err = zmq_setsockopt(socket, option, value, length);
} else {
auto length = 0U;
auto value = nullptr;
err = zmq_setsockopt(socket, option, value, length);
}
if (err < 0) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return;
}
}
template <typename T>
Napi::Value Socket::GetSockOpt(const Napi::CallbackInfo& info) {
Arg::Validator const args{
Arg::Required<Arg::Number>("Identifier must be a number"),
};
if (args.ThrowIfInvalid(info)) {
return Env().Undefined();
}
auto const option = info[0].As<Napi::Number>();
T value = 0;
size_t length = sizeof(value);
if (zmq_getsockopt(socket, option, &value, &length) < 0) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return Env().Undefined();
}
return Napi::Number::New(Env(), static_cast<double>(value));
}
template <typename T>
void Socket::SetSockOpt(const Napi::CallbackInfo& info) {
Arg::Validator const args{
Arg::Required<Arg::Number>("Identifier must be a number"),
Arg::Required<Arg::Number>("Option value must be a number"),
};
if (args.ThrowIfInvalid(info)) {
return;
}
int32_t const option = info[0].As<Napi::Number>();
WarnUnlessImmediateOption(option);
T value = NumberCast<T>(info[1].As<Napi::Number>());
if (zmq_setsockopt(socket, option, &value, sizeof(value)) < 0) {
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
return;
}
/* Mirror a few options that are used internally. */
switch (option) {
case ZMQ_SNDTIMEO:
send_timeout = static_cast<int64_t>(value);
break;
case ZMQ_RCVTIMEO:
receive_timeout = static_cast<int64_t>(value);
break;
default:
break;
}
}
Napi::Value Socket::GetEvents(const Napi::CallbackInfo& /*info*/) {
/* Reuse the same observer object every time it is accessed. */
if (observer_ref.IsEmpty()) {
observer_ref.Reset(module.Observer.New({Value()}), 1);
}
return observer_ref.Value();
}
Napi::Value Socket::GetContext(const Napi::CallbackInfo& /*info*/) {
return context_ref.Value();
}
Napi::Value Socket::GetClosed(const Napi::CallbackInfo& /*info*/) {
return Napi::Boolean::New(Env(), state == State::Closed);
}
Napi::Value Socket::GetReadable(const Napi::CallbackInfo& /*info*/) {
return Napi::Boolean::New(Env(), HasEvents(ZMQ_POLLIN));
}
Napi::Value Socket::GetWritable(const Napi::CallbackInfo& /*info*/) {
return Napi::Boolean::New(Env(), HasEvents(ZMQ_POLLOUT));
}
void Socket::Initialize(Module& module, Napi::Object& exports) {
auto proto = {
InstanceMethod<&Socket::Bind>("bind"),
InstanceMethod<&Socket::Unbind>("unbind"),
InstanceMethod<&Socket::BindSync>("bindSync"),
InstanceMethod<&Socket::BindSync>("unbindSync"),