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
37namespace zmq
38{
39class io_thread_t;
40class session_base_t;
41struct address_t;
42
43class 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 ();
55
56 protected:
57 // Handlers for incoming commands.
58 void process_plug ();
59 void process_term (int linger_);
60
61 // Handlers for I/O events.
62 void in_event ();
63 void timer_event (int id_);
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 // Reference to the session we belong to.
95 zmq::session_base_t *const _session;
96
97 private:
98 // ID of the timer used to delay the reconnection.
99 enum
100 {
101 reconnect_timer_id = 1
102 };
103
104 // Internal function to return a reconnect backoff delay.
105 // Will modify the current_reconnect_ivl used for next call
106 // Returns the currently used interval
107 int get_new_reconnect_ivl ();
108
109 virtual void start_connecting () = 0;
110
111 // If true, connecter is waiting a while before trying to connect.
112 const bool _delayed_start;
113
114 // True iff a timer has been started.
115 bool _reconnect_timer_started;
116
117 // Current reconnect ivl, updated for backoff strategy
118 int _current_reconnect_ivl;
119
120 ZMQ_NON_COPYABLE_NOR_MOVABLE (stream_connecter_base_t)
121};
122}
123
124#endif
125