mirror of
https://github.com/zeromq/libzmq.git
synced 2025-10-27 19:10:22 +01:00
Problem: data members are duplicated across tcp_connecter_t, ipc_connecter_t, tipc_connecter_t
Solution: extract common base class stream_connecter_base_t
This commit is contained in:
@@ -821,6 +821,8 @@ set(cxx-sources
|
|||||||
stdint.hpp
|
stdint.hpp
|
||||||
stream.hpp
|
stream.hpp
|
||||||
stream_engine.hpp
|
stream_engine.hpp
|
||||||
|
stream_connecter_base.hpp
|
||||||
|
stream_connecter_base.cpp
|
||||||
sub.hpp
|
sub.hpp
|
||||||
tcp.hpp
|
tcp.hpp
|
||||||
tcp_address.hpp
|
tcp_address.hpp
|
||||||
|
|||||||
@@ -194,6 +194,8 @@ src_libzmq_la_SOURCES = \
|
|||||||
src/stdint.hpp \
|
src/stdint.hpp \
|
||||||
src/stream.cpp \
|
src/stream.cpp \
|
||||||
src/stream.hpp \
|
src/stream.hpp \
|
||||||
|
src/stream_connecter_base.cpp \
|
||||||
|
src/stream_connecter_base.hpp \
|
||||||
src/stream_engine.cpp \
|
src/stream_engine.cpp \
|
||||||
src/stream_engine.hpp \
|
src/stream_engine.hpp \
|
||||||
src/sub.cpp \
|
src/sub.cpp \
|
||||||
|
|||||||
@@ -54,22 +54,12 @@
|
|||||||
zmq::ipc_connecter_t::ipc_connecter_t (class io_thread_t *io_thread_,
|
zmq::ipc_connecter_t::ipc_connecter_t (class io_thread_t *io_thread_,
|
||||||
class session_base_t *session_,
|
class session_base_t *session_,
|
||||||
const options_t &options_,
|
const options_t &options_,
|
||||||
const address_t *addr_,
|
address_t *addr_,
|
||||||
bool delayed_start_) :
|
bool delayed_start_) :
|
||||||
own_t (io_thread_, options_),
|
stream_connecter_base_t (
|
||||||
io_object_t (io_thread_),
|
io_thread_, session_, options_, addr_, delayed_start_)
|
||||||
_addr (addr_),
|
|
||||||
_s (retired_fd),
|
|
||||||
_handle (static_cast<handle_t> (NULL)),
|
|
||||||
_delayed_start (delayed_start_),
|
|
||||||
_reconnect_timer_started (false),
|
|
||||||
_session (session_),
|
|
||||||
_current_reconnect_ivl (options.reconnect_ivl)
|
|
||||||
{
|
{
|
||||||
zmq_assert (_addr);
|
|
||||||
zmq_assert (_addr->protocol == protocol_name::ipc);
|
zmq_assert (_addr->protocol == protocol_name::ipc);
|
||||||
_addr->to_string (_endpoint);
|
|
||||||
_socket = _session->get_socket ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
zmq::ipc_connecter_t::~ipc_connecter_t ()
|
zmq::ipc_connecter_t::~ipc_connecter_t ()
|
||||||
|
|||||||
@@ -34,17 +34,11 @@
|
|||||||
&& !defined ZMQ_HAVE_VXWORKS
|
&& !defined ZMQ_HAVE_VXWORKS
|
||||||
|
|
||||||
#include "fd.hpp"
|
#include "fd.hpp"
|
||||||
#include "own.hpp"
|
#include "stream_connecter_base.hpp"
|
||||||
#include "stdint.hpp"
|
|
||||||
#include "io_object.hpp"
|
|
||||||
|
|
||||||
namespace zmq
|
namespace zmq
|
||||||
{
|
{
|
||||||
class io_thread_t;
|
class ipc_connecter_t : public stream_connecter_base_t
|
||||||
class session_base_t;
|
|
||||||
struct address_t;
|
|
||||||
|
|
||||||
class ipc_connecter_t : public own_t, public io_object_t
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// If 'delayed_start' is true connecter first waits for a while,
|
// If 'delayed_start' is true connecter first waits for a while,
|
||||||
@@ -52,7 +46,7 @@ class ipc_connecter_t : public own_t, public io_object_t
|
|||||||
ipc_connecter_t (zmq::io_thread_t *io_thread_,
|
ipc_connecter_t (zmq::io_thread_t *io_thread_,
|
||||||
zmq::session_base_t *session_,
|
zmq::session_base_t *session_,
|
||||||
const options_t &options_,
|
const options_t &options_,
|
||||||
const address_t *addr_,
|
address_t *addr_,
|
||||||
bool delayed_start_);
|
bool delayed_start_);
|
||||||
~ipc_connecter_t ();
|
~ipc_connecter_t ();
|
||||||
|
|
||||||
@@ -95,33 +89,6 @@ class ipc_connecter_t : public own_t, public io_object_t
|
|||||||
// retired_fd if the connection was unsuccessful.
|
// retired_fd if the connection was unsuccessful.
|
||||||
fd_t connect ();
|
fd_t connect ();
|
||||||
|
|
||||||
// Address to connect to. Owned by session_base_t.
|
|
||||||
const address_t *_addr;
|
|
||||||
|
|
||||||
// Underlying socket.
|
|
||||||
fd_t _s;
|
|
||||||
|
|
||||||
// Handle corresponding to the listening socket.
|
|
||||||
handle_t _handle;
|
|
||||||
|
|
||||||
// If true, connecter is waiting a while before trying to connect.
|
|
||||||
const bool _delayed_start;
|
|
||||||
|
|
||||||
// True iff a timer has been started.
|
|
||||||
bool _reconnect_timer_started;
|
|
||||||
|
|
||||||
// Reference to the session we belong to.
|
|
||||||
zmq::session_base_t *_session;
|
|
||||||
|
|
||||||
// Current reconnect ivl, updated for backoff strategy
|
|
||||||
int _current_reconnect_ivl;
|
|
||||||
|
|
||||||
// String representation of endpoint to connect to
|
|
||||||
std::string _endpoint;
|
|
||||||
|
|
||||||
// Socket
|
|
||||||
zmq::socket_base_t *_socket;
|
|
||||||
|
|
||||||
ipc_connecter_t (const ipc_connecter_t &);
|
ipc_connecter_t (const ipc_connecter_t &);
|
||||||
const ipc_connecter_t &operator= (const ipc_connecter_t &);
|
const ipc_connecter_t &operator= (const ipc_connecter_t &);
|
||||||
};
|
};
|
||||||
|
|||||||
57
src/stream_connecter_base.cpp
Normal file
57
src/stream_connecter_base.cpp
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
||||||
|
|
||||||
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
||||||
|
|
||||||
|
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
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "precompiled.hpp"
|
||||||
|
#include "stream_connecter_base.hpp"
|
||||||
|
#include "session_base.hpp"
|
||||||
|
#include "address.hpp"
|
||||||
|
|
||||||
|
zmq::stream_connecter_base_t::stream_connecter_base_t (
|
||||||
|
zmq::io_thread_t *io_thread_,
|
||||||
|
zmq::session_base_t *session_,
|
||||||
|
const zmq::options_t &options_,
|
||||||
|
zmq::address_t *addr_,
|
||||||
|
bool delayed_start_) :
|
||||||
|
own_t (io_thread_, options_),
|
||||||
|
io_object_t (io_thread_),
|
||||||
|
_addr (addr_),
|
||||||
|
_s (retired_fd),
|
||||||
|
_handle (static_cast<handle_t> (NULL)),
|
||||||
|
_delayed_start (delayed_start_),
|
||||||
|
_reconnect_timer_started (false),
|
||||||
|
_session (session_),
|
||||||
|
_current_reconnect_ivl (options.reconnect_ivl),
|
||||||
|
_socket (_session->get_socket ())
|
||||||
|
{
|
||||||
|
zmq_assert (_addr);
|
||||||
|
_addr->to_string (_endpoint);
|
||||||
|
// TODO the return value is unused! what if it fails? if this is impossible
|
||||||
|
// or does not matter, change such that endpoint in initialized using an
|
||||||
|
// initializer, and make endpoint const
|
||||||
|
}
|
||||||
90
src/stream_connecter_base.hpp
Normal file
90
src/stream_connecter_base.hpp
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
||||||
|
|
||||||
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
||||||
|
|
||||||
|
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
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
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 __STREAM_CONNECTER_BASE_HPP_INCLUDED__
|
||||||
|
#define __STREAM_CONNECTER_BASE_HPP_INCLUDED__
|
||||||
|
|
||||||
|
#include "fd.hpp"
|
||||||
|
#include "own.hpp"
|
||||||
|
#include "io_object.hpp"
|
||||||
|
|
||||||
|
namespace zmq
|
||||||
|
{
|
||||||
|
class io_thread_t;
|
||||||
|
class session_base_t;
|
||||||
|
struct address_t;
|
||||||
|
|
||||||
|
class stream_connecter_base_t : public own_t, public io_object_t
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// If 'delayed_start' is true connecter first waits for a while,
|
||||||
|
// then starts connection process.
|
||||||
|
stream_connecter_base_t (zmq::io_thread_t *io_thread_,
|
||||||
|
zmq::session_base_t *session_,
|
||||||
|
const options_t &options_,
|
||||||
|
address_t *addr_,
|
||||||
|
bool delayed_start_);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// Address to connect to. Owned by session_base_t.
|
||||||
|
// It is non-const since some parts may change during opening.
|
||||||
|
address_t *const _addr;
|
||||||
|
|
||||||
|
// Underlying socket.
|
||||||
|
fd_t _s;
|
||||||
|
|
||||||
|
// Handle corresponding to the listening socket, if file descriptor is
|
||||||
|
// registered with the poller, or NULL.
|
||||||
|
handle_t _handle;
|
||||||
|
|
||||||
|
// If true, connecter is waiting a while before trying to connect.
|
||||||
|
const bool _delayed_start;
|
||||||
|
|
||||||
|
// True iff a timer has been started.
|
||||||
|
bool _reconnect_timer_started;
|
||||||
|
|
||||||
|
// Reference to the session we belong to.
|
||||||
|
zmq::session_base_t *const _session;
|
||||||
|
|
||||||
|
// Current reconnect ivl, updated for backoff strategy
|
||||||
|
int _current_reconnect_ivl;
|
||||||
|
|
||||||
|
// String representation of endpoint to connect to
|
||||||
|
std::string _endpoint;
|
||||||
|
|
||||||
|
// Socket
|
||||||
|
zmq::socket_base_t *const _socket;
|
||||||
|
|
||||||
|
private:
|
||||||
|
stream_connecter_base_t (const stream_connecter_base_t &);
|
||||||
|
const stream_connecter_base_t &operator= (const stream_connecter_base_t &);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -69,24 +69,11 @@ zmq::tcp_connecter_t::tcp_connecter_t (class io_thread_t *io_thread_,
|
|||||||
const options_t &options_,
|
const options_t &options_,
|
||||||
address_t *addr_,
|
address_t *addr_,
|
||||||
bool delayed_start_) :
|
bool delayed_start_) :
|
||||||
own_t (io_thread_, options_),
|
stream_connecter_base_t (
|
||||||
io_object_t (io_thread_),
|
io_thread_, session_, options_, addr_, delayed_start_),
|
||||||
_addr (addr_),
|
_connect_timer_started (false)
|
||||||
_s (retired_fd),
|
|
||||||
_handle (static_cast<handle_t> (NULL)),
|
|
||||||
_delayed_start (delayed_start_),
|
|
||||||
_connect_timer_started (false),
|
|
||||||
_reconnect_timer_started (false),
|
|
||||||
_session (session_),
|
|
||||||
_current_reconnect_ivl (options.reconnect_ivl),
|
|
||||||
_socket (_session->get_socket ())
|
|
||||||
{
|
{
|
||||||
zmq_assert (_addr);
|
|
||||||
zmq_assert (_addr->protocol == protocol_name::tcp);
|
zmq_assert (_addr->protocol == protocol_name::tcp);
|
||||||
_addr->to_string (_endpoint);
|
|
||||||
// TODO the return value is unused! what if it fails? if this is impossible
|
|
||||||
// or does not matter, change such that endpoint in initialized using an
|
|
||||||
// initializer, and make endpoint const
|
|
||||||
}
|
}
|
||||||
|
|
||||||
zmq::tcp_connecter_t::~tcp_connecter_t ()
|
zmq::tcp_connecter_t::~tcp_connecter_t ()
|
||||||
|
|||||||
@@ -31,17 +31,12 @@
|
|||||||
#define __TCP_CONNECTER_HPP_INCLUDED__
|
#define __TCP_CONNECTER_HPP_INCLUDED__
|
||||||
|
|
||||||
#include "fd.hpp"
|
#include "fd.hpp"
|
||||||
#include "own.hpp"
|
|
||||||
#include "stdint.hpp"
|
#include "stdint.hpp"
|
||||||
#include "io_object.hpp"
|
#include "stream_connecter_base.hpp"
|
||||||
|
|
||||||
namespace zmq
|
namespace zmq
|
||||||
{
|
{
|
||||||
class io_thread_t;
|
class tcp_connecter_t : public stream_connecter_base_t
|
||||||
class session_base_t;
|
|
||||||
struct address_t;
|
|
||||||
|
|
||||||
class tcp_connecter_t : public own_t, public io_object_t
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// If 'delayed_start' is true connecter first waits for a while,
|
// If 'delayed_start' is true connecter first waits for a while,
|
||||||
@@ -102,34 +97,8 @@ class tcp_connecter_t : public own_t, public io_object_t
|
|||||||
// Tunes a connected socket.
|
// Tunes a connected socket.
|
||||||
bool tune_socket (fd_t fd_);
|
bool tune_socket (fd_t fd_);
|
||||||
|
|
||||||
// Address to connect to. Owned by session_base_t.
|
|
||||||
address_t *const _addr;
|
|
||||||
|
|
||||||
// Underlying socket.
|
|
||||||
fd_t _s;
|
|
||||||
|
|
||||||
// Handle corresponding to the listening socket, if file descriptor is
|
|
||||||
// registered with the poller, or NULL.
|
|
||||||
handle_t _handle;
|
|
||||||
|
|
||||||
// If true, connecter is waiting a while before trying to connect.
|
|
||||||
const bool _delayed_start;
|
|
||||||
|
|
||||||
// True iff a timer has been started.
|
// True iff a timer has been started.
|
||||||
bool _connect_timer_started;
|
bool _connect_timer_started;
|
||||||
bool _reconnect_timer_started;
|
|
||||||
|
|
||||||
// Reference to the session we belong to.
|
|
||||||
zmq::session_base_t *const _session;
|
|
||||||
|
|
||||||
// Current reconnect ivl, updated for backoff strategy
|
|
||||||
int _current_reconnect_ivl;
|
|
||||||
|
|
||||||
// String representation of endpoint to connect to
|
|
||||||
std::string _endpoint;
|
|
||||||
|
|
||||||
// Socket
|
|
||||||
zmq::socket_base_t *const _socket;
|
|
||||||
|
|
||||||
tcp_connecter_t (const tcp_connecter_t &);
|
tcp_connecter_t (const tcp_connecter_t &);
|
||||||
const tcp_connecter_t &operator= (const tcp_connecter_t &);
|
const tcp_connecter_t &operator= (const tcp_connecter_t &);
|
||||||
|
|||||||
@@ -56,22 +56,12 @@
|
|||||||
zmq::tipc_connecter_t::tipc_connecter_t (class io_thread_t *io_thread_,
|
zmq::tipc_connecter_t::tipc_connecter_t (class io_thread_t *io_thread_,
|
||||||
class session_base_t *session_,
|
class session_base_t *session_,
|
||||||
const options_t &options_,
|
const options_t &options_,
|
||||||
const address_t *addr_,
|
address_t *addr_,
|
||||||
bool delayed_start_) :
|
bool delayed_start_) :
|
||||||
own_t (io_thread_, options_),
|
stream_connecter_base_t (
|
||||||
io_object_t (io_thread_),
|
io_thread_, session_, options_, addr_, delayed_start_)
|
||||||
_addr (addr_),
|
|
||||||
_s (retired_fd),
|
|
||||||
_handle (static_cast<handle_t> (NULL)),
|
|
||||||
_delayed_start (delayed_start_),
|
|
||||||
_reconnect_timer_started (false),
|
|
||||||
_session (session_),
|
|
||||||
_current_reconnect_ivl (options.reconnect_ivl)
|
|
||||||
{
|
{
|
||||||
zmq_assert (_addr);
|
|
||||||
zmq_assert (_addr->protocol == "tipc");
|
zmq_assert (_addr->protocol == "tipc");
|
||||||
_addr->to_string (_endpoint);
|
|
||||||
_socket = _session->get_socket ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
zmq::tipc_connecter_t::~tipc_connecter_t ()
|
zmq::tipc_connecter_t::~tipc_connecter_t ()
|
||||||
|
|||||||
@@ -35,17 +35,11 @@
|
|||||||
#if defined ZMQ_HAVE_TIPC
|
#if defined ZMQ_HAVE_TIPC
|
||||||
|
|
||||||
#include "fd.hpp"
|
#include "fd.hpp"
|
||||||
#include "own.hpp"
|
#include "stream_connecter_base.hpp"
|
||||||
#include "stdint.hpp"
|
|
||||||
#include "io_object.hpp"
|
|
||||||
|
|
||||||
namespace zmq
|
namespace zmq
|
||||||
{
|
{
|
||||||
class io_thread_t;
|
class tipc_connecter_t : public stream_connecter_base_t
|
||||||
class session_base_t;
|
|
||||||
struct address_t;
|
|
||||||
|
|
||||||
class tipc_connecter_t : public own_t, public io_object_t
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// If 'delayed_start' is true connecter first waits for a while,
|
// If 'delayed_start' is true connecter first waits for a while,
|
||||||
@@ -53,7 +47,7 @@ class tipc_connecter_t : public own_t, public io_object_t
|
|||||||
tipc_connecter_t (zmq::io_thread_t *io_thread_,
|
tipc_connecter_t (zmq::io_thread_t *io_thread_,
|
||||||
zmq::session_base_t *session_,
|
zmq::session_base_t *session_,
|
||||||
const options_t &options_,
|
const options_t &options_,
|
||||||
const address_t *addr_,
|
address_t *addr_,
|
||||||
bool delayed_start_);
|
bool delayed_start_);
|
||||||
~tipc_connecter_t ();
|
~tipc_connecter_t ();
|
||||||
|
|
||||||
@@ -86,33 +80,6 @@ class tipc_connecter_t : public own_t, public io_object_t
|
|||||||
// retired_fd if the connection was unsuccessful.
|
// retired_fd if the connection was unsuccessful.
|
||||||
fd_t connect ();
|
fd_t connect ();
|
||||||
|
|
||||||
// Address to connect to. Owned by session_base_t.
|
|
||||||
const address_t *_addr;
|
|
||||||
|
|
||||||
// Underlying socket.
|
|
||||||
fd_t _s;
|
|
||||||
|
|
||||||
// Handle corresponding to the listening socket.
|
|
||||||
handle_t _handle;
|
|
||||||
|
|
||||||
// If true, connecter is waiting a while before trying to connect.
|
|
||||||
const bool _delayed_start;
|
|
||||||
|
|
||||||
// True iff a timer has been started.
|
|
||||||
bool _reconnect_timer_started;
|
|
||||||
|
|
||||||
// Reference to the session we belong to.
|
|
||||||
zmq::session_base_t *_session;
|
|
||||||
|
|
||||||
// Current reconnect ivl, updated for backoff strategy
|
|
||||||
int _current_reconnect_ivl;
|
|
||||||
|
|
||||||
// String representation of endpoint to connect to
|
|
||||||
std::string _endpoint;
|
|
||||||
|
|
||||||
// Socket
|
|
||||||
zmq::socket_base_t *_socket;
|
|
||||||
|
|
||||||
// Internal function to return a reconnect backoff delay.
|
// Internal function to return a reconnect backoff delay.
|
||||||
// Will modify the current_reconnect_ivl used for next call
|
// Will modify the current_reconnect_ivl used for next call
|
||||||
// Returns the currently used interval
|
// Returns the currently used interval
|
||||||
|
|||||||
Reference in New Issue
Block a user