msg.hpp 9.72 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Martin Sustrik's avatar
Martin Sustrik committed
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
Martin Sustrik's avatar
Martin Sustrik committed
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
25

26
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

Martin Sustrik's avatar
Martin Sustrik committed
30 31
#ifndef __ZMQ_MSG_HPP_INCLUDE__
#define __ZMQ_MSG_HPP_INCLUDE__
Martin Sustrik's avatar
Martin Sustrik committed
32 33

#include <stddef.h>
Pieter Hintjens's avatar
Pieter Hintjens committed
34
#include <stdio.h>
Martin Sustrik's avatar
Martin Sustrik committed
35

36
#include "config.hpp"
37
#include "err.hpp"
38
#include "fd.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
39
#include "atomic_counter.hpp"
40
#include "metadata.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
41

42 43 44 45 46 47 48 49
//  Signature for free function to deallocate the message content.
//  Note that it has to be declared as "C" so that it is the same as
//  zmq_free_fn defined in zmq.h.
extern "C"
{
    typedef void (msg_free_fn) (void *data, void *hint);
}

50 51
namespace zmq
{
Martin Sustrik's avatar
Martin Sustrik committed
52

53 54
    //  Note that this structure needs to be explicitly constructed
    //  (init functions) and destructed (close function).
55

56
    class msg_t
Martin Sustrik's avatar
Martin Sustrik committed
57
    {
58
    public:
59

60 61
        //  Shared message buffer. Message data are either allocated in one
        //  continuous block along with this structure - thus avoiding one
evoskuil's avatar
evoskuil committed
62
        //  malloc/free pair or they are stored in user-supplied memory.
63 64 65 66 67 68 69 70 71 72 73 74 75
        //  In the latter case, ffn member stores pointer to the function to be
        //  used to deallocate the data. If the buffer is actually shared (there
        //  are at least 2 references to it) refcount member contains number of
        //  references.
        struct content_t
        {
            void *data;
            size_t size;
            msg_free_fn *ffn;
            void *hint;
            zmq::atomic_counter_t refcnt;
        };

76
        //  Message flags.
77 78
        enum
        {
79 80
            more = 1,           //  Followed by more parts
            command = 2,        //  Command frame (see ZMTP spec)
81
            credential = 32,
82
            identity = 64,
83
            shared = 128
84 85
        };

86
        bool check () const;
87 88 89 90
        int init();

        int init (void* data, size_t size_,
                  msg_free_fn* ffn_, void* hint,
91
                  content_t* content_ = NULL);
92

93
        int init_size (size_t size_);
94
        int init_data (void *data_, size_t size_, msg_free_fn *ffn_,
95
                       void *hint_);
96
        int init_external_storage(content_t* content_, void *data_, size_t size_,
97
                                  msg_free_fn *ffn_, void *hint_);
98
        int init_delimiter ();
99 100
        int init_join ();
        int init_leave ();
101 102 103 104
        int close ();
        int move (msg_t &src_);
        int copy (msg_t &src_);
        void *data ();
105 106
        size_t size () const;
        unsigned char flags () const;
107 108
        void set_flags (unsigned char flags_);
        void reset_flags (unsigned char flags_);
109 110
        metadata_t *metadata () const;
        void set_metadata (metadata_t *metadata_);
111
        void reset_metadata ();
Martin Hurton's avatar
Martin Hurton committed
112
        bool is_identity () const;
113
        bool is_credential () const;
114
        bool is_delimiter () const;
115 116
        bool is_join () const;
        bool is_leave () const;
117 118 119
        bool is_vsm () const;
        bool is_cmsg () const;
        bool is_zcmsg() const;
120 121
        uint32_t get_routing_id ();
        int set_routing_id (uint32_t routing_id_);
122
        int reset_routing_id ();
somdoron's avatar
somdoron committed
123 124
        const char * group ();
        int set_group (const char* group_);
125
        int set_group (const char*, size_t length);
126 127 128 129 130

        //  After calling this function you can copy the message in POD-style
        //  refs_ times. No need to call copy.
        void add_refs (int refs_);

131 132 133
        //  Removes references previously added by add_refs. If the number of
        //  references drops to 0, the message is closed and false is returned.
        bool rm_refs (int refs_);
134

135 136
        //  Size in bytes of the largest message that is still copied around
        //  rather than being reference-counted.
137
        enum { msg_t_size = 64 };
138 139
        enum { max_vsm_size = msg_t_size - (sizeof (metadata_t *) +
                                            3 +
somdoron's avatar
somdoron committed
140
                                            16 +
141
                                            sizeof (uint32_t))};
142 143 144
    private:
        zmq::atomic_counter_t* refcnt();

145 146 147 148
        //  Different message types.
        enum type_t
        {
            type_min = 101,
Uli Köhler's avatar
Uli Köhler committed
149
            //  VSM messages store the content in the message itself
150
            type_vsm = 101,
Uli Köhler's avatar
Uli Köhler committed
151
            //  LMSG messages store the content in malloc-ed memory
152
            type_lmsg = 102,
Uli Köhler's avatar
Uli Köhler committed
153
            //  Delimiter messages are used in envelopes
154
            type_delimiter = 103,
Uli Köhler's avatar
Uli Köhler committed
155
            //  CMSG messages point to constant data
156
            type_cmsg = 104,
157

158 159 160
            // zero-copy LMSG message for v2_decoder
            type_zclmsg = 105,

161 162 163 164 165 166 167
            //  Join message for radio_dish
            type_join = 106,

            //  Leave message for radio_dish
            type_leave = 107,

            type_max = 107
168
        };
169

170
        //  Note that fields shared between different message types are not
Marin Atanasov Nikolov's avatar
Marin Atanasov Nikolov committed
171
        //  moved to the parent class (msg_t). This way we get tighter packing
172 173 174 175
        //  of the data. Shared fields can be accessed via 'base' member of
        //  the union.
        union {
            struct {
176
                metadata_t *metadata;
177 178
                unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
                                                    2 +
somdoron's avatar
somdoron committed
179
                                                    16 +
180
                                                    sizeof (uint32_t))];
181 182
                unsigned char type;
                unsigned char flags;
somdoron's avatar
somdoron committed
183
                char group [16];
184
                uint32_t routing_id;
185 186
            } base;
            struct {
187
                metadata_t *metadata;
188 189
                unsigned char data [max_vsm_size];
                unsigned char size;
190 191
                unsigned char type;
                unsigned char flags;
somdoron's avatar
somdoron committed
192
                char group [16];
193
                uint32_t routing_id;
194
            } vsm;
195 196 197
            struct {
                metadata_t *metadata;
                content_t *content;
198 199 200
                unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
                                                    sizeof (content_t*) +
                                                    2 +
somdoron's avatar
somdoron committed
201
                                                    16 +
202
                                                    sizeof (uint32_t))];
