Replace socket_handler::fill_buffer() recursion with loop (#8389)
[libt2n] / src / socket_wrapper.cpp
1 /*
2 Copyright (C) 2008 by Intra2net AG - Gerd v. Egidy
3
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
6
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
9
10 As a special exception, if other files instantiate templates or use macros
11 or inline functions from this file, or you compile this file and link it
12 with other works to produce a work based on this file, this file
13 does not by itself cause the resulting work to be covered
14 by the GNU General Public License.
15
16 However the source code for this file must still be made available
17 in accordance with section (3) of the GNU General Public License.
18
19 This exception does not invalidate any other reasons why a work based
20 on this file might be covered by the GNU General Public License.
21 */
22
23 #include <functional>
24 #include <string>
25
26 #include <socket_wrapper.hxx>
27
28 namespace libt2n
29 {
30
31 /// set logging for coming and current connections
32 void BasicSocketWrapper::set_logging(std::ostream *_logstream, log_level_values _log_level)
33 {
34     ConnectionWrapper::set_logging(_logstream,_log_level);
35
36     if (connection_established())
37         get_connection()->set_logging(_logstream,_log_level);
38 }
39
40
41 /// return active connection, create new tcp or unix connection if not existing
42 client_connection* BasicSocketWrapper::get_connection(void)
43 {
44     if (c.get() == NULL)
45     {
46         if (socket_type == tcp_s)
47             c=std::auto_ptr<socket_client_connection>
48                 (new socket_client_connection(port,server,connect_timeout_usec,max_retries,logstream,log_level));
49         else if (socket_type == unix_s)
50             c=std::auto_ptr<socket_client_connection>
51                 (new socket_client_connection(path,connect_timeout_usec,max_retries,logstream,log_level));
52     }
53
54     return c.get();
55 }
56
57 /// try to reconnect max_retries time if we encounter a t2n_communication_error 
58 /// during execution of the command
59 bool ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
60 {
61     int tries=0;
62
63     while(true)
64     {
65         try
66         {
67             // we always reconnect if something went wrong before:
68             // makes sure the buffers are clean of half-sent packets etc.
69             if (tries > 0)
70                 c->reconnect();
71
72             f();
73             // we were successful
74             return true;
75         }
76         catch(t2n_connect_error &e)
77         {
78             // reconnect already tries max_tries times to reconnect: we are done if that failed
79             throw(e);
80         }
81         catch(t2n_communication_error &e)
82         {
83             // aborts the loop with an exception after max_retries iterations
84             // retries means that the first try doesn't count: use >
85             if (tries > max_retries)
86                 throw(e);
87
88             // otherwise ignore the exception and reconnect in the next iteration
89         }
90
91         tries++;
92     }
93
94     return false;
95 }
96
97 /// return active connection, return a dummy-connection if we can't establish one
98 client_connection* ReconnectIgnoreFailureSocketWrapper::get_connection(void)
99 {
100     client_connection* tmp=BasicSocketWrapper::get_connection();
101
102     if (tmp->is_closed())
103     {
104         // throw away the closed connection...
105         c.reset();
106         // ...return the dummy one instead
107         tmp=&dc;
108     }
109
110     return tmp;
111 }
112
113 /// try to execute the command, may ignore the command if server not available
114 bool ReconnectIgnoreFailureSocketWrapper::handle(command_client* stubBase, boost::function< void() > f)
115 {
116     if (!connection_established())
117     {
118         // dummy connection is in place: try to establish a real one
119         client_connection* tmp=get_connection();
120
121         if (tmp != &dc)
122         {
123             // success! we've got a real connection
124             stubBase->replace_connection(tmp);
125         }
126     }
127
128     // only try to handle the call if we've got a real connection
129     if (connection_established())
130     {
131         try
132         {
133             ReconnectSocketWrapper::handle(stubBase,f);
134             return true;
135         }
136         catch(t2n_communication_error &e)
137         {
138             // ignore
139         }
140     }
141
142     return false;
143 }
144
145 }