libzmq master
The Intelligent Transport Layer

msg.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2011 250bpm s.r.o.
00003     Copyright (c) 2007-2009 iMatix Corporation
00004     Copyright (c) 2011 VMware, Inc.
00005     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
00006 
00007     This file is part of 0MQ.
00008 
00009     0MQ is free software; you can redistribute it and/or modify it under
00010     the terms of the GNU Lesser General Public License as published by
00011     the Free Software Foundation; either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     0MQ is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 
00023 #ifndef __ZMQ_MSG_HPP_INCLUDE__
00024 #define __ZMQ_MSG_HPP_INCLUDE__
00025 
00026 #include <stddef.h>
00027 
00028 #include "config.hpp"
00029 #include "atomic_counter.hpp"
00030 
00031 //  Signature for free function to deallocate the message content.
00032 //  Note that it has to be declared as "C" so that it is the same as
00033 //  zmq_free_fn defined in zmq.h.
00034 extern "C"
00035 {
00036     typedef void (msg_free_fn) (void *data, void *hint);
00037 }
00038 
00039 namespace zmq
00040 {
00041 
00042     //  Note that this structure needs to be explicitly constructed
00043     //  (init functions) and destructed (close function).
00044 
00045     class msg_t
00046     {
00047     public:
00048 
00049         //  Mesage flags.
00050         enum
00051         {
00052             more = 1,
00053             identity = 64,
00054             shared = 128
00055         };
00056 
00057         bool check ();
00058         int init ();
00059         int init_size (size_t size_);
00060         int init_data (void *data_, size_t size_, msg_free_fn *ffn_,
00061             void *hint_);
00062         int init_delimiter ();
00063         int close ();
00064         int move (msg_t &src_);
00065         int copy (msg_t &src_);
00066         void *data ();
00067         size_t size ();
00068         unsigned char flags ();
00069         void set_flags (unsigned char flags_);
00070         void reset_flags (unsigned char flags_);
00071         bool is_delimiter ();
00072         bool is_vsm ();
00073 
00074         //  After calling this function you can copy the message in POD-style
00075         //  refs_ times. No need to call copy.
00076         void add_refs (int refs_);
00077 
00078         //  Removes references previously added by add_refs. If the number of
00079         //  references drops to 0, the message is closed and false is returned.
00080         bool rm_refs (int refs_);
00081 
00082     private:
00083 
00084         //  Size in bytes of the largest message that is still copied around
00085         //  rather than being reference-counted.
00086         enum {max_vsm_size = 29};
00087 
00088         //  Shared message buffer. Message data are either allocated in one
00089         //  continuous block along with this structure - thus avoiding one
00090         //  malloc/free pair or they are stored in used-supplied memory.
00091         //  In the latter case, ffn member stores pointer to the function to be
00092         //  used to deallocate the data. If the buffer is actually shared (there
00093         //  are at least 2 references to it) refcount member contains number of
00094         //  references.
00095         struct content_t
00096         {
00097             void *data;
00098             size_t size;
00099             msg_free_fn *ffn;
00100             void *hint;
00101             zmq::atomic_counter_t refcnt;
00102         };
00103 
00104         //  Different message types.
00105         enum type_t
00106         {
00107             type_min = 101,
00108             type_vsm = 101,
00109             type_lmsg = 102,
00110             type_delimiter = 103,
00111             type_max = 103
00112         };
00113 
00114         //  Note that fields shared between different message types are not
00115         //  moved to tha parent class (msg_t). This way we ger tighter packing
00116         //  of the data. Shared fields can be accessed via 'base' member of
00117         //  the union.
00118         union {
00119             struct {
00120                 unsigned char unused [max_vsm_size + 1];
00121                 unsigned char type;
00122                 unsigned char flags;
00123             } base;
00124             struct {
00125                 unsigned char data [max_vsm_size];
00126                 unsigned char size;
00127                 unsigned char type;
00128                 unsigned char flags;
00129             } vsm;
00130             struct {
00131                 content_t *content;
00132                 unsigned char unused [max_vsm_size + 1 - sizeof (content_t*)];
00133                 unsigned char type;
00134                 unsigned char flags;
00135             } lmsg;
00136             struct {
00137                 unsigned char unused [max_vsm_size + 1];
00138                 unsigned char type;
00139                 unsigned char flags;
00140             } delimiter;
00141         } u;
00142     };
00143 
00144 }
00145 
00146 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines