forked from bvarga/delphizmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmqapi.pas
2891 lines (2512 loc) · 70.5 KB
/
zmqapi.pas
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
{
Copyright (c) 2012 Varga Balázs ([email protected])
This file is part of 0MQ Delphi binding
0MQ Delphi binding is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 3 of the
License, or (at your option) any later version.
0MQ Delphi binding is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}
unit zmqapi;
{$ifdef FPC}
{$mode delphi}{$H+}
{$else}
{$WARN IMPLICIT_STRING_CAST OFF}
{$WARN IMPLICIT_STRING_CAST_LOSS OFF}
{$endif}
{$I zmq.inc}
interface
uses
{$ifdef UNIX}
BaseUnix,
{$else}
Windows,
{$endif}
Classes
, SysUtils
, zmq
;
const
ZMQEAGAIN = 11;
{$ifdef UNIX}
ZMQEINTR = ESysEINTR;
{$endif}
type
{$ifdef zmq3}
TZMQMonitorEvent = (
meConnected,
meConnectDelayed,
meConnectRetried,
meListening,
meBindFailed,
meAccepted,
meAcceptFailed,
meClosed,
meCloseFailed,
meDisconnected
);
TZMQMonitorEvents = set of TZMQMonitorEvent;
const
cZMQMonitorEventsAll = [ meConnected,
meConnectDelayed,
meConnectRetried,
meListening,
meBindFailed,
meAccepted,
meAcceptFailed,
meClosed,
meCloseFailed,
meDisconnected
];
type
{$endif}
UInt64 = Int64;
EZMQException = class( Exception )
private
errnum: Integer;
public
constructor Create; overload;
constructor Create( lerrn: Integer ); overload;
property Num: Integer read errnum;
end;
EZMQTerminate = class(EAbort);
TZMQContext = class;
TZMQSocket = class;
TZMQSendFlag = ( {$ifdef zmq3}sfDontWait{$else}sfNoBlock{$endif}, sfSndMore );
TZMQSendFlags = set of TZMQSendFlag;
TZMQRecvFlag = ( {$ifdef zmq3}rfDontWait{$else}rfNoBlock{$endif} );
TZMQRecvFlags = set of TZMQRecvFlag;
TZMQMessageProperty = ( mpMore );
TZMQFrame = class
private
fMessage: zmq_msg_t;
function getAsInteger: Integer;
procedure setAsInteger(const Value: Integer);
function getAsHexString: AnsiString;
procedure setAsHexString(const Value: AnsiString);
procedure CheckResult( rc: Integer );
{$ifdef zmq3}
function getProperty( prop: TZMQMessageProperty ): Integer;
procedure setProperty( prop: TZMQMessageProperty; value: Integer );
{$endif}
function getAsUtf8String: Utf8String;
procedure setAsUtf8String(const Value: Utf8String);
function getAsByte: Byte;
procedure setAsByte(const Value: Byte);
function getAsBytes: TBytes;
procedure setAsBytes(const Value: TBytes);
public
constructor create; overload;
constructor create( size: size_t ); overload;
constructor create( data: Pointer; size: size_t; ffn: free_fn; hint: Pointer = nil ); overload;
destructor Destroy; override;
procedure rebuild; overload;
procedure rebuild( size: size_t ); overload;
procedure rebuild( data: Pointer; size: size_t; ffn: free_fn; hint: Pointer = nil ); overload;
procedure move( msg: TZMQFrame );
procedure copy( msg: TZMQFrame );
function data: Pointer;
function size: size_t;
{$ifdef zmq3}
function more: Boolean;
{$endif}
function dup: TZMQFrame;
// convert the data into a readable string.
function dump: Utf8String;
// copy the whole content of the stream to the message.
procedure LoadFromStream( strm: TStream );
procedure SaveToStream( strm: TStream );
property asUtf8String: Utf8String read getAsUtf8String write setAsUtf8String;
property asHexString: AnsiString read getAsHexString write setAsHexString;
property asInteger: Integer read getAsInteger write setAsInteger;
property asByte: Byte read getAsByte write setAsByte;
property asBytes: TBytes read getAsBytes write setAsBytes;
end;
// for multipart message
TZMQMsg = class
private
msgs: TList;
csize: Cardinal;
cursor: Integer;
function getItem(indx: Integer): TZMQFrame;
protected
public
constructor create;
destructor Destroy; override;
// Return size of message, i.e. number of frames (0 or more).
function size: Integer;
// Return size of message, i.e. number of frames (0 or more).
function content_size: Integer;
// Push frame to the front of the message, i.e. before all other frames.
// Message takes ownership of frame, will destroy it when message is sent.
// Set the cursor to 0
// Returns 0 on success, -1 on error.
function push( msg: TZMQFrame ): Integer;
function pushstr( str: Utf8String ): Integer;
// Remove first frame from message, if any. Returns frame, or NULL. Caller
// now owns frame and must destroy it when finished with it.
// Set the cursor to 0
function pop: TZMQFrame;
function popstr: Utf8String;
function popint: Integer;
// Add frame to the end of the message, i.e. after all other frames.
// Message takes ownership of frame, will destroy it when message is sent.
// Set the cursor to 0
// Returns 0 on success
function add( msg: TZMQFrame ): Integer;
function addstr( msg: Utf8String ): Integer;
function addint( msg: Integer ): Integer;
function addByte( msg: Byte): Integer;
function addBytes(const msg: TBytes ): Integer;
// Push frame plus empty frame to front of message, before first frame.
// Message takes ownership of frame, will destroy it when message is sent.
procedure wrap( msg: TZMQFrame );
// Pop frame off front of message, caller now owns frame
// If next frame is empty, pops and destroys that empty frame.
function unwrap: TZMQFrame;
// Remove specified frame from list, if present. Does not destroy frame.
// Set the cursor to 0
procedure remove( msg: TZMQFrame );
// Set cursor to first frame in message. Returns frame, or NULL.
function first: TZMQFrame;
// Return the next frame. If there are no more frames, returns NULL. To move
// to the first frame call zmsg_first(). Advances the cursor.
function next: TZMQFrame;
// Return the last frame. If there are no frames, returns NULL.
// Set the cursor to the last
function last: TZMQFrame;
// Create copy of message, as new message object
function dup: TZMQMsg;
// dumpt message
function dump: Utf8String;
function saveasHex: Utf8String;
procedure loadfromHex( data: Utf8String );
procedure Clear;
property item[indx: Integer]: TZMQFrame read getItem; default;
end;
TZMQSocketType = ( stPair, stPub, stSub, stReq, stRep, stDealer,
stRouter, stPull, stPush, stXPub, stXSub );
TZMQPollEvent = ( pePollIn, pePollOut, pePollErr );
TZMQPollEvents = set of TZMQPollEvent;
{$ifdef zmq3}
TZMQKeepAlive = ( kaDefault, kaFalse, kaTrue );
TZMQEvent = record
event: TZMQMonitorEvent;
addr: AnsiString;
case TZMQMonitorEvent of
meConnected,
meListening,
meAccepted,
meClosed,
meDisconnected:
(
fd: Integer;
);
meConnectDelayed,
meBindFailed,
meAcceptFailed,
meCloseFailed:
(
err: Integer;
);
meConnectRetried: ( //connect_retried
interval: Integer;
);
end;
TZMQMonitorProc = procedure( event: TZMQEvent ) of object;
PZMQMonitorRec = ^TZMQMonitorRec;
TZMQMonitorRec = record
terminated: Boolean;
context: TZMQContext;
addr: AnsiString;
proc: TZMQMonitorProc;
end;
{$endif}
TZMQSocket = class
// low level
protected
fSocket: Pointer;
fContext: TZMQContext;
private
fRaiseEAgain: Boolean;
{$ifdef zmq3}
fAcceptFilter: TStringList;
fMonitorRec: PZMQMonitorRec;
fMonitorThread: THandle;
{$endif}
procedure close;
procedure setSockOpt( option: Integer; optval: Pointer; optvallen: size_t );
procedure getSockOpt( option: Integer; optval: Pointer; var optvallen: size_t );
function send( var msg: TZMQFrame; flags: Integer = 0 ): Integer; overload;
function recv( var msg: TZMQFrame; flags: Integer = 0 ): Integer; overload;
public
procedure bind( addr: AnsiString );
procedure connect( addr: AnsiString );
{$ifdef zmq3}
procedure unbind( addr: AnsiString );
procedure disconnect( addr: AnsiString );
{$endif}
// helpers
private
function CheckResult( rc: Integer ): Integer;
function getSockOptInt64( option: Integer ): Int64;
function getSockOptInteger( option: Integer ): Integer;
procedure setSockOptInt64( option: Integer; const Value: Int64 );
procedure setSockOptInteger( option: Integer; const Value: Integer );
public
constructor Create;
destructor Destroy; override;
function getSocketType: TZMQSocketType;
function getrcvMore: Boolean;
function getRcvTimeout: Integer;
function getSndTimeout: Integer;
function getAffinity: UInt64;
function getIdentity: ShortString;
function getRate: {$ifdef zmq3}Integer{$else}int64{$endif};
function getRecoveryIvl: {$ifdef zmq3}Integer{$else}int64{$endif};
function getSndBuf: {$ifdef zmq3}Integer{$else}UInt64{$endif};
function getRcvBuf: {$ifdef zmq3}Integer{$else}UInt64{$endif};
function getLinger: Integer;
function getReconnectIvl: Integer;
function getReconnectIvlMax: Integer;
function getBacklog: Integer;
function getFD: Pointer;
function getEvents: TZMQPollEvents;
function getHWM: {$ifdef zmq3}Integer{$else}UInt64{$endif};
{$ifdef zmq3}
function getSndHWM: Integer;
function getRcvHWM: Integer;
procedure setSndHWM( const Value: Integer );
procedure setRcvHWM( const Value: Integer );
procedure setMaxMsgSize( const Value: Int64 );
function getMaxMsgSize: Int64;
function getMulticastHops: Integer;
procedure setMulticastHops( const Value: Integer );
function getIPv4Only: Boolean;
procedure setIPv4Only( const Value: Boolean );
function getLastEndpoint: AnsiString;
function getKeepAlive: TZMQKeepAlive;
procedure setKeepAlive( const Value: TZMQKeepAlive );
function getKeepAliveIdle: Integer;
procedure setKeepAliveIdle( const Value: Integer );
function getKeepAliveCnt: Integer;
procedure setKeepAliveCnt( const Value: Integer );
function getKeepAliveIntvl: Integer;
procedure setKeepAliveIntvl( const Value: Integer );
function getAcceptFilter( indx: Integer ): AnsiString;
procedure setAcceptFilter( indx: Integer; const Value: AnsiString );
procedure setRouterMandatory( const Value: Boolean );
{$else}
function getSwap: Int64;
function getRecoveryIvlMSec: Int64;
function getMCastLoop: Int64;
procedure setSwap( const Value: Int64 );
procedure setRecoveryIvlMSec( const Value: Int64 );
procedure setMCastLoop( const Value: Int64 );
{$endif}
procedure setHWM( const Value: {$ifdef zmq3}Integer{$else}UInt64{$endif} );
procedure setRcvTimeout( const Value: Integer );
procedure setSndTimeout( const Value: Integer );
procedure setAffinity( const Value: UInt64 );
procedure setIdentity( const Value: ShortString );
procedure setRate( const Value: {$ifdef zmq3}Integer{$else}int64{$endif} );
procedure setRecoveryIvl( const Value: {$ifdef zmq3}Integer{$else}int64{$endif} );
procedure setSndBuf( const Value: {$ifdef zmq3}Integer{$else}UInt64{$endif} );
procedure setRcvBuf( const Value: {$ifdef zmq3}Integer{$else}UInt64{$endif} );
procedure setLinger( const Value: Integer );
procedure setReconnectIvl( const Value: Integer );
procedure setReconnectIvlMax( const Value: Integer );
procedure setBacklog( const Value: Integer );
procedure Subscribe( filter: AnsiString );
procedure unSubscribe( filter: AnsiString );
function send( var msg: TZMQFrame; flags: TZMQSendFlags = [] ): Integer; overload;
function send( strm: TStream; size: Integer; flags: TZMQSendFlags = [] ): Integer; overload;
function send( msg: Utf8String; flags: TZMQSendFlags = [] ): Integer; overload;
function send( msg: TBytes; flags: TZMQSendFlags = [] ): Integer; overload;
function send( var msgs: TZMQMsg; dontwait: Boolean = false ): Integer; overload;
function send( msg: Array of Utf8String; dontwait: Boolean = false ): Integer; overload;
function send( msg: TStrings; dontwait: Boolean = false ): Integer; overload;
{$ifdef zmq3}
function sendBuffer( const Buffer; len: Size_t; flags: TZMQSendFlags = [] ): Integer;
{$endif}
function recv( msg: TZMQFrame; flags: TZMQRecvFlags = [] ): Integer; overload;
function recv( strm: TStream; flags: TZMQRecvFlags = [] ): Integer; overload;
function recv( var msg: Utf8String; flags: TZMQRecvFlags = [] ): Integer; overload;
function recv( var msg: TBytes; flags: TZMQRecvFlags = [] ): Integer; overload;
function recv( var msgs: TZMQMsg; flags: TZMQRecvFlags = [] ): Integer; overload;
function recv( msg: TStrings; flags: TZMQRecvFlags = [] ): Integer; overload;
{$ifdef zmq3}
function recvBuffer( var Buffer; len: size_t; flags: TZMQRecvFlags = [] ): Integer;
procedure RegisterMonitor( proc: TZMQMonitorProc; events: TZMQMonitorEvents = cZMQMonitorEventsAll );
procedure DeRegisterMonitor;
{$endif}
property SocketType: TZMQSocketType read getSocketType;
property RcvMore: Boolean read getRcvMore;
{$ifdef zmq3}
property SndHWM: Integer read getSndHWM write setSndHwm;
property RcvHWM: Integer read getRcvHWM write setRcvHwm;
property MaxMsgSize: Int64 read getMaxMsgSize write setMaxMsgSize;
property MulticastHops: Integer read getMulticastHops write setMulticastHops;
property IPv4Only: Boolean read getIPv4Only write setIPv4Only;
property LastEndpoint: AnsiString read getLastEndpoint;
property KeepAlive: TZMQKeepAlive read getKeepAlive write setKeepAlive;
property KeepAliveIdle: Integer read getKeepAliveIdle write setKeepAliveIdle;
property KeepAliveCnt: Integer read getKeepAliveCnt write setKeepAliveCnt;
property KeepAliveIntvl: Integer read getKeepAliveIntvl write setKeepAliveIntvl;
procedure AddAcceptFilter( addr: AnsiString );
property AcceptFilter[indx: Integer]: AnsiString read getAcceptFilter write setAcceptFilter;
property RouterMandatory: Boolean write setRouterMandatory;
{$else}
property Swap: Int64 read getSwap write setSwap;
property RecoveryIvlMSec: Int64 read getRecoveryIvlMSec write setRecoveryIvlMSec;
property MCastLoop: Int64 read getMCastLoop write setMCastLoop;
{$endif}
property HWM: {$ifdef zmq3}Integer{$else}UInt64{$endif} read getHWM write setHWM;
property RcvTimeout: Integer read getRcvTimeout write setRcvTimeout;
property SndTimeout: Integer read getSndTimeout write setSndTimeout;
property Affinity: UInt64 read getAffinity write setAffinity;
property Identity: ShortString read getIdentity write setIdentity;
property Rate: {$ifdef zmq3}Integer{$else}int64{$endif} read getRate write setRate;
property RecoveryIvl: {$ifdef zmq3}Integer{$else}int64{$endif} read getRecoveryIvl write setRecoveryIvl;
property SndBuf: {$ifdef zmq3}Integer{$else}UInt64{$endif} read getSndBuf write setSndBuf;
property RcvBuf: {$ifdef zmq3}Integer{$else}UInt64{$endif} read getRcvBuf write setRcvBuf;
property Linger: Integer read getLinger write setLinger;
property ReconnectIvl: Integer read getReconnectIvl write setReconnectIvl;
property ReconnectIvlMax: Integer read getReconnectIvlMax write setReconnectIvlMax;
property Backlog: Integer read getBacklog write setBacklog;
property FD: Pointer read getFD;
property Events: TZMQPollEvents read getEvents;
property Context: TZMQContext read fContext;
property SocketPtr: Pointer read fSocket;
property RaiseEAgain: Boolean read fRaiseEAgain write fRaiseEAgain;
end;
TZMQContext = class
private
fContext: Pointer;
fSockets: TList;
fLinger: Integer;
{$ifdef zmq3}
function getOption( option: Integer ): Integer;
procedure setOption( option, optval: Integer );
function getIOThreads: Integer;
procedure setIOThreads( const Value: Integer );
function getMaxSockets: Integer;
procedure setMaxSockets( const Value: Integer );
{$endif}
protected
fTerminated: Boolean;
fMainThread: Boolean;
constructor createShadow( context: TZMQContext );
procedure CheckResult( rc: Integer );
procedure RemoveSocket( lSocket: TZMQSocket );
public
constructor create{$ifndef zmq3}( io_threads: Integer = 1 ){$endif};
destructor Destroy; override;
function Shadow: TZMQContext;
function Socket( stype: TZMQSocketType ): TZMQSocket;
procedure Terminate;
{$IFDEF zmq3}
procedure Shutdown;
{$ENDIF}
property ContextPtr: Pointer read fContext;
// < -1 means dont change linger when destroy
property Linger: Integer read fLinger write fLinger;
property Terminated: Boolean read fTerminated;
{$ifdef zmq3}
property IOThreads: Integer read getIOThreads write setIOThreads;
property MaxSockets: Integer read getMaxSockets write setMaxSockets;
{$endif}
end;
type
TZMQFree = zmq.free_fn;
TZMQPollItem = record
socket: TZMQSocket;
events: TZMQPollEvents;
revents: TZMQPollEvents;
end;
TZMQPollItemA = array of TZMQPollItem;
TZMQPollEventProc = procedure( socket: TZMQSocket; event: TZMQPollEvents ) of object;
TZMQExceptionProc = procedure( exception: Exception ) of object;
TZMQPoller = class( TThread )
private
fContext: TZMQContext;
fOwnContext: Boolean;
sPair: TZMQSocket;
fAddr: AnsiString;
fPollItem: array of zmq.pollitem_t;
fPollSocket: array of TZMQSocket;
fPollItemCapacity,
fPollItemCount: Integer;
fTimeOut: Integer;
fPollNumber: Integer;
cs: TRTLCriticalSection;
fSync: Boolean;
fonException: TZMQExceptionProc;
fonTimeOut: TNotifyEvent;
fonEvent: TZMQPollEventProc;
function getPollItem(indx: Integer): TZMQPollItem;
procedure CheckResult( rc: Integer );
procedure AddToPollItems( socket: TZMQSocket; events: TZMQPollEvents );
procedure DelFromPollItems( socket: TZMQSocket; events: TZMQPollEvents; indx: Integer );
function getPollResult(indx: Integer): TZMQPollItem;
protected
procedure Execute; override;
public
constructor Create( lSync: Boolean = false; lContext: TZMQContext = nil );
destructor Destroy; override;
procedure Register( socket: TZMQSocket; events: TZMQPollEvents; bWait: Boolean = false );
procedure Deregister( socket: TZMQSocket; events: TZMQPollEvents; bWait: Boolean = false );
procedure setPollNumber( const Value: Integer; bWait: Boolean = false );
function poll( timeout: Longint = -1; lPollNumber: Integer = -1 ): Integer;
property pollResult[indx: Integer]: TZMQPollItem read getPollResult;
property PollNumber: Integer read fPollNumber;
property PollItem[indx: Integer]: TZMQPollItem read getPollItem;
property onEvent: TZMQPollEventProc read fonEvent write fonEvent;
property onException: TZMQExceptionProc read fonException write fonException;
property onTimeOut: TNotifyEvent read fonTimeOut write fonTimeOut;
end;
TZMQDevice = ( dStreamer, dForwarder, dQueue );
TZMQPollRec = record
socket: TZMQSocket;
events: TZMQPollEvents;
end;
TZMQPollRecA = array of TZMQPollRec;
function ZMQPoll( var pia: TZMQPollItemA; piaSize: Integer = -1; timeout: Integer = -1 ): Integer; overload;
function ZMQPoll( var pia: TZMQPollItem; piaSize: Integer = 1; timeout: Integer = -1 ): Integer; overload;
{$ifdef zmq3}
procedure ZMQProxy( frontend, backend, capture: TZMQSocket );
{$endif}
procedure ZMQDevice( device: TZMQDevice; insocket, outsocket: TZMQSocket );
procedure ZMQVersion(var major, minor, patch: Integer);
procedure ZMQTerminate;
var
ZMQTerminated: Boolean = false;
type
// Thread related functions.
TDetachedThreadMeth = procedure( args: Pointer; context: TZMQContext ) of object;
TAttachedThreadMeth = procedure( args: Pointer; Context: TZMQContext; Pipe: TZMQSocket ) of object;
TDetachedThreadProc = procedure( args: Pointer; context: TZMQContext );
TAttachedThreadProc = procedure( args: Pointer; Context: TZMQContext; Pipe: TZMQSocket );
TZMQThread = class( TThread )
private
//attached thread pipe
fPipe: TZMQSocket;
// attached thread pipe in the new thread.
thrPipe: TZMQSocket;
fDetachedMeth: TDetachedThreadMeth;
fAttachedMeth: TAttachedThreadMeth;
fDetachedProc: TDetachedThreadProc;
fAttachedProc: TAttachedThreadProc;
fContext: TZMQContext;
fArgs: Pointer;
public
constructor Create( lArgs: Pointer; ctx: TZMQContext );
constructor CreateAttached( lAttachedMeth: TAttachedThreadMeth; ctx: TZMQContext; lArgs: Pointer );
constructor CreateDetached( lDetachedMeth: TDetachedThreadMeth; lArgs: Pointer );
constructor CreateAttachedProc( lAttachedProc: TAttachedThreadProc; ctx: TZMQContext; lArgs: Pointer );
constructor CreateDetachedProc( lDetachedProc: TDetachedThreadProc; lArgs: Pointer );
destructor Destroy; override;
protected
procedure Execute; override;
procedure DoExecute; virtual;
public
property Pipe: TZMQSocket read fPipe;
property Args: Pointer read fArgs;
property Context: TZMQContext read fContext;
end;
implementation
var
contexts: TList;
cs: TRTLCriticalSection;
{$ifndef UNIX}
function console_handler( dwCtrlType: DWORD ): BOOL; stdcall; forward;
{$endif}
{ EZMQException }
constructor EZMQException.Create;
begin
errnum := zmq_errno;
inherited Create( String( AnsiString( zmq_strerror( errnum ) ) ) );
end;
constructor EZMQException.Create( lerrn: Integer );
begin
errnum := lerrn;
inherited Create( String( AnsiString( zmq_strerror( errnum ) ) ) );
end;
{ TZMQMessage }
constructor TZMQFrame.Create;
begin
CheckResult( zmq_msg_init( fMessage ) );
end;
constructor TZMQFrame.Create( size: size_t );
begin
CheckResult( zmq_msg_init_size( fMessage, size ) );
end;
constructor TZMQFrame.Create( data: Pointer; size: size_t;
ffn: free_fn; hint: Pointer );
begin
CheckResult( zmq_msg_init_data( fMessage, data, size, ffn, hint ) );
end;
destructor TZMQFrame.Destroy;
begin
CheckResult( zmq_msg_close( fMessage ) );
inherited;
end;
procedure TZMQFrame.CheckResult( rc: Integer );
begin
if rc = 0 then
begin
// ok
end else
if rc = -1 then
begin
raise EZMQException.Create;
end else
raise EZMQException.Create('Function result is not 0, or -1!');
end;
procedure TZMQFrame.rebuild;
begin
CheckResult( zmq_msg_close( fMessage ) );
CheckResult( zmq_msg_init( fMessage ) );
end;
procedure TZMQFrame.rebuild( size: size_t );
begin
CheckResult( zmq_msg_close( fMessage ) );
CheckResult( zmq_msg_init_size( fMessage, size ) );
end;
procedure TZMQFrame.rebuild( data: Pointer; size: size_t; ffn: free_fn; hint: Pointer = nil );
begin
CheckResult( zmq_msg_close( fMessage ) );
CheckResult( zmq_msg_init_data( fMessage, data, size, ffn, hint ) );
end;
procedure TZMQFrame.move( msg: TZMQFrame );
begin
CheckResult( zmq_msg_move( fMessage, msg.fMessage ) );
end;
procedure TZMQFrame.copy( msg: TZMQFrame );
begin
CheckResult( zmq_msg_copy( fMessage, msg.fMessage ) );
end;
function TZMQFrame.data: Pointer;
begin
result := zmq_msg_data( fMessage );
end;
function TZMQFrame.size: size_t;
begin
result := zmq_msg_size( fMessage );
end;
{$ifdef zmq3}
function TZMQFrame.getProperty( prop: TZMQMessageProperty ): Integer;
begin
result := zmq_msg_get( fMessage, Byte( prop ) );
if result = -1 then
raise EZMQException.Create
else
raise EZMQException.Create( 'zmq_msg_more return value undefined!' );
end;
procedure TZMQFrame.setProperty( prop: TZMQMessageProperty; value: Integer );
begin
CheckResult( zmq_msg_set( fMessage, Byte( prop ), value ) );
end;
function TZMQFrame.more: Boolean;
var
rc: Integer;
begin
rc := zmq_msg_more( fMessage );
if rc = 0 then
result := false else
if rc = 1 then
result := true else
raise EZMQException.Create( 'zmq_msg_more return value undefined!' );
end;
{$endif}
function TZMQFrame.dup: TZMQFrame;
begin
result := TZMQFrame.create( size );
System.Move( data^, result.data^, size );
end;
function TZMQFrame.dump: Utf8String;
var
sUtf8: Utf8String;
iSize: Integer;
begin
// not complete.
iSize := size;
if iSize = 0 then
result := ''
else if AnsiChar(data^) = #0 then
begin
SetLength( sutf8, iSize * 2 );
BinToHex( data, PAnsiChar(sutf8), iSize );
result := sutf8;
end else
result := asUtf8String;
end;
function TZMQFrame.getAsByte: Byte;
begin
Result := Byte(data^);
end;
function TZMQFrame.getAsBytes: TBytes;
begin
SetLength(Result, size);
System.Move(data^, Result[0], size);
end;
function TZMQFrame.getAsHexString: AnsiString;
begin
SetLength( result, size * 2 );
BinToHex( data, PAnsiChar(result), size );
end;
function TZMQFrame.getAsInteger: Integer;
begin
result := Integer(data^);
end;
function TZMQFrame.getAsUtf8String: Utf8String;
begin
SetString( Result, PAnsiChar(data), size );
end;
procedure TZMQFrame.setAsByte(const Value: Byte);
var
iSize: Integer;
begin
iSize := SizeOf( Value );
rebuild( iSize );
Integer(data^) := Value;
end;
procedure TZMQFrame.setAsBytes(const Value: TBytes);
begin
rebuild(Length(Value));
System.Move(Value[0], data^, Length(Value));
end;
procedure TZMQFrame.setAsHexString( const Value: AnsiString );
var
iSize: Integer;
begin
iSize := Length( Value ) div 2;
rebuild( iSize );
HexToBin( PAnsiChar( value ), data, iSize );
end;
procedure TZMQFrame.setAsInteger( const Value: Integer );
var
iSize: Integer;
begin
iSize := SizeOf( Value );
rebuild( iSize );
Integer(data^) := Value;
end;
procedure TZMQFrame.setAsUtf8String( const Value: Utf8String );
var
iSize: Integer;
begin
iSize := Length( Value );
rebuild( iSize );
System.Move( Value[1], data^, iSize );
end;
procedure TZMQFrame.LoadFromStream( strm: TStream );
begin
strm.Position := 0;
if strm.size <> size then
rebuild( strm.Size );
strm.ReadBuffer( data^, strm.Size );
end;
procedure TZMQFrame.SaveToStream( strm: TStream );
begin
strm.WriteBuffer( data^, size );
end;
{ TZMQMsg }
constructor TZMQMsg.create;
begin
msgs := TList.Create;
csize := 0;
cursor := 0;
end;
destructor TZMQMsg.Destroy;
begin
Clear;
msgs.Free;
inherited;
end;
function TZMQMsg.size: Integer;
begin
result := msgs.Count;
end;
function TZMQMsg.content_size: Integer;
begin
result := csize;
end;
function TZMQMsg.push( msg: TZMQFrame ): Integer;
begin
try
msgs.Insert( 0, msg );
csize := csize + msg.size;
result := 0;
cursor := 0;
except
result := -1
end;
end;
function TZMQMsg.pushstr( str: Utf8String ): Integer;
var
frm: TZMQFrame;
begin
frm := TZMQFrame.create;
frm.asUtf8String := str;
result := push( frm );
end;
function TZMQMsg.pop: TZMQFrame;
begin
if size > 0 then
begin
result := msgs[0];
csize := csize - result.size;
msgs.Delete( 0 );
cursor := 0;
end else
result := nil;
end;
function TZMQMsg.popstr: Utf8String;
var
frame: TZMQFrame;
begin
frame := pop;
try
result := frame.asUtf8String;
finally
frame.Free;
end;
end;
function TZMQMsg.popint: Integer;
var
frame: TZMQFrame;
begin
frame := pop;
try
result := frame.asInteger;
finally
frame.Free;
end;
end;
function TZMQMsg.add( msg: TZMQFrame ): Integer;
begin
try
msgs.Add( msg );
csize := csize + msg.size;
result := 0;
cursor := 0;
except
result := -1;
end;
end;
function TZMQMsg.addstr( msg: Utf8String ): Integer;
var
frame: TZMQFrame;
begin
frame := TZMQFrame.create;
frame.asUtf8String := msg;
result := add( frame );
end;
function TZMQMsg.addByte(msg: Byte): Integer;
var
frame: TZMQFrame;
begin
frame := TZMQFrame.create( sizeOf( Byte ) );
frame.asByte := msg;
result := add( frame );
end;
function TZMQMsg.addBytes(const msg: TBytes): Integer;
var
frame: TZMQFrame;
begin
frame := TZMQFrame.create( Length(msg) );
frame.asBytes := msg;
result := add( frame );
end;
function TZMQMsg.addint( msg: Integer ): Integer;
var
frame: TZMQFrame;
begin
frame := TZMQFrame.create( sizeOf( Integer ) );
frame.asInteger := msg;
result := add( frame );
end;
procedure TZMQMsg.wrap( msg: TZMQFrame );
begin
push( TZMQFrame.create( 0 ) );
push( msg );
end;
function TZMQMsg.unwrap: TZMQFrame;
begin
result := pop;