1 /*
2     Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4     This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6     libzmq is free software; you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 3 of the License, or
9     (at your option) any later version.
10 
11     As a special exception, the Contributors give you permission to link
12     this library with independent modules to produce an executable,
13     regardless of the license terms of these independent modules, and to
14     copy and distribute the resulting executable under terms of your choice,
15     provided that you also meet, for each linked independent module, the
16     terms and conditions of the license of that module. An independent
17     module is a module which is not derived from or based on this library.
18     If you modify this library, you must extend this exception to your
19     version of the library.
20 
21     libzmq is distributed in the hope that it will be useful, but WITHOUT
22     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23     FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24     License for more details.
25 
26     You should have received a copy of the GNU Lesser General Public License
27     along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #ifndef __STREAM_CONNECTER_BASE_HPP_INCLUDED__
31 #define __STREAM_CONNECTER_BASE_HPP_INCLUDED__
32 
33 #include "fd.hpp"
34 #include "own.hpp"
35 #include "io_object.hpp"
36 
37 namespace zmq
38 {
39 class io_thread_t;
40 class session_base_t;
41 struct address_t;
42 
43 class stream_connecter_base_t : public own_t, public io_object_t
44 {
45   public:
46     //  If 'delayed_start' is true connecter first waits for a while,
47     //  then starts connection process.
48     stream_connecter_base_t (zmq::io_thread_t *io_thread_,
49                              zmq::session_base_t *session_,
50                              const options_t &options_,
51                              address_t *addr_,
52                              bool delayed_start_);
53 
54     ~stream_connecter_base_t () ZMQ_OVERRIDE;
55 
56   protected:
57     //  Handlers for incoming commands.
58     void process_plug () ZMQ_FINAL;
59     void process_term (int linger_) ZMQ_OVERRIDE;
60 
61     //  Handlers for I/O events.
62     void in_event () ZMQ_OVERRIDE;
63     void timer_event (int id_) ZMQ_OVERRIDE;
64 
65     //  Internal function to create the engine after connection was established.
66     virtual void create_engine (fd_t fd, const std::string &local_address_);
67 
68     //  Internal function to add a reconnect timer
69     void add_reconnect_timer ();
70 
71     //  Removes the handle from the poller.
72     void rm_handle ();
73 
74     //  Close the connecting socket.
75     void close ();
76 
77     //  Address to connect to. Owned by session_base_t.
78     //  It is non-const since some parts may change during opening.
79     address_t *const _addr;
80 
81     //  Underlying socket.
82     fd_t _s;
83 
84     //  Handle corresponding to the listening socket, if file descriptor is
85     //  registered with the poller, or NULL.
86     handle_t _handle;
87 
88     // String representation of endpoint to connect to
89     std::string _endpoint;
90 
91     // Socket
92     zmq::socket_base_t *const _socket;
93 
94   private:
95     //  ID of the timer used to delay the reconnection.
96     enum
97     {
98         reconnect_timer_id = 1
99     };
100 
101     //  Internal function to return a reconnect backoff delay.
102     //  Will modify the current_reconnect_ivl used for next call
103     //  Returns the currently used interval
104     int get_new_reconnect_ivl ();
105 
106     virtual void start_connecting () = 0;
107 
108     //  If true, connecter is waiting a while before trying to connect.
109     const bool _delayed_start;
110 
111     //  True iff a timer has been started.
112     bool _reconnect_timer_started;
113 
114     //  Current reconnect ivl, updated for backoff strategy
115     int _current_reconnect_ivl;
116 
117     ZMQ_NON_COPYABLE_NOR_MOVABLE (stream_connecter_base_t)
118 
119   protected:
120     //  Reference to the session we belong to.
121     zmq::session_base_t *const _session;
122 };
123 }
124 
125 #endif
126