2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2011-10-31 16:20:30 +01:00
|
|
|
Copyright (c) 2009-2011 250bpm s.r.o.
|
2011-11-01 18:06:11 +01:00
|
|
|
Copyright (c) 2007-2009 iMatix Corporation
|
2011-03-02 16:30:40 +01:00
|
|
|
Copyright (c) 2007-2011 Other 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"
|
|
|
|
#include "tcp_address.hpp"
|
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;
|
|
|
|
|
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:
|
|
|
|
|
2011-07-26 18:35:40 +02:00
|
|
|
// If 'delay' is true connecter first waits for a while, then starts
|
2011-07-26 00:43:57 +02:00
|
|
|
// 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_,
|
2011-07-28 13:19:55 +02:00
|
|
|
const char *address_, bool delay_);
|
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 ();
|
|
|
|
|
|
|
|
// 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 ();
|
|
|
|
|
2010-01-15 14:11:39 +01:00
|
|
|
// Set address to connect to.
|
2011-07-28 13:19:55 +02:00
|
|
|
int set_address (const char *addr_);
|
2009-08-09 16:12:09 +02:00
|
|
|
|
2011-07-26 18:35:40 +02:00
|
|
|
// Open TCP connecting socket. Returns -1 in case of error,
|
2009-07-29 12:07:54 +02:00
|
|
|
// 0 if connect was successfull immediately and 1 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 ();
|
|
|
|
|
2009-08-09 16:12:09 +02:00
|
|
|
// Address to connect to.
|
2011-08-18 17:40:42 +02:00
|
|
|
tcp_address_t address;
|
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.
|
|
|
|
bool wait;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
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
|