1 /* ========================================================================= 2 zmsg - working with multipart messages 3 4 Copyright (c) the Contributors as noted in the AUTHORS file. 5 This file is part of CZMQ, the high-level C binding for 0MQ: 6 http://czmq.zeromq.org. 7 8 This Source Code Form is subject to the terms of the Mozilla Public 9 License, v. 2.0. If a copy of the MPL was not distributed with this 10 file, You can obtain one at http://mozilla.org/MPL/2.0/. 11 ========================================================================= 12 */ 13 14 #ifndef __ZMSG_H_INCLUDED__ 15 #define __ZMSG_H_INCLUDED__ 16 17 #ifdef __cplusplus 18 extern "C" { 19 #endif 20 21 // @warning THE FOLLOWING @INTERFACE BLOCK IS AUTO-GENERATED BY ZPROJECT 22 // @warning Please edit the model at "api/zmsg.api" to make changes. 23 // @interface 24 // This is a stable class, and may not change except for emergencies. It 25 // is provided in stable builds. 26 // This class has draft methods, which may change over time. They are not 27 // in stable releases, by default. Use --enable-drafts to enable. 28 // Create a new empty message object 29 CZMQ_EXPORT zmsg_t * 30 zmsg_new (void); 31 32 // Receive message from socket, returns zmsg_t object or NULL if the recv 33 // was interrupted. Does a blocking recv. If you want to not block then use 34 // the zloop class or zmsg_recv_nowait or zmq_poll to check for socket input 35 // before receiving. 36 CZMQ_EXPORT zmsg_t * 37 zmsg_recv (void *source); 38 39 // Load/append an open file into new message, return the message. 40 // Returns NULL if the message could not be loaded. 41 CZMQ_EXPORT zmsg_t * 42 zmsg_load (FILE *file); 43 44 // Decodes a serialized message frame created by zmsg_encode () and returns 45 // a new zmsg_t object. Returns NULL if the frame was badly formatted or 46 // there was insufficient memory to work. 47 CZMQ_EXPORT zmsg_t * 48 zmsg_decode (zframe_t *frame); 49 50 // Generate a signal message encoding the given status. A signal is a short 51 // message carrying a 1-byte success/failure code (by convention, 0 means 52 // OK). Signals are encoded to be distinguishable from "normal" messages. 53 CZMQ_EXPORT zmsg_t * 54 zmsg_new_signal (byte status); 55 56 // Destroy a message object and all frames it contains 57 CZMQ_EXPORT void 58 zmsg_destroy (zmsg_t **self_p); 59 60 // Send message to destination socket, and destroy the message after sending 61 // it successfully. If the message has no frames, sends nothing but destroys 62 // the message anyhow. Nullifies the caller's reference to the message (as 63 // it is a destructor). 64 CZMQ_EXPORT int 65 zmsg_send (zmsg_t **self_p, void *dest); 66 67 // Send message to destination socket as part of a multipart sequence, and 68 // destroy the message after sending it successfully. Note that after a 69 // zmsg_sendm, you must call zmsg_send or another method that sends a final 70 // message part. If the message has no frames, sends nothing but destroys 71 // the message anyhow. Nullifies the caller's reference to the message (as 72 // it is a destructor). 73 CZMQ_EXPORT int 74 zmsg_sendm (zmsg_t **self_p, void *dest); 75 76 // Return size of message, i.e. number of frames (0 or more). 77 CZMQ_EXPORT size_t 78 zmsg_size (zmsg_t *self); 79 80 // Return total size of all frames in message. 81 CZMQ_EXPORT size_t 82 zmsg_content_size (zmsg_t *self); 83 84 // Push frame to the front of the message, i.e. before all other frames. 85 // Message takes ownership of frame, will destroy it when message is sent. 86 // Returns 0 on success, -1 on error. Deprecates zmsg_push, which did not 87 // nullify the caller's frame reference. 88 CZMQ_EXPORT int 89 zmsg_prepend (zmsg_t *self, zframe_t **frame_p); 90 91 // Add frame to the end of the message, i.e. after all other frames. 92 // Message takes ownership of frame, will destroy it when message is sent. 93 // Returns 0 on success. Deprecates zmsg_add, which did not nullify the 94 // caller's frame reference. 95 CZMQ_EXPORT int 96 zmsg_append (zmsg_t *self, zframe_t **frame_p); 97 98 // Remove first frame from message, if any. Returns frame, or NULL. 99 // Caller owns return value and must destroy it when done. 100 CZMQ_EXPORT zframe_t * 101 zmsg_pop (zmsg_t *self); 102 103 // Push block of memory to front of message, as a new frame. 104 // Returns 0 on success, -1 on error. 105 CZMQ_EXPORT int 106 zmsg_pushmem (zmsg_t *self, const void *data, size_t size); 107 108 // Add block of memory to the end of the message, as a new frame. 109 // Returns 0 on success, -1 on error. 110 CZMQ_EXPORT int 111 zmsg_addmem (zmsg_t *self, const void *data, size_t size); 112 113 // Push string as new frame to front of message. 114 // Returns 0 on success, -1 on error. 115 CZMQ_EXPORT int 116 zmsg_pushstr (zmsg_t *self, const char *string); 117 118 // Push string as new frame to end of message. 119 // Returns 0 on success, -1 on error. 120 CZMQ_EXPORT int 121 zmsg_addstr (zmsg_t *self, const char *string); 122 123 // Push formatted string as new frame to front of message. 124 // Returns 0 on success, -1 on error. 125 CZMQ_EXPORT int 126 zmsg_pushstrf (zmsg_t *self, const char *format, ...) CHECK_PRINTF (2); 127 128 // Push formatted string as new frame to end of message. 129 // Returns 0 on success, -1 on error. 130 CZMQ_EXPORT int 131 zmsg_addstrf (zmsg_t *self, const char *format, ...) CHECK_PRINTF (2); 132 133 // Pop frame off front of message, return as fresh string. If there were 134 // no more frames in the message, returns NULL. 135 // Caller owns return value and must destroy it when done. 136 CZMQ_EXPORT char * 137 zmsg_popstr (zmsg_t *self); 138 139 // Push encoded message as a new frame. Message takes ownership of 140 // submessage, so the original is destroyed in this call. Returns 0 on 141 // success, -1 on error. 142 CZMQ_EXPORT int 143 zmsg_addmsg (zmsg_t *self, zmsg_t **msg_p); 144 145 // Remove first submessage from message, if any. Returns zmsg_t, or NULL if 146 // decoding was not successful. 147 // Caller owns return value and must destroy it when done. 148 CZMQ_EXPORT zmsg_t * 149 zmsg_popmsg (zmsg_t *self); 150 151 // Remove specified frame from list, if present. Does not destroy frame. 152 CZMQ_EXPORT void 153 zmsg_remove (zmsg_t *self, zframe_t *frame); 154 155 // Set cursor to first frame in message. Returns frame, or NULL, if the 156 // message is empty. Use this to navigate the frames as a list. 157 CZMQ_EXPORT zframe_t * 158 zmsg_first (zmsg_t *self); 159 160 // Return the next frame. If there are no more frames, returns NULL. To move 161 // to the first frame call zmsg_first(). Advances the cursor. 162 CZMQ_EXPORT zframe_t * 163 zmsg_next (zmsg_t *self); 164 165 // Return the last frame. If there are no frames, returns NULL. 166 CZMQ_EXPORT zframe_t * 167 zmsg_last (zmsg_t *self); 168 169 // Save message to an open file, return 0 if OK, else -1. The message is 170 // saved as a series of frames, each with length and data. Note that the 171 // file is NOT guaranteed to be portable between operating systems, not 172 // versions of CZMQ. The file format is at present undocumented and liable 173 // to arbitrary change. 174 CZMQ_EXPORT int 175 zmsg_save (zmsg_t *self, FILE *file); 176 177 // Serialize multipart message to a single message frame. Use this method 178 // to send structured messages across transports that do not support 179 // multipart data. Allocates and returns a new frame containing the 180 // serialized message. To decode a serialized message frame, use 181 // zmsg_decode (). 182 // Caller owns return value and must destroy it when done. 183 CZMQ_EXPORT zframe_t * 184 zmsg_encode (zmsg_t *self); 185 186 // Create copy of message, as new message object. Returns a fresh zmsg_t 187 // object. If message is null, or memory was exhausted, returns null. 188 // Caller owns return value and must destroy it when done. 189 CZMQ_EXPORT zmsg_t * 190 zmsg_dup (zmsg_t *self); 191 192 // Send message to zsys log sink (may be stdout, or system facility as 193 // configured by zsys_set_logstream). 194 // Long messages are truncated. 195 CZMQ_EXPORT void 196 zmsg_print (zmsg_t *self); 197 198 // Return true if the two messages have the same number of frames and each 199 // frame in the first message is identical to the corresponding frame in the 200 // other message. As with zframe_eq, return false if either message is NULL. 201 CZMQ_EXPORT bool 202 zmsg_eq (zmsg_t *self, zmsg_t *other); 203 204 // Return signal value, 0 or greater, if message is a signal, -1 if not. 205 CZMQ_EXPORT int 206 zmsg_signal (zmsg_t *self); 207 208 // Probe the supplied object, and report if it looks like a zmsg_t. 209 CZMQ_EXPORT bool 210 zmsg_is (void *self); 211 212 // Self test of this class. 213 CZMQ_EXPORT void 214 zmsg_test (bool verbose); 215 216 #ifdef CZMQ_BUILD_DRAFT_API 217 // *** Draft method, for development use, may change without warning *** 218 // Return message routing ID, if the message came from a ZMQ_SERVER socket. 219 // Else returns zero. 220 CZMQ_EXPORT uint32_t 221 zmsg_routing_id (zmsg_t *self); 222 223 // *** Draft method, for development use, may change without warning *** 224 // Set routing ID on message. This is used if/when the message is sent to a 225 // ZMQ_SERVER socket. 226 CZMQ_EXPORT void 227 zmsg_set_routing_id (zmsg_t *self, uint32_t routing_id); 228 229 // *** Draft method, for development use, may change without warning *** 230 // Send message to zsys log sink (may be stdout, or system facility as 231 // configured by zsys_set_logstream). 232 // Message length is specified; no truncation unless length is zero. 233 // Backwards compatible with zframe_print when length is zero. 234 CZMQ_EXPORT void 235 zmsg_print_n (zmsg_t *self, size_t size); 236 237 #endif // CZMQ_BUILD_DRAFT_API 238 // @end 239 240 241 // DEPRECATED as over-engineered, poor style 242 // Pop frame off front of message, caller now owns frame 243 // If next frame is empty, pops and destroys that empty frame. 244 CZMQ_EXPORT zframe_t * 245 zmsg_unwrap (zmsg_t *self); 246 247 // DEPRECATED as poor style -- callers should use zloop or zpoller 248 // Receive message from socket, returns zmsg_t object, or NULL either if 249 // there was no input waiting, or the recv was interrupted. 250 CZMQ_EXPORT zmsg_t * 251 zmsg_recv_nowait (void *source); 252 253 // DEPRECATED as unsafe -- does not nullify frame reference. 254 // Push frame plus empty frame to front of message, before first frame. 255 // Message takes ownership of frame, will destroy it when message is sent. 256 CZMQ_EXPORT void 257 zmsg_wrap (zmsg_t *self, zframe_t *frame); 258 259 // DEPRECATED - will be removed for next + 1 stable release 260 // Add frame to the front of the message, i.e. before all other frames. 261 // Message takes ownership of frame, will destroy it when message is sent. 262 // Returns 0 on success, -1 on error. 263 CZMQ_EXPORT int 264 zmsg_push (zmsg_t *self, zframe_t *frame); 265 266 // DEPRECATED - will be removed for next stable release 267 CZMQ_EXPORT int 268 zmsg_add (zmsg_t *self, zframe_t *frame); 269 270 // DEPRECATED as inconsistent; breaks principle that logging should all go 271 // to a single destination. 272 // Print message to open stream 273 // Truncates to first 10 frames, for readability. 274 CZMQ_EXPORT void 275 zmsg_fprint (zmsg_t *self, FILE *file); 276 277 // Compiler hints 278 CZMQ_EXPORT int zmsg_addstrf (zmsg_t *self, const char *format, ...) CHECK_PRINTF (2); 279 CZMQ_EXPORT int zmsg_pushstrf (zmsg_t *self, const char *format, ...) CHECK_PRINTF (2); 280 281 #ifdef __cplusplus 282 } 283 #endif 284 285 // Deprecated method aliases 286 #define zmsg_dump(s) zmsg_print(s) 287 #define zmsg_dump_to_stream(s,F) zmsg_fprint(s,F) 288 289 #endif 290