2009-08-09 09:24:48 +02:00
|
|
|
/*
|
2011-10-31 16:20:30 +01:00
|
|
|
Copyright (c) 2009-2011 250bpm s.r.o.
|
2011-11-01 18:06:11 +01:00
|
|
|
Copyright (c) 2007-2009 iMatix Corporation
|
2011-03-02 16:30:40 +01:00
|
|
|
Copyright (c) 2007-2011 Other 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"
|
2012-09-02 18:19:15 +02:00
|
|
|
#include "i_msg_sink.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-05-04 02:32:46 +01:00
|
|
|
#include "../include/zmq.h"
|
2009-08-09 09:24:48 +02:00
|
|
|
|
|
|
|
namespace zmq
|
|
|
|
{
|
|
|
|
|
2011-11-09 15:22:20 +01:00
|
|
|
class io_thread_t;
|
|
|
|
class session_base_t;
|
|
|
|
|
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.
|
|
|
|
|
2012-09-02 18:19:15 +02:00
|
|
|
class stream_engine_t : public io_object_t, public i_engine, public i_msg_sink
|
2009-08-09 09:24:48 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-08-04 11:41:33 +01: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 ();
|
2010-08-11 14:09:56 +02:00
|
|
|
void activate_in ();
|
|
|
|
void activate_out ();
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2012-09-02 18:19:15 +02:00
|
|
|
// i_msg_sink interface implementation.
|
|
|
|
virtual int push_msg (msg_t *msg_);
|
|
|
|
|
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_);
|
|
|
|
|
|
|
|
// Reads data from the socket (up to 'size' bytes). Returns the number
|
|
|
|
// of bytes actually read (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 read (void *data_, size_t size_);
|
|
|
|
|
|
|
|
// Underlying socket.
|
|
|
|
fd_t s;
|
|
|
|
|
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
|
|
|
// Maximum size of a greeting message:
|
|
|
|
// preamble (10 bytes) + version (1 byte) + remaining_length (1 byte) +
|
|
|
|
// up to 255 remaining bytes.
|
|
|
|
const static size_t maximum_greeting_size = 10 + 1 + 1 + 255;
|
|
|
|
|
|
|
|
// Size of v1 greeting message:
|
|
|
|
// preamble (10 bytes) + version (1 byte) + remaining_length (1 byte) +
|
|
|
|
// socket_type (1)
|
|
|
|
const static size_t v1_greeting_size = 10 + 1 + 1 + 1;
|
|
|
|
|
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;
|
2012-04-29 17:13:18 +02:00
|
|
|
bool input_error;
|
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;
|
|
|
|
|
|
|
|
// The receive buffer holding the greeting message
|
|
|
|
// that we are receiving from the peer.
|
|
|
|
unsigned char greeting [maximum_greeting_size];
|
|
|
|
|
|
|
|
// The number of bytes of the greeting message that
|
|
|
|
// we have already received.
|
|
|
|
unsigned int greeting_bytes_read;
|
|
|
|
|
|
|
|
// The size of the greeting message.
|
|
|
|
unsigned int greeting_size;
|
|
|
|
|
|
|
|
// The send buffer holding the greeting message
|
|
|
|
// that we are sending to the peer.
|
|
|
|
unsigned char greeting_output_buffer [v1_greeting_size];
|
|
|
|
|
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
|
|
|
|
std::string endpoint;
|
|
|
|
|
2010-09-19 22:45:48 +02:00
|
|
|
bool plugged;
|
|
|
|
|
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
|