/
zstr.c
213 lines (179 loc) · 6.02 KB
/
zstr.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
211
212
213
/* =========================================================================
zstr - sending and receiving strings
-------------------------------------------------------------------------
Copyright (c) 1991-2013 iMatix Corporation <www.imatix.com>
Copyright other contributors as noted in the AUTHORS file.
This file is part of CZMQ, the high-level C binding for 0MQ:
http://czmq.zeromq.org.
This 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.
This software 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/>.
=========================================================================
*/
/*
@header
The zstr class provides utility functions for sending and receiving C
strings across 0MQ sockets. It sends strings without a terminating null,
and appends a null byte on received strings. This class is for simple
message sending.
@discuss
@end
*/
#include "../include/czmq.h"
static int
s_send_string (void *zocket, bool more, const char *format, va_list argptr)
{
assert (zocket);
// Format string into buffer
int size = 255 + 1;
char *string = (char *) malloc (size);
int required = vsnprintf (string, size, format, argptr);
if (required >= size) {
size = required + 1;
string = (char *) realloc (string, size);
vsnprintf (string, size, format, argptr);
}
// Now send formatted string
zmq_msg_t message;
zmq_msg_init_size (&message, strlen (string));
memcpy (zmq_msg_data (&message), string, strlen (string));
int rc = zmq_sendmsg (zocket, &message, more? ZMQ_SNDMORE: 0);
zmq_msg_close (&message);
free (string);
return rc == -1? -1: 0;
}
// --------------------------------------------------------------------------
// Receive C string from socket. Caller must free returned string. Returns
// NULL if the context is being terminated or the process was interrupted.
char *
zstr_recv (void *zocket)
{
assert (zocket);
zmq_msg_t message;
zmq_msg_init (&message);
if (zmq_recvmsg (zocket, &message, 0) < 0)
return NULL;
size_t size = zmq_msg_size (&message);
char *string = (char *) malloc (size + 1);
memcpy (string, zmq_msg_data (&message), size);
zmq_msg_close (&message);
string [size] = 0;
return string;
}
// --------------------------------------------------------------------------
// Receive C string from socket, if socket had input ready. Caller must
// free returned string. Returns NULL if there was no input waiting, or if
// the context was terminated. Use zctx_interrupted to exit any loop that
// relies on this method.
char *
zstr_recv_nowait (void *zocket)
{
assert (zocket);
zmq_msg_t message;
zmq_msg_init (&message);
if (zmq_recvmsg (zocket, &message, ZMQ_DONTWAIT) < 0)
return NULL;
size_t size = zmq_msg_size (&message);
char *string = (char *) malloc (size + 1);
memcpy (string, zmq_msg_data (&message), size);
zmq_msg_close (&message);
string [size] = 0;
return string;
}
// --------------------------------------------------------------------------
// Send C string to socket. Returns 0 if successful, -1 if there was an
// error.
int
zstr_send (void *zocket, const char *format, ...)
{
assert (zocket);
assert (format);
va_list argptr;
va_start (argptr, format);
int rc = s_send_string (zocket, false, format, argptr);
va_end (argptr);
return rc;
}
// Deprecated, does exactly the same
int
zstr_sendf (void *zocket, const char *format, ...)
{
assert (zocket);
assert (format);
va_list argptr;
va_start (argptr, format);
int rc = s_send_string (zocket, false, format, argptr);
va_end (argptr);
return rc;
}
// --------------------------------------------------------------------------
// Send a string to a socket in 0MQ string format, with MORE flag
int
zstr_sendm (void *zocket, const char *format, ...)
{
assert (zocket);
assert (format);
va_list argptr;
va_start (argptr, format);
int rc = s_send_string (zocket, true, format, argptr);
va_end (argptr);
return rc;
}
// Deprecated, does exactly the same
int
zstr_sendfm (void *zocket, const char *format, ...)
{
assert (zocket);
assert (format);
va_list argptr;
va_start (argptr, format);
int rc = s_send_string (zocket, true, format, argptr);
va_end (argptr);
return rc;
}
// --------------------------------------------------------------------------
// Selftest
int
zstr_test (bool verbose)
{
printf (" * zstr: ");
// @selftest
zctx_t *ctx = zctx_new ();
assert (ctx);
void *output = zsocket_new (ctx, ZMQ_PAIR);
assert (output);
zsocket_bind (output, "inproc://zstr.test");
void *input = zsocket_new (ctx, ZMQ_PAIR);
assert (input);
zsocket_connect (input, "inproc://zstr.test");
// Send ten strings, five strings with MORE flag and then END
int string_nbr;
for (string_nbr = 0; string_nbr < 10; string_nbr++)
zstr_send (output, "this is string %d", string_nbr);
for (string_nbr = 0; string_nbr < 5; string_nbr++)
zstr_sendm (output, "this is string %d", string_nbr);
zstr_send (output, "END");
// Read and count until we receive END
string_nbr = 0;
for (string_nbr = 0;; string_nbr++) {
char *string = zstr_recv (input);
if (streq (string, "END")) {
free (string);
break;
}
free (string);
}
assert (string_nbr == 15);
zctx_destroy (&ctx);
// @end
printf ("OK\n");
return 0;
}