1 /*
2     Copyright (c) 2011-2012 250bpm s.r.o.
3     Copyright (c) 2011 Other contributors as noted in the AUTHORS file
4 
5     This file is part of Crossroads I/O project.
6 
7     Crossroads I/O is free software; you can redistribute it and/or modify it
8     under the terms of the GNU Lesser General Public License as published by
9     the Free Software Foundation; either version 3 of the License, or
10     (at your option) any later version.
11 
12     Crossroads is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU Lesser General Public License for more details.
16 
17     You should have received a copy of the GNU Lesser General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __IPC_CONNECTER_HPP_INCLUDED__
22 #define __IPC_CONNECTER_HPP_INCLUDED__
23 
24 #include "platform.hpp"
25 
26 #if !defined XS_HAVE_WINDOWS && !defined XS_HAVE_OPENVMS
27 
28 #include "fd.hpp"
29 #include "own.hpp"
30 #include "stdint.hpp"
31 #include "io_object.hpp"
32 #include "address.hpp"
33 
34 namespace xs
35 {
36 
37     class io_thread_t;
38     class session_base_t;
39 
40     class ipc_connecter_t : public own_t, public io_object_t
41     {
42     public:
43 
44         //  If 'delay' is true connecter first waits for a while, then starts
45         //  connection process.
46         ipc_connecter_t (xs::io_thread_t *io_thread_,
47             xs::session_base_t *session_, const options_t &options_,
48             bool delay_);
49         ~ipc_connecter_t ();
50 
51         //  Set address to connect to.
52         int set_address (const char *addr_);
53 
54     private:
55 
56         //  ID of the timer used to delay the reconnection.
57         enum {reconnect_timer_id = 1};
58 
59         //  Handlers for incoming commands.
60         void process_plug ();
61 
62         //  Handlers for I/O events.
63         void in_event (fd_t fd_);
64         void out_event (fd_t fd_);
65         void timer_event (handle_t handle_);
66 
67         //  Internal function to start the actual connection establishment.
68         void start_connecting ();
69 
70         //  Internal function to add a reconnect timer
71         void add_reconnect_timer();
72 
73         //  Internal function to return a reconnect backoff delay.
74         //  Will modify the current_reconnect_ivl used for next call
75         //  Returns the currently used interval
76         int get_new_reconnect_ivl ();
77 
78         //  Open IPC connecting socket. Returns -1 in case of error,
79         //  0 if connect was successfull immediately. Returns -1 with
80         //  EAGAIN errno if async connect was launched.
81         int open ();
82 
83         //  Close the connecting socket.
84         void close ();
85 
86         //  Get the file descriptor of newly created connection. Returns
87         //  retired_fd if the connection was unsuccessfull.
88         fd_t connect ();
89 
90         //  Address to connect to.
91         address_t address;
92 
93         //  Underlying socket.
94         fd_t s;
95 
96         //  Handle corresponding to the listening socket or NULL if the socket
97         //  is not registered with the io_thread.
98         handle_t handle;
99 
100         //  If true, connecter is waiting a while before trying to connect.
101         bool wait;
102 
103         //  Reference to the session we belong to.
104         xs::session_base_t *session;
105 
106         //  Current reconnect ivl, updated for backoff strategy
107         int current_reconnect_ivl;
108 
109         //  Handle of the reconnect timer, if active. NULL otherwise.
110         handle_t reconnect_timer;
111 
112         ipc_connecter_t (const ipc_connecter_t&);
113         const ipc_connecter_t &operator = (const ipc_connecter_t&);
114     };
115 
116 }
117 
118 #endif
119 
120 #endif
121 
122