0
0
mirror of https://github.com/zeromq/libzmq.git synced 2025-01-22 07:29:31 +08:00
libzmq/src/stream_engine.hpp

211 lines
5.7 KiB
C++
Raw Normal View History

/*
Copyright (c) 2007-2015 Contributors as noted in the AUTHORS file
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
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
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ZMQ_STREAM_ENGINE_HPP_INCLUDED__
#define __ZMQ_STREAM_ENGINE_HPP_INCLUDED__
#include <stddef.h>
#include "fd.hpp"
#include "i_engine.hpp"
#include "io_object.hpp"
#include "i_encoder.hpp"
#include "i_decoder.hpp"
2009-09-16 11:02:18 +02:00
#include "options.hpp"
#include "socket_base.hpp"
#include "../include/zmq.h"
#include "metadata.hpp"
namespace zmq
{
// Protocol revisions
enum
{
ZMTP_1_0 = 0,
2013-04-28 10:15:25 +02:00
ZMTP_2_0 = 1
};
class io_thread_t;
class msg_t;
class session_base_t;
2013-05-13 22:34:27 +02:00
class mechanism_t;
// This engine handles any socket with SOCK_STREAM semantics,
// e.g. TCP socket or an UNIX domain socket.
class stream_engine_t : public io_object_t, public i_engine
{
public:
enum error_reason_t {
protocol_error,
connection_error,
timeout_error
};
stream_engine_t (fd_t fd_, const options_t &options_,
const std::string &endpoint);
~stream_engine_t ();
2009-08-12 09:40:16 +02:00
// i_engine interface implementation.
void plug (zmq::io_thread_t *io_thread_,
zmq::session_base_t *session_);
void terminate ();
void restart_input ();
void restart_output ();
void zap_msg_available ();
2009-08-12 09:40:16 +02:00
// i_poll_events interface implementation.
void in_event ();
void out_event ();
void timer_event (int id_);
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 (error_reason_t reason);
2009-08-12 09:40:16 +02:00
// Receives the greeting message from the peer.
int receive_greeting ();
// Detects the protocol used by the peer.
bool handshake ();
2014-05-05 19:51:38 +02:00
int identity_msg (msg_t *msg_);
int process_identity_msg (msg_t *msg_);
int next_handshake_command (msg_t *msg);
int process_handshake_command (msg_t *msg);
2013-04-28 10:15:25 +02:00
int pull_msg_from_session (msg_t *msg_);
int push_msg_to_session (msg_t *msg);
int push_raw_msg_to_session (msg_t *msg);
int write_credential (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
int write_subscription_msg (msg_t *msg_);
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);
void set_handshake_timer();
2015-02-18 21:24:57 -06:00
typedef metadata_t::dict_t properties_t;
bool init_properties (properties_t & properties);
// Underlying socket.
fd_t s;
// True iff this is server's engine.
bool as_server;
msg_t tx_msg;
2012-11-10 23:05:10 +01:00
2009-08-12 09:40:16 +02:00
handle_t handle;
unsigned char *inpos;
size_t insize;
i_decoder *decoder;
2009-08-12 09:40:16 +02:00
unsigned char *outpos;
size_t outsize;
i_encoder *encoder;
2009-08-12 09:40:16 +02:00
// Metadata to be attached to received messages. May be NULL.
metadata_t *metadata;
// 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;
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];
2013-03-12 15:56:10 +01:00
// Size of greeting received so far
unsigned int greeting_bytes_read;
// The session this engine is attached to.
zmq::session_base_t *session;
options_t options;
// String representation of endpoint
std::string endpoint;
bool plugged;
2014-05-05 19:51:38 +02:00
int (stream_engine_t::*next_msg) (msg_t *msg_);
2014-05-05 19:51:38 +02:00
int (stream_engine_t::*process_msg) (msg_t *msg_);
bool io_error;
// Indicates whether the engine is to inject a phantom
// subscription message into the incoming stream.
// Needed to support old peers.
bool subscription_required;
2013-05-13 22:34:27 +02:00
mechanism_t *mechanism;
// True iff the engine couldn't consume the last decoded message.
bool input_stopped;
// True iff the engine doesn't have any message to encode.
bool output_stopped;
2013-04-28 10:15:25 +02:00
// ID of the handshake timer
enum {handshake_timer_id = 0x40};
// True is linger timer is running.
bool has_handshake_timer;
// Socket
zmq::socket_base_t *socket;
std::string peer_address;
stream_engine_t (const stream_engine_t&);
const stream_engine_t &operator = (const stream_engine_t&);
};
}
#endif