2009-08-09 09:24:48 +02:00
|
|
|
/*
|
2013-03-12 13:17:00 +01:00
|
|
|
Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
|
2009-08-09 09:24:48 +02:00
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
2010-10-30 15:08:28 +02:00
|
|
|
the terms of the GNU Lesser General Public License as published by
|
2009-08-09 09:24:48 +02:00
|
|
|
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
|
2010-10-30 15:08:28 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2009-08-09 09:24:48 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-08-09 09:24:48 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-08-16 12:44:34 +02:00
|
|
|
#ifndef __ZMQ_STREAM_ENGINE_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_STREAM_ENGINE_HPP_INCLUDED__
|
2009-08-09 09:24:48 +02:00
|
|
|
|
2009-12-08 15:41:50 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2011-07-26 00:43:57 +02:00
|
|
|
#include "fd.hpp"
|
2009-08-30 08:18:31 +02:00
|
|
|
#include "i_engine.hpp"
|
2009-08-09 09:24:48 +02:00
|
|
|
#include "io_object.hpp"
|
2012-09-04 19:44:20 +02:00
|
|
|
#include "i_encoder.hpp"
|
|
|
|
#include "i_decoder.hpp"
|
2009-09-16 11:02:18 +02:00
|
|
|
#include "options.hpp"
|
2012-09-21 12:53:31 +01:00
|
|
|
#include "socket_base.hpp"
|
2012-05-04 02:32:46 +01:00
|
|
|
#include "../include/zmq.h"
|
2009-08-09 09:24:48 +02:00
|
|
|
|
|
|
|
namespace zmq
|
|
|
|
{
|
2013-03-12 15:38:48 +01:00
|
|
|
// Protocol revisions
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ZMTP_1_0 = 0,
|
2013-04-28 10:15:25 +02:00
|
|
|
ZMTP_2_0 = 1
|
2013-03-12 15:38:48 +01:00
|
|
|
};
|
2009-08-09 09:24:48 +02:00
|
|
|
|
2011-11-09 15:22:20 +01:00
|
|
|
class io_thread_t;
|
2013-03-18 02:00:00 +01:00
|
|
|
class msg_t;
|
2011-11-09 15:22:20 +01:00
|
|
|
class session_base_t;
|
2013-05-13 22:34:27 +02:00
|
|
|
class mechanism_t;
|
2011-11-09 15:22:20 +01:00
|
|
|
|
2011-08-16 12:44:34 +02:00
|
|
|
// This engine handles any socket with SOCK_STREAM semantics,
|
|
|
|
// e.g. TCP socket or an UNIX domain socket.
|
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
class stream_engine_t : public io_object_t, public i_engine
|
2009-08-09 09:24:48 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2013-05-17 17:46:30 +02:00
|
|
|
stream_engine_t (fd_t fd_, const options_t &options_,
|
|
|
|
const std::string &endpoint);
|
2011-08-16 12:44:34 +02:00
|
|
|
~stream_engine_t ();
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2009-08-30 08:18:31 +02:00
|
|
|
// i_engine interface implementation.
|
2011-11-09 15:22:20 +01:00
|
|
|
void plug (zmq::io_thread_t *io_thread_,
|
|
|
|
zmq::session_base_t *session_);
|
2010-08-14 08:37:38 +02:00
|
|
|
void terminate ();
|
2013-09-26 09:37:04 +02:00
|
|
|
void restart_input ();
|
|
|
|
void restart_output ();
|
2013-06-06 13:13:10 +02:00
|
|
|
void zap_msg_available ();
|
2009-08-12 09:40:16 +02:00
|
|
|
|
|
|
|
// i_poll_events interface implementation.
|
|
|
|
void in_event ();
|
|
|
|
void out_event ();
|
2009-08-09 09:24:48 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2012-05-27 15:15:09 +02:00
|
|
|
// Unplug the engine from the session.
|
|
|
|
void unplug ();
|
|
|
|
|
2009-08-12 09:40:16 +02:00
|
|
|
// Function to handle network disconnections.
|
|
|
|
void error ();
|
|
|
|
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
// Receives the greeting message from the peer.
|
|
|
|
int receive_greeting ();
|
|
|
|
|
|
|
|
// Detects the protocol used by the peer.
|
|
|
|
bool handshake ();
|
|
|
|
|
2011-07-26 00:43:57 +02:00
|
|
|
// Writes data to the socket. Returns the number of bytes actually
|
|
|
|
// written (even zero is to be considered to be a success). In case
|
|
|
|
// of error or orderly shutdown by the other peer -1 is returned.
|
|
|
|
int write (const void *data_, size_t size_);
|
|
|
|
|
2013-10-09 10:51:30 +02:00
|
|
|
// Reads data from the socket (up to 'size' bytes).
|
|
|
|
// Returns the number of bytes actually read or -1 on error.
|
|
|
|
// Zero indicates the peer has closed the connection.
|
2011-07-26 00:43:57 +02:00
|
|
|
int read (void *data_, size_t size_);
|
|
|
|
|
2013-04-12 11:59:49 +02:00
|
|
|
int read_identity (msg_t *msg_);
|
|
|
|
int write_identity (msg_t *msg_);
|
2013-03-18 02:00:00 +01:00
|
|
|
|
2013-09-04 17:59:45 +02:00
|
|
|
int next_handshake_command (msg_t *msg);
|
|
|
|
int process_handshake_command (msg_t *msg);
|
2013-04-28 10:15:25 +02:00
|
|
|
|
2013-04-12 11:59:49 +02:00
|
|
|
int pull_msg_from_session (msg_t *msg_);
|
|
|
|
int push_msg_to_session (msg_t *msg);
|
|
|
|
|
2013-06-22 11:48:33 +02:00
|
|
|
int pull_and_encode (msg_t *msg_);
|
|
|
|
int decode_and_push (msg_t *msg_);
|
|
|
|
int push_one_then_decode_and_push (msg_t *msg_);
|
|
|
|
|
2013-05-13 22:34:27 +02:00
|
|
|
void mechanism_ready ();
|
2013-04-28 10:15:25 +02:00
|
|
|
|
2013-04-12 11:59:49 +02:00
|
|
|
int write_subscription_msg (msg_t *msg_);
|
2013-03-18 02:00:00 +01:00
|
|
|
|
2013-04-28 10:15:25 +02:00
|
|
|
size_t add_property (unsigned char *ptr,
|
|
|
|
const char *name, const void *value, size_t value_len);
|
|
|
|
|
2011-07-26 00:43:57 +02:00
|
|
|
// Underlying socket.
|
|
|
|
fd_t s;
|
|
|
|
|
2013-05-14 10:41:37 +02:00
|
|
|
// True iff this is server's engine.
|
|
|
|
bool as_server;
|
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
msg_t tx_msg;
|
2012-11-10 23:05:10 +01:00
|
|
|
|
2009-08-12 09:40:16 +02:00
|
|
|
handle_t handle;
|
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
unsigned char *inpos;
|
2009-12-08 15:41:50 +01:00
|
|
|
size_t insize;
|
2012-09-04 19:44:20 +02:00
|
|
|
i_decoder *decoder;
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
unsigned char *outpos;
|
2009-12-08 15:41:50 +01:00
|
|
|
size_t outsize;
|
2012-09-04 19:44:20 +02:00
|
|
|
i_encoder *encoder;
|
2009-08-12 09:40:16 +02:00
|
|
|
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
// When true, we are still trying to determine whether
|
|
|
|
// the peer is using versioned protocol, and if so, which
|
|
|
|
// version. When false, normal message flow has started.
|
|
|
|
bool handshaking;
|
|
|
|
|
2013-04-28 10:15:25 +02:00
|
|
|
static const size_t signature_size = 10;
|
|
|
|
|
|
|
|
// Size of ZMTP/1.0 and ZMTP/2.0 greeting message
|
|
|
|
static const size_t v2_greeting_size = 12;
|
|
|
|
|
|
|
|
// Size of ZMTP/3.0 greeting message
|
|
|
|
static const size_t v3_greeting_size = 64;
|
|
|
|
|
|
|
|
// Expected greeting size.
|
|
|
|
size_t greeting_size;
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
|
2013-03-12 15:56:10 +01:00
|
|
|
// Greeting received from, and sent to peer
|
2013-04-28 10:15:25 +02:00
|
|
|
unsigned char greeting_recv [v3_greeting_size];
|
|
|
|
unsigned char greeting_send [v3_greeting_size];
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
|
2013-03-12 15:56:10 +01:00
|
|
|
// Size of greeting received so far
|
|
|
|
unsigned int greeting_bytes_read;
|
Extend ZTP/1.0 protocol
The new protocol adds support for protocol version and exchanges the
socket type, so that the library can reject a connection when the
sockets do not match.
The protocol was designed so that it's possible to detect and fully
support ZTP/1.0 peers.
When a new connection is set up, peers exchange greeting messages. The
greeting message encodes both the protocol verion and the socket type.
The format of the greeting message is as follows:
greeting = tag1, adaptation, tag2, version, length, socket_type
tag1 = BYTE / 0xff
adaptation = 8 BYTES
tag2 = BYTE / 0x7f
version = BYTE / 1
length = BYTE / 1
socket_type = BYTE
The protocol does not define the value of adaptation field.
When interoperability with ZTP/1.0 peers is required, the adaptaion
encodes, in network byte order, the length of identity message increased
by 1. When adaptaion consists of eight zeros, the current
implementatatio of 0MQ 2.x closes the connection.
This patch supports both ZTP/1.0 and new protocol.
2012-09-01 13:59:22 +02:00
|
|
|
|
2011-07-24 18:25:30 +02:00
|
|
|
// The session this engine is attached to.
|
2011-11-09 15:22:20 +01:00
|
|
|
zmq::session_base_t *session;
|
2009-08-09 09:24:48 +02:00
|
|
|
|
2009-12-10 09:47:24 +01:00
|
|
|
options_t options;
|
|
|
|
|
2012-05-04 02:32:46 +01:00
|
|
|
// String representation of endpoint
|
2012-11-17 10:06:09 +00:00
|
|
|
std::string endpoint;
|
2012-05-04 02:32:46 +01:00
|
|
|
|
2010-09-19 22:45:48 +02:00
|
|
|
bool plugged;
|
2013-02-01 17:32:28 +09:00
|
|
|
bool terminating;
|
2010-09-19 22:45:48 +02:00
|
|
|
|
2013-04-12 11:59:49 +02:00
|
|
|
int (stream_engine_t::*read_msg) (msg_t *msg_);
|
|
|
|
|
|
|
|
int (stream_engine_t::*write_msg) (msg_t *msg_);
|
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
bool io_error;
|
|
|
|
|
2013-09-04 17:59:45 +02:00
|
|
|
// Indicates whether the engine is to inject a phantom
|
|
|
|
// subscription message into the incoming stream.
|
2013-03-18 02:00:00 +01:00
|
|
|
// Needed to support old peers.
|
|
|
|
bool subscription_required;
|
|
|
|
|
2013-05-13 22:34:27 +02:00
|
|
|
mechanism_t *mechanism;
|
|
|
|
|
2013-06-06 13:13:10 +02:00
|
|
|
// True iff the engine couldn't consume the last decoded message.
|
2013-09-26 09:37:04 +02:00
|
|
|
bool input_stopped;
|
2013-06-06 13:13:10 +02:00
|
|
|
|
|
|
|
// True iff the engine doesn't have any message to encode.
|
2013-09-26 09:37:04 +02:00
|
|
|
bool output_stopped;
|
2013-04-28 10:15:25 +02:00
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// Socket
|
|
|
|
zmq::socket_base_t *socket;
|
|
|
|
|
2013-07-18 09:39:19 +02:00
|
|
|
std::string peer_address;
|
|
|
|
|
2011-08-16 12:44:34 +02:00
|
|
|
stream_engine_t (const stream_engine_t&);
|
|
|
|
const stream_engine_t &operator = (const stream_engine_t&);
|
2009-08-09 09:24:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|