msg.hpp 8.44 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2015 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"
Martin Sustrik's avatar
Martin Sustrik committed
37
#include "atomic_counter.hpp"
38
#include "metadata.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
39

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

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

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

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

58
        //  Message flags.
59 60
        enum
        {
61 62
            more = 1,           //  Followed by more parts
            command = 2,        //  Command frame (see ZMTP spec)
63
            credential = 32,
64
            identity = 64,
65
            shared = 128
66 67 68
        };

        bool check ();
69 70 71 72 73 74
        int init();

        int init (void* data, size_t size_,
                  msg_free_fn* ffn_, void* hint,
                  zmq::atomic_counter_t* refcnt_ = NULL);

75
        int init_size (size_t size_);
76
        int init_data (void *data_, size_t size_, msg_free_fn *ffn_,
77 78 79
                       void *hint_);
        int init_external_storage(void *data_, size_t size_, zmq::atomic_counter_t* ctr,
                                  msg_free_fn *ffn_, void *hint_);
80 81 82 83 84
        int init_delimiter ();
        int close ();
        int move (msg_t &src_);
        int copy (msg_t &src_);
        void *data ();
85
        size_t size ();
86 87 88
        unsigned char flags ();
        void set_flags (unsigned char flags_);
        void reset_flags (unsigned char flags_);
89 90
        int64_t fd ();
        void set_fd (int64_t fd_);
91 92
        metadata_t *metadata () const;
        void set_metadata (metadata_t *metadata_);
93
        void reset_metadata ();
Martin Hurton's avatar
Martin Hurton committed
94
        bool is_identity () const;
95
        bool is_credential () const;
96
        bool is_delimiter () const;
97 98 99
        bool is_vsm () const;
        bool is_cmsg () const;
        bool is_zcmsg() const;
100 101
        uint32_t get_routing_id();
        int set_routing_id(uint32_t routing_id_);
102 103 104 105 106

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

107 108 109
        //  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_);
110

111 112
        //  Size in bytes of the largest message that is still copied around
        //  rather than being reference-counted.
113
        enum { msg_t_size = 64 };
114
        enum { max_vsm_size = msg_t_size - (8 + sizeof (metadata_t *) + 3 + sizeof(uint32_t)) };
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    private:
        zmq::atomic_counter_t* refcnt();

        //  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;
        };

135 136 137 138
        //  Different message types.
        enum type_t
        {
            type_min = 101,
Uli Köhler's avatar
Uli Köhler committed
139
            //  VSM messages store the content in the message itself
140
            type_vsm = 101,
Uli Köhler's avatar
Uli Köhler committed
141
            //  LMSG messages store the content in malloc-ed memory
142
            type_lmsg = 102,
Uli Köhler's avatar
Uli Köhler committed
143
            //  Delimiter messages are used in envelopes
144
            type_delimiter = 103,
Uli Köhler's avatar
Uli Köhler committed
145
            //  CMSG messages point to constant data
146
            type_cmsg = 104,
147

148 149 150 151 152
            // zero-copy LMSG message for v2_decoder
            type_zclmsg = 105,

            type_max = 105
        };
153

154 155
        // the file descriptor where this message originated, needs to be 64bit due to alignment
        int64_t file_desc;
156 157

        //  Note that fields shared between different message types are not
Marin Atanasov Nikolov's avatar
Marin Atanasov Nikolov committed
158
        //  moved to the parent class (msg_t). This way we get tighter packing
159 160 161 162
        //  of the data. Shared fields can be accessed via 'base' member of
        //  the union.
        union {
            struct {
163
                metadata_t *metadata;
164
                unsigned char unused [msg_t_size - (8 + sizeof (metadata_t *) + 2 + sizeof(uint32_t))];
165 166
                unsigned char type;
                unsigned char flags;
167
                uint32_t routing_id;
168 169
            } base;
            struct {
170
                metadata_t *metadata;
171 172
                unsigned char data [max_vsm_size];
                unsigned char size;
173 174
                unsigned char type;
                unsigned char flags;
175
                uint32_t routing_id;
176
            } vsm;
177 178 179 180 181 182 183 184 185 186 187 188
            struct {
                metadata_t *metadata;
                content_t *content;
                unsigned char unused [msg_t_size - (8 + sizeof (metadata_t *)
                                                      + sizeof (content_t*)
                                                      + 2
                                                      + sizeof(uint32_t))];
                unsigned char type;
                unsigned char flags;
                uint32_t routing_id;
            } lmsg;
            struct {
189
                metadata_t *metadata;
190 191 192 193
                void *data;
                size_t size;
                msg_free_fn *ffn;
                void *hint;
194
                zmq::atomic_counter_t* refcnt;
195
                unsigned char unused [msg_t_size - (8 + sizeof (metadata_t *)
196 197 198 199 200 201 202
                                                      + sizeof (void*)
                                                      + sizeof (size_t)
                                                      + sizeof (msg_free_fn*)
                                                      + sizeof (void*)
                                                      + sizeof (zmq::atomic_counter_t*)
                                                      + 2
                                                      + sizeof(uint32_t))];
203 204
                unsigned char type;
                unsigned char flags;
205
                uint32_t routing_id;
206
            } zclmsg;
207
            struct {
208
                metadata_t *metadata;
209 210 211
                void* data;
                size_t size;
                unsigned char unused
212
                    [msg_t_size - (8 + sizeof (metadata_t *) + sizeof (void*) + sizeof (size_t) + 2 + sizeof(uint32_t))];
213 214
                unsigned char type;
                unsigned char flags;
215
                uint32_t routing_id;
216
            } cmsg;
217
            struct {
218
                metadata_t *metadata;
219
                unsigned char unused [msg_t_size - (8 + sizeof (metadata_t *) + 2 + sizeof(uint32_t))];
220 221
                unsigned char type;
                unsigned char flags;
222
                uint32_t routing_id;
223 224
            } delimiter;
        } u;
Martin Sustrik's avatar
Martin Sustrik committed
225 226
    };

227
}
Martin Sustrik's avatar
Martin Sustrik committed
228 229

#endif