2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2014-01-02 12:00:57 +01:00
|
|
|
Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
|
2009-07-29 12:07:54 +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-07-29 12:07:54 +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-07-29 12:07:54 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-07-29 12:07:54 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-07-26 18:35:40 +02:00
|
|
|
#ifndef __TCP_CONNECTER_HPP_INCLUDED__
|
|
|
|
#define __TCP_CONNECTER_HPP_INCLUDED__
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
#include "fd.hpp"
|
2011-07-26 00:43:57 +02:00
|
|
|
#include "own.hpp"
|
|
|
|
#include "stdint.hpp"
|
2011-08-18 17:40:42 +02:00
|
|
|
#include "io_object.hpp"
|
2012-05-04 02:32:46 +01:00
|
|
|
#include "../include/zmq.h"
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
namespace zmq
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
|
2011-11-09 15:22:20 +01:00
|
|
|
class io_thread_t;
|
|
|
|
class session_base_t;
|
2012-02-02 14:56:51 +01:00
|
|
|
struct address_t;
|
2011-11-09 15:22:20 +01:00
|
|
|
|
2011-07-26 00:43:57 +02:00
|
|
|
class tcp_connecter_t : public own_t, public io_object_t
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-06-13 02:33:02 +02:00
|
|
|
// If 'delayed_start' is true connecter first waits for a while,
|
|
|
|
// then starts connection process.
|
2011-11-09 15:22:20 +01:00
|
|
|
tcp_connecter_t (zmq::io_thread_t *io_thread_,
|
|
|
|
zmq::session_base_t *session_, const options_t &options_,
|
2014-03-12 12:53:13 -04:00
|
|
|
address_t *addr_, bool delayed_start_);
|
2009-07-29 12:07:54 +02:00
|
|
|
~tcp_connecter_t ();
|
|
|
|
|
2011-07-26 00:43:57 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
// ID of the timer used to delay the reconnection.
|
|
|
|
enum {reconnect_timer_id = 1};
|
|
|
|
|
|
|
|
// Handlers for incoming commands.
|
|
|
|
void process_plug ();
|
2012-06-12 14:28:32 +01:00
|
|
|
void process_term (int linger_);
|
2011-07-26 00:43:57 +02:00
|
|
|
|
|
|
|
// Handlers for I/O events.
|
|
|
|
void in_event ();
|
|
|
|
void out_event ();
|
|
|
|
void timer_event (int id_);
|
|
|
|
|
|
|
|
// Internal function to start the actual connection establishment.
|
|
|
|
void start_connecting ();
|
|
|
|
|
|
|
|
// Internal function to add a reconnect timer
|
|
|
|
void add_reconnect_timer();
|
|
|
|
|
|
|
|
// Internal function to return a reconnect backoff delay.
|
|
|
|
// Will modify the current_reconnect_ivl used for next call
|
|
|
|
// Returns the currently used interval
|
|
|
|
int get_new_reconnect_ivl ();
|
|
|
|
|
2011-07-26 18:35:40 +02:00
|
|
|
// Open TCP connecting socket. Returns -1 in case of error,
|
2011-12-21 16:21:55 +01:00
|
|
|
// 0 if connect was successfull immediately. Returns -1 with
|
|
|
|
// EAGAIN errno if async connect was launched.
|
2009-08-09 16:12:09 +02:00
|
|
|
int open ();
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
// Close the connecting socket.
|
2011-07-29 09:37:43 +02:00
|
|
|
void close ();
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
// Get the file descriptor of newly created connection. Returns
|
|
|
|
// retired_fd if the connection was unsuccessfull.
|
|
|
|
fd_t connect ();
|
|
|
|
|
2012-02-02 14:56:51 +01:00
|
|
|
// Address to connect to. Owned by session_base_t.
|
2014-03-12 12:53:13 -04:00
|
|
|
address_t *addr;
|
2009-08-09 16:12:09 +02:00
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
// Underlying socket.
|
|
|
|
fd_t s;
|
|
|
|
|
2011-07-26 00:43:57 +02:00
|
|
|
// Handle corresponding to the listening socket.
|
|
|
|
handle_t handle;
|
|
|
|
|
|
|
|
// If true file descriptor is registered with the poller and 'handle'
|
|
|
|
// contains valid value.
|
|
|
|
bool handle_valid;
|
|
|
|
|
|
|
|
// If true, connecter is waiting a while before trying to connect.
|
2012-06-13 02:33:02 +02:00
|
|
|
const bool delayed_start;
|
|
|
|
|
|
|
|
// True iff a timer has been started.
|
|
|
|
bool timer_started;
|
2011-07-26 00:43:57 +02:00
|
|
|
|
|
|
|
// Reference to the session we belong to.
|
2011-11-09 15:22:20 +01:00
|
|
|
zmq::session_base_t *session;
|
2011-07-26 00:43:57 +02:00
|
|
|
|
|
|
|
// Current reconnect ivl, updated for backoff strategy
|
|
|
|
int current_reconnect_ivl;
|
|
|
|
|
2012-05-04 02:32:46 +01:00
|
|
|
// String representation of endpoint to connect to
|
|
|
|
std::string endpoint;
|
|
|
|
|
2012-09-21 12:53:31 +01:00
|
|
|
// Socket
|
|
|
|
zmq::socket_base_t *socket;
|
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
tcp_connecter_t (const tcp_connecter_t&);
|
2011-01-13 11:44:23 +01:00
|
|
|
const tcp_connecter_t &operator = (const tcp_connecter_t&);
|
2009-07-29 12:07:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|