-
Notifications
You must be signed in to change notification settings - Fork 49
/
ZMQ.API.pas
277 lines (241 loc) · 9.69 KB
/
ZMQ.API.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
unit ZMQ.API;
{ Interface unit for cross-platform ZeroMQ libaries }
interface
{$IF Defined(ANDROID) or Defined(IOS) or Defined(MACOS64)}
{$DEFINE STATIC_BINDING}
{$ELSE}
{$DEFINE DYNAMIC_BINDING}
{$ENDIF}
const
{$IF Defined(MSWINDOWS)}
{$IFDEF WIN64}
ZMQ_LIB = 'libzmq-win64.dll';
_PU = '';
{$ELSE}
ZMQ_LIB = 'libzmq-win32.dll';
_PU = '';
{$ENDIF}
{$ELSEIF Defined(LINUX)}
ZMQ_LIB = 'libzmq-linux64.so';
_PU = '';
{$ELSEIF Defined(MACOS)}
{$IFDEF IOS}
{$IFDEF CPUARM}
ZMQ_LIB = 'libzmq-ios.a';
_PU = '';
{$ELSE}
ZMQ_LIB = 'libzmq-iossim.dylib'; { not supported }
_PU = '';
{$ENDIF}
{$ELSE}
{$IFDEF MACOS64}
ZMQ_LIB = 'libzmq-osx64.a';
_PU = '';
{$ELSE}
ZMQ_LIB = 'libzmq-osx32.dylib';
_PU = '_';
{$ENDIF}
{$ENDIF}
{$ELSEIF Defined(ANDROID64)}
ZMQ_LIB = 'libzmq-android64.a';
_PU = '';
{$ELSEIF Defined(ANDROID32)}
ZMQ_LIB = 'libzmq-android32.a';
_PU = '';
{$ENDIF}
const
ZMQ_PAIR = 0;
ZMQ_PUB = 1;
ZMQ_SUB = 2;
ZMQ_REQ = 3;
ZMQ_REP = 4;
ZMQ_DEALER = 5;
ZMQ_ROUTER = 6;
ZMQ_PULL = 7;
ZMQ_PUSH = 8;
ZMQ_XPUB = 9;
ZMQ_XSUB = 10;
ZMQ_STREAM = 11;
ZMQ_SNDMORE = 2;
ZMQ_RCVMORE = 13;
ZMQ_LINGER = 17;
ZMQ_SNDHWM = 23;
ZMQ_RCVHWM = 24;
ZMQ_IPV6 = 42;
ZMQ_CURVE_SERVER = 47;
ZMQ_CURVE_PUBLICKEY = 48;
ZMQ_CURVE_SECRETKEY = 49;
ZMQ_CURVE_SERVERKEY = 50;
ZMQ_POLLIN = 1;
ZMQ_POLLOUT = 2;
type
zmq_msg_t = record
opaque: array[0..63] of Byte;
end;
pzmq_msg_t = ^zmq_msg_t;
zmq_pollitem_t = record
socket: Pointer;
{$IFDEF MSWINDOWS}
fd: NativeInt;
{$ELSE}
fd: Integer;
{$ENDIF}
events: SmallInt;
revents: SmallInt;
end;
pzmq_pollitem_t = ^zmq_pollitem_t;
{$IFDEF STATIC_BINDING}
{ ZeroMQ contexts }
function zmq_ctx_new(): Pointer; cdecl; external ZMQ_LIB name _PU + 'zmq_ctx_new'
{$IF Defined(MACOS)}
{$IFDEF IOS}
{$IFDEF CPUARM}
dependency 'c++'
{$ENDIF}
{$ELSE}
{$IFDEF MACOS64}
dependency 'c++'
{$ENDIF}
{$ENDIF}
{$ELSEIF Defined(ANDROID64)}
dependency 'c++_static' dependency 'c++abi' // -lc++_static -lc++abi
{$ELSEIF Defined(ANDROID32)}
dependency 'gnustl_static'
{$ENDIF};
function zmq_ctx_term(context: Pointer): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_ctx_term';
{ ZeroMQ messages }
function zmq_msg_init(msg: pzmq_msg_t): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_msg_init';
function zmq_msg_init_size(msg: pzmq_msg_t; size: NativeUInt): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_msg_init_size';
function zmq_msg_send(msg: pzmq_msg_t; socket: Pointer; flags: Integer): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_msg_send';
function zmq_msg_recv(msg: pzmq_msg_t; socket: Pointer; flags: Integer): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_msg_recv';
function zmq_msg_close(msg: pzmq_msg_t): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_msg_close';
function zmq_msg_data(msg: pzmq_msg_t): Pointer; cdecl; external ZMQ_LIB name _PU + 'zmq_msg_data';
function zmq_msg_size(msg: pzmq_msg_t): NativeUInt; cdecl; external ZMQ_LIB name _PU + 'zmq_msg_size';
{ ZeroMQ sockets }
function zmq_socket(context: Pointer; _type: Integer): Pointer; cdecl; external ZMQ_LIB name _PU + 'zmq_socket';
function zmq_close(socket: Pointer): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_close';
function zmq_setsockopt(socket: Pointer; option_name: Integer; const option_value: Pointer; option_len: NativeUInt): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_setsockopt';
function zmq_getsockopt (socket: Pointer; option_name: Integer; option_value: Pointer; option_len: PNativeUInt): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_getsockopt';
function zmq_bind(socket: Pointer; const endpoint: MarshaledAString): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_bind';
function zmq_connect(socket: Pointer; const endpoint: MarshaledAString): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_connect';
function zmq_unbind(socket: Pointer; const endpoint: MarshaledAString): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_unbind';
function zmq_disconnect(socket: Pointer; const endpoint: MarshaledAString): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_disconnect';
function zmq_sendmsg(socket: Pointer; msg: pzmq_msg_t; flags: Integer): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_sendmsg';
function zmq_recvmsg(socket: Pointer; msg: pzmq_msg_t; flags: Integer): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_recvmsg';
{ ZeroMQ polling }
function zmq_poll(items: pzmq_pollitem_t; nitems: Integer; timeout: {$IFDEF POSIX}NativeInt{$ELSE}Integer{$ENDIF}): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_poll';
{ ZeroMQ Curve security }
function zmq_z85_decode(dest: PByte; str: MarshaledAString): PByte; cdecl; external ZMQ_LIB name _PU + 'zmq_z85_decode';
function zmq_curve_keypair(z85_public_key, z85_secret_key: MarshaledAString): Integer; cdecl; external ZMQ_LIB name _PU + 'zmq_curve_keypair';
{$ENDIF}
{$IFDEF DYNAMIC_BINDING}
var
{ ZeroMQ contexts }
zmq_ctx_new: function: Pointer; cdecl = nil;
zmq_ctx_term: function(context: Pointer): Integer; cdecl = nil;
{ ZeroMQ messages }
zmq_msg_init: function(msg: pzmq_msg_t): Integer; cdecl = nil;
zmq_msg_init_size: function(msg: pzmq_msg_t; size: NativeUInt): Integer; cdecl = nil;
zmq_msg_send: procedure(msg: pzmq_msg_t; socket: Pointer; flags: Integer); cdecl = nil;
zmq_msg_recv: procedure(msg: pzmq_msg_t; socket: Pointer; flags: Integer); cdecl = nil;
zmq_msg_close: function(msg: pzmq_msg_t): Integer; cdecl = nil;
zmq_msg_data: function(msg: pzmq_msg_t): Pointer; cdecl = nil;
zmq_msg_size: function(msg: pzmq_msg_t): NativeUInt; cdecl = nil;
{ ZeroMQ sockets }
zmq_socket: function(context: Pointer; _type: Integer): Pointer; cdecl = nil;
zmq_close: function(socket: Pointer): Integer; cdecl = nil;
zmq_setsockopt: function(socket: Pointer; option_name: Integer; const option_value: Pointer; option_len: NativeUInt): Integer; cdecl = nil;
zmq_getsockopt: function(socket: Pointer; option_name: Integer; option_value: Pointer; option_len: PNativeUInt): Integer; cdecl = nil;
zmq_bind: function(socket: Pointer; const endpoint: MarshaledAString): Integer; cdecl = nil;
zmq_connect: function(socket: Pointer; const endpoint: MarshaledAString): Integer; cdecl = nil;
zmq_unbind: function(socket: Pointer; const endpoint: MarshaledAString): Integer; cdecl = nil;
zmq_disconnect: function(socket: Pointer; const endpoint: MarshaledAString): Integer; cdecl = nil;
zmq_sendmsg: function(socket: Pointer; msg: pzmq_msg_t; flags: Integer): Integer; cdecl = nil;
zmq_recvmsg: function(socket: Pointer; msg: pzmq_msg_t; flags: Integer): Integer; cdecl = nil;
{ ZeroMQ polling }
zmq_poll: function(items: pzmq_pollitem_t; nitems: Integer; timeout: {$IFDEF POSIX}NativeInt{$ELSE}Integer{$ENDIF}): Integer; cdecl = nil;
{ ZeroMQ Curve security }
zmq_z85_decode: function(dest: PByte; str: MarshaledAString): PByte; cdecl = nil;
zmq_curve_keypair: function(z85_public_key, z85_secret_key: MarshaledAString): Integer; cdecl = nil;
{$ENDIF}
implementation
uses
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF}
System.SysUtils;
{$IFDEF DYNAMIC_BINDING}
var
ZeroMQHandle: HMODULE = 0;
{ Library }
function LoadLib(const ALibFile: String): HMODULE;
begin
Result := LoadLibrary(PChar(ALibFile));
end;
function FreeLib(ALibModule: HMODULE): Boolean;
begin
Result := FreeLibrary(ALibModule);
end;
function GetProc(AModule: HMODULE; const AProcName: String): Pointer;
begin
Result := GetProcAddress(AModule, PChar(AProcName));
if (Result = nil) then
begin
{$IFDEF CONSOLE}
Writeln(Format('%s is not found', [AProcName]));
{$ENDIF}
raise Exception.CreateFmt('%s is not found', [AProcName]);
end;
end;
procedure LoadZeroMQ;
begin
if (ZeroMQHandle <> 0) then Exit;
ZeroMQHandle := LoadLib(ZMQ_LIB);
if (ZeroMQHandle = 0) then
begin
{$IFDEF CONSOLE}
Writeln(Format('Load %s failed. You must deploy the library with your module.', [ZMQ_LIB]));
{$ENDIF}
raise Exception.CreateFmt('Load %s failed. You must deploy the library with your module.', [ZMQ_LIB]);
Exit;
end;
{ ZeroMQ contexts }
zmq_ctx_new := GetProc(ZeroMQHandle, 'zmq_ctx_new');
zmq_ctx_term := GetProc(ZeroMQHandle, 'zmq_ctx_term');
{ ZeroMQ messages }
zmq_msg_init := GetProc(ZeroMQHandle, 'zmq_msg_init');
zmq_msg_init_size := GetProc(ZeroMQHandle, 'zmq_msg_init_size');
zmq_msg_send := GetProc(ZeroMQHandle, 'zmq_msg_send');
zmq_msg_recv := GetProc(ZeroMQHandle, 'zmq_msg_recv');
zmq_msg_close := GetProc(ZeroMQHandle, 'zmq_msg_close');
zmq_msg_data := GetProc(ZeroMQHandle, 'zmq_msg_data');
zmq_msg_size := GetProc(ZeroMQHandle, 'zmq_msg_size');
{ ZeroMQ sockets }
zmq_socket := GetProc(ZeroMQHandle, 'zmq_socket');
zmq_close := GetProc(ZeroMQHandle, 'zmq_close');
zmq_setsockopt := GetProc(ZeroMQHandle, 'zmq_setsockopt');
zmq_getsockopt := GetProc(ZeroMQHandle, 'zmq_getsockopt');
zmq_bind := GetProc(ZeroMQHandle, 'zmq_bind');
zmq_connect := GetProc(ZeroMQHandle, 'zmq_connect');
zmq_unbind := GetProc(ZeroMQHandle, 'zmq_unbind');
zmq_disconnect := GetProc(ZeroMQHandle, 'zmq_disconnect');
zmq_sendmsg := GetProc(ZeroMQHandle, 'zmq_sendmsg');
zmq_recvmsg := GetProc(ZeroMQHandle, 'zmq_recvmsg');
{ ZeroMQ polling }
zmq_poll := GetProc(ZeroMQHandle, 'zmq_poll');
{ ZeroMQ Curve security }
zmq_z85_decode := GetProc(ZeroMQHandle, 'zmq_z85_decode');
zmq_curve_keypair := GetProc(ZeroMQHandle, 'zmq_curve_keypair');
end;
procedure UnloadZeroMQ;
begin
if (ZeroMQHandle = 0) then Exit;
FreeLib(ZeroMQHandle);
ZeroMQHandle := 0;
end;
initialization
LoadZeroMQ;
finalization
UnloadZeroMQ;
{$ENDIF}
end.