1ZFRAME(3) CZMQ Manual ZFRAME(3)
2
3
4
6 zframe - Class for working with single message frames
7
9 // This is a stable class, and may not change except for emergencies. It
10 // is provided in stable builds.
11 // This class has draft methods, which may change over time. They are not
12 // in stable releases, by default. Use --enable-drafts to enable.
13 #define ZFRAME_MORE 1 //
14 #define ZFRAME_REUSE 2 //
15 #define ZFRAME_DONTWAIT 4 //
16
17 // Create a new frame. If size is not null, allocates the frame data
18 // to the specified size. If additionally, data is not null, copies
19 // size octets from the specified data into the frame body.
20 CZMQ_EXPORT zframe_t *
21 zframe_new (const void *data, size_t size);
22
23 // Create an empty (zero-sized) frame
24 CZMQ_EXPORT zframe_t *
25 zframe_new_empty (void);
26
27 // Create a frame with a specified string content.
28 CZMQ_EXPORT zframe_t *
29 zframe_from (const char *string);
30
31 // Receive frame from socket, returns zframe_t object or NULL if the recv
32 // was interrupted. Does a blocking recv, if you want to not block then use
33 // zpoller or zloop.
34 CZMQ_EXPORT zframe_t *
35 zframe_recv (void *source);
36
37 // Destroy a frame
38 CZMQ_EXPORT void
39 zframe_destroy (zframe_t **self_p);
40
41 // Send a frame to a socket, destroy frame after sending.
42 // Return -1 on error, 0 on success.
43 CZMQ_EXPORT int
44 zframe_send (zframe_t **self_p, void *dest, int flags);
45
46 // Return number of bytes in frame data
47 CZMQ_EXPORT size_t
48 zframe_size (zframe_t *self);
49
50 // Return address of frame data
51 CZMQ_EXPORT byte *
52 zframe_data (zframe_t *self);
53
54 // Return meta data property for frame
55 // The caller shall not modify or free the returned value, which shall be
56 // owned by the message.
57 CZMQ_EXPORT const char *
58 zframe_meta (zframe_t *self, const char *property);
59
60 // Create a new frame that duplicates an existing frame. If frame is null,
61 // or memory was exhausted, returns null.
62 // Caller owns return value and must destroy it when done.
63 CZMQ_EXPORT zframe_t *
64 zframe_dup (zframe_t *self);
65
66 // Return frame data encoded as printable hex string, useful for 0MQ UUIDs.
67 // Caller must free string when finished with it.
68 // Caller owns return value and must destroy it when done.
69 CZMQ_EXPORT char *
70 zframe_strhex (zframe_t *self);
71
72 // Return frame data copied into freshly allocated string
73 // Caller must free string when finished with it.
74 // Caller owns return value and must destroy it when done.
75 CZMQ_EXPORT char *
76 zframe_strdup (zframe_t *self);
77
78 // Return TRUE if frame body is equal to string, excluding terminator
79 CZMQ_EXPORT bool
80 zframe_streq (zframe_t *self, const char *string);
81
82 // Return frame MORE indicator (1 or 0), set when reading frame from socket
83 // or by the zframe_set_more() method
84 CZMQ_EXPORT int
85 zframe_more (zframe_t *self);
86
87 // Set frame MORE indicator (1 or 0). Note this is NOT used when sending
88 // frame to socket, you have to specify flag explicitly.
89 CZMQ_EXPORT void
90 zframe_set_more (zframe_t *self, int more);
91
92 // Return TRUE if two frames have identical size and data
93 // If either frame is NULL, equality is always false.
94 CZMQ_EXPORT bool
95 zframe_eq (zframe_t *self, zframe_t *other);
96
97 // Set new contents for frame
98 CZMQ_EXPORT void
99 zframe_reset (zframe_t *self, const void *data, size_t size);
100
101 // Send message to zsys log sink (may be stdout, or system facility as
102 // configured by zsys_set_logstream). Prefix shows before frame, if not null.
103 CZMQ_EXPORT void
104 zframe_print (zframe_t *self, const char *prefix);
105
106 // Probe the supplied object, and report if it looks like a zframe_t.
107 CZMQ_EXPORT bool
108 zframe_is (void *self);
109
110 // Self test of this class.
111 CZMQ_EXPORT void
112 zframe_test (bool verbose);
113
114 #ifdef CZMQ_BUILD_DRAFT_API
115 // *** Draft method, for development use, may change without warning ***
116 // Return frame routing ID, if the frame came from a ZMQ_SERVER socket.
117 // Else returns zero.
118 CZMQ_EXPORT uint32_t
119 zframe_routing_id (zframe_t *self);
120
121 // *** Draft method, for development use, may change without warning ***
122 // Set routing ID on frame. This is used if/when the frame is sent to a
123 // ZMQ_SERVER socket.
124 CZMQ_EXPORT void
125 zframe_set_routing_id (zframe_t *self, uint32_t routing_id);
126
127 // *** Draft method, for development use, may change without warning ***
128 // Return frame group of radio-dish pattern.
129 CZMQ_EXPORT const char *
130 zframe_group (zframe_t *self);
131
132 // *** Draft method, for development use, may change without warning ***
133 // Set group on frame. This is used if/when the frame is sent to a
134 // ZMQ_RADIO socket.
135 // Return -1 on error, 0 on success.
136 CZMQ_EXPORT int
137 zframe_set_group (zframe_t *self, const char *group);
138
139 #endif // CZMQ_BUILD_DRAFT_API
140 Please add '@interface' section in './../src/zframe.c'.
141
143 The zframe class provides methods to send and receive single message
144 frames across 0MQ sockets. A frame corresponds to one zmq_msg_t. When
145 you read a frame from a socket, the zframe_more() method indicates if
146 the frame is part of an unfinished multipart message. The zframe_send
147 method normally destroys the frame, but with the ZFRAME_REUSE flag, you
148 can send the same frame many times. Frames are binary, and this class
149 has no special support for text data.
150
151 Please add @discuss section in ./../src/zframe.c.
152
154 From zframe_test method.
155
156 // Create two PAIR sockets and connect over inproc
157 zsock_t *output = zsock_new (ZMQ_PAIR);
158 assert (output);
159 int port = zsock_bind (output, "tcp://127.0.0.1:*");
160 assert (port != -1);
161 zsock_t *input = zsock_new (ZMQ_PAIR);
162 assert (input);
163 rc = zsock_connect (input, "tcp://127.0.0.1:%d", port);
164 assert (rc != -1);
165
166 // Send five different frames, test ZFRAME_MORE
167 int frame_nbr;
168 for (frame_nbr = 0; frame_nbr < 5; frame_nbr++) {
169 frame = zframe_new ("Hello", 5);
170 assert (frame);
171 rc = zframe_send (&frame, output, ZFRAME_MORE);
172 assert (rc == 0);
173 }
174 // Send same frame five times, test ZFRAME_REUSE
175 frame = zframe_new ("Hello", 5);
176 assert (frame);
177 for (frame_nbr = 0; frame_nbr < 5; frame_nbr++) {
178 rc = zframe_send (&frame, output, ZFRAME_MORE + ZFRAME_REUSE);
179 assert (rc == 0);
180 }
181 assert (frame);
182 zframe_t *copy = zframe_dup (frame);
183 assert (zframe_eq (frame, copy));
184 zframe_destroy (&frame);
185 assert (!zframe_eq (frame, copy));
186 assert (zframe_size (copy) == 5);
187 zframe_destroy (©);
188 assert (!zframe_eq (frame, copy));
189
190 // Test zframe_new_empty
191 frame = zframe_new_empty ();
192 assert (frame);
193 assert (zframe_size (frame) == 0);
194 zframe_destroy (&frame);
195
196 // Send END frame
197 frame = zframe_new ("NOT", 3);
198 assert (frame);
199 zframe_reset (frame, "END", 3);
200 char *string = zframe_strhex (frame);
201 assert (streq (string, "454E44"));
202 freen (string);
203 string = zframe_strdup (frame);
204 assert (streq (string, "END"));
205 freen (string);
206 rc = zframe_send (&frame, output, 0);
207 assert (rc == 0);
208
209 // Read and count until we receive END
210 frame_nbr = 0;
211 for (frame_nbr = 0;; frame_nbr++) {
212 zframe_t *frame = zframe_recv (input);
213 if (zframe_streq (frame, "END")) {
214 zframe_destroy (&frame);
215 break;
216 }
217 assert (zframe_more (frame));
218 zframe_set_more (frame, 0);
219 assert (zframe_more (frame) == 0);
220 zframe_destroy (&frame);
221 }
222 assert (frame_nbr == 10);
223
224 #if (ZMQ_VERSION >= ZMQ_MAKE_VERSION (4, 1, 0))
225 // Test zframe_meta
226 frame = zframe_new ("Hello", 5);
227 assert (frame);
228 rc = zframe_send (&frame, output, 0);
229 assert (rc == 0);
230 frame = zframe_recv (input);
231 const char *meta = zframe_meta (frame, "Socket-Type");
232 assert (meta != NULL);
233 assert (streq (meta, "PAIR"));
234 assert (zframe_meta (frame, "nonexistent") == NULL);
235 zframe_destroy (&frame);
236 #endif
237
238 zsock_destroy (&input);
239 zsock_destroy (&output);
240
241 #if defined (ZMQ_SERVER)
242 // Create server and client sockets and connect over inproc
243 zsock_t *server = zsock_new_server ("inproc://zframe-test-routing");
244 assert (server);
245 zsock_t *client = zsock_new_client ("inproc://zframe-test-routing");
246 assert (client);
247
248 // Send request from client to server
249 zframe_t *request = zframe_new ("Hello", 5);
250 assert (request);
251 rc = zframe_send (&request, client, 0);
252 assert (rc == 0);
253 assert (!request);
254
255 // Read request and send reply
256 request = zframe_recv (server);
257 assert (request);
258 assert (zframe_streq (request, "Hello"));
259 assert (zframe_routing_id (request));
260
261 zframe_t *reply = zframe_new ("World", 5);
262 assert (reply);
263 zframe_set_routing_id (reply, zframe_routing_id (request));
264 rc = zframe_send (&reply, server, 0);
265 assert (rc == 0);
266 zframe_destroy (&request);
267
268 // Read reply
269 reply = zframe_recv (client);
270 assert (zframe_streq (reply, "World"));
271 assert (zframe_routing_id (reply) == 0);
272 zframe_destroy (&reply);
273
274 // Client and server disallow multipart
275 frame = zframe_new ("Hello", 5);
276 rc = zframe_send (&frame, client, ZFRAME_MORE);
277 assert (rc == -1);
278 rc = zframe_send (&frame, server, ZFRAME_MORE);
279 assert (rc == -1);
280 zframe_destroy (&frame);
281
282 zsock_destroy (&client);
283 zsock_destroy (&server);
284 #endif
285
286 #ifdef ZMQ_RADIO
287 // Create radio and dish sockets and connect over inproc
288 zsock_t *radio = zsock_new_radio ("inproc://zframe-test-radio");
289 assert (radio);
290 zsock_t *dish = zsock_new_dish ("inproc://zframe-test-radio");
291 assert (dish);
292
293 // Join the group
294 rc = zsock_join (dish, "World");
295 assert (rc == 0);
296
297 // Publish message from radio
298 zframe_t *message = zframe_new ("Hello", 5);
299 assert (message);
300 rc = zframe_set_group (message, "World");
301 assert (rc == 0);
302 rc = zframe_send (&message, radio, 0);
303 assert (rc == 0);
304 assert (!message);
305
306 // Receive the message from dish
307 message = zframe_recv (dish);
308 assert (message);
309 assert (zframe_streq (message, "Hello"));
310 assert (strcmp("World", zframe_group (message)) == 0);
311 zframe_destroy (&message);
312
313 zsock_destroy (&dish);
314 zsock_destroy (&radio);
315 #else
316 frame = zframe_new ("Hello", 5);
317 rc = zframe_set_group (frame, "World");
318 assert(rc == -1);
319 assert(errno == ENOTSUP);
320 zframe_destroy (&frame);
321 #endif
322
323 #if defined (__WINDOWS__)
324 zsys_shutdown();
325 #endif
326
327
329 The czmq manual was written by the authors in the AUTHORS file.
330
332 Main web site:
333
334 Report bugs to the email <zeromq-dev@lists.zeromq.org[1]>
335
337 Copyright (c) the Contributors as noted in the AUTHORS file. This file
338 is part of CZMQ, the high-level C binding for 0MQ:
339 http://czmq.zeromq.org. This Source Code Form is subject to the terms
340 of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
341 distributed with this file, You can obtain one at
342 http://mozilla.org/MPL/2.0/. LICENSE included with the czmq
343 distribution.
344
346 1. zeromq-dev@lists.zeromq.org
347 mailto:zeromq-dev@lists.zeromq.org
348
349
350
351CZMQ 4.1.1 07/24/2019 ZFRAME(3)