forked from EmielM/zmqcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmqcat.c
210 lines (167 loc) · 4.52 KB
/
zmqcat.c
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
/*
* (C) Emiel Mols, 2012. Released under the Simplified BSD License.
* Attribution is very much appreciated.
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <getopt.h>
#include <zmq.h>
#define SEND_BUFFER_SIZE 8192
void zmqcat_recv(void* socket, int type, int verbose) {
if (type == ZMQ_PUSH || type == ZMQ_PUB)
return;
int ok;
int64_t rcvmore = 0;
size_t rcvmore_size = sizeof(rcvmore);
zmq_msg_t msg;
ok = zmq_msg_init(&msg);
if (ok < 0) {
fprintf(stderr, "error %d: %s\n", errno, zmq_strerror(errno));
return;
}
// Read single message (all frames) and dump to stdout.
do {
ok = zmq_msg_recv(&msg, socket, 0);
if (ok < 0) {
fprintf(stderr, "error %d: %s\n", errno, zmq_strerror(errno));
return;
}
zmq_getsockopt(socket, ZMQ_RCVMORE, &rcvmore, &rcvmore_size);
size_t size = zmq_msg_size(&msg);
if (verbose)
fprintf(stderr, "receiving %ld bytes\n", size);
fwrite(zmq_msg_data(&msg), 1, size, stdout);
} while (rcvmore);
}
void zmqcat_send(void* socket, int type, int verbose) {
char *sbuf, *tbuf;
if (type == ZMQ_PULL || type == ZMQ_SUB)
return;
// Read stdin into a forward linked buffer. First
// chunk is on the stack, subsequent chunks are
// allocated on the heap when necessary.
char stack_buffer[SEND_BUFFER_SIZE + sizeof(char *)];
*(char **)(stack_buffer+SEND_BUFFER_SIZE) = NULL;
char* buffer = stack_buffer;
size_t total = 0;
while (1) {
size_t read = fread(buffer, 1, SEND_BUFFER_SIZE, stdin);
total += read;
if (read != SEND_BUFFER_SIZE)
break;
char *new_buffer = malloc(SEND_BUFFER_SIZE + sizeof(char *));
*((char **)&new_buffer[SEND_BUFFER_SIZE]) = NULL;
*((char **)&buffer[SEND_BUFFER_SIZE]) = new_buffer;
buffer = new_buffer;
}
int ok;
zmq_msg_t msg;
ok = zmq_msg_init_size(&msg, total);
if (ok < 0) {
fprintf(stderr, "error %d: %s\n", errno, zmq_strerror(errno));
goto dealloc;
}
char *msg_at = (char *)zmq_msg_data(&msg);
char *msg_end = msg_at + total;
buffer = stack_buffer;
while (buffer != NULL) {
size_t chunk_size = SEND_BUFFER_SIZE;
if (msg_at + chunk_size >= msg_end)
chunk_size = msg_end - msg_at;
memcpy(msg_at, buffer, chunk_size);
buffer = *(char **)&buffer[SEND_BUFFER_SIZE];
msg_at += chunk_size;
}
if (verbose)
fprintf(stderr, "sending %ld bytes\n", total);
ok = zmq_msg_send(&msg, socket, 0);
if (ok < 0)
fprintf(stderr, "error %d: %s\n", errno, zmq_strerror(errno));
dealloc:
sbuf = *(char **)&stack_buffer[SEND_BUFFER_SIZE];
while (sbuf) {
tbuf = sbuf;
sbuf = *(char **)&sbuf[SEND_BUFFER_SIZE];
free(tbuf);
}
}
int main(int argc, char *argv[]) {
int type = ZMQ_PUSH;
char *endpoint = NULL;
int bind = 0;
int verbose = 0;
char c;
while ((c = getopt(argc, argv, "t:e:bv")) != -1) {
if (c == 't') {
if (!strcasecmp(optarg, "pull"))
type = ZMQ_PULL;
else if (!strcasecmp(optarg, "req"))
type = ZMQ_REQ;
else if (!strcasecmp(optarg, "rep"))
type = ZMQ_REP;
else if (!strcasecmp(optarg, "pub"))
type = ZMQ_PUB;
else if (!strcasecmp(optarg, "sub"))
type = ZMQ_PUB;
}
else if (c == 'e') {
endpoint = optarg;
}
else if (c == 'b') {
bind = 1;
}
else if (c == 'v') {
verbose = 1;
}
}
if (!endpoint) {
fprintf(stderr, "usage: %s [-t type] -e endpoint [-b] [-v]\n", argv[0]);
fprintf(stderr, " -t : PUSH | PULL | REQ | REP | PUB | SUB\n");
fprintf(stderr, " -e : endpoint, e.g. \"tcp://127.0.0.1:5000\"\n");
fprintf(stderr, " -b : bind instead of connect\n");
fprintf(stderr, " -v : verbose output to stderr\n");
return 254;
}
void *ctx = zmq_init(1);
if (ctx == NULL) {
fprintf(stderr, "error %d: %s\n", errno, zmq_strerror(errno));
return 1;
}
void *socket = zmq_socket(ctx, type);
if (socket == NULL) {
fprintf(stderr, "error %d: %s\n", errno, zmq_strerror(errno));
return 1;
}
int ok;
if (bind)
ok = zmq_bind(socket, endpoint);
else
ok = zmq_connect(socket, endpoint);
if (ok < 0) {
fprintf(stderr, "error %d: %s\n", errno, zmq_strerror(errno));
return 1;
}
if (verbose)
fprintf(stderr, "%s to %s\n", (bind ? "bound" : "connecting"), endpoint);
if (type == ZMQ_REP) {
zmqcat_recv(socket, type, verbose);
zmqcat_send(socket, type, verbose);
}
else {
zmqcat_send(socket, type, verbose);
zmqcat_recv(socket, type, verbose);
}
ok = zmq_close(socket);
if (ok < 0) {
fprintf(stderr, "error %d: %s\n", errno, zmq_strerror(errno));
return 1;
}
ok = zmq_term(ctx);
if (ok < 0) {
fprintf(stderr, "error %d: %s\n", errno, zmq_strerror(errno));
return 1;
}
return 0;
}