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

    This file is part of 0MQ.

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

    0MQ 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
14
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
15

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

Martin Sustrik's avatar
Martin Sustrik committed
20 21
#ifndef __ZMQ_MSG_HPP_INCLUDE__
#define __ZMQ_MSG_HPP_INCLUDE__
Martin Sustrik's avatar
Martin Sustrik committed
22 23

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

26
#include "config.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
27
#include "atomic_counter.hpp"
28
#include "metadata.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
29

30 31 32 33 34 35 36 37
//  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);
}

38 39
namespace zmq
{
Martin Sustrik's avatar
Martin Sustrik committed
40

41 42
    //  Note that this structure needs to be explicitly constructed
    //  (init functions) and destructed (close function).
43

44
    class msg_t
Martin Sustrik's avatar
Martin Sustrik committed
45
    {
46 47
    public:

48
        //  Message flags.
49 50
        enum
        {
51 52
            more = 1,           //  Followed by more parts
            command = 2,        //  Command frame (see ZMTP spec)
53
            credential = 32,
54
            identity = 64,
55
            shared = 128
56 57 58 59 60
        };

        bool check ();
        int init ();
        int init_size (size_t size_);
61
        int init_data (void *data_, size_t size_, msg_free_fn *ffn_,
62 63 64 65 66 67
            void *hint_);
        int init_delimiter ();
        int close ();
        int move (msg_t &src_);
        int copy (msg_t &src_);
        void *data ();
68
        size_t size ();
69 70 71
        unsigned char flags ();
        void set_flags (unsigned char flags_);
        void reset_flags (unsigned char flags_);
72 73
        int64_t fd ();
        void set_fd (int64_t fd_);
74 75
        metadata_t *metadata () const;
        void set_metadata (metadata_t *metadata_);
76
        void reset_metadata ();
Martin Hurton's avatar
Martin Hurton committed
77
        bool is_identity () const;
78
        bool is_credential () const;
79
        bool is_delimiter () const;
80
        bool is_vsm ();
81
        bool is_cmsg ();
82 83 84 85 86

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

87 88 89
        //  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_);
90 91 92

    private:

93 94
        //  Size in bytes of the largest message that is still copied around
        //  rather than being reference-counted.
95
        enum { msg_t_size = 48 };
96
        enum { max_vsm_size = msg_t_size - (8 + sizeof (metadata_t *) + 3) };
97

98 99 100 101 102 103 104 105 106 107
        //  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;
108
            size_t size;
109
            msg_free_fn *ffn;
110 111 112 113 114 115 116 117
            void *hint;
            zmq::atomic_counter_t refcnt;
        };

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

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 139
                metadata_t *metadata;
                unsigned char unused [msg_t_size - (8 + sizeof (metadata_t *) + 2)];
140 141 142 143
                unsigned char type;
                unsigned char flags;
            } base;
            struct {
144
                metadata_t *metadata;
145 146
                unsigned char data [max_vsm_size];
                unsigned char size;
147 148 149 150
                unsigned char type;
                unsigned char flags;
            } vsm;
            struct {
151
                metadata_t *metadata;
152
                content_t *content;
153
                unsigned char unused [msg_t_size - (8 + sizeof (metadata_t *) + sizeof (content_t*) + 2)];
154 155 156
                unsigned char type;
                unsigned char flags;
            } lmsg;
157
            struct {
158
                metadata_t *metadata;
159 160 161
                void* data;
                size_t size;
                unsigned char unused
162
                    [msg_t_size - (8 + sizeof (metadata_t *) + sizeof (void*) + sizeof (size_t) + 2)];
163 164 165
                unsigned char type;
                unsigned char flags;
            } cmsg;
166
            struct {
167 168
                metadata_t *metadata;
                unsigned char unused [msg_t_size - (8 + sizeof (metadata_t *) + 2)];
169 170 171 172
                unsigned char type;
                unsigned char flags;
            } delimiter;
        } u;
Martin Sustrik's avatar
Martin Sustrik committed
173 174
    };

175
}
Martin Sustrik's avatar
Martin Sustrik committed
176 177

#endif