-
Notifications
You must be signed in to change notification settings - Fork 6
/
zmq.tcl
2406 lines (2331 loc) · 73.1 KB
/
zmq.tcl
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
package require critcl 3
namespace eval ::zmq {
}
critcl::license {Jos Decoster} {LGPLv3 / BSD}
critcl::summary {A Tcl wrapper for the ZeroMQ messaging library}
critcl::description {
zmq is a Tcl binding for the zeromq library (http://www.zeromq.org/)
for interprocess communication.
}
critcl::subject ZeroMQ ZMQ 0MQ ;#\u2205MQ
critcl::subject {messaging} {inter process communication} RPC
critcl::subject {message queue} {queue} broadcast communication
critcl::subject {producer - consumer} {publish - subscribe}
critcl::meta origin https://github.com/jdc8/tclzmq
critcl::userconfig define mode {choose mode of ZMQ to build and link against.} {static dynamic}
if {[string match "win32*" [::critcl::targetplatform]]} {
critcl::clibraries -llibzmq -luuid -lws2_32 -lcomctl32 -lrpcrt4
switch -exact -- [critcl::userconfig query mode] {
static {
critcl::cflags /DDLL_EXPORT
}
dynamic {
}
}
} else {
switch -exact -- [critcl::userconfig query mode] {
static {
critcl::clibraries -l:libzmq.a -lstdc++
}
dynamic {
critcl::clibraries -lzmq
}
}
critcl::clibraries -lpthread -lm
if {[string match "macosx*" [::critcl::targetplatform]]} {
critcl::clibraries -lgcc_eh
} elseif {[string match "*mingw32*" [::critcl::targetplatform]]} {
critcl::clibraries -luuid
} else {
critcl::clibraries -lrt -luuid
}
}
#critcl::cflags -ansi -pedantic -Wall
# Get local build configuration
if {[file exists "[file dirname [info script]]/zmq_config.tcl"]} {
set fd [open "[file dirname [info script]]/zmq_config.tcl"]
eval [read $fd]
close $fd
}
critcl::tcl 8.5
critcl::tsources zmq_helper.tcl
critcl::ccode {
#include "errno.h"
#include "string.h"
#include "stdio.h"
#include "zmq.h"
#ifndef _WIN32
#include "pthread.h"
#endif
#ifdef _MSC_VER
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h>
#endif
#ifndef ZMQ_HWM
#define ZMQ_HWM 1
#endif
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 1, 0)
/* Socket event data */
typedef struct {
uint16_t event; // id of the event as bitfield
int32_t value; // value is either error code, fd or reconnect interval
} zmq_event_t;
#endif
typedef struct {
Tcl_Interp* interp;
Tcl_HashTable* readableCommands;
Tcl_HashTable* writableCommands;
Tcl_HashTable* contextClientData;
Tcl_HashTable* socketClientData;
int block_time;
int id;
} ZmqClientData;
typedef struct {
void* context;
Tcl_Obj* tcl_cmd;
ZmqClientData* zmqClientData;
} ZmqContextClientData;
typedef struct {
void* context;
void* socket;
Tcl_Obj* tcl_cmd;
ZmqClientData* zmqClientData;
} ZmqSocketClientData;
typedef struct {
void* message;
Tcl_Obj* tcl_cmd;
ZmqClientData* zmqClientData;
} ZmqMessageClientData;
typedef struct {
Tcl_Event event; /* Must be first */
Tcl_Interp* ip;
Tcl_Obj* cmd;
} ZmqEvent;
static int last_zmq_errno = 0;
static void zmq_free_client_data(void* p) { ckfree(p); }
static void zmq_ckfree(void* p, void* h) { ckfree(p); }
static void* known_command(Tcl_Interp* ip, Tcl_Obj* obj, const char* what) {
Tcl_CmdInfo ci;
if (!Tcl_GetCommandInfo(ip, Tcl_GetStringFromObj(obj, 0), &ci)) {
Tcl_Obj* err;
err = Tcl_NewObj();
Tcl_AppendToObj(err, what, -1);
Tcl_AppendToObj(err, " \"", -1);
Tcl_AppendObjToObj(err, obj);
Tcl_AppendToObj(err, "\" does not exists", -1);
Tcl_SetObjResult(ip, err);
return 0;
}
return ci.objClientData;
}
static void* known_context(Tcl_Interp* ip, Tcl_Obj* obj)
{
void* p = known_command(ip, obj, "context");
if (p)
return ((ZmqContextClientData*)p)->context;
return 0;
}
static void* known_socket(Tcl_Interp* ip, Tcl_Obj* obj)
{
void* p = known_command(ip, obj, "socket");
if (p)
return ((ZmqSocketClientData*)p)->socket;
return 0;
}
static void* known_message(Tcl_Interp* ip, Tcl_Obj* obj)
{
void* p = known_command(ip, obj, "message");
if (p)
return ((ZmqMessageClientData*)p)->message;
return 0;
}
static const char* conames[] = { "IO_THREADS", "MAX_SOCKETS", NULL };
static const int conames_cget[] = { 1, 1 };
static int get_context_option(Tcl_Interp* ip, Tcl_Obj* obj, int* name)
{
enum ExObjCOptionNames { CON_IO_THREADS, CON_MAX_SOCKETS };
int index = -1;
if (Tcl_GetIndexFromObj(ip, obj, conames, "name", 0, &index) != TCL_OK)
return TCL_ERROR;
switch((enum ExObjCOptionNames)index) {
case CON_IO_THREADS: *name = ZMQ_IO_THREADS; break;
case CON_MAX_SOCKETS: *name = ZMQ_MAX_SOCKETS; break;
}
return TCL_OK;
}
static const char* monames[] = { "MORE", NULL };
static const int monames_cget[] = { 1 };
static int get_message_option(Tcl_Interp* ip, Tcl_Obj* obj, int* name)
{
enum ExObjMOptionNames { MSG_MORE };
int index = -1;
if (Tcl_GetIndexFromObj(ip, obj, monames, "name", 0, &index) != TCL_OK)
return TCL_ERROR;
switch((enum ExObjMOptionNames)index) {
case MSG_MORE: *name = ZMQ_MORE; break;
}
return TCL_OK;
}
static const char* sonames[] = { "HWM", "SNDHWM", "RCVHWM", "AFFINITY", "IDENTITY", "SUBSCRIBE", "UNSUBSCRIBE",
"RATE", "RECOVERY_IVL", "SNDBUF", "RCVBUF", "RCVMORE", "FD", "EVENTS",
"TYPE", "LINGER", "RECONNECT_IVL", "BACKLOG", "RECONNECT_IVL_MAX",
"MAXMSGSIZE", "MULTICAST_HOPS", "RCVTIMEO", "SNDTIMEO", "LAST_ENDPOINT",
"TCP_KEEPALIVE", "TCP_KEEPALIVE_CNT", "TCP_KEEPALIVE_IDLE",
"TCP_KEEPALIVE_INTVL", "TCP_ACCEPT_FILTER", "IMMEDIATE",
"ROUTER_MANDATORY", "XPUB_VERBOSE", "MECHANISM",
"PLAIN_SERVER", "PLAIN_USERNAME", "PLAIN_PASSWORD",
"CURVE_SERVER", "CURVE_PUBLICKEY", "CURVE_SECRETKEY", "CURVE_SERVERKEY",
"PROBE_ROUTER", "REQ_CORRELATE", "REQ_RELAXED", "CONFLATE", "ZAP_DOMAIN",
"IPV6", NULL };
static int sonames_cget[] = { 0, 1, 1, 1, 1, 0, 0,
1, 1, 1, 1, 1, 0, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1,
1, 0, 1,
0, 0, 1,
1, 1, 1,
2, 2, 2, 2,
0, 0, 0, 0, 1,
1, 0 };
static int get_socket_option(Tcl_Interp* ip, Tcl_Obj* obj, int* name)
{
enum ExObjOptionNames { ON_HWM, ON_SNDHWM, ON_RCVHWM, ON_AFFINITY, ON_IDENTITY, ON_SUBSCRIBE, ON_UNSUBSCRIBE,
ON_RATE, ON_RECOVERY_IVL, ON_SNDBUF, ON_RCVBUF, ON_RCVMORE, ON_FD, ON_EVENTS,
ON_TYPE, ON_LINGER, ON_RECONNECT_IVL, ON_BACKLOG, ON_RECONNECT_IVL_MAX,
ON_MAXMSGSIZE, ON_MULTICAST_HOPS, ON_RCVTIMEO, ON_SNDTIMEO, ON_LAST_ENDPOINT,
ON_TCP_KEEPALIVE, ON_TCP_KEEPALIVE_CNT, ON_TCP_KEEPALIVE_IDLE,
ON_TCP_KEEPALIVE_INTVL, ON_TCP_ACCEPT_FILTER, ON_IMMEDIATE,
ON_ROUTER_MANDATORY, ON_XPUB_VERBOSE, ON_MECHANISM,
ON_PLAIN_SERVER, ON_PLAIN_USERNAME, ON_PLAIN_PASSWORD,
ON_CURVE_SERVER, ON_CURVE_PUBLICKEY, ON_CURVE_SECRETKEY, ON_CURVE_SERVERKEY,
ON_PROBE_ROUTER, ON_REQ_CORRELATE, ON_REQ_RELAXED, ON_CONFLATE, ON_ZAP_DOMAIN,
ON_IPV6 };
int index = -1;
if (Tcl_GetIndexFromObj(ip, obj, sonames, "name", 0, &index) != TCL_OK)
return TCL_ERROR;
switch((enum ExObjOptionNames)index) {
case ON_HWM: *name = ZMQ_HWM; break;
case ON_AFFINITY: *name = ZMQ_AFFINITY; break;
case ON_IDENTITY: *name = ZMQ_IDENTITY; break;
case ON_SUBSCRIBE: *name = ZMQ_SUBSCRIBE; break;
case ON_UNSUBSCRIBE: *name = ZMQ_UNSUBSCRIBE; break;
case ON_RATE: *name = ZMQ_RATE; break;
case ON_RECOVERY_IVL: *name = ZMQ_RECOVERY_IVL; break;
case ON_SNDBUF: *name = ZMQ_SNDBUF; break;
case ON_RCVBUF: *name = ZMQ_RCVBUF; break;
case ON_RCVMORE: *name = ZMQ_RCVMORE; break;
case ON_FD: *name = ZMQ_FD; break;
case ON_EVENTS: *name = ZMQ_EVENTS; break;
case ON_TYPE: *name = ZMQ_TYPE; break;
case ON_LINGER: *name = ZMQ_LINGER; break;
case ON_RECONNECT_IVL: *name = ZMQ_RECONNECT_IVL; break;
case ON_BACKLOG: *name = ZMQ_BACKLOG; break;
case ON_RECONNECT_IVL_MAX: *name = ZMQ_RECONNECT_IVL_MAX; break;
case ON_MAXMSGSIZE: *name = ZMQ_MAXMSGSIZE; break;
case ON_SNDHWM: *name = ZMQ_SNDHWM; break;
case ON_RCVHWM: *name = ZMQ_RCVHWM; break;
case ON_MULTICAST_HOPS: *name = ZMQ_MULTICAST_HOPS; break;
case ON_RCVTIMEO: *name = ZMQ_RCVTIMEO; break;
case ON_SNDTIMEO: *name = ZMQ_SNDTIMEO; break;
case ON_LAST_ENDPOINT: *name = ZMQ_LAST_ENDPOINT; break;
case ON_ROUTER_MANDATORY: *name = ZMQ_ROUTER_MANDATORY; break;
case ON_TCP_KEEPALIVE: *name = ZMQ_TCP_KEEPALIVE; break;
case ON_TCP_KEEPALIVE_CNT: *name = ZMQ_TCP_KEEPALIVE_CNT; break;
case ON_TCP_KEEPALIVE_IDLE: *name = ZMQ_TCP_KEEPALIVE_IDLE; break;
case ON_TCP_KEEPALIVE_INTVL: *name = ZMQ_TCP_KEEPALIVE_INTVL; break;
case ON_TCP_ACCEPT_FILTER: *name = ZMQ_TCP_ACCEPT_FILTER; break;
case ON_IMMEDIATE: *name = ZMQ_IMMEDIATE; break;
case ON_XPUB_VERBOSE: *name = ZMQ_XPUB_VERBOSE; break;
case ON_IPV6: *name = ZMQ_IPV6; break;
case ON_MECHANISM: *name = ZMQ_MECHANISM; break;
case ON_PLAIN_SERVER: *name = ZMQ_PLAIN_SERVER; break;
case ON_PLAIN_USERNAME: *name = ZMQ_PLAIN_USERNAME; break;
case ON_PLAIN_PASSWORD: *name = ZMQ_PLAIN_PASSWORD; break;
case ON_CURVE_SERVER: *name = ZMQ_CURVE_SERVER; break;
case ON_CURVE_PUBLICKEY: *name = ZMQ_CURVE_PUBLICKEY; break;
case ON_CURVE_SECRETKEY: *name = ZMQ_CURVE_SECRETKEY; break;
case ON_CURVE_SERVERKEY: *name = ZMQ_CURVE_SERVERKEY; break;
case ON_PROBE_ROUTER: *name = ZMQ_PROBE_ROUTER; break;
case ON_REQ_CORRELATE: *name = ZMQ_REQ_CORRELATE; break;
case ON_REQ_RELAXED: *name = ZMQ_REQ_RELAXED; break;
case ON_CONFLATE: *name = ZMQ_CONFLATE; break;
case ON_ZAP_DOMAIN: *name = ZMQ_ZAP_DOMAIN; break;
}
return TCL_OK;
}
static int get_mechanism(Tcl_Interp* ip, Tcl_Obj* obj, int* name)
{
static const char* mflags[] = {"NULL", "PLAIN", "CURVE", NULL};
enum ExObjMechanismNames { OM_NULL, OM_PLAIN, OM_CURVE };
int index = -1;
if (Tcl_GetIndexFromObj(ip, obj, mflags, "mechanism", 0, &index) != TCL_OK)
return TCL_ERROR;
switch((enum ExObjMechanismNames)index) {
case OM_NULL: *name = ZMQ_NULL; break;
case OM_PLAIN: *name = ZMQ_PLAIN; break;
case OM_CURVE: *name = ZMQ_CURVE; break;
}
return TCL_OK;
}
static int get_poll_flags(Tcl_Interp* ip, Tcl_Obj* fl, int* events)
{
int objc = 0;
Tcl_Obj** objv = 0;
int i = 0;
if (Tcl_ListObjGetElements(ip, fl, &objc, &objv) != TCL_OK) {
Tcl_SetObjResult(ip, Tcl_NewStringObj("event flags not specified as list", -1));
return TCL_ERROR;
}
for(i = 0; i < objc; i++) {
static const char* eflags[] = {"POLLIN", "POLLOUT", "POLLERR", NULL};
enum ExObjEventFlags {ZEF_POLLIN, ZEF_POLLOUT, ZEF_POLLERR};
int efindex = -1;
if (Tcl_GetIndexFromObj(ip, objv[i], eflags, "event_flag", 0, &efindex) != TCL_OK)
return TCL_ERROR;
switch((enum ExObjEventFlags)efindex) {
case ZEF_POLLIN: *events = *events | ZMQ_POLLIN; break;
case ZEF_POLLOUT: *events = *events | ZMQ_POLLOUT; break;
case ZEF_POLLERR: *events = *events | ZMQ_POLLERR; break;
}
}
return TCL_OK;
}
static Tcl_Obj* set_poll_flags(Tcl_Interp* ip, int revents)
{
Tcl_Obj* fresult = Tcl_NewListObj(0, NULL);
if (revents & ZMQ_POLLIN) {
Tcl_ListObjAppendElement(ip, fresult, Tcl_NewStringObj("POLLIN", -1));
}
if (revents & ZMQ_POLLOUT) {
Tcl_ListObjAppendElement(ip, fresult, Tcl_NewStringObj("POLLOUT", -1));
}
if (revents & ZMQ_POLLERR) {
Tcl_ListObjAppendElement(ip, fresult, Tcl_NewStringObj("POLLERR", -1));
}
return fresult;
}
static int get_monitor_flags(Tcl_Interp* ip, Tcl_Obj* fl, int* events)
{
int objc = 0;
Tcl_Obj** objv = 0;
int i = 0;
if (Tcl_ListObjGetElements(ip, fl, &objc, &objv) != TCL_OK) {
Tcl_SetObjResult(ip, Tcl_NewStringObj("monitor events not specified as list", -1));
return TCL_ERROR;
}
for(i = 0; i < objc; i++) {
static const char* eflags[] = {"CONNECTED", "CONNECT_DELAYED", "CONNECT_RETRIED", "LISTENING", "BIND_FAILED", "ACCEPTED", "ACCEPT_FAILED", "CLOSED", "CLOSE_FAILED", "DISCONNECTED", "MONITOR_STOPPED", "ALL", NULL};
enum ExObjEventFlags {ZEV_CONNECTED, ZEV_CONNECT_DELAYED, ZEV_CONNECT_RETRIED, ZEV_LISTENING, ZEV_BIND_FAILED, ZEV_ACCEPTED, ZEV_ACCEPT_FAILED, ZEV_CLOSED, ZEV_CLOSE_FAILED, ZEV_DISCONNECTED, ZEV_MONITOR_STOPPED, ZEV_ALL};
int efindex = -1;
if (Tcl_GetIndexFromObj(ip, objv[i], eflags, "monitor_event_flag", 0, &efindex) != TCL_OK)
return TCL_ERROR;
switch((enum ExObjEventFlags)efindex) {
case ZEV_CONNECTED: *events = *events | ZMQ_EVENT_CONNECTED; break;
case ZEV_CONNECT_DELAYED: *events = *events | ZMQ_EVENT_CONNECT_DELAYED; break;
case ZEV_CONNECT_RETRIED: *events = *events | ZMQ_EVENT_CONNECT_RETRIED; break;
case ZEV_LISTENING: *events = *events | ZMQ_EVENT_LISTENING; break;
case ZEV_BIND_FAILED: *events = *events | ZMQ_EVENT_BIND_FAILED; break;
case ZEV_ACCEPTED: *events = *events | ZMQ_EVENT_ACCEPTED; break;
case ZEV_ACCEPT_FAILED: *events = *events | ZMQ_EVENT_ACCEPT_FAILED; break;
case ZEV_CLOSED: *events = *events | ZMQ_EVENT_CLOSED; break;
case ZEV_CLOSE_FAILED: *events = *events | ZMQ_EVENT_CLOSE_FAILED; break;
case ZEV_DISCONNECTED: *events = *events | ZMQ_EVENT_DISCONNECTED; break;
case ZEV_MONITOR_STOPPED: *events = *events | ZMQ_EVENT_MONITOR_STOPPED; break;
case ZEV_ALL: *events = *events | ZMQ_EVENT_ALL; break;
}
}
return TCL_OK;
}
static int get_recv_send_flag(Tcl_Interp* ip, Tcl_Obj* fl, int* flags)
{
int objc = 0;
Tcl_Obj** objv = 0;
int i = 0;
if (Tcl_ListObjGetElements(ip, fl, &objc, &objv) != TCL_OK) {
Tcl_SetObjResult(ip, Tcl_NewStringObj("flags not specified as list", -1));
return TCL_ERROR;
}
for(i = 0; i < objc; i++) {
static const char* rsflags[] = {"DONTWAIT", "NOBLOCK", "SNDMORE", NULL};
enum ExObjRSFlags {RSF_DONTWAIT, RSF_NOBLOCK, RSF_SNDMORE};
int index = -1;
if (Tcl_GetIndexFromObj(ip, objv[i], rsflags, "flag", 0, &index) != TCL_OK)
return TCL_ERROR;
switch((enum ExObjRSFlags)index) {
case RSF_DONTWAIT: *flags = *flags | ZMQ_DONTWAIT; break;
case RSF_NOBLOCK: *flags = *flags | ZMQ_DONTWAIT; break;
case RSF_SNDMORE: *flags = *flags | ZMQ_SNDMORE; break;
}
}
return TCL_OK;
}
static Tcl_Obj* zmq_s_dump(Tcl_Interp* ip, const char* data, int size)
{
int is_text = 1;
int char_nbr;
char buffer[TCL_INTEGER_SPACE+4];
Tcl_Obj *result;
for (char_nbr = 0; char_nbr < size && is_text; char_nbr++)
if ((unsigned char) data [char_nbr] < 32
|| (unsigned char) data [char_nbr] > 127)
is_text = 0;
sprintf(buffer, "[%03d] ", size);
result = Tcl_NewStringObj(buffer, -1);
if (is_text) {
Tcl_AppendToObj(result, data, size);
} else {
for (char_nbr = 0; char_nbr < size; char_nbr++) {
sprintf(buffer, "%02X", data[char_nbr]);
Tcl_AppendToObj(result, buffer, 2);
}
}
return result;
}
static int cget_context_option_as_tcl_obj(ClientData cd, Tcl_Interp* ip, Tcl_Obj* optObj, Tcl_Obj** result)
{
int name = 0;
void* zmqp = ((ZmqContextClientData*)cd)->context;
ZmqClientData* zmqClientData = ((ZmqContextClientData*)cd)->zmqClientData;
*result = 0;
if (get_context_option(ip, optObj, &name) != TCL_OK)
return TCL_ERROR;
int val = zmq_ctx_get(zmqp, name);
last_zmq_errno = zmq_errno();
if (val < 0) {
*result = Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1);
return TCL_ERROR;
}
*result = Tcl_NewIntObj(val);
return TCL_OK;
}
static int cget_context_option(ClientData cd, Tcl_Interp* ip, Tcl_Obj* optObj)
{
Tcl_Obj* result = 0;
int rt = cget_context_option_as_tcl_obj(cd, ip, optObj, &result);
if (result)
Tcl_SetObjResult(ip, result);
return rt;
}
static int cset_context_option_as_tcl_obj(ClientData cd, Tcl_Interp* ip, Tcl_Obj* optObj, Tcl_Obj* valObj)
{
int name = 0;
void* zmqp = ((ZmqContextClientData*)cd)->context;
ZmqClientData* zmqClientData = ((ZmqContextClientData*)cd)->zmqClientData;
int rt = 0;
if (get_context_option(ip, optObj, &name) != TCL_OK)
return TCL_ERROR;
int val = -1;
if (Tcl_GetIntFromObj(ip, valObj, &val) != TCL_OK) {
Tcl_SetObjResult(ip, Tcl_NewStringObj("Wrong option value, expected integer", -1));
return TCL_ERROR;
}
rt = zmq_ctx_set(zmqp, name, val);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
return TCL_OK;
}
int zmq_context_objcmd(ClientData cd, Tcl_Interp* ip, int objc, Tcl_Obj* const objv[]) {
static const char* methods[] = {"cget", "configure", "destroy", "get", "set", "term", NULL};
enum ExObjContextMethods {EXCTXOBJ_CGET, EXCTXOBJ_CONFIGURE, EXCTXOBJ_DESTROY, EXCTXOBJ_GET,
EXCTXOBJ_SET, EXCTXOBJ_TERM};
int index = -1;
void* zmqp = ((ZmqContextClientData*)cd)->context;
int rt = 0;
if (objc < 2) {
Tcl_WrongNumArgs(ip, 1, objv, "method ?argument ...?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(ip, objv[1], methods, "method", 0, &index) != TCL_OK)
return TCL_ERROR;
switch((enum ExObjContextMethods)index) {
case EXCTXOBJ_CONFIGURE:
{
if (objc == 2) {
/* Return all options */
int cnp = 0;
Tcl_Obj* cresult = Tcl_NewListObj(0, NULL);
while(conames[cnp]) {
if (conames_cget[cnp]) {
Tcl_Obj* result = 0;
Tcl_Obj* cname = Tcl_NewStringObj(conames[cnp], -1);
Tcl_Obj* oresult = 0;
int rt = cget_context_option_as_tcl_obj(cd, ip, cname, &result);
if (rt != TCL_OK) {
if (result)
Tcl_SetObjResult(ip, result);
return rt;
}
oresult = Tcl_NewListObj(0, NULL);
Tcl_ListObjAppendElement(ip, oresult, cname);
Tcl_ListObjAppendElement(ip, oresult, result);
Tcl_ListObjAppendElement(ip, cresult, oresult);
}
cnp++;
}
Tcl_SetObjResult(ip, cresult);
}
else if (objc == 3) {
/* Get specified option */
Tcl_Obj* result = 0;
Tcl_Obj* oresult = 0;
int rt = cget_context_option_as_tcl_obj(cd, ip, objv[2], &result);
if (rt != TCL_OK) {
if (result)
Tcl_SetObjResult(ip, result);
return rt;
}
oresult = Tcl_NewListObj(0, NULL);
Tcl_ListObjAppendElement(ip, oresult, objv[2]);
Tcl_ListObjAppendElement(ip, oresult, result);
Tcl_SetObjResult(ip, oresult);
}
else if ((objc % 2) == 0) {
/* Set specified options */
int i;
for(i = 2; i < objc; i += 2)
if (cset_context_option_as_tcl_obj(cd, ip, objv[i], objv[i+1]) != TCL_OK)
return TCL_ERROR;
}
else {
Tcl_WrongNumArgs(ip, 2, objv, "?name? ?value option value ...?");
return TCL_ERROR;
}
break;
}
case EXCTXOBJ_DESTROY:
case EXCTXOBJ_TERM:
{
Tcl_HashEntry* hashEntry = 0;
if (objc != 2) {
Tcl_WrongNumArgs(ip, 2, objv, "");
return TCL_ERROR;
}
rt = zmq_ctx_destroy(zmqp);
last_zmq_errno = zmq_errno();
if (rt == 0) {
Tcl_DecrRefCount(((ZmqContextClientData*)cd)->tcl_cmd);
Tcl_DeleteCommand(ip, Tcl_GetStringFromObj(objv[0], 0));
}
else {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
hashEntry = Tcl_FindHashEntry(((ZmqContextClientData*)cd)->zmqClientData->contextClientData, zmqp);
if (hashEntry)
Tcl_DeleteHashEntry(hashEntry);
break;
}
case EXCTXOBJ_CGET:
case EXCTXOBJ_GET:
{
if (objc != 3) {
Tcl_WrongNumArgs(ip, 2, objv, "name");
return TCL_ERROR;
}
return cget_context_option(cd, ip, objv[2]);
}
case EXCTXOBJ_SET:
{
if (objc != 4) {
Tcl_WrongNumArgs(ip, 2, objv, "name value");
return TCL_ERROR;
}
return cset_context_option_as_tcl_obj(cd, ip, objv[2], objv[3]);
}
}
return TCL_OK;
}
static int cget_socket_option_as_tcl_obj(ClientData cd, Tcl_Interp* ip, Tcl_Obj* optObj, Tcl_Obj** result)
{
int name = 0;
void* sockp = ((ZmqSocketClientData*)cd)->socket;
*result = 0;
if (get_socket_option(ip, optObj, &name) != TCL_OK)
return TCL_ERROR;
switch(name) {
/* int options */
case ZMQ_SNDHWM:
case ZMQ_RCVHWM:
case ZMQ_TYPE:
case ZMQ_LINGER:
case ZMQ_RECONNECT_IVL:
case ZMQ_RECONNECT_IVL_MAX:
case ZMQ_BACKLOG:
case ZMQ_RCVMORE:
case ZMQ_RATE:
case ZMQ_SNDBUF:
case ZMQ_RCVBUF:
case ZMQ_RECOVERY_IVL:
case ZMQ_MULTICAST_HOPS:
case ZMQ_RCVTIMEO:
case ZMQ_SNDTIMEO:
case ZMQ_TCP_KEEPALIVE:
case ZMQ_TCP_KEEPALIVE_CNT:
case ZMQ_TCP_KEEPALIVE_IDLE:
case ZMQ_TCP_KEEPALIVE_INTVL:
case ZMQ_IMMEDIATE:
case ZMQ_IPV6:
case ZMQ_PLAIN_SERVER:
case ZMQ_CURVE_SERVER:
case ZMQ_PROBE_ROUTER:
case ZMQ_REQ_CORRELATE:
case ZMQ_REQ_RELAXED:
{
int val = 0;
size_t len = sizeof(int);
int rt = zmq_getsockopt(sockp, name, &val, &len);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
*result = Tcl_NewIntObj(val);
break;
}
case ZMQ_EVENTS:
{
int val = 0;
size_t len = sizeof(int);
int rt = zmq_getsockopt(sockp, name, &val, &len);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
*result = set_poll_flags(ip, val);
break;
}
/* uint64_t options */
case ZMQ_AFFINITY:
{
uint64_t val = 0;
size_t len = sizeof(uint64_t);
int rt = zmq_getsockopt(sockp, name, &val, &len);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
*result = Tcl_NewWideIntObj(val);
break;
}
/* int64_t options */
case ZMQ_MAXMSGSIZE:
{
int64_t val = 0;
size_t len = sizeof(int64_t);
int rt = zmq_getsockopt(sockp, name, &val, &len);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
*result = Tcl_NewWideIntObj(val);
break;
}
/* binary options */
case ZMQ_IDENTITY:
{
const char val[256];
size_t len = 256;
int rt = zmq_getsockopt(sockp, name, (void*)val, &len);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
*result = Tcl_NewStringObj(val, len);
break;
}
case ZMQ_LAST_ENDPOINT:
case ZMQ_PLAIN_USERNAME:
case ZMQ_PLAIN_PASSWORD:
case ZMQ_CURVE_PUBLICKEY:
case ZMQ_CURVE_SECRETKEY:
case ZMQ_CURVE_SERVERKEY:
case ZMQ_ZAP_DOMAIN:
{
const char val[256];
size_t len = 256;
int rt = zmq_getsockopt(sockp, name, (void*)val, &len);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
/* Length of string including trailing zero is returned */
*result = Tcl_NewStringObj(val, len-1);
break;
}
case ZMQ_MECHANISM:
{
int val = 0;
size_t len = sizeof(int);
int rt = zmq_getsockopt(sockp, name, &val, &len);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
switch(val) {
case ZMQ_NULL: *result = Tcl_NewStringObj("NULL", -1); break;
case ZMQ_PLAIN: *result = Tcl_NewStringObj("PLAIN", -1); break;
case ZMQ_CURVE: *result = Tcl_NewStringObj("CURVE", -1); break;
default: *result = Tcl_NewStringObj("NULL", -1); break;
}
break;
}
default:
{
Tcl_SetObjResult(ip, Tcl_NewStringObj("unsupported option", -1));
return TCL_ERROR;
}
}
return TCL_OK;
}
static int cget_socket_option(ClientData cd, Tcl_Interp* ip, Tcl_Obj* optObj)
{
Tcl_Obj* result = 0;
int rt = cget_socket_option_as_tcl_obj(cd, ip, optObj, &result);
if (result)
Tcl_SetObjResult(ip, result);
return rt;
}
static int cset_socket_option_as_tcl_obj(ClientData cd, Tcl_Interp* ip, Tcl_Obj* optObj, Tcl_Obj* valObj, Tcl_Obj* sizeObj)
{
void* sockp = ((ZmqSocketClientData*)cd)->socket;
int name = -1;
if (get_socket_option(ip, optObj, &name) != TCL_OK)
return TCL_ERROR;
switch(name) {
/* int options */
case ZMQ_HWM:
{
int val = 0;
int rt = 0;
if (Tcl_GetIntFromObj(ip, valObj, &val) != TCL_OK) {
Tcl_SetObjResult(ip, Tcl_NewStringObj("Wrong HWM argument, expected integer", -1));
return TCL_ERROR;
}
rt = zmq_setsockopt(sockp, ZMQ_SNDHWM, &val, sizeof val);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
rt = zmq_setsockopt(sockp, ZMQ_RCVHWM, &val, sizeof val);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
break;
}
case ZMQ_SNDHWM:
case ZMQ_RCVHWM:
case ZMQ_LINGER:
case ZMQ_RECONNECT_IVL:
case ZMQ_RECONNECT_IVL_MAX:
case ZMQ_BACKLOG:
case ZMQ_RATE:
case ZMQ_RECOVERY_IVL:
case ZMQ_SNDBUF:
case ZMQ_RCVBUF:
case ZMQ_MULTICAST_HOPS:
case ZMQ_RCVTIMEO:
case ZMQ_SNDTIMEO:
case ZMQ_ROUTER_MANDATORY:
case ZMQ_TCP_KEEPALIVE:
case ZMQ_TCP_KEEPALIVE_CNT:
case ZMQ_TCP_KEEPALIVE_IDLE:
case ZMQ_TCP_KEEPALIVE_INTVL:
case ZMQ_IMMEDIATE:
case ZMQ_XPUB_VERBOSE:
case ZMQ_IPV6:
case ZMQ_PLAIN_SERVER:
case ZMQ_CURVE_SERVER:
case ZMQ_PROBE_ROUTER:
case ZMQ_REQ_CORRELATE:
case ZMQ_REQ_RELAXED:
case ZMQ_CONFLATE:
{
int val = 0;
int rt = 0;
if (Tcl_GetIntFromObj(ip, valObj, &val) != TCL_OK) {
Tcl_SetObjResult(ip, Tcl_NewStringObj("Wrong argument, expected integer", -1));
return TCL_ERROR;
}
rt = zmq_setsockopt(sockp, name, &val, sizeof val);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
break;
}
/* uint64_t options */
case ZMQ_AFFINITY:
{
Tcl_WideInt val = 0;
uint64_t uval = 0;
int rt = 0;
if (Tcl_GetWideIntFromObj(ip, valObj, &val) != TCL_OK) {
Tcl_SetObjResult(ip, Tcl_NewStringObj("Wrong argument, expected integer", -1));
return TCL_ERROR;
}
uval = val;
rt = zmq_setsockopt(sockp, name, &uval, sizeof uval);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
break;
}
/* int64_t options */
case ZMQ_MAXMSGSIZE:
{
Tcl_WideInt val = 0;
int rt = 0;
if (Tcl_GetWideIntFromObj(ip, valObj, &val) != TCL_OK) {
Tcl_SetObjResult(ip, Tcl_NewStringObj("Wrong argument, expected integer", -1));
return TCL_ERROR;
}
rt = zmq_setsockopt(sockp, name, &val, sizeof val);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
break;
}
/* binary options */
case ZMQ_IDENTITY:
case ZMQ_SUBSCRIBE:
case ZMQ_UNSUBSCRIBE:
{
int len = 0;
const char* val = 0;
int rt = 0;
int size = -1;
val = Tcl_GetStringFromObj(valObj, &len);
if (sizeObj) {
if (Tcl_GetIntFromObj(ip, sizeObj, &size) != TCL_OK) {
Tcl_SetObjResult(ip, Tcl_NewStringObj("Wrong size argument, expected integer", -1));
return TCL_ERROR;
}
}
else
size = len;
rt = zmq_setsockopt(sockp, name, val, size);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
break;
}
case ZMQ_TCP_ACCEPT_FILTER:
case ZMQ_PLAIN_USERNAME:
case ZMQ_PLAIN_PASSWORD:
case ZMQ_CURVE_PUBLICKEY:
case ZMQ_CURVE_SECRETKEY:
case ZMQ_CURVE_SERVERKEY:
case ZMQ_ZAP_DOMAIN:
{
int len = 0;
const char* val = 0;
int rt = 0;
int size = -1;
val = Tcl_GetStringFromObj(valObj, &len);
if (sizeObj) {
if (Tcl_GetIntFromObj(ip, sizeObj, &size) != TCL_OK) {
Tcl_SetObjResult(ip, Tcl_NewStringObj("Wrong size argument, expected integer", -1));
return TCL_ERROR;
}
}
else
size = len;
if (size == 0)
rt = zmq_setsockopt(sockp, name, 0, 0);
else
rt = zmq_setsockopt(sockp, name, val, size);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
break;
}
default:
{
Tcl_SetObjResult(ip, Tcl_NewStringObj("unsupported option", -1));
return TCL_ERROR;
}
}
return TCL_OK;
}
int zmq_socket_objcmd(ClientData cd, Tcl_Interp* ip, int objc, Tcl_Obj* const objv[]) {
static const char* methods[] = {"bind", "cget", "close", "configure", "connect", "destroy", "disconnect", "get",
"getsockopt", "readable", "recv_msg", "send_msg", "dump", "recv", "send",
"sendmore", "set", "setsockopt", "unbind", "writable", "recv_monitor_event",
"monitor", NULL};
enum ExObjSocketMethods {EXSOCKOBJ_BIND, EXSOCKOBJ_CGET, EXSOCKOBJ_CLOSE, EXSOCKOBJ_CONFIGURE, EXSOCKOBJ_CONNECT,
EXSOCKOBJ_DESTROY, EXSOCKOBJ_DISCONNECT, EXSOCKOBJ_GET, EXSOCKOBJ_GETSOCKETOPT,
EXSOCKOBJ_READABLE, EXSOCKOBJ_RECV, EXSOCKOBJ_SEND, EXSOCKOBJ_S_DUMP, EXSOCKOBJ_S_RECV,
EXSOCKOBJ_S_SEND, EXSOCKOBJ_S_SENDMORE, EXSOCKOBJ_SET, EXSOCKOBJ_SETSOCKETOPT, EXSOCKOBJ_UNBIND,
EXSOCKOBJ_WRITABLE, EXSOCKOBJ_RECV_MONITOR_EVENT, EXSOCKOBJ_MONITOR};
int index = -1;
void* sockp = ((ZmqSocketClientData*)cd)->socket;
ZmqClientData* zmqClientData = (((ZmqSocketClientData*)cd)->zmqClientData);
if (objc < 2) {
Tcl_WrongNumArgs(ip, 1, objv, "method ?argument ...?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(ip, objv[1], methods, "method", 0, &index) != TCL_OK)
return TCL_ERROR;
switch((enum ExObjSocketMethods)index) {
case EXSOCKOBJ_BIND:
{
int rt = 0;
const char* endpoint = 0;
if (objc != 3) {
Tcl_WrongNumArgs(ip, 2, objv, "endpoint");
return TCL_ERROR;
}
endpoint = Tcl_GetStringFromObj(objv[2], 0);
rt = zmq_bind(sockp, endpoint);
last_zmq_errno = zmq_errno();
if (rt != 0) {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
break;
}
case EXSOCKOBJ_CLOSE:
case EXSOCKOBJ_DESTROY:
{
Tcl_HashEntry* hashEntry = 0;
int rt = 0;
if (objc != 2) {
Tcl_WrongNumArgs(ip, 2, objv, "");
return TCL_ERROR;
}
rt = zmq_close(sockp);
last_zmq_errno = zmq_errno();
if (rt == 0) {
Tcl_DecrRefCount(((ZmqSocketClientData*)cd)->tcl_cmd);
Tcl_DeleteCommand(ip, Tcl_GetStringFromObj(objv[0], 0));
}
else {
Tcl_SetObjResult(ip, Tcl_NewStringObj(zmq_strerror(last_zmq_errno), -1));
return TCL_ERROR;
}
hashEntry = Tcl_FindHashEntry(zmqClientData->readableCommands, sockp);
if (hashEntry)
Tcl_DeleteHashEntry(hashEntry);
hashEntry = Tcl_FindHashEntry(zmqClientData->writableCommands, sockp);
if (hashEntry)
Tcl_DeleteHashEntry(hashEntry);
hashEntry = Tcl_FindHashEntry(zmqClientData->socketClientData, sockp);
if (hashEntry)
Tcl_DeleteHashEntry(hashEntry);
break;
}
case EXSOCKOBJ_CONFIGURE:
{
if (objc == 2) {
/* Return all options */
int cnp = 0;
Tcl_Obj* cresult = Tcl_NewListObj(0, NULL);
while(sonames[cnp]) {
if (sonames_cget[cnp]) {
Tcl_Obj* result = 0;
Tcl_Obj* oresult = 0;
Tcl_Obj* cname = Tcl_NewStringObj(sonames[cnp], -1);