msg.hpp 5.37 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 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
            credential = 32,
53
            identity = 64,
54
            shared = 128
55 56 57 58 59
        };

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

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

83 84 85
        //  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_);
86 87 88

    private:

89 90 91 92
        //  Size in bytes of the largest message that is still copied around
        //  rather than being reference-counted.
        enum {max_vsm_size = 29};

93 94 95 96 97 98 99 100 101 102
        //  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;
103
            size_t size;
104
            msg_free_fn *ffn;
105 106 107 108 109 110 111 112
            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
113
            //  VSM messages store the content in the message itself
114
            type_vsm = 101,
Uli Köhler's avatar
Uli Köhler committed
115
            //  LMSG messages store the content in malloc-ed memory
116
            type_lmsg = 102,
Uli Köhler's avatar
Uli Köhler committed
117
            //  Delimiter messages are used in envelopes
118
            type_delimiter = 103,
Uli Köhler's avatar
Uli Köhler committed
119
            //  CMSG messages point to constant data
120 121
            type_cmsg = 104,
            type_max = 104
122
        };
123 124 125
  
        // the file descriptor where this message originated, needs to be 64bit due to alignment
        int64_t file_desc;
126 127 128 129 130 131 132

        //  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 {
133
                unsigned char unused [max_vsm_size + 1];
134 135 136 137
                unsigned char type;
                unsigned char flags;
            } base;
            struct {
138 139
                unsigned char data [max_vsm_size];
                unsigned char size;
140 141 142 143
                unsigned char type;
                unsigned char flags;
            } vsm;
            struct {
144 145
                content_t *content;
                unsigned char unused [max_vsm_size + 1 - sizeof (content_t*)];
146 147 148
                unsigned char type;
                unsigned char flags;
            } lmsg;
149 150 151 152 153 154 155 156
            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;
157
            struct {
158
                unsigned char unused [max_vsm_size + 1];
159 160 161 162
                unsigned char type;
                unsigned char flags;
            } delimiter;
        } u;
Martin Sustrik's avatar
Martin Sustrik committed
163 164
    };

165
}
Martin Sustrik's avatar
Martin Sustrik committed
166 167

#endif