14
14
15
15
ASyncTCP::ASyncTCP ()
16
16
: mIsConnected(false ), mIsClosing(false ),
17
- mSocket(mIos ), mReconnectTimer(mIos )
17
+ mSocket(mIos ), mReconnectTimer(mIos ),
18
+ mDoReconnect(true ), mIsReconnecting(false )
18
19
{
19
20
}
20
21
@@ -144,18 +145,6 @@ bool ASyncTCP::set_tcp_keepalive()
144
145
}
145
146
*/
146
147
147
- void ASyncTCP::read ()
148
- {
149
- if (!mIsConnected ) return ;
150
- if (mIsClosing ) return ;
151
-
152
- mSocket .async_read_some (boost::asio::buffer (m_buffer,sizeof (m_buffer)),
153
- boost::bind (&ASyncTCP::handle_read,
154
- this ,
155
- boost::asio::placeholders::error,
156
- boost::asio::placeholders::bytes_transferred));
157
- }
158
-
159
148
// callbacks
160
149
161
150
void ASyncTCP::handle_connect (const boost::system::error_code& error)
@@ -183,20 +172,42 @@ void ASyncTCP::handle_connect(const boost::system::error_code& error)
183
172
mIsConnected = false ;
184
173
185
174
OnError (error);
186
- _log. Log (LOG_ERROR, " TCP: Error: %s " , error. message (). c_str () );
175
+ OnErrorInt ( error);
187
176
188
- // schedule a timer to reconnect after 30 seconds
189
- mReconnectTimer .expires_from_now (boost::posix_time::seconds (RECONNECT_TIME));
190
- mReconnectTimer .async_wait (boost::bind (&ASyncTCP::do_reconnect, this , boost::asio::placeholders::error));
177
+ if (!mDoReconnect )
178
+ {
179
+ OnDisconnect ();
180
+ return ;
181
+ }
182
+ if (!mIsReconnecting )
183
+ {
184
+ mIsReconnecting = true ;
185
+ _log.Log (LOG_STATUS, " TCP: Reconnecting in %d seconds..." , RECONNECT_TIME);
186
+ // schedule a timer to reconnect after 30 seconds
187
+ mReconnectTimer .expires_from_now (boost::posix_time::seconds (RECONNECT_TIME));
188
+ mReconnectTimer .async_wait (boost::bind (&ASyncTCP::do_reconnect, this , boost::asio::placeholders::error));
189
+ }
191
190
}
192
191
}
193
192
193
+ void ASyncTCP::read ()
194
+ {
195
+ if (!mIsConnected ) return ;
196
+ if (mIsClosing ) return ;
197
+
198
+ mSocket .async_read_some (boost::asio::buffer (m_buffer, sizeof (m_buffer)),
199
+ boost::bind (&ASyncTCP::handle_read,
200
+ this ,
201
+ boost::asio::placeholders::error,
202
+ boost::asio::placeholders::bytes_transferred));
203
+ }
204
+
194
205
void ASyncTCP::handle_read (const boost::system::error_code& error, size_t bytes_transferred)
195
206
{
196
207
if (!error)
197
208
{
198
- // Read next
199
209
OnData (m_buffer,bytes_transferred);
210
+ // Read next
200
211
// This gives some work to the io_service before it is started
201
212
mIos .post (boost::bind (&ASyncTCP::read, this ));
202
213
}
@@ -209,10 +220,19 @@ void ASyncTCP::handle_read(const boost::system::error_code& error, size_t bytes_
209
220
210
221
// let listeners know
211
222
OnError (error);
212
-
213
- // schedule a timer to reconnect after 30 seconds
214
- mReconnectTimer .expires_from_now (boost::posix_time::seconds (RECONNECT_TIME));
215
- mReconnectTimer .async_wait (boost::bind (&ASyncTCP::do_reconnect, this , boost::asio::placeholders::error));
223
+ if (!mDoReconnect )
224
+ {
225
+ OnDisconnect ();
226
+ return ;
227
+ }
228
+ if (!mIsReconnecting )
229
+ {
230
+ mIsReconnecting = true ;
231
+ _log.Log (LOG_STATUS, " TCP: Reconnecting in %d seconds..." , RECONNECT_TIME);
232
+ // schedule a timer to reconnect after 30 seconds
233
+ mReconnectTimer .expires_from_now (boost::posix_time::seconds (RECONNECT_TIME));
234
+ mReconnectTimer .async_wait (boost::bind (&ASyncTCP::do_reconnect, this , boost::asio::placeholders::error));
235
+ }
216
236
}
217
237
else
218
238
do_close ();
@@ -229,9 +249,20 @@ void ASyncTCP::write_end(const boost::system::error_code& error)
229
249
OnError (error);
230
250
231
251
mIsConnected = false ;
232
- // schedule a timer to reconnect after 30 seconds
233
- mReconnectTimer .expires_from_now (boost::posix_time::seconds (RECONNECT_TIME));
234
- mReconnectTimer .async_wait (boost::bind (&ASyncTCP::do_reconnect, this , boost::asio::placeholders::error));
252
+
253
+ if (!mDoReconnect )
254
+ {
255
+ OnDisconnect ();
256
+ return ;
257
+ }
258
+ if (!mIsReconnecting )
259
+ {
260
+ mIsReconnecting = true ;
261
+ _log.Log (LOG_STATUS, " TCP: Reconnecting in %d seconds..." , RECONNECT_TIME);
262
+ // schedule a timer to reconnect after 30 seconds
263
+ mReconnectTimer .expires_from_now (boost::posix_time::seconds (RECONNECT_TIME));
264
+ mReconnectTimer .async_wait (boost::bind (&ASyncTCP::do_reconnect, this , boost::asio::placeholders::error));
265
+ }
235
266
}
236
267
}
237
268
}
@@ -270,7 +301,37 @@ void ASyncTCP::do_reconnect(const boost::system::error_code& error)
270
301
// close current socket if necessary
271
302
mSocket .close ();
272
303
304
+ if (!mDoReconnect )
305
+ {
306
+ return ;
307
+ }
308
+ mReconnectTimer .cancel ();
273
309
// try to reconnect, then call handle_connect
310
+ _log.Log (LOG_STATUS, " TCP: Reconnecting..." );
274
311
mSocket .async_connect (mEndPoint ,
275
312
boost::bind (&ASyncTCP::handle_connect, this , boost::asio::placeholders::error));
313
+ mIsReconnecting = false ;
314
+ }
315
+
316
+ void ASyncTCP::OnErrorInt (const boost::system::error_code& error)
317
+ {
318
+ if (
319
+ (error == boost::asio::error::address_in_use) ||
320
+ (error == boost::asio::error::connection_refused) ||
321
+ (error == boost::asio::error::access_denied) ||
322
+ (error == boost::asio::error::host_unreachable) ||
323
+ (error == boost::asio::error::timed_out)
324
+ )
325
+ {
326
+ _log.Log (LOG_STATUS, " TCP: Connection problem (Unable to connect to specified IP/Port)" );
327
+ }
328
+ else if (
329
+ (error == boost::asio::error::eof) ||
330
+ (error == boost::asio::error::connection_reset)
331
+ )
332
+ {
333
+ _log.Log (LOG_STATUS, " TCP: Connection reset! (Disconnected)" );
334
+ }
335
+ else
336
+ _log.Log (LOG_ERROR, " TCP: Error: %s" , error.message ().c_str ());
276
337
}
0 commit comments