2009-08-09 09:24:48 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2009-08-09 09:24:48 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2009-08-09 09:24:48 +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-08-09 09:24:48 +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-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/>.
|
|
|
|
*/
|
|
|
|
|
2019-07-17 17:57:44 +03:00
|
|
|
#ifndef __ZMQ_STREAM_ENGINE_BASE_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_STREAM_ENGINE_BASE_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"
|
2014-05-02 12:33:26 +02:00
|
|
|
#include "metadata.hpp"
|
2018-05-18 17:47:47 +02:00
|
|
|
#include "msg.hpp"
|
2019-07-17 17:57:44 +03:00
|
|
|
#include "tcp.hpp"
|
2009-08-09 09:24:48 +02:00
|
|
|
|
|
|
|
namespace zmq
|
|
|
|
{
|
2018-02-01 11:46:09 +01:00
|
|
|
class io_thread_t;
|
|
|
|
class session_base_t;
|
|
|
|
class mechanism_t;
|
2011-11-09 15:22:20 +01:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
// This engine handles any socket with SOCK_STREAM semantics,
|
|
|
|
// e.g. TCP socket or an UNIX domain socket.
|
2011-08-16 12:44:34 +02:00
|
|
|
|
2019-07-17 17:57:44 +03:00
|
|
|
class stream_engine_base_t : public io_object_t, public i_engine
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
|
|
|
public:
|
2019-07-17 17:57:44 +03:00
|
|
|
stream_engine_base_t (fd_t fd_,
|
|
|
|
const options_t &options_,
|
|
|
|
const endpoint_uri_pair_t &endpoint_uri_pair_);
|
|
|
|
~stream_engine_base_t ();
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
// i_engine interface implementation.
|
|
|
|
void plug (zmq::io_thread_t *io_thread_, zmq::session_base_t *session_);
|
|
|
|
void terminate ();
|
2018-08-09 13:17:21 +02:00
|
|
|
bool restart_input ();
|
2018-02-01 11:46:09 +01:00
|
|
|
void restart_output ();
|
|
|
|
void zap_msg_available ();
|
2019-02-01 05:43:45 -05:00
|
|
|
const endpoint_uri_pair_t &get_endpoint () const;
|
2009-08-09 09:24:48 +02:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
// i_poll_events interface implementation.
|
|
|
|
void in_event ();
|
|
|
|
void out_event ();
|
|
|
|
void timer_event (int id_);
|
2012-05-27 15:15:09 +02:00
|
|
|
|
2019-07-17 17:57:44 +03:00
|
|
|
protected:
|
|
|
|
typedef metadata_t::dict_t properties_t;
|
|
|
|
bool init_properties (properties_t &properties_);
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
// Function to handle network disconnections.
|
2019-07-17 17:57:44 +03:00
|
|
|
virtual void error (error_reason_t reason_);
|
2013-04-28 10:15:25 +02:00
|
|
|
|
2018-05-24 17:58:30 +02:00
|
|
|
int next_handshake_command (msg_t *msg_);
|
|
|
|
int process_handshake_command (msg_t *msg_);
|
2013-04-12 11:59:49 +02:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
int pull_msg_from_session (msg_t *msg_);
|
2018-05-24 17:58:30 +02:00
|
|
|
int push_msg_to_session (msg_t *msg_);
|
2015-02-18 12:28:58 -06:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
int pull_and_encode (msg_t *msg_);
|
|
|
|
int decode_and_push (msg_t *msg_);
|
2014-05-09 13:54:24 +00:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
void set_handshake_timer ();
|
2015-02-18 21:24:57 -06:00
|
|
|
|
2019-07-17 17:57:44 +03:00
|
|
|
virtual bool handshake () { return true; };
|
|
|
|
virtual void plug_internal (){};
|
2015-03-16 21:39:16 -04:00
|
|
|
|
2019-07-17 17:57:44 +03:00
|
|
|
virtual int process_command_message (msg_t *msg_) { return -1; };
|
|
|
|
virtual int produce_ping_message (msg_t *msg_) { return -1; };
|
|
|
|
virtual int process_heartbeat_message (msg_t *msg_) { return -1; };
|
|
|
|
virtual int produce_pong_message (msg_t *msg_) { return -1; };
|
2011-07-26 00:43:57 +02:00
|
|
|
|
2019-09-29 18:30:37 +03:00
|
|
|
virtual int read (void *data, size_t size_);
|
|
|
|
virtual int write (const void *data_, size_t size_);
|
|
|
|
|
2019-10-04 20:45:54 +03:00
|
|
|
void reset_pollout () { io_object_t::reset_pollout (_handle); }
|
2019-07-17 17:57:44 +03:00
|
|
|
void set_pollout () { io_object_t::set_pollout (_handle); }
|
|
|
|
void set_pollin () { io_object_t::set_pollin (_handle); }
|
|
|
|
session_base_t *session () { return _session; }
|
|
|
|
socket_base_t *socket () { return _socket; }
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2019-07-17 17:57:44 +03:00
|
|
|
const options_t _options;
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
unsigned char *_inpos;
|
|
|
|
size_t _insize;
|
|
|
|
i_decoder *_decoder;
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
unsigned char *_outpos;
|
|
|
|
size_t _outsize;
|
|
|
|
i_encoder *_encoder;
|
2014-05-02 12:33:26 +02:00
|
|
|
|
2019-07-17 17:57:44 +03:00
|
|
|
mechanism_t *_mechanism;
|
2013-03-18 02:00:00 +01:00
|
|
|
|
2019-07-17 17:57:44 +03:00
|
|
|
int (stream_engine_base_t::*_next_msg) (msg_t *msg_);
|
|
|
|
int (stream_engine_base_t::*_process_msg) (msg_t *msg_);
|
2013-05-13 22:34:27 +02:00
|
|
|
|
2019-07-17 17:57:44 +03:00
|
|
|
// Metadata to be attached to received messages. May be NULL.
|
|
|
|
metadata_t *_metadata;
|
2013-06-06 13:13:10 +02:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
// True iff the engine couldn't consume the last decoded message.
|
2018-05-27 11:10:39 +02:00
|
|
|
bool _input_stopped;
|
2013-04-28 10:15:25 +02:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
// True iff the engine doesn't have any message to encode.
|
2018-05-27 11:10:39 +02:00
|
|
|
bool _output_stopped;
|
2014-05-09 13:54:24 +00:00
|
|
|
|
2019-07-17 17:57:44 +03:00
|
|
|
// Representation of the connected endpoints.
|
|
|
|
const endpoint_uri_pair_t _endpoint_uri_pair;
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
// ID of the handshake timer
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
handshake_timer_id = 0x40
|
|
|
|
};
|
2014-05-09 13:54:24 +00:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
// True is linger timer is running.
|
2018-05-27 11:10:39 +02:00
|
|
|
bool _has_handshake_timer;
|
2015-03-16 21:39:16 -04:00
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
// Heartbeat stuff
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
heartbeat_ivl_timer_id = 0x80,
|
|
|
|
heartbeat_timeout_timer_id = 0x81,
|
|
|
|
heartbeat_ttl_timer_id = 0x82
|
|
|
|
};
|
2018-05-27 11:10:39 +02:00
|
|
|
bool _has_ttl_timer;
|
|
|
|
bool _has_timeout_timer;
|
|
|
|
bool _has_heartbeat_timer;
|
2012-09-21 12:53:31 +01:00
|
|
|
|
2013-07-18 09:39:19 +02:00
|
|
|
|
2019-01-31 07:17:18 -05:00
|
|
|
const std::string _peer_address;
|
2009-08-09 09:24:48 +02:00
|
|
|
|
2019-07-17 17:57:44 +03:00
|
|
|
private:
|
|
|
|
bool in_event_internal ();
|
|
|
|
|
|
|
|
// Unplug the engine from the session.
|
|
|
|
void unplug ();
|
|
|
|
|
|
|
|
int write_credential (msg_t *msg_);
|
|
|
|
int push_one_then_decode_and_push (msg_t *msg_);
|
|
|
|
|
|
|
|
void mechanism_ready ();
|
|
|
|
|
|
|
|
// Underlying socket.
|
|
|
|
fd_t _s;
|
|
|
|
|
|
|
|
handle_t _handle;
|
|
|
|
|
|
|
|
bool _plugged;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
msg_t _tx_msg;
|
|
|
|
|
|
|
|
bool _io_error;
|
|
|
|
|
|
|
|
// The session this engine is attached to.
|
|
|
|
zmq::session_base_t *_session;
|
|
|
|
|
|
|
|
// Socket
|
|
|
|
zmq::socket_base_t *_socket;
|
|
|
|
|
|
|
|
stream_engine_base_t (const stream_engine_base_t &);
|
|
|
|
const stream_engine_base_t &operator= (const stream_engine_base_t &);
|
2018-02-01 11:46:09 +01:00
|
|
|
};
|
2009-08-09 09:24:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|