msg.hpp 9.21 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 "fd.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
38
#include "atomic_counter.hpp"
39
#include "metadata.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
40

41 42 43 44 45 46 47 48
//  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);
}

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

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

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

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        //  Shared message buffer. Message data are either allocated in one
        //  continuous block along with this structure - thus avoiding one
        //  malloc/free pair or they are stored in used-supplied memory.
        //  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;
        };

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

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

        int init (void* data, size_t size_,
                  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 99
        int init_join ();
        int init_leave ();
100 101 102 103
        int close ();
        int move (msg_t &src_);
        int copy (msg_t &src_);
        void *data ();
104
        size_t size ();
105 106 107
        unsigned char flags ();
        void set_flags (unsigned char flags_);
        void reset_flags (unsigned char flags_);
108 109
        metadata_t *metadata () const;
        void set_metadata (metadata_t *metadata_);
110
        void reset_metadata ();
Martin Hurton's avatar
Martin Hurton committed
111
        bool is_identity () const;
112
        bool is_credential () const;
113
        bool is_delimiter () const;
114 115
        bool is_join () const;
        bool is_leave () const;
116 117 118
        bool is_vsm () const;
        bool is_cmsg () const;
        bool is_zcmsg() const;
119 120
        uint32_t get_routing_id ();
        int set_routing_id (uint32_t routing_id_);
121
        int reset_routing_id ();
somdoron's avatar
somdoron committed
122 123
        const char * group ();
        int set_group (const char* group_);
124
        int set_group (const char*, size_t length);
125 126 127 128 129

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

130 131 132
        //  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_);
133

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

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

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

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

            //  Leave message for radio_dish
            type_leave = 107,

            type_max = 107
167
        };
168

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

249
}
Martin Sustrik's avatar
Martin Sustrik committed
250 251

#endif