msg.hpp 5.12 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2013 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 28
#include "atomic_counter.hpp"

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

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

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

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

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

        bool check ();
        int init ();
        int init_size (size_t size_);
59
        int init_data (void *data_, size_t size_, msg_free_fn *ffn_,
60 61 62 63 64 65
            void *hint_);
        int init_delimiter ();
        int close ();
        int move (msg_t &src_);
        int copy (msg_t &src_);
        void *data ();
66
        size_t size ();
67 68 69
        unsigned char flags ();
        void set_flags (unsigned char flags_);
        void reset_flags (unsigned char flags_);
Martin Hurton's avatar
Martin Hurton committed
70
        bool is_identity () const;
71
        bool is_delimiter ();
72
        bool is_vsm ();
73
        bool is_cmsg ();
74 75 76 77 78

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

79 80 81
        //  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_);
82 83 84

    private:

85 86 87 88
        //  Size in bytes of the largest message that is still copied around
        //  rather than being reference-counted.
        enum {max_vsm_size = 29};

89 90 91 92 93 94 95 96 97 98
        //  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;
99
            size_t size;
100
            msg_free_fn *ffn;
101 102 103 104 105 106 107 108
            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
109
            //  VSM messages store the content in the message itself
110
            type_vsm = 101,
Uli Köhler's avatar
Uli Köhler committed
111
            //  LMSG messages store the content in malloc-ed memory
112
            type_lmsg = 102,
Uli Köhler's avatar
Uli Köhler committed
113
            //  Delimiter messages are used in envelopes
114
            type_delimiter = 103,
Uli Köhler's avatar
Uli Köhler committed
115
            //  CMSG messages point to constant data
116 117
            type_cmsg = 104,
            type_max = 104
118 119 120 121 122 123 124 125
        };

        //  Note that fields shared between different message types are not
        //  moved to tha parent class (msg_t). This way we ger tighter packing
        //  of the data. Shared fields can be accessed via 'base' member of
        //  the union.
        union {
            struct {
126
                unsigned char unused [max_vsm_size + 1];
127 128 129 130
                unsigned char type;
                unsigned char flags;
            } base;
            struct {
131 132
                unsigned char data [max_vsm_size];
                unsigned char size;
133 134 135 136
                unsigned char type;
                unsigned char flags;
            } vsm;
            struct {
137 138
                content_t *content;
                unsigned char unused [max_vsm_size + 1 - sizeof (content_t*)];
139 140 141
                unsigned char type;
                unsigned char flags;
            } lmsg;
142 143 144 145 146 147 148 149
            struct {
                void* data;
                size_t size;
                unsigned char unused
                    [max_vsm_size + 1 - sizeof (void*) - sizeof (size_t)];
                unsigned char type;
                unsigned char flags;
            } cmsg;
150
            struct {
151
                unsigned char unused [max_vsm_size + 1];
152 153 154 155
                unsigned char type;
                unsigned char flags;
            } delimiter;
        } u;
Martin Sustrik's avatar
Martin Sustrik committed
156 157
    };

158
}
Martin Sustrik's avatar
Martin Sustrik committed
159 160

#endif