libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
msg.hpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #ifndef __ZMQ_MSG_HPP_INCLUDE__
31 #define __ZMQ_MSG_HPP_INCLUDE__
32 
33 #include <stddef.h>
34 #include <stdio.h>
35 
36 #include "config.hpp"
37 #include "fd.hpp"
38 #include "atomic_counter.hpp"
39 #include "metadata.hpp"
40 
41 // Signature for free function to deallocate the message content.
42 // Note that it has to be declared as "C" so that it is the same as
43 // zmq_free_fn defined in zmq.h.
44 extern "C"
45 {
46  typedef void (msg_free_fn) (void *data, void *hint);
47 }
48 
49 namespace zmq
50 {
51 
52  // Note that this structure needs to be explicitly constructed
53  // (init functions) and destructed (close function).
54 
55  class msg_t
56  {
57  public:
58 
59  // Shared message buffer. Message data are either allocated in one
60  // continuous block along with this structure - thus avoiding one
61  // malloc/free pair or they are stored in used-supplied memory.
62  // In the latter case, ffn member stores pointer to the function to be
63  // used to deallocate the data. If the buffer is actually shared (there
64  // are at least 2 references to it) refcount member contains number of
65  // references.
66  struct content_t
67  {
68  void *data;
69  size_t size;
71  void *hint;
73  };
74 
75  // Message flags.
76  enum
77  {
78  more = 1, // Followed by more parts
79  command = 2, // Command frame (see ZMTP spec)
80  credential = 32,
81  identity = 64,
82  shared = 128
83  };
84 
85  bool check ();
86  int init();
87 
88  int init (void* data, size_t size_,
89  msg_free_fn* ffn_, void* hint,
90  content_t* content_ = NULL);
91 
92  int init_size (size_t size_);
93  int init_data (void *data_, size_t size_, msg_free_fn *ffn_,
94  void *hint_);
95  int init_external_storage(content_t* content_, void *data_, size_t size_,
96  msg_free_fn *ffn_, void *hint_);
97  int init_delimiter ();
98  int init_join ();
99  int init_leave ();
100  int close ();
101  int move (msg_t &src_);
102  int copy (msg_t &src_);
103  void *data ();
104  size_t size ();
105  unsigned char flags ();
106  void set_flags (unsigned char flags_);
107  void reset_flags (unsigned char flags_);
108  metadata_t *metadata () const;
109  void set_metadata (metadata_t *metadata_);
110  void reset_metadata ();
111  bool is_identity () const;
112  bool is_credential () const;
113  bool is_delimiter () const;
114  bool is_join () const;
115  bool is_leave () const;
116  bool is_vsm () const;
117  bool is_cmsg () const;
118  bool is_zcmsg() const;
119  uint32_t get_routing_id ();
120  int set_routing_id (uint32_t routing_id_);
121  int reset_routing_id ();
122  const char * group ();
123  int set_group (const char* group_);
124  int set_group (const char*, size_t length);
125 
126  // After calling this function you can copy the message in POD-style
127  // refs_ times. No need to call copy.
128  void add_refs (int refs_);
129 
130  // Removes references previously added by add_refs. If the number of
131  // references drops to 0, the message is closed and false is returned.
132  bool rm_refs (int refs_);
133 
134  // Size in bytes of the largest message that is still copied around
135  // rather than being reference-counted.
136  enum { msg_t_size = 64 };
137  enum { max_vsm_size = msg_t_size - (sizeof (metadata_t *) +
138  3 +
139  16 +
140  sizeof (uint32_t))};
141  private:
143 
144  // Different message types.
145  enum type_t
146  {
147  type_min = 101,
148  // VSM messages store the content in the message itself
149  type_vsm = 101,
150  // LMSG messages store the content in malloc-ed memory
151  type_lmsg = 102,
152  // Delimiter messages are used in envelopes
154  // CMSG messages point to constant data
155  type_cmsg = 104,
156 
157  // zero-copy LMSG message for v2_decoder
158  type_zclmsg = 105,
159 
160  // Join message for radio_dish
161  type_join = 106,
162 
163  // Leave message for radio_dish
164  type_leave = 107,
165 
166  type_max = 107
167  };
168 
169  // Note that fields shared between different message types are not
170  // moved to the parent class (msg_t). This way we get tighter packing
171  // of the data. Shared fields can be accessed via 'base' member of
172  // the union.
173  union {
174  struct {
176  unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
177  2 +
178  16 +
179  sizeof (uint32_t))];
180  unsigned char type;
181  unsigned char flags;
182  char group [16];
183  uint32_t routing_id;
184  } base;
185  struct {
187  unsigned char data [max_vsm_size];
188  unsigned char size;
189  unsigned char type;
190  unsigned char flags;
191  char group [16];
192  uint32_t routing_id;
193  } vsm;
194  struct {
197  unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
198  sizeof (content_t*) +
199  2 +
200  16 +
201  sizeof (uint32_t))];
202  unsigned char type;
203  unsigned char flags;
204  char group [16];
205  uint32_t routing_id;
206  } lmsg;
207  struct {
210  unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
211  sizeof (content_t*) +
212  2 +
213  16 +
214  sizeof (uint32_t))];
215  unsigned char type;
216  unsigned char flags;
217  char group [16];
218  uint32_t routing_id;
219  } zclmsg;
220  struct {
222  void* data;
223  size_t size;
224  unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
225  sizeof (void*) +
226  sizeof (size_t) +
227  2 +
228  16 +
229  sizeof (uint32_t))];
230  unsigned char type;
231  unsigned char flags;
232  char group [16];
233  uint32_t routing_id;
234  } cmsg;
235  struct {
237  unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
238  2 +
239  16 +
240  sizeof (uint32_t))];
241  unsigned char type;
242  unsigned char flags;
243  char group [16];
244  uint32_t routing_id;
245  } delimiter;
246  } u;
247  };
248 
249 }
250 
251 #endif
uint32_t routing_id
Definition: msg.hpp:183
int close()
Definition: msg.cpp:217
int set_group(const char *group_)
Definition: msg.cpp:548
zmq::atomic_counter_t refcnt
Definition: msg.hpp:72
bool rm_refs(int refs_)
Definition: msg.cpp:480
int move(msg_t &src_)
Definition: msg.cpp:274
struct zmq::msg_t::@41::@46 cmsg
bool is_identity() const
Definition: msg.cpp:417
void( msg_free_fn)(void *data, void *hint)
Definition: msg.hpp:46
int init_external_storage(content_t *content_, void *data_, size_t size_, msg_free_fn *ffn_, void *hint_)
Definition: msg.cpp:126
void add_refs(int refs_)
Definition: msg.cpp:457
msg_free_fn * ffn
Definition: msg.hpp:70
unsigned char type
Definition: msg.hpp:180
metadata_t * metadata
Definition: msg.hpp:175
unsigned char size
Definition: msg.hpp:188
int init_size(size_t size_)
Definition: msg.cpp:93
struct zmq::msg_t::@41::@44 lmsg
struct zmq::msg_t::@41::@45 zclmsg
void reset_flags(unsigned char flags_)
Definition: msg.cpp:389
bool check()
Definition: msg.cpp:50
bool is_join() const
Definition: msg.cpp:447
int init_leave()
Definition: msg.cpp:207
int init()
Definition: msg.cpp:82
union zmq::msg_t::@41 u
content_t * content
Definition: msg.hpp:196
int init_delimiter()
Definition: msg.cpp:187
int init_join()
Definition: msg.cpp:197
int copy(msg_t &src_)
Definition: msg.cpp:295
unsigned char flags()
int reset_routing_id()
Definition: msg.cpp:537
const char * group()
bool is_vsm() const
Definition: msg.cpp:432
void set_flags(unsigned char flags_)
Definition: msg.cpp:384
bool is_zcmsg() const
Definition: msg.cpp:442
bool is_cmsg() const
Definition: msg.cpp:437
struct zmq::msg_t::@41::@43 vsm
bool is_leave() const
Definition: msg.cpp:452
int set_routing_id(uint32_t routing_id_)
Definition: msg.cpp:527
unsigned char unused[msg_t_size-(sizeof(metadata_t *)+ 2+ 16+ sizeof(uint32_t))]
Definition: msg.hpp:179
bool is_delimiter() const
Definition: msg.cpp:427
struct zmq::msg_t::@41::@47 delimiter
void * data
Definition: msg.hpp:222
Definition: address.hpp:35
void reset_metadata()
Definition: msg.cpp:407
uint32_t get_routing_id()
Definition: msg.cpp:522
bool is_credential() const
Definition: msg.cpp:422
metadata_t * metadata() const
unsigned char flags
Definition: msg.hpp:181
int init_data(void *data_, size_t size_, msg_free_fn *ffn_, void *hint_)
Definition: msg.cpp:148
struct zmq::msg_t::@41::@42 base
void set_metadata(metadata_t *metadata_)
Definition: msg.cpp:399