2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
2009-07-29 12:07:54 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq 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 GNU Lesser General Public
|
|
|
|
License for more details.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-07-29 12:07:54 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
#ifndef __ZMQ_MSG_HPP_INCLUDE__
|
|
|
|
#define __ZMQ_MSG_HPP_INCLUDE__
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
#include <stddef.h>
|
2012-02-16 15:55:18 -06:00
|
|
|
#include <stdio.h>
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
#include "config.hpp"
|
2017-03-29 16:48:40 -07:00
|
|
|
#include "err.hpp"
|
2016-01-28 11:25:26 +02:00
|
|
|
#include "fd.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
#include "atomic_counter.hpp"
|
2014-05-03 21:00:42 +02:00
|
|
|
#include "metadata.hpp"
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2018-05-28 18:00:11 +01:00
|
|
|
// bits 2-5
|
|
|
|
#define CMD_TYPE_MASK 0x1c
|
|
|
|
|
2011-05-03 23:20:43 +02:00
|
|
|
// 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" {
|
2020-02-23 17:38:29 +00:00
|
|
|
typedef void(msg_free_fn) (void *data_, void *hint_);
|
2011-05-03 23:20:43 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
namespace zmq
|
|
|
|
{
|
2011-04-21 22:27:48 +02:00
|
|
|
// Note that this structure needs to be explicitly constructed
|
|
|
|
// (init functions) and destructed (close function).
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2018-05-28 19:59:44 +01:00
|
|
|
static const char cancel_cmd_name[] = "\6CANCEL";
|
|
|
|
static const char sub_cmd_name[] = "\x9SUBSCRIBE";
|
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
class msg_t
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
2011-04-21 22:27:48 +02:00
|
|
|
public:
|
|
|
|
// 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 user-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
|
2016-01-29 10:45:44 +02:00
|
|
|
// references.
|
2011-04-21 22:27:48 +02:00
|
|
|
struct content_t
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2016-01-29 10:45:44 +02:00
|
|
|
void *data;
|
|
|
|
size_t size;
|
|
|
|
msg_free_fn *ffn;
|
|
|
|
void *hint;
|
|
|
|
zmq::atomic_counter_t refcnt;
|
|
|
|
};
|
|
|
|
|
2013-08-18 22:50:50 +02:00
|
|
|
// Message flags.
|
2011-04-21 22:27:48 +02:00
|
|
|
enum
|
|
|
|
{
|
2013-09-04 17:59:45 +02:00
|
|
|
more = 1, // Followed by more parts
|
|
|
|
command = 2, // Command frame (see ZMTP spec)
|
2018-05-28 18:00:11 +01:00
|
|
|
// Command types, use only bits 2-5 and compare with ==, not bitwise,
|
|
|
|
// a command can never be of more that one type at the same time
|
|
|
|
ping = 4,
|
|
|
|
pong = 8,
|
|
|
|
subscribe = 12,
|
|
|
|
cancel = 16,
|
2020-01-18 18:44:50 +02:00
|
|
|
close_cmd = 20,
|
2014-01-12 21:58:36 +01:00
|
|
|
credential = 32,
|
2017-09-06 17:45:56 +02:00
|
|
|
routing_id = 64,
|
2011-11-01 13:39:54 +01:00
|
|
|
shared = 128
|
2011-04-21 22:27:48 +02:00
|
|
|
};
|
|
|
|
|
2017-03-25 23:49:23 -07:00
|
|
|
bool check () const;
|
2015-06-13 17:30:36 +02:00
|
|
|
int init ();
|
|
|
|
|
2018-05-24 17:58:30 +02:00
|
|
|
int init (void *data_,
|
2015-06-13 17:30:36 +02:00
|
|
|
size_t size_,
|
|
|
|
msg_free_fn *ffn_,
|
2018-05-24 17:58:30 +02:00
|
|
|
void *hint_,
|
2016-01-29 10:45:44 +02:00
|
|
|
content_t *content_ = NULL);
|
2015-06-13 17:30:36 +02:00
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
int init_size (size_t size_);
|
2020-04-09 22:59:43 +00:00
|
|
|
int init_buffer (const void *buf_, size_t size_);
|
2011-05-03 23:20:43 +02:00
|
|
|
int init_data (void *data_, size_t size_, msg_free_fn *ffn_, void *hint_);
|
2016-01-29 10:45:44 +02:00
|
|
|
int init_external_storage (content_t *content_,
|
|
|
|
void *data_,
|
|
|
|
size_t size_,
|
2015-06-13 17:30:36 +02:00
|
|
|
msg_free_fn *ffn_,
|
|
|
|
void *hint_);
|
2011-04-21 22:27:48 +02:00
|
|
|
int init_delimiter ();
|
2016-01-28 18:20:01 +02:00
|
|
|
int init_join ();
|
|
|
|
int init_leave ();
|
2018-05-28 19:59:44 +01:00
|
|
|
int init_subscribe (const size_t size_, const unsigned char *topic);
|
|
|
|
int init_cancel (const size_t size_, const unsigned char *topic);
|
2011-04-21 22:27:48 +02:00
|
|
|
int close ();
|
|
|
|
int move (msg_t &src_);
|
|
|
|
int copy (msg_t &src_);
|
|
|
|
void *data ();
|
2017-03-25 23:49:23 -07:00
|
|
|
size_t size () const;
|
|
|
|
unsigned char flags () const;
|
2011-04-21 22:27:48 +02:00
|
|
|
void set_flags (unsigned char flags_);
|
|
|
|
void reset_flags (unsigned char flags_);
|
2014-05-03 21:00:42 +02:00
|
|
|
metadata_t *metadata () const;
|
|
|
|
void set_metadata (metadata_t *metadata_);
|
2014-08-13 08:51:13 +02:00
|
|
|
void reset_metadata ();
|
2017-09-06 17:45:56 +02:00
|
|
|
bool is_routing_id () const;
|
2014-01-12 21:58:36 +01:00
|
|
|
bool is_credential () const;
|
2014-01-08 07:49:02 +01:00
|
|
|
bool is_delimiter () const;
|
2016-01-28 18:20:01 +02:00
|
|
|
bool is_join () const;
|
|
|
|
bool is_leave () const;
|
2018-05-28 18:00:11 +01:00
|
|
|
bool is_ping () const;
|
|
|
|
bool is_pong () const;
|
2020-01-18 18:44:50 +02:00
|
|
|
bool is_close_cmd () const;
|
2018-05-28 18:00:11 +01:00
|
|
|
|
|
|
|
// These are called on each message received by the session_base class,
|
|
|
|
// so get them inlined to avoid the overhead of 2 function calls per msg
|
2020-02-04 11:57:58 +01:00
|
|
|
bool is_subscribe () const
|
2018-05-28 18:00:11 +01:00
|
|
|
{
|
|
|
|
return (_u.base.flags & CMD_TYPE_MASK) == subscribe;
|
|
|
|
}
|
2020-02-04 11:57:58 +01:00
|
|
|
|
|
|
|
bool is_cancel () const
|
2018-05-28 18:00:11 +01:00
|
|
|
{
|
|
|
|
return (_u.base.flags & CMD_TYPE_MASK) == cancel;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t command_body_size () const;
|
|
|
|
void *command_body ();
|
2015-06-13 17:30:36 +02:00
|
|
|
bool is_vsm () const;
|
|
|
|
bool is_cmsg () const;
|
2018-05-28 16:30:20 +02:00
|
|
|
bool is_lmsg () const;
|
2015-06-13 17:30:36 +02:00
|
|
|
bool is_zcmsg () const;
|
2019-12-25 15:51:26 +01:00
|
|
|
uint32_t get_routing_id () const;
|
2015-09-11 13:15:00 -04:00
|
|
|
int set_routing_id (uint32_t routing_id_);
|
2016-01-19 20:05:22 +02:00
|
|
|
int reset_routing_id ();
|
2019-12-25 15:51:26 +01:00
|
|
|
const char *group () const;
|
2016-01-28 13:43:23 +02:00
|
|
|
int set_group (const char *group_);
|
2018-05-24 17:58:30 +02:00
|
|
|
int set_group (const char *, size_t length_);
|
2011-04-21 22:27:48 +02:00
|
|
|
|
|
|
|
// After calling this function you can copy the message in POD-style
|
|
|
|
// refs_ times. No need to call copy.
|
|
|
|
void add_refs (int refs_);
|
|
|
|
|
2011-09-02 13:44:22 +02:00
|
|
|
// 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_);
|
2011-04-21 22:27:48 +02:00
|
|
|
|
2020-02-03 15:26:29 +01:00
|
|
|
void shrink (size_t new_size_);
|
|
|
|
|
2011-07-08 18:12:59 +02:00
|
|
|
// Size in bytes of the largest message that is still copied around
|
|
|
|
// rather than being reference-counted.
|
2015-01-10 16:44:55 -06:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
msg_t_size = 64
|
|
|
|
};
|
2016-01-28 11:25:26 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
max_vsm_size =
|
|
|
|
msg_t_size - (sizeof (metadata_t *) + 3 + 16 + sizeof (uint32_t))
|
2016-05-04 15:03:15 +03:00
|
|
|
};
|
2018-05-28 18:00:11 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
ping_cmd_name_size = 5, // 4PING
|
|
|
|
cancel_cmd_name_size = 7, // 6CANCEL
|
|
|
|
sub_cmd_name_size = 10 // 9SUBSCRIBE
|
|
|
|
};
|
2015-06-13 17:30:36 +02:00
|
|
|
|
2013-08-19 14:45:29 +02:00
|
|
|
private:
|
|
|
|
zmq::atomic_counter_t *refcnt ();
|
2014-04-29 22:30:51 +02:00
|
|
|
|
2015-06-13 17:30:36 +02:00
|
|
|
// Different message types.
|
2011-04-21 22:27:48 +02:00
|
|
|
enum type_t
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
2011-04-21 22:27:48 +02:00
|
|
|
type_min = 101,
|
2015-06-13 17:30:36 +02:00
|
|
|
// VSM messages store the content in the message itself
|
2011-04-21 22:27:48 +02:00
|
|
|
type_vsm = 101,
|
2015-06-13 17:30:36 +02:00
|
|
|
// LMSG messages store the content in malloc-ed memory
|
|
|
|
type_lmsg = 102,
|
2013-08-19 14:45:29 +02:00
|
|
|
// Delimiter messages are used in envelopes
|
2011-04-21 22:27:48 +02:00
|
|
|
type_delimiter = 103,
|
2013-08-19 14:45:29 +02:00
|
|
|
// CMSG messages point to constant data
|
2013-08-18 23:40:38 +02:00
|
|
|
type_cmsg = 104,
|
2015-06-13 17:30:36 +02:00
|
|
|
|
2016-01-28 18:20:01 +02:00
|
|
|
// zero-copy LMSG message for v2_decoder
|
|
|
|
type_zclmsg = 105,
|
|
|
|
|
|
|
|
// Join message for radio_dish
|
|
|
|
type_join = 106,
|
|
|
|
|
2011-05-03 23:20:43 +02:00
|
|
|
// Leave message for radio_dish
|
2016-01-28 18:20:01 +02:00
|
|
|
type_leave = 107,
|
2015-05-29 23:47:03 +02:00
|
|
|
|
2015-06-13 17:30:36 +02:00
|
|
|
type_max = 107
|
2009-07-29 12:07:54 +02:00
|
|
|
};
|
|
|
|
|
2020-02-08 17:44:53 +02:00
|
|
|
enum group_type_t
|
|
|
|
{
|
|
|
|
group_type_short,
|
|
|
|
group_type_long
|
|
|
|
};
|
|
|
|
|
|
|
|
struct long_group_t
|
|
|
|
{
|
|
|
|
char group[ZMQ_GROUP_MAX_LENGTH + 1];
|
|
|
|
atomic_counter_t refcnt;
|
|
|
|
};
|
|
|
|
|
|
|
|
union group_t
|
|
|
|
{
|
|
|
|
unsigned char type;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
unsigned char type;
|
|
|
|
char group[15];
|
|
|
|
} sgroup;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
unsigned char type;
|
|
|
|
long_group_t *content;
|
|
|
|
} lgroup;
|
|
|
|
};
|
|
|
|
|
2017-03-30 13:46:00 -07:00
|
|
|
// Note that fields shared between different message types are not
|
|
|
|
// moved to the parent class (msg_t). This way we get tighter packing
|
|
|
|
// of the data. Shared fields can be accessed via 'base' member of
|
2011-04-21 22:27:48 +02:00
|
|
|
// the union.
|
2018-02-01 11:46:09 +01:00
|
|
|
union
|
2017-03-29 16:48:40 -07:00
|
|
|
{
|
2017-03-30 13:46:00 -07:00
|
|
|
struct
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
2014-05-03 21:00:42 +02:00
|
|
|
metadata_t *metadata;
|
2020-02-08 17:44:53 +02:00
|
|
|
unsigned char unused[msg_t_size
|
|
|
|
- (sizeof (metadata_t *) + 2
|
|
|
|
+ sizeof (uint32_t) + sizeof (group_t))];
|
2011-04-21 22:27:48 +02:00
|
|
|
unsigned char type;
|
2017-03-30 13:46:00 -07:00
|
|
|
unsigned char flags;
|
2015-02-02 01:17:37 +02:00
|
|
|
uint32_t routing_id;
|
2020-02-08 17:44:53 +02:00
|
|
|
group_t group;
|
2017-03-30 13:46:00 -07:00
|
|
|
} base;
|
2018-02-01 11:46:09 +01:00
|
|
|
struct
|
|
|
|
{
|
2015-06-13 17:30:36 +02:00
|
|
|
metadata_t *metadata;
|
2017-03-30 13:46:00 -07:00
|
|
|
unsigned char data[max_vsm_size];
|
2011-07-08 18:12:59 +02:00
|
|
|
unsigned char size;
|
2011-04-21 22:27:48 +02:00
|
|
|
unsigned char type;
|
2015-06-13 17:30:36 +02:00
|
|
|
unsigned char flags;
|
2017-03-30 13:46:00 -07:00
|
|
|
uint32_t routing_id;
|
2020-02-08 17:44:53 +02:00
|
|
|
group_t group;
|
2018-02-01 11:46:09 +01:00
|
|
|
} vsm;
|
2017-03-29 16:48:40 -07:00
|
|
|
struct
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
2017-03-29 16:48:40 -07:00
|
|
|
metadata_t *metadata;
|
|
|
|
content_t *content;
|
2020-02-08 17:44:53 +02:00
|
|
|
unsigned char
|
|
|
|
unused[msg_t_size
|
|
|
|
- (sizeof (metadata_t *) + sizeof (content_t *) + 2
|
|
|
|
+ sizeof (uint32_t) + sizeof (group_t))];
|
2011-04-21 22:27:48 +02:00
|
|
|
unsigned char type;
|
|
|
|
unsigned char flags;
|
2017-03-29 16:48:40 -07:00
|
|
|
uint32_t routing_id;
|
2020-02-08 17:44:53 +02:00
|
|
|
group_t group;
|
2018-02-01 11:46:09 +01:00
|
|
|
} lmsg;
|
|
|
|
struct
|
|
|
|
{
|
2017-03-29 16:48:40 -07:00
|
|
|
metadata_t *metadata;
|
2016-01-29 10:45:44 +02:00
|
|
|
content_t *content;
|
2020-02-08 17:44:53 +02:00
|
|
|
unsigned char
|
|
|
|
unused[msg_t_size
|
|
|
|
- (sizeof (metadata_t *) + sizeof (content_t *) + 2
|
|
|
|
+ sizeof (uint32_t) + sizeof (group_t))];
|
2011-04-21 22:27:48 +02:00
|
|
|
unsigned char type;
|
|
|
|
unsigned char flags;
|
2015-02-02 01:17:37 +02:00
|
|
|
uint32_t routing_id;
|
2020-02-08 17:44:53 +02:00
|
|
|
group_t group;
|
2015-06-13 17:30:36 +02:00
|
|
|
} zclmsg;
|
2018-02-01 11:46:09 +01:00
|
|
|
struct
|
|
|
|
{
|
2014-05-03 21:00:42 +02:00
|
|
|
metadata_t *metadata;
|
2018-02-01 11:46:09 +01:00
|
|
|
void *data;
|
2013-08-18 23:40:38 +02:00
|
|
|
size_t size;
|
2020-02-08 17:44:53 +02:00
|
|
|
unsigned char unused[msg_t_size
|
|
|
|
- (sizeof (metadata_t *) + sizeof (void *)
|
|
|
|
+ sizeof (size_t) + 2 + sizeof (uint32_t)
|
|
|
|
+ sizeof (group_t))];
|
2013-08-18 23:40:38 +02:00
|
|
|
unsigned char type;
|
|
|
|
unsigned char flags;
|
2015-02-02 01:17:37 +02:00
|
|
|
uint32_t routing_id;
|
2020-02-08 17:44:53 +02:00
|
|
|
group_t group;
|
2018-02-01 11:46:09 +01:00
|
|
|
} cmsg;
|
|
|
|
struct
|
|
|
|
{
|
2014-05-03 21:00:42 +02:00
|
|
|
metadata_t *metadata;
|
2020-02-08 17:44:53 +02:00
|
|
|
unsigned char unused[msg_t_size
|
|
|
|
- (sizeof (metadata_t *) + 2
|
|
|
|
+ sizeof (uint32_t) + sizeof (group_t))];
|
2011-04-21 22:27:48 +02:00
|
|
|
unsigned char type;
|
|
|
|
unsigned char flags;
|
2015-02-02 01:17:37 +02:00
|
|
|
uint32_t routing_id;
|
2020-02-08 17:44:53 +02:00
|
|
|
group_t group;
|
2017-03-30 13:46:00 -07:00
|
|
|
} delimiter;
|
2018-05-27 11:10:39 +02:00
|
|
|
} _u;
|
2018-02-01 11:46:09 +01:00
|
|
|
};
|
2017-03-29 16:48:40 -07:00
|
|
|
|
2018-05-24 17:58:30 +02:00
|
|
|
inline int close_and_return (zmq::msg_t *msg_, int echo_)
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
2017-03-30 13:46:00 -07:00
|
|
|
// Since we abort on close failure we preserve errno for success case.
|
2019-12-25 13:51:21 +01:00
|
|
|
const int err = errno;
|
2018-05-24 17:58:30 +02:00
|
|
|
const int rc = msg_->close ();
|
2017-03-29 16:48:40 -07:00
|
|
|
errno_assert (rc == 0);
|
2017-03-30 13:46:00 -07:00
|
|
|
errno = err;
|
2018-05-24 17:58:30 +02:00
|
|
|
return echo_;
|
2018-02-01 11:46:09 +01:00
|
|
|
}
|
|
|
|
|
2018-05-24 17:58:30 +02:00
|
|
|
inline int close_and_return (zmq::msg_t msg_[], int count_, int echo_)
|
2017-03-29 16:48:40 -07:00
|
|
|
{
|
2018-05-24 17:58:30 +02:00
|
|
|
for (int i = 0; i < count_; i++)
|
|
|
|
close_and_return (&msg_[i], 0);
|
|
|
|
return echo_;
|
2017-03-29 16:48:40 -07:00
|
|
|
}
|
2009-08-21 14:29:22 +02:00
|
|
|
}
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
#endif
|