msg.hpp 4.54 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3
    Copyright (c) 2007-2009 iMatix Corporation
4
    Copyright (c) 2011 VMware, Inc.
5
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
6 7 8 9

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
10
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
11 12 13 14 15 16
    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
17
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
18

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

Martin Sustrik's avatar
Martin Sustrik committed
23 24
#ifndef __ZMQ_MSG_HPP_INCLUDE__
#define __ZMQ_MSG_HPP_INCLUDE__
Martin Sustrik's avatar
Martin Sustrik committed
25 26

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

29
#include "config.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
30 31
#include "atomic_counter.hpp"

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

40 41
namespace zmq
{
Martin Sustrik's avatar
Martin Sustrik committed
42

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

46
    class msg_t
Martin Sustrik's avatar
Martin Sustrik committed
47
    {
48 49 50 51 52
    public:

        //  Mesage flags.
        enum
        {
53
            more = 1,
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 72
        unsigned char flags ();
        void set_flags (unsigned char flags_);
        void reset_flags (unsigned char flags_);
        bool is_delimiter ();
73
        bool is_vsm ();
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 109 110 111 112 113 114 115 116 117 118 119 120
            void *hint;
            zmq::atomic_counter_t refcnt;
        };

        //  Different message types.
        enum type_t
        {
            type_min = 101,
            type_vsm = 101,
            type_lmsg = 102,
            type_delimiter = 103,
            type_max = 103
        };

        //  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 {
121
                unsigned char unused [max_vsm_size + 1];
122 123 124 125
                unsigned char type;
                unsigned char flags;
            } base;
            struct {
126 127
                unsigned char data [max_vsm_size];
                unsigned char size;
128 129 130 131
                unsigned char type;
                unsigned char flags;
            } vsm;
            struct {
132 133
                content_t *content;
                unsigned char unused [max_vsm_size + 1 - sizeof (content_t*)];
134 135 136 137
                unsigned char type;
                unsigned char flags;
            } lmsg;
            struct {
138
                unsigned char unused [max_vsm_size + 1];
139 140 141 142
                unsigned char type;
                unsigned char flags;
            } delimiter;
        } u;
Martin Sustrik's avatar
Martin Sustrik committed
143 144
    };

145
}
Martin Sustrik's avatar
Martin Sustrik committed
146 147

#endif