libzmq master
The Intelligent Transport Layer

tcp_connecter.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2011 250bpm s.r.o.
00003     Copyright (c) 2007-2009 iMatix Corporation
00004     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
00005 
00006     This file is part of 0MQ.
00007 
00008     0MQ is free software; you can redistribute it and/or modify it under
00009     the terms of the GNU Lesser General Public License as published by
00010     the Free Software Foundation; either version 3 of the License, or
00011     (at your option) any later version.
00012 
00013     0MQ is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public License
00019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #ifndef __TCP_CONNECTER_HPP_INCLUDED__
00023 #define __TCP_CONNECTER_HPP_INCLUDED__
00024 
00025 #include "fd.hpp"
00026 #include "own.hpp"
00027 #include "stdint.hpp"
00028 #include "io_object.hpp"
00029 #include "tcp_address.hpp"
00030 
00031 namespace zmq
00032 {
00033 
00034     class tcp_connecter_t : public own_t, public io_object_t
00035     {
00036     public:
00037 
00038         //  If 'delay' is true connecter first waits for a while, then starts
00039         //  connection process.
00040         tcp_connecter_t (class io_thread_t *io_thread_,
00041             class session_base_t *session_, const options_t &options_,
00042             const char *address_, bool delay_);
00043         ~tcp_connecter_t ();
00044 
00045     private:
00046 
00047         //  ID of the timer used to delay the reconnection.
00048         enum {reconnect_timer_id = 1};
00049 
00050         //  Handlers for incoming commands.
00051         void process_plug ();
00052 
00053         //  Handlers for I/O events.
00054         void in_event ();
00055         void out_event ();
00056         void timer_event (int id_);
00057 
00058         //  Internal function to start the actual connection establishment.
00059         void start_connecting ();
00060 
00061         //  Internal function to add a reconnect timer
00062         void add_reconnect_timer();
00063 
00064         //  Internal function to return a reconnect backoff delay.
00065         //  Will modify the current_reconnect_ivl used for next call
00066         //  Returns the currently used interval
00067         int get_new_reconnect_ivl ();
00068 
00069         //  Set address to connect to.
00070         int set_address (const char *addr_);
00071 
00072         //  Open TCP connecting socket. Returns -1 in case of error,
00073         //  0 if connect was successfull immediately and 1 if async connect
00074         //  was launched.
00075         int open ();
00076 
00077         //  Close the connecting socket.
00078         void close ();
00079 
00080         //  Get the file descriptor of newly created connection. Returns
00081         //  retired_fd if the connection was unsuccessfull.
00082         fd_t connect ();
00083 
00084         //  Address to connect to.
00085         tcp_address_t address;
00086 
00087         //  Underlying socket.
00088         fd_t s;
00089 
00090         //  Handle corresponding to the listening socket.
00091         handle_t handle;
00092 
00093         //  If true file descriptor is registered with the poller and 'handle'
00094         //  contains valid value.
00095         bool handle_valid;
00096 
00097         //  If true, connecter is waiting a while before trying to connect.
00098         bool wait;
00099 
00100         //  Reference to the session we belong to.
00101         class session_base_t *session;
00102 
00103         //  Current reconnect ivl, updated for backoff strategy
00104         int current_reconnect_ivl;
00105 
00106         tcp_connecter_t (const tcp_connecter_t&);
00107         const tcp_connecter_t &operator = (const tcp_connecter_t&);
00108     };
00109 
00110 }
00111 
00112 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines