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