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 // Destroy an item
116 typedef void (zframe_destructor_fn) (
117 void **hint);
118
119 // *** Draft method, for development use, may change without warning ***
120 // Create a new frame from memory. Take ownership of the memory and calling the destructor
121 // on destroy.
122 CZMQ_EXPORT zframe_t *
123 zframe_frommem (void *data, size_t size, zframe_destructor_fn destructor, void *hint);
124
125 // *** Draft method, for development use, may change without warning ***
126 // Return frame routing ID, if the frame came from a ZMQ_SERVER socket.
127 // Else returns zero.
128 CZMQ_EXPORT uint32_t
129 zframe_routing_id (zframe_t *self);
130
131 // *** Draft method, for development use, may change without warning ***
132 // Set routing ID on frame. This is used if/when the frame is sent to a
133 // ZMQ_SERVER socket.
134 CZMQ_EXPORT void
135 zframe_set_routing_id (zframe_t *self, uint32_t routing_id);
136
137 // *** Draft method, for development use, may change without warning ***
138 // Return frame group of radio-dish pattern.
139 CZMQ_EXPORT const char *
140 zframe_group (zframe_t *self);
141
142 // *** Draft method, for development use, may change without warning ***
143 // Set group on frame. This is used if/when the frame is sent to a
144 // ZMQ_RADIO socket.
145 // Return -1 on error, 0 on success.
146 CZMQ_EXPORT int
147 zframe_set_group (zframe_t *self, const char *group);
148
149 #endif // CZMQ_BUILD_DRAFT_API
150 Please add '@interface' section in './../src/zframe.c'.
151
153 The zframe class provides methods to send and receive single message
154 frames across 0MQ sockets. A frame corresponds to one zmq_msg_t. When
155 you read a frame from a socket, the zframe_more() method indicates if
156 the frame is part of an unfinished multipart message. The zframe_send
157 method normally destroys the frame, but with the ZFRAME_REUSE flag, you
158 can send the same frame many times. Frames are binary, and this class
159 has no special support for text data.
160
161 Please add @discuss section in ./../src/zframe.c.
162
164 From zframe_test method.
165
166 // Create two PAIR sockets and connect over TCP
167 zsock_t *output = zsock_new (ZMQ_PAIR);
168 assert (output);
169 int port = zsock_bind (output, "tcp://127.0.0.1:*");
170 assert (port != -1);
171 zsock_t *input = zsock_new (ZMQ_PAIR);
172 assert (input);
173 rc = zsock_connect (input, "tcp://127.0.0.1:%d", port);
174 assert (rc != -1);
175
176 // Send five different frames, test ZFRAME_MORE
177 int frame_nbr;
178 for (frame_nbr = 0; frame_nbr < 5; frame_nbr++) {
179 frame = zframe_new ("Hello", 5);
180 assert (frame);
181 rc = zframe_send (&frame, output, ZFRAME_MORE);
182 assert (rc == 0);
183 }
184 // Send same frame five times, test ZFRAME_REUSE
185 frame = zframe_new ("Hello", 5);
186 assert (frame);
187 for (frame_nbr = 0; frame_nbr < 5; frame_nbr++) {
188 rc = zframe_send (&frame, output, ZFRAME_MORE + ZFRAME_REUSE);
189 assert (rc == 0);
190 }
191 assert (frame);
192 zframe_t *copy = zframe_dup (frame);
193 assert (zframe_eq (frame, copy));
194 zframe_destroy (&frame);
195 assert (!zframe_eq (frame, copy));
196 assert (zframe_size (copy) == 5);
197 zframe_destroy (©);
198 assert (!zframe_eq (frame, copy));
199
200 // Test zframe_new_empty
201 frame = zframe_new_empty ();
202 assert (frame);
203 assert (zframe_size (frame) == 0);
204 zframe_destroy (&frame);
205
206 // Send END frame
207 frame = zframe_new ("NOT", 3);
208 assert (frame);
209 zframe_reset (frame, "END", 3);
210 char *string = zframe_strhex (frame);
211 assert (streq (string, "454E44"));
212 freen (string);
213 string = zframe_strdup (frame);
214 assert (streq (string, "END"));
215 freen (string);
216 rc = zframe_send (&frame, output, 0);
217 assert (rc == 0);
218
219 // Read and count until we receive END
220 frame_nbr = 0;
221 for (frame_nbr = 0;; frame_nbr++) {
222 zframe_t *frame = zframe_recv (input);
223 if (zframe_streq (frame, "END")) {
224 zframe_destroy (&frame);
225 break;
226 }
227 assert (zframe_more (frame));
228 zframe_set_more (frame, 0);
229 assert (zframe_more (frame) == 0);
230 zframe_destroy (&frame);
231 }
232 assert (frame_nbr == 10);
233
234 #if (ZMQ_VERSION >= ZMQ_MAKE_VERSION (4, 1, 0))
235 // Test zframe_meta
236 frame = zframe_new ("Hello", 5);
237 assert (frame);
238 rc = zframe_send (&frame, output, 0);
239 assert (rc == 0);
240 frame = zframe_recv (input);
241 const char *meta = zframe_meta (frame, "Socket-Type");
242 assert (meta != NULL);
243 assert (streq (meta, "PAIR"));
244 assert (zframe_meta (frame, "nonexistent") == NULL);
245 zframe_destroy (&frame);
246 #endif
247
248 zsock_destroy (&input);
249 zsock_destroy (&output);
250
251 #if defined (ZMQ_SERVER)
252 // Create server and client sockets and connect over inproc
253 zsock_t *server = zsock_new_server ("inproc://zframe-test-routing");
254 assert (server);
255 zsock_t *client = zsock_new_client ("inproc://zframe-test-routing");
256 assert (client);
257
258 // Send request from client to server
259 zframe_t *request = zframe_new ("Hello", 5);
260 assert (request);
261 rc = zframe_send (&request, client, 0);
262 assert (rc == 0);
263 assert (!request);
264
265 // Read request and send reply
266 request = zframe_recv (server);
267 assert (request);
268 assert (zframe_streq (request, "Hello"));
269 assert (zframe_routing_id (request));
270
271 zframe_t *reply = zframe_new ("World", 5);
272 assert (reply);
273 zframe_set_routing_id (reply, zframe_routing_id (request));
274 rc = zframe_send (&reply, server, 0);
275 assert (rc == 0);
276 zframe_destroy (&request);
277
278 // Read reply
279 reply = zframe_recv (client);
280 assert (zframe_streq (reply, "World"));
281 assert (zframe_routing_id (reply) == 0);
282 zframe_destroy (&reply);
283
284 // Client and server disallow multipart
285 frame = zframe_new ("Hello", 5);
286 rc = zframe_send (&frame, client, ZFRAME_MORE);
287 assert (rc == -1);
288 rc = zframe_send (&frame, server, ZFRAME_MORE);
289 assert (rc == -1);
290 zframe_destroy (&frame);
291
292 zsock_destroy (&client);
293 zsock_destroy (&server);
294 #endif
295
296 #ifdef ZMQ_RADIO
297 // Create radio and dish sockets and connect over inproc
298 zsock_t *radio = zsock_new_radio ("inproc://zframe-test-radio");
299 assert (radio);
300 zsock_t *dish = zsock_new_dish ("inproc://zframe-test-radio");
301 assert (dish);
302
303 // Join the group
304 rc = zsock_join (dish, "World");
305 assert (rc == 0);
306
307 // Publish message from radio
308 zframe_t *message = zframe_new ("Hello", 5);
309 assert (message);
310 rc = zframe_set_group (message, "World");
311 assert (rc == 0);
312 rc = zframe_send (&message, radio, 0);
313 assert (rc == 0);
314 assert (!message);
315
316 // Receive the message from dish
317 message = zframe_recv (dish);
318 assert (message);
319 assert (zframe_streq (message, "Hello"));
320 assert (strcmp("World", zframe_group (message)) == 0);
321 zframe_destroy (&message);
322
323 zsock_destroy (&dish);
324 zsock_destroy (&radio);
325 #else
326 frame = zframe_new ("Hello", 5);
327 rc = zframe_set_group (frame, "World");
328 assert(rc == -1);
329 assert(errno == ENOTSUP);
330 zframe_destroy (&frame);
331 #endif
332
333 char str[] = "hello";
334 frame = zframe_frommem (str, 5, mem_destructor, str);
335 assert (frame);
336 zframe_destroy (&frame);
337
338 // The destructor doesn't free the memory, only changing the strid,
339 // so we can check if the destructor was invoked
340 assert (streq (str, "world"));
341
342 #if defined (__WINDOWS__)
343 zsys_shutdown();
344 #endif
345
346
348 The czmq manual was written by the authors in the AUTHORS file.
349
351 Main web site:
352
353 Report bugs to the email <zeromq-dev@lists.zeromq.org[1]>
354
356 Copyright (c) the Contributors as noted in the AUTHORS file. This file
357 is part of CZMQ, the high-level C binding for 0MQ:
358 http://czmq.zeromq.org. This Source Code Form is subject to the terms
359 of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
360 distributed with this file, You can obtain one at
361 http://mozilla.org/MPL/2.0/. LICENSE included with the czmq
362 distribution.
363
365 1. zeromq-dev@lists.zeromq.org
366 mailto:zeromq-dev@lists.zeromq.org
367
368
369
370CZMQ 4.2.0 01/28/2020 ZFRAME(3)