203 204
                unsigned char type;
                unsigned char flags;
somdoron's avatar
somdoron committed
205
                char group [16];
206 207 208
                uint32_t routing_id;
            } lmsg;
            struct {
209
                metadata_t *metadata;
210
                content_t *content;
211
                unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
212
                                                    sizeof (content_t*) +
213
                                                    2 +
somdoron's avatar
somdoron committed
214
                                                    16 +
215
                                                    sizeof (uint32_t))];
216 217
                unsigned char type;
                unsigned char flags;
somdoron's avatar
somdoron committed
218
                char group [16];
219
                uint32_t routing_id;
220
            } zclmsg;
221
            struct {
222
                metadata_t *metadata;
223 224
                void* data;
                size_t size;
225 226 227 228
                unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
                                                    sizeof (void*) +
                                                    sizeof (size_t) +
                                                    2 +
somdoron's avatar
somdoron committed
229
                                                    16 +
230
                                                    sizeof (uint32_t))];
231 232
                unsigned char type;
                unsigned char flags;
somdoron's avatar
somdoron committed
233
                char group [16];
234
                uint32_t routing_id;
235
            } cmsg;
236
            struct {
237
                metadata_t *metadata;
238 239
                unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
                                                    2 +
somdoron's avatar
somdoron committed
240
                                                    16 +
241
                                                    sizeof (uint32_t))];
242 243
                unsigned char type;
                unsigned char flags;
somdoron's avatar
somdoron committed
244
                char group [16];
245
                uint32_t routing_id;
246 247
            } delimiter;
        } u;
Martin Sustrik's avatar
Martin Sustrik committed
248 249
    };

250
    inline int close_and_return (zmq::msg_t *msg, int echo)
251
    {
252 253
        // Since we abort on close failure we preserve errno for success case.
        int err = errno;
254 255
        const int rc = msg->close ();
        errno_assert (rc == 0);
256 257
        errno = err;
        return echo;
258 259
    }

260
    inline int close_and_return (zmq::msg_t msg [], int count, int echo)
261 262
    {
        for (int i = 0; i < count; i++)
263 264
            close_and_return (&msg [i], 0);
        return echo;
265
    }
266
}
Martin Sustrik's avatar
Martin Sustrik committed
267 268

#endif