msg.hpp 7.73 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
        //  Message flags.
58 59
        enum
        {
60 61
            more = 1,           //  Followed by more parts
            command = 2,        //  Command frame (see ZMTP spec)
62
            credential = 32,
63
            identity = 64,
64
            shared = 128
65 66 67
        };

        bool check ();
68 69
        int init (void *data_, size_t size_, msg_free_fn *ffn_,
                  void *hint_);
70 71
        int init ();
        int init_size (size_t size_);
72
        int init_data (void *data_, size_t size_, msg_free_fn *ffn_,
73 74 75 76 77 78
            void *hint_);
        int init_delimiter ();
        int close ();
        int move (msg_t &src_);
        int copy (msg_t &src_);
        void *data ();
79
        size_t size ();
80 81 82
        unsigned char flags ();
        void set_flags (unsigned char flags_);
        void reset_flags (unsigned char flags_);
83 84
        int64_t fd ();
        void set_fd (int64_t fd_);
85 86
        metadata_t *metadata () const;
        void set_metadata (metadata_t *metadata_);
87
        void reset_metadata ();
Martin Hurton's avatar
Martin Hurton committed
88
        bool is_identity () const;
89
        bool is_credential () const;
90
        bool is_delimiter () const;
91
        bool is_vsm ();
92
        bool is_lmsg () const;
93
        bool is_cmsg ();
94 95
        uint32_t get_routing_id();
        int set_routing_id(uint32_t routing_id_);
96 97 98 99 100

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

101 102 103
        //  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_);
104 105 106

    private:

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

112 113 114 115
        //  Different message types.
        enum type_t
        {
            type_min = 101,
Uli Köhler's avatar
Uli Köhler committed
116
            //  VSM messages store the content in the message itself
117
            type_vsm = 101,
Uli Köhler's avatar
Uli Köhler committed
118
            //  LMSG messages store the content in malloc-ed memory
119
            type_lmsg = 102,
Uli Köhler's avatar
Uli Köhler committed
120
            //  Delimiter messages are used in envelopes
121
            type_delimiter = 103,
Uli Köhler's avatar
Uli Köhler committed
122
            //  CMSG messages point to constant data
123 124
            type_cmsg = 104,
            type_max = 104
125
        };
126

127 128
        atomic_counter_t& msg_counter();

129 130
        // the file descriptor where this message originated, needs to be 64bit due to alignment
        int64_t file_desc;
131 132

        //  Note that fields shared between different message types are not
133
        //  moved to tha parent class (msg_t). This way we get tighter packing
134 135 136 137
        //  of the data. Shared fields can be accessed via 'base' member of
        //  the union.
        union {
            struct {
138
                metadata_t *metadata;
139
                unsigned char unused [msg_t_size - (8 + sizeof (metadata_t *) + 2 + sizeof(uint32_t))];
140 141
                unsigned char type;
                unsigned char flags;
142
                uint32_t routing_id;
143 144
            } base;
            struct {
145
                metadata_t *metadata;
146 147
                unsigned char data [max_vsm_size];
                unsigned char size;
148 149
                unsigned char type;
                unsigned char flags;
150
                uint32_t routing_id;
151
            } vsm;
152
            struct lmsg_t {
153
                metadata_t *metadata;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
                //  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.
                void *data;
                size_t size;
                msg_free_fn *ffn;
                void *hint;
                // create an aligned block for an atomic_counter_t object
                union aligned_atomic_counter_storage {
                    zmq::atomic_counter_t::integer_t maxAlign;
                    unsigned char counter[ sizeof(zmq::atomic_counter_t) ];
                } refcnt;
                unsigned char unused [msg_t_size - (8 + sizeof (metadata_t *)
                                                    + sizeof(void*)
                                                    + sizeof(size_t)
                                                    + sizeof(msg_free_fn*)
                                                    + sizeof(void*)
                                                    + sizeof(aligned_atomic_counter_storage)
                                                    + 2
                                                    + sizeof(uint32_t))];
178 179
                unsigned char type;
                unsigned char flags;
180
                uint32_t routing_id;
181
            } lmsg;
182
            struct {
183
                metadata_t *metadata;
184 185 186
                void* data;
                size_t size;
                unsigned char unused
187
                    [msg_t_size - (8 + sizeof (metadata_t *) + sizeof (void*) + sizeof (size_t) + 2 + sizeof(uint32_t))];
188 189
                unsigned char type;
                unsigned char flags;
190
                uint32_t routing_id;
191
            } cmsg;
192
            struct {
193
                metadata_t *metadata;
194
                unsigned char unused [msg_t_size - (8 + sizeof (metadata_t *) + 2 + sizeof(uint32_t))];
195 196
                unsigned char type;
                unsigned char flags;
197
                uint32_t routing_id;
198 199
            } delimiter;
        } u;
Martin Sustrik's avatar
Martin Sustrik committed
200 201
    };

202
}
Martin Sustrik's avatar
Martin Sustrik committed
203 204

#endif