-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzmqhelper.cpp
259 lines (196 loc) · 5.28 KB
/
zmqhelper.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
#include "config.h"
#include <assert.h> /* for assert */
#include <stdlib.h> /* for size_t */
#include <sys/time.h> /* for gettimeofday */
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <zmq.h>
#include "zmqhelper.h"
#if DEBUG
#include "integrator.h"
#endif
using namespace std;
zmqx_sigfunc _zmqx_sigfunc=NULL;
int _zmqx_sigfunc_context=0;
void *_zmqx_sigfunc_object=NULL;
int _zmqx_interrupted=0;
static int _zmqx_interrupted_in_process=0;
void zmqx_register_handler(zmqx_sigfunc function, int context, void *object)
{
_zmqx_sigfunc = function;
_zmqx_sigfunc_context = context;
_zmqx_sigfunc_object = object;
//reset signal checks
_zmqx_interrupted_in_process=0;
_zmqx_interrupted=0;
}
void zmqx_signal_handler (int signal_value)
{
_zmqx_interrupted = 1;
}
void zmqx_catch_signals (void)
{
struct sigaction action;
action.sa_handler = zmqx_signal_handler;
action.sa_flags = 0;
sigemptyset (&action.sa_mask);
sigaction (SIGINT, &action, NULL);
sigaction (SIGTERM, &action, NULL);
}
void zmqx_interrupt_check()
{
/* check for interrupt but don't recheck if already processing interrupt */
if (_zmqx_interrupted && !_zmqx_interrupted_in_process) {
#if DEBUG
CERR << "zmqx_interrupt_check interrupt detected" << endl;
#endif
_zmqx_interrupted_in_process = 1;
if (_zmqx_sigfunc != NULL) {
#if DEBUG
CERR << "zmqx_interrupt_check calling callback" << endl;
#endif
_zmqx_sigfunc(_zmqx_sigfunc_object);
}
}
}
int zmqx_recv(void *socket, string &buf) {
char buffer [256];
int size = 0;
zmqx_interrupt_check();
size = zmq_recv (socket, buffer, 255, 0);
if (size >= 0) {
if (size > 255) {
size = 255;
}
buffer[size] = 0;
buf.assign(buffer);
}
else if (size == -1) {
assert(errno == EINTR);
}
else {
assert(0);
}
zmqx_interrupt_check();
return size;
}
int zmqx_recv(void *socket, uint8_t* &buf, uint32_t buf_size) {
int size;
zmqx_interrupt_check();
buf = new uint8_t[buf_size];
size = zmq_recv(socket, buf, buf_size, 0);
assert((size >= 0 && size <= buf_size)
|| (size == -1 && errno == EINTR)
);
zmqx_interrupt_check();
return size;
}
int zmqx_irecv(void *socket, string &buf) {
char buffer [256];
int size = 0;
zmqx_interrupt_check();
size = zmq_recv(socket, buffer, 255, ZMQ_DONTWAIT);
if (size >= 0) {
if (size > 255) {
size = 255;
}
buffer[size] = 0;
buf.assign(buffer);
}
else if (size == -1) {
assert(errno == EINTR || errno == EAGAIN);
buf.clear();
}
else {
assert(0);
}
zmqx_interrupt_check();
return size;
}
int zmqx_send(void *socket, const string &s) {
int size;
zmqx_interrupt_check();
do{
size = zmq_send(socket, s.data(), s.size(), 0);
assert(size == s.size()
|| (size == -1 && errno == EINTR)
);
zmqx_interrupt_check();
}while(size < 0);
return size;
}
int zmqx_send(void *socket, uint8_t *buf, uint32_t buf_size) {
int size;
zmqx_interrupt_check();
size = zmq_send(socket, buf, buf_size, 0);
assert((size >= 0 && size <= buf_size)
|| (size == -1 && errno == EINTR)
);
zmqx_interrupt_check();
return size;
}
int zmqx_sendmore(void *socket, const string &s) {
int size;
zmqx_interrupt_check();
do{
size = zmq_send(socket, s.data(), s.size(), ZMQ_SNDMORE);
assert(size == s.size()
|| (size == -1 && errno == EINTR)
);
zmqx_interrupt_check();
//if we are here then we ignored the signal.
}while(size < 0);
return size;
}
int zmqx_sendmore(void *socket, uint8_t *buf, uint32_t buf_size) {
int size;
zmqx_interrupt_check();
size = zmq_send(socket, buf, buf_size, ZMQ_SNDMORE);
assert(size == buf_size
|| (size == -1 && errno == EINTR)
);
zmqx_interrupt_check();
return size;
}
int zmqx_send(void *socket, int context, const string &command) {
int totalSize = 0;
zmqx_interrupt_check();
totalSize += zmqx_sendmore(socket, context);
totalSize += zmqx_send (socket, command);
zmqx_interrupt_check();
return totalSize;
}
int zmqx_sendmore(void *socket, int context, const string &command) {
int totalSize = 0;
zmqx_interrupt_check();
totalSize += zmqx_sendmore(socket, context);
totalSize += zmqx_sendmore(socket, command);
zmqx_interrupt_check();
return totalSize;
}
int zmqx_poll(zmq_pollitem_t* socks, uint32_t buff_size)
{
zmqx_interrupt_check();
int rc = zmq_poll(socks, buff_size, 0);
assert(rc >= 0
|| (rc == -1 && errno == EINTR)
);
zmqx_interrupt_check();
return rc;
}
unsigned long zmqx_utime()
{
struct timeval tv;
zmqx_interrupt_check();
(void) gettimeofday(&tv, NULL);
zmqx_interrupt_check();
return tv.tv_sec*1000000 + tv.tv_usec;
}
#define randof(num) (int) ((float) (num) * random () / (RAND_MAX + 1.0))
string gen_id() {
char identity [16];
srandom(zmqx_utime());
sprintf (identity, "%05d-%04X-%04X", getpid(), randof (0x10000), randof (0x10000));
return string(identity);
}