-
Notifications
You must be signed in to change notification settings - Fork 1
/
zmq.cpp
418 lines (328 loc) · 13.8 KB
/
zmq.cpp
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
#include "hphp/runtime/base/base-includes.h"
#include "hphp/util/string-vsnprintf.h"
#include "zmq.h"
#include "zmq_common.h"
namespace HPHP {
const StaticString
s_code("code"),
s_message("message"),
s_ZMQ("ZMQ"),
s_ZMQSocketException("ZMQSocketException"),
s_SOCKET_PAIR("SOCKET_PAIR"),
s_SOCKET_PUB("SOCKET_PUB"),
s_SOCKET_SUB("SOCKET_SUB"),
#if ZMQ_VERSION_MAJOR >= 3
s_SOCKET_XSUB("SOCKET_XSUB"),
s_SOCKET_XPUB("SOCKET_XPUB"),
#endif
s_SOCKET_REQ("SOCKET_REQ"),
s_SOCKET_REP("SOCKET_REP"),
s_SOCKET_XREQ("SOCKET_XREQ"),
s_SOCKET_XREP("SOCKET_XREP"),
s_SOCKET_PUSH("SOCKET_PUSH"),
s_SOCKET_PULL("SOCKET_PULL"),
s_SOCKET_DEALER("SOCKET_DEALER"),
s_SOCKET_ROUTER("SOCKET_ROUTER"),
#if ZMQ_MAJOR_VERSION >= 4
s_SOCKET_STREAM("SOCKET_STREAM"),
#endif
s_SOCKET_UPSTREAM("SOCKET_UPSTREAM"),
s_SOCKET_DOWNSTREAM("SOCKET_DOWNSTREAM"),
#if ZMQ_VERSION_MAJOR == 3 && ZMQ_VERSION_MINOR == 0
s_MODE_SNDLABEL("MODE_SNDLABEL"),
#endif
s_POLL_IN("POLL_IN"),
s_POLL_OUT("POLL_OUT"),
s_MODE_SNDMORE("MODE_SNDMORE"),
s_MODE_NOBLOCK("MODE_NOBLOCK"),
s_MODE_DONTWAIT("MODE_DONTWAIT"),
s_DEVICE_FORWARDER("DEVICE_FORWARDER"),
s_DEVICE_QUEUE("DEVICE_QUEUE"),
s_DEVICE_STREAMER("DEVICE_STREAMER"),
s_ERR_INTERNAL("ERR_INTERNAL"),
s_ERR_EAGAIN("ERR_EAGAIN"),
s_ERR_ENOTSUP("ERR_ENOTSUP"),
s_ERR_EFSM("ERR_EFSM"),
s_ERR_ETERM("ERR_ETERM"),
s_SOCKOPT_HWM("SOCKOPT_HWM"),
s_SOCKOPT_SNDHWM("SOCKOPT_SNDHWM"),
s_SOCKOPT_RCVHWM("SOCKOPT_RCVHWM"),
s_SOCKOPT_AFFINITY("SOCKOPT_AFFINITY"),
s_SOCKOPT_IDENTITY("SOCKOPT_IDENTITY"),
s_SOCKOPT_RATE("SOCKOPT_RATE"),
s_SOCKOPT_RECOVERY_IVL("SOCKOPT_RECOVERY_IVL"),
s_SOCKOPT_SNDBUF("SOCKOPT_SNDBUF"),
s_SOCKOPT_RCVBUF("SOCKOPT_RCVBUF"),
s_SOCKOPT_LINGER("SOCKOPT_LINGER"),
s_SOCKOPT_RECONNECT_IVL("SOCKOPT_RECONNECT_IVL"),
s_SOCKOPT_RECONNECT_IVL_MAX("SOCKOPT_RECONNECT_IVL_MAX"),
s_SOCKOPT_BACKLOG("SOCKOPT_BACKLOG"),
s_SOCKOPT_MAXMSGSIZE("SOCKOPT_MAXMSGSIZE"),
s_SOCKOPT_SUBSCRIBE("SOCKOPT_SUBSCRIBE"),
s_SOCKOPT_UNSUBSCRIBE("SOCKOPT_UNSUBSCRIBE"),
s_SOCKOPT_TYPE("SOCKOPT_TYPE"),
s_SOCKOPT_RCVMORE("SOCKOPT_RCVMORE"),
s_SOCKOPT_FD("SOCKOPT_FD"),
s_SOCKOPT_EVENTS("SOCKOPT_EVENTS"),
s_SOCKOPT_SNDTIMEO("SOCKOPT_SNDTIMEO"),
s_SOCKOPT_RCVTIMEO("SOCKOPT_RCVTIMEO"),
s_SOCKOPT_IPV4ONLY("SOCKOPT_IPV4ONLY"),
s_SOCKOPT_LAST_ENDPOINT("SOCKOPT_LAST_ENDPOINT"),
s_SOCKOPT_TCP_KEEPALIVE("SOCKOPT_TCP_KEEPALIVE"),
s_SOCKOPT_TCP_KEEPALIVE_IDLE("SOCKOPT_TCP_KEEPALIVE_IDLE"),
s_SOCKOPT_TCP_KEEPALIVE_CNT("SOCKOPT_TCP_KEEPALIVE_CNT"),
s_SOCKOPT_TCP_KEEPALIVE_INTVL("SOCKOPT_TCP_KEEPALIVE_INTVL"),
s_SOCKOPT_TCP_ACCEPT_FILTER("SOCKOPT_TCP_ACCEPT_FILTER"),
s_SOCKOPT_DELAY_ATTACH_ON_CONNECT("SOCKOPT_DELAY_ATTACH_ON_CONNECT"),
s_SOCKOPT_XPUB_VERBOSE("SOCKOPT_XPUB_VERBOSE"),
s_SOCKOPT_ROUTER_RAW("SOCKOPT_ROUTER_RAW"),
s_SOCKOPT_IPV6("SOCKOPT_IPV6"),
s_SOCKOPT_PLAIN_SERVER("SOCKOPT_PLAIN_SERVER"),
s_SOCKOPT_PLAIN_USERNAME("SOCKOPT_PLAIN_USERNAME"),
s_SOCKOPT_PLAIN_PASSWORD("SOCKOPT_PLAIN_PASSWORD"),
s_SOCKOPT_CURVE_SERVER("SOCKOPT_CURVE_SERVER"),
s_SOCKOPT_CURVE_PUBLICKEY("SOCKOPT_CURVE_PUBLICKEY"),
s_SOCKOPT_CURVE_SECRETKEY("SOCKOPT_CURVE_SECRETKEY"),
s_SOCKOPT_CURVE_SERVERKEY("SOCKOPT_CURVE_SERVERKEY"),
s_SOCKOPT_PROBE_ROUTER("SOCKOPT_PROBE_ROUTER"),
s_SOCKOPT_REQ_CORRELATE("SOCKOPT_REQ_CORRELATE"),
s_SOCKOPT_REQ_RELAXED("SOCKOPT_REQ_RELAXED"),
s_SOCKOPT_CONFLATE("SOCKOPT_CONFLATE"),
s_SOCKOPT_ZAP_DOMAIN("SOCKOPT_ZAP_DOMAIN");
template<class T>
ALWAYS_INLINE
static T* getResource(const Object& obj, const char* varName) {
auto var = obj->o_get(varName, true, s_zmqsocket.get());
if (var.getType() == KindOfNull) {
return nullptr;
}
return var.asCResRef().getTyped<T>();
}
ALWAYS_INLINE
static Variant setVariable(const Object& obj, const char* varName, const Variant& varValue) {
return obj->o_set(varName, varValue, s_zmqsocket.get());
}
static void throwException(const String& clsName, int code, const char *fmt, ...) {
Class* cls = Unit::loadClass(clsName.get());
ObjectData* obj = ObjectData::newInstance(cls);
obj->o_set(s_code, code, clsName);
std::string msg;
va_list ap;
va_start(ap, fmt);
string_vsnprintf(msg, fmt, ap);
va_end(ap);
obj->o_set(s_message, msg, clsName);
throw Object(obj);
}
// ZMQContext
static void HHVM_METHOD(ZMQContext, __construct, int64_t io_threads) {
ContextData *context = ContextData::GetPersistent(io_threads);
if (context == nullptr) {
context = new ContextData(io_threads);
}
ContextData::SetPersistent(io_threads, context);
this_->o_set(s_context, context, s_zmqcontext.get());
}
// ZMQSocket
static void HHVM_METHOD(ZMQSocket, __construct, const Object& zmqcontext, int64_t type, const String& persistent_id, const Object& on_new_socket) {
auto context = get_context(zmqcontext);
auto socket = newres<SocketData>(context->get(), type);
setVariable(this_, "socket", Resource(socket));
}
static Object HHVM_METHOD(ZMQSocket, bind, const String& dsn, bool force) {
auto socket = getResource<SocketData>(this_, "socket");
int result = zmq_bind(socket->get(), dsn.c_str());
if (result != 0) {
throwException(s_ZMQSocketException, errno, "Failed to bind the ZMQ: %s", zmq_strerror(errno));
}
return this_;
}
static Object HHVM_METHOD(ZMQSocket, unbind, const String& dsn) {
auto socket = getResource<SocketData>(this_, "socket");
int result = zmq_unbind(socket->get(), dsn.c_str());
if (result != 0) {
throwException(s_ZMQSocketException, errno, "Failed to unbind the ZMQ socket: %s", zmq_strerror(errno));
}
return this_;
}
static Object HHVM_METHOD(ZMQSocket, connect, const String& dsn, bool force) {
auto socket = getResource<SocketData>(this_, "socket");
int result = zmq_connect(socket->get(), dsn.c_str());
if (result != 0) {
throwException(s_ZMQSocketException, errno, "Error creating socket: %s", zmq_strerror(errno));
}
return this_;
}
static Object HHVM_METHOD(ZMQSocket, disconnect, const String& dsn) {
auto socket = getResource<SocketData>(this_, "socket");
int result = zmq_disconnect(socket->get(), dsn.c_str());
if (result != 0) {
throwException(s_ZMQSocketException, errno, "Failed to disconnect the ZMQ socket: %s", zmq_strerror(errno));
}
return this_;
}
static Variant HHVM_METHOD(ZMQSocket, recv, int64_t mode) {
auto socket = getResource<SocketData>(this_, "socket");
zmq_msg_t msg;
int result = zmq_msg_init(&msg);
if (result != 0) {
throwException(s_ZMQSocketException, errno, "Failed to initialize message structure: %s", zmq_strerror(errno));
}
result = zmq_msg_recv(&msg, socket->get(), mode);
int errno_ = errno;
if (result == -1) {
zmq_msg_close(&msg);
if (errno_ == EAGAIN) {
return false;
}
throwException(s_ZMQSocketException, errno, "Failed to receive message: %s", zmq_strerror(errno));
}
std::string data((const char*) zmq_msg_data(&msg), zmq_msg_size(&msg));
zmq_msg_close(&msg);
return data;
}
static Variant HHVM_METHOD(ZMQSocket, send, const String& message, int64_t mode) {
auto socket = getResource<SocketData>(this_, "socket");
int result = zmq_send(socket->get(), message.c_str(), message.size(), mode);
if (result == -1) {
if (errno == EAGAIN) {
return false;
}
throwException(s_ZMQSocketException, errno, "Failed to send message: %s", zmq_strerror(errno));
}
return this_;
}
static Object HHVM_METHOD(ZMQSocket, setSockOpt, int key, const Variant& value) {
auto socket = getResource<SocketData>(this_, "socket");
int result = 0;
if(value.isNumeric()==true){
int64_t val = value.toInt64();
size_t size = sizeof(val);
result = zmq_setsockopt(socket->get(), key, &val, size);
}
else{
String sValue = value.toString();
result = zmq_setsockopt(socket->get(), key, sValue.c_str(), sValue.size());
}
if (result != 0) {
throwException(s_ZMQSocketException, errno, "Failed to set socket option: %s", zmq_strerror(errno));
}
return this_;
}
static int64_t HHVM_METHOD(ZMQSocket, getSocketType) {
auto socket = getResource<SocketData>(this_, "socket");
int type;
size_t type_size;
type_size = sizeof(int);
int result = zmq_getsockopt(socket->get(), ZMQ_TYPE, &type, &type_size);
if (result != -1) {
return type;
}
return -1;
}
static class ZMQExtension : public Extension {
public:
ZMQExtension() : Extension("zmq") {}
virtual void moduleInit() {
HHVM_ME(ZMQContext, __construct);
HHVM_ME(ZMQSocket, __construct);
HHVM_ME(ZMQSocket, bind);
HHVM_ME(ZMQSocket, unbind);
HHVM_ME(ZMQSocket, connect);
HHVM_ME(ZMQSocket, disconnect);
HHVM_ME(ZMQSocket, recv);
HHVM_ME(ZMQSocket, send);
HHVM_ME(ZMQSocket, setSockOpt);
HHVM_ME(ZMQSocket, getSocketType);
#define REGISTER_ZMQ_CONST_INT(name, value) \
Native::registerClassConstant<KindOfInt64>(s_ZMQ.get(), name.get(), \
(int64_t) value); \
REGISTER_ZMQ_CONST_INT(s_SOCKET_PAIR, ZMQ_PAIR);
REGISTER_ZMQ_CONST_INT(s_SOCKET_PUB, ZMQ_PUB);
REGISTER_ZMQ_CONST_INT(s_SOCKET_SUB, ZMQ_SUB);
#if ZMQ_VERSION_MAJOR >= 3
REGISTER_ZMQ_CONST_INT(s_SOCKET_XSUB, ZMQ_XSUB);
REGISTER_ZMQ_CONST_INT(s_SOCKET_XPUB, ZMQ_XPUB);
#endif
REGISTER_ZMQ_CONST_INT(s_SOCKET_REQ, ZMQ_REQ);
REGISTER_ZMQ_CONST_INT(s_SOCKET_REP, ZMQ_REP);
REGISTER_ZMQ_CONST_INT(s_SOCKET_XREQ, ZMQ_XREQ);
REGISTER_ZMQ_CONST_INT(s_SOCKET_XREP, ZMQ_XREP);
REGISTER_ZMQ_CONST_INT(s_SOCKET_PUSH, ZMQ_PUSH);
REGISTER_ZMQ_CONST_INT(s_SOCKET_PULL, ZMQ_PULL);
REGISTER_ZMQ_CONST_INT(s_SOCKET_DEALER, ZMQ_DEALER);
REGISTER_ZMQ_CONST_INT(s_SOCKET_ROUTER, ZMQ_ROUTER);
#if ZMQ_MAJOR_VERSION >= 4
REGISTER_ZMQ_CONST_INT(s_SOCKET_STREAM, ZMQ_STREAM);
#endif
REGISTER_ZMQ_CONST_INT(s_SOCKET_UPSTREAM, ZMQ_PULL);
REGISTER_ZMQ_CONST_INT(s_SOCKET_DOWNSTREAM, ZMQ_PUSH);
#if ZMQ_VERSION_MAJOR == 3 && ZMQ_VERSION_MINOR == 0
REGISTER_ZMQ_CONST_INT(s_MODE_SNDLABEL, ZMQ_SNDLABEL);
#endif
REGISTER_ZMQ_CONST_INT(s_POLL_IN, ZMQ_POLLIN);
REGISTER_ZMQ_CONST_INT(s_POLL_OUT, ZMQ_POLLOUT);
REGISTER_ZMQ_CONST_INT(s_MODE_SNDMORE, ZMQ_SNDMORE);
REGISTER_ZMQ_CONST_INT(s_MODE_NOBLOCK, ZMQ_DONTWAIT);
REGISTER_ZMQ_CONST_INT(s_MODE_DONTWAIT, ZMQ_DONTWAIT);
REGISTER_ZMQ_CONST_INT(s_DEVICE_FORWARDER, ZMQ_FORWARDER);
REGISTER_ZMQ_CONST_INT(s_DEVICE_QUEUE, ZMQ_QUEUE);
REGISTER_ZMQ_CONST_INT(s_DEVICE_STREAMER, ZMQ_STREAMER);
REGISTER_ZMQ_CONST_INT(s_ERR_INTERNAL, -99);
REGISTER_ZMQ_CONST_INT(s_ERR_EAGAIN, EAGAIN);
REGISTER_ZMQ_CONST_INT(s_ERR_ENOTSUP, ENOTSUP);
REGISTER_ZMQ_CONST_INT(s_ERR_EFSM, EFSM);
REGISTER_ZMQ_CONST_INT(s_ERR_ETERM, ETERM);
#if ZMQ_VERSION_MAJOR >= 3
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_SNDHWM, ZMQ_SNDHWM);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_RCVHWM, ZMQ_RCVHWM);
#else
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_HWM, ZMQ_HWM);
#endif
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_AFFINITY, ZMQ_AFFINITY);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_IDENTITY, ZMQ_IDENTITY);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_RATE, ZMQ_RATE);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_RECOVERY_IVL, ZMQ_RECOVERY_IVL);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_SNDBUF, ZMQ_SNDBUF);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_RCVBUF, ZMQ_RCVBUF);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_LINGER, ZMQ_LINGER);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_RECONNECT_IVL, ZMQ_RECONNECT_IVL);
REGISTER_ZMQ_CONST_INT(s_SOCK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ZMQ_CONST_INT(s_SOCKOPT_DELAY_ATTACH_ON_CONNECT, ZMQ_DELAY_ATTACH_ON_CONNECT);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_XPUB_VERBOSE, ZMQ_XPUB_VERBOSE);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_ROUTER_RAW, ZMQ_ROUTER_RAW);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_IPV6, ZMQ_IPV6);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_PLAIN_SERVER, ZMQ_PLAIN_SERVER);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_PLAIN_USERNAME, ZMQ_PLAIN_USERNAME);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_PLAIN_PASSWORD, ZMQ_PLAIN_PASSWORD);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_CURVE_SERVER, ZMQ_CURVE_SERVER);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_CURVE_PUBLICKEY, ZMQ_CURVE_PUBLICKEY);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_CURVE_SECRETKEY, ZMQ_CURVE_SECRETKEY);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_CURVE_SERVERKEY, ZMQ_CURVE_SERVERKEY);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_PROBE_ROUTER, ZMQ_PROBE_ROUTER);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_REQ_CORRELATE, ZMQ_REQ_CORRELATE);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_REQ_RELAXED, ZMQ_REQ_RELAXED);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_CONFLATE, ZMQ_CONFLATE);
REGISTER_ZMQ_CONST_INT(s_SOCKOPT_ZAP_DOMAIN, ZMQ_ZAP_DOMAIN);
loadSystemlib();
}
} s_zmq_extension;
HHVM_GET_MODULE(zmq)
} // namespace HPHP