2009-08-12 09:40:16 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2009-08-12 09:40:16 +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-12 09:40:16 +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-12 09:40:16 +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-12 09:40:16 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-09-15 10:00:23 +02:00
|
|
|
#ifndef __ZMQ_SESSION_BASE_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_SESSION_BASE_HPP_INCLUDED__
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2011-07-24 18:25:30 +02:00
|
|
|
#include <string>
|
2012-08-25 01:42:31 +02:00
|
|
|
#include <stdarg.h>
|
2011-07-24 18:25:30 +02:00
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
#include "own.hpp"
|
2010-10-16 10:53:29 +02:00
|
|
|
#include "io_object.hpp"
|
2010-08-06 17:49:37 +02:00
|
|
|
#include "pipe.hpp"
|
2012-09-21 13:53:31 +02:00
|
|
|
#include "socket_base.hpp"
|
2014-05-17 21:04:38 +02:00
|
|
|
#include "stream_engine.hpp"
|
2009-08-12 09:40:16 +02:00
|
|
|
|
|
|
|
namespace zmq
|
|
|
|
{
|
|
|
|
|
2011-11-09 15:22:20 +01:00
|
|
|
class pipe_t;
|
|
|
|
class io_thread_t;
|
|
|
|
class socket_base_t;
|
|
|
|
struct i_engine;
|
2012-02-02 14:56:51 +01:00
|
|
|
struct address_t;
|
2011-11-09 15:22:20 +01:00
|
|
|
|
2011-09-15 10:00:23 +02:00
|
|
|
class session_base_t :
|
2010-08-11 14:09:56 +02:00
|
|
|
public own_t,
|
2010-10-16 10:53:29 +02:00
|
|
|
public io_object_t,
|
2013-03-18 02:00:00 +01:00
|
|
|
public i_pipe_events
|
2009-08-12 09:40:16 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2011-09-15 10:00:23 +02:00
|
|
|
// Create a session of the particular type.
|
2011-11-09 15:22:20 +01:00
|
|
|
static session_base_t *create (zmq::io_thread_t *io_thread_,
|
2013-12-03 09:59:26 +01:00
|
|
|
bool active_, zmq::socket_base_t *socket_,
|
2014-03-12 17:53:13 +01:00
|
|
|
const options_t &options_, address_t *addr_);
|
2009-12-23 19:37:56 +01:00
|
|
|
|
2011-05-25 10:25:51 +02:00
|
|
|
// To be used once only, when creating the session.
|
2011-11-09 15:22:20 +01:00
|
|
|
void attach_pipe (zmq::pipe_t *pipe_);
|
2011-05-25 10:25:51 +02:00
|
|
|
|
2011-07-24 18:25:30 +02:00
|
|
|
// Following functions are the interface exposed towards the engine.
|
2012-05-29 21:59:22 +02:00
|
|
|
virtual void reset ();
|
2009-08-12 09:40:16 +02:00
|
|
|
void flush ();
|
2014-05-17 21:04:38 +02:00
|
|
|
void engine_error (zmq::stream_engine_t::error_reason_t reason);
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// i_pipe_events interface implementation.
|
2011-11-09 15:22:20 +01:00
|
|
|
void read_activated (zmq::pipe_t *pipe_);
|
|
|
|
void write_activated (zmq::pipe_t *pipe_);
|
|
|
|
void hiccuped (zmq::pipe_t *pipe_);
|
2013-05-28 16:49:24 +02:00
|
|
|
void pipe_terminated (zmq::pipe_t *pipe_);
|
2009-08-27 10:54:28 +02:00
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
// Delivers a message. Returns 0 if successful; -1 otherwise.
|
|
|
|
// The function takes ownership of the message.
|
2016-01-28 17:31:05 +01:00
|
|
|
virtual int push_msg (msg_t *msg_);
|
2013-03-18 02:00:00 +01:00
|
|
|
|
2013-06-06 13:13:10 +02:00
|
|
|
int zap_connect ();
|
2014-12-03 22:46:28 +01:00
|
|
|
bool zap_enabled ();
|
2013-06-06 13:13:10 +02:00
|
|
|
|
2013-03-18 02:00:00 +01:00
|
|
|
// Fetches a message. Returns 0 if successful; -1 otherwise.
|
|
|
|
// The caller is responsible for freeing the message when no
|
|
|
|
// longer used.
|
2016-01-28 17:31:05 +01:00
|
|
|
virtual int pull_msg (msg_t *msg_);
|
2013-03-18 02:00:00 +01:00
|
|
|
|
2013-06-06 13:13:10 +02:00
|
|
|
// Receives message from ZAP socket.
|
|
|
|
// Returns 0 on success; -1 otherwise.
|
|
|
|
// The caller is responsible for freeing the message.
|
|
|
|
int read_zap_msg (msg_t *msg_);
|
|
|
|
|
|
|
|
// Sends message to ZAP socket.
|
|
|
|
// Returns 0 on success; -1 otherwise.
|
|
|
|
// The function takes ownership of the message.
|
|
|
|
int write_zap_msg (msg_t *msg_);
|
|
|
|
|
2012-09-21 13:53:31 +02:00
|
|
|
socket_base_t *get_socket ();
|
2017-08-08 14:10:20 +02:00
|
|
|
const char *get_endpoint () const;
|
2012-05-04 03:32:46 +02:00
|
|
|
|
2011-09-15 10:00:23 +02:00
|
|
|
protected:
|
2009-09-02 10:22:23 +02:00
|
|
|
|
2013-12-03 09:59:26 +01:00
|
|
|
session_base_t (zmq::io_thread_t *io_thread_, bool active_,
|
2011-11-09 15:22:20 +01:00
|
|
|
zmq::socket_base_t *socket_, const options_t &options_,
|
2014-03-12 17:53:13 +01:00
|
|
|
address_t *addr_);
|
2012-02-02 14:56:51 +01:00
|
|
|
virtual ~session_base_t ();
|
2011-09-15 10:00:23 +02:00
|
|
|
|
|
|
|
private:
|
2009-09-02 10:22:23 +02:00
|
|
|
|
2011-07-24 18:25:30 +02:00
|
|
|
void start_connecting (bool wait_);
|
2010-08-06 17:49:37 +02:00
|
|
|
|
2013-12-03 09:59:26 +01:00
|
|
|
void reconnect ();
|
2011-05-30 10:07:34 +02:00
|
|
|
|
2009-08-12 09:40:16 +02:00
|
|
|
// Handlers for incoming commands.
|
|
|
|
void process_plug ();
|
2011-11-09 15:22:20 +01:00
|
|
|
void process_attach (zmq::i_engine *engine_);
|
2010-10-16 10:53:29 +02:00
|
|
|
void process_term (int linger_);
|
|
|
|
|
|
|
|
// i_poll_events handlers.
|
|
|
|
void timer_event (int id_);
|
2010-08-11 14:09:56 +02:00
|
|
|
|
2010-10-13 10:09:46 +02:00
|
|
|
// Remove any half processed messages. Flush unflushed messages.
|
|
|
|
// Call this function when engine disconnect to get rid of leftovers.
|
|
|
|
void clean_pipes ();
|
|
|
|
|
2011-07-24 18:25:30 +02:00
|
|
|
// If true, this session (re)connects to the peer. Otherwise, it's
|
|
|
|
// a transient session created by the listener.
|
2014-09-11 16:00:48 +02:00
|
|
|
const bool active;
|
2011-07-24 18:25:30 +02:00
|
|
|
|
2011-05-22 17:26:53 +02:00
|
|
|
// Pipe connecting the session to its socket.
|
2011-11-09 15:22:20 +01:00
|
|
|
zmq::pipe_t *pipe;
|
2013-06-06 11:00:41 +02:00
|
|
|
|
2013-06-06 13:13:10 +02:00
|
|
|
// Pipe used to exchange messages with ZAP socket.
|
|
|
|
zmq::pipe_t *zap_pipe;
|
|
|
|
|
2012-06-12 16:34:48 +02:00
|
|
|
// This set is added to with pipes we are disconnecting, but haven't yet completed
|
2013-06-06 11:00:41 +02:00
|
|
|
std::set <pipe_t *> terminating_pipes;
|
2012-06-12 15:52:18 +02:00
|
|
|
|
2010-03-20 15:04:30 +01:00
|
|
|
// This flag is true if the remainder of the message being processed
|
|
|
|
// is still in the in pipe.
|
|
|
|
bool incomplete_in;
|
|
|
|
|
2011-05-26 11:30:25 +02:00
|
|
|
// True if termination have been suspended to push the pending
|
|
|
|
// messages to the network.
|
|
|
|
bool pending;
|
2011-05-25 10:25:51 +02:00
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
// The protocol I/O engine connected to the session.
|
2011-11-09 15:22:20 +01:00
|
|
|
zmq::i_engine *engine;
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
// The socket the session belongs to.
|
2011-11-09 15:22:20 +01:00
|
|
|
zmq::socket_base_t *socket;
|
2010-08-11 14:09:56 +02:00
|
|
|
|
|
|
|
// I/O thread the session is living in. It will be used to plug in
|
|
|
|
// the engines into the same thread.
|
2011-11-09 15:22:20 +01:00
|
|
|
zmq::io_thread_t *io_thread;
|
2010-08-11 14:09:56 +02:00
|
|
|
|
2010-10-16 10:53:29 +02:00
|
|
|
// ID of the linger timer
|
|
|
|
enum {linger_timer_id = 0x20};
|
|
|
|
|
|
|
|
// True is linger timer is running.
|
|
|
|
bool has_linger_timer;
|
|
|
|
|
2011-07-24 18:25:30 +02:00
|
|
|
// Protocol and address to use when connecting.
|
2014-03-12 17:53:13 +01:00
|
|
|
address_t *addr;
|
2011-07-24 18:25:30 +02:00
|
|
|
|
2011-09-15 10:00:23 +02:00
|
|
|
session_base_t (const session_base_t&);
|
|
|
|
const session_base_t &operator = (const session_base_t&);
|
2009-08-12 09:40:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|