This repository was archived by the owner on Jan 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasio_async_tcp_client_template.hpp
More file actions
486 lines (447 loc) · 12 KB
/
asio_async_tcp_client_template.hpp
File metadata and controls
486 lines (447 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#ifndef __ASIO_ASYNC_TCP_CLIENT_TEMPLATE_HPP__
#define __ASIO_ASYNC_TCP_CLIENT_TEMPLATE_HPP__
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/noncopyable.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <iostream>
#include "std_timer_class.hpp"
//默认30秒超时时间,如果超过时间无数据则自动断开
#define ASYNC_CLIENT_DEFAULT_TIMEOUT_SEC 60
//检查超时的时间间隔
#define ASYNC_CLIENT_DEFAULT_TIMER_CHECK_TIME 1
//默认缓冲区大小
#define ASYNC_CLIENT_DEFAULT_BUFFER_BYTES_SIZE 4096
//async_tcp_client不能再栈上使用
class async_tcp_client :
public boost::enable_shared_from_this<async_tcp_client>,
private boost::noncopyable
{
public:
async_tcp_client(boost::asio::io_service &io_service)
: m_io_service(io_service),
m_resolver(io_service),
m_socket(io_service),
m_io_mutex(), m_close_mutex(),
m_timeouts(ASYNC_CLIENT_DEFAULT_TIMEOUT_SEC), m_last_action_time_sec(),
m_io_timer(io_service), m_connect_timer(io_service),
n_socket_status(SOCKET_STATUS_NORMAL),
m_buffer_size(ASYNC_CLIENT_DEFAULT_BUFFER_BYTES_SIZE)
{
}
virtual ~async_tcp_client()
{
//虚析构函数
//std::cout << "delete tcp session" << std::endl;
}
boost::asio::io_service &get_io_service()
{
return m_io_service;
}
boost::asio::ip::tcp::socket &socket()
{
return m_socket;
}
static boost::asio::ip::tcp::endpoint reslove_endpoint(const std::string host, const std::string port)
{
boost::asio::io_service reslove_io_service;
boost::asio::ip::tcp::resolver tmp_resolver(reslove_io_service);
boost::asio::ip::tcp::resolver::iterator it =
tmp_resolver.resolve(boost::asio::ip::tcp::resolver::query(host, port));
return *it;
}
void async_connect_server(
const std::string host, const std::string port,
int timeouts = ASYNC_CLIENT_DEFAULT_TIMEOUT_SEC
)
{
m_timeouts = timeouts;
boost::asio::ip::tcp::resolver::iterator it =
m_resolver.resolve(boost::asio::ip::tcp::resolver::query(host, port));
async_start_connect(it);
}
void async_connect_server(boost::asio::ip::tcp::endpoint input_endpoint,
int timeouts = ASYNC_CLIENT_DEFAULT_TIMEOUT_SEC)
{
// boost::asio::ip::tcp::resolver::iterator it =
// m_resolver.resolve(boost::asio::ip::tcp::resolver::query(host, port));
// it->endpoint()
m_timeouts = timeouts;
async_start_connect(input_endpoint);
}
void async_recv_data()
{
//申请缓冲区
boost::shared_ptr<char> data_buffer(make_safe_buffer(m_buffer_size, true));
/*
boost::shared_ptr<char> data_buffer(
new char[m_buffer_size],
std::default_delete<char[]>()
);
memset(data_buffer.get(), 0, m_buffer_size);
*/
m_last_action_time_sec.restart();
m_socket.async_read_some(boost::asio::buffer(data_buffer.get(),
m_buffer_size),
boost::bind(&async_tcp_client::handle_tcp_read, shared_from_this(),
boost::asio::placeholders::error,
data_buffer,
boost::asio::placeholders::bytes_transferred)
);
}
void async_send_data(const std::string str_data)
{
boost::shared_ptr<std::string> pstr(new std::string(str_data));
//*pstr = str_data;
m_last_action_time_sec.restart();
m_socket.async_write_some(boost::asio::buffer(*pstr),
boost::bind(&async_tcp_client::handle_tcp_write, shared_from_this(),
boost::asio::placeholders::error,
pstr,
boost::asio::placeholders::bytes_transferred
)
);
/*
boost::asio::async_write(m_socket,boost::asio::buffer(*pstr),
boost::bind(&async_tcp_client::handle_tcp_write, shared_from_this(),
boost::asio::placeholders::error,
pstr,
boost::asio::placeholders::bytes_transferred
)
);
*/
}
void close_tcp_session()
{
//确保只有一个close函数在运行
//Linux下多个close函数一起执行会导致程序崩溃,而Windows不会?
boost::mutex::scoped_lock lk(m_close_mutex);
if (n_socket_status != SOCKET_STATUS_CLOSED)
{
n_socket_status = SOCKET_STATUS_CLOSED;
close_socket(m_socket);
all_timer_cancel();
handle_protocol_closed();
}
}
virtual void handle_protocol_connected()
{
//留给用户实现协议
async_send_data("Client Hello World\n");
return;
}
virtual void handle_protocol_read(boost::shared_ptr<char> data_buffer,
size_t bytes_transferre)
{
//留给用户实现协议
async_send_data("Client Say Hello\n");
return;
}
virtual void handle_protocol_write(boost::shared_ptr<std::string> pstr,
std::size_t bytes_transferred)
{
//留给用户实现协议
//默认发送成功后就异步接收
async_recv_data();
return;
}
virtual void handle_protocol_timer_first_start()
{
io_timer_call_at(ASYNC_CLIENT_DEFAULT_TIMER_CHECK_TIME);
}
virtual void handle_protocol_timer(time_t last_trans_elapsed)
{
if (last_trans_elapsed > ASYNC_CLIENT_DEFAULT_TIMEOUT_SEC)
{
close_tcp_session();
return;
}
io_timer_call_at(ASYNC_CLIENT_DEFAULT_TIMER_CHECK_TIME);
return;
}
virtual void handle_protocol_closed()
{
//本函数内调用close_tcp_session()会导致死锁。
//如果派生类中没有需要主动close的资源,请不要重写此函数。
return;
}
protected:
void set_no_delay()
{
try
{
boost::asio::ip::tcp::no_delay noDelayOption(true);
m_socket.set_option(noDelayOption);
}
catch (std::exception &e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
}
void set_last_action_time()
{
m_last_action_time_sec.restart();
}
//获取上次操作的时间
time_t get_last_trans_elapsed()
{
return m_last_action_time_sec.elapsed();
}
//n秒后调用handle_io_timer回调函数
void io_timer_call_at(const int n_second)
{
m_io_timer.expires_from_now(boost::posix_time::seconds(
n_second));
m_io_timer.async_wait(
boost::bind(&async_tcp_client::handle_io_timer,
shared_from_this(),
boost::asio::placeholders::error
)
);
}
void io_timer_call_at_infin(const int n_second)
{
m_io_timer.expires_from_now(boost::posix_time::pos_infin);
}
//n秒后调用handle_connect_timer回调函数
void connect_timer_call_at(const int n_second)
{
m_connect_timer.expires_from_now(boost::posix_time::seconds(
n_second));
m_connect_timer.async_wait(
boost::bind(&async_tcp_client::handle_connect_timer,
shared_from_this(),
boost::asio::placeholders::error
)
);
}
void connect_timer_call_at_infin(const int n_second)
{
m_connect_timer.expires_from_now(boost::posix_time::pos_infin);
}
void set_buffer_size(const int new_buffer_size)
{
m_buffer_size = new_buffer_size;
}
/*
void set_default_timeouts(int timeouts)
{
m_timeouts = timeouts;
}
*/
private:
void async_start_connect(boost::asio::ip::tcp::endpoint endpoint)
{
/*
std::cout << "Trying connect "
<< endpoint
<< " ..." << std::endl;
*/
//记录开始时间
m_last_action_time_sec.restart();
//timeout check
connect_timer_call_at(m_timeouts);
// Start the asynchronous connect operation.
m_socket.async_connect(endpoint,
boost::bind(&async_tcp_client::handle_tcp_once_connect,
shared_from_this(),
boost::asio::placeholders::error)
);
}
void async_start_connect(boost::asio::ip::tcp::resolver::iterator endpoint_iter)
{
if (endpoint_iter != boost::asio::ip::tcp::resolver::iterator())
{
/*
std::cout << "Trying connect "
<< endpoint_iter->endpoint()
<< " ..." << std::endl;
*/
//记录开始时间
m_last_action_time_sec.restart();
//timeout check
connect_timer_call_at(m_timeouts);
// Start the asynchronous connect operation.
m_socket.async_connect(endpoint_iter->endpoint(),
boost::bind(&async_tcp_client::handle_tcp_iterator_connect,
shared_from_this(),
boost::asio::placeholders::error,
endpoint_iter)
);
}
else
{
//std::cout << "No more endpoint. Shutdown Client" << std::endl;
// There are no more endpoints to try. Shut down the client.
close_tcp_session();
}
}
//取消所有定时器所有任务
void all_timer_cancel()
{
m_io_timer.cancel();
m_connect_timer.cancel();
}
//关闭socket
void close_socket(boost::asio::ip::tcp::socket &close_socket)
{
if (close_socket.is_open() == true)
{
boost::system::error_code ec;
close_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
ec.clear();
close_socket.close(ec);
ec.clear();
}
}
//io timer回调函数
void handle_io_timer(const boost::system::error_code &error)
{
boost::mutex::scoped_lock lk(m_io_mutex);
if (n_socket_status != SOCKET_STATUS_CLOSED)
{
time_t last_trans_elapsed = m_last_action_time_sec.elapsed();
handle_protocol_timer(last_trans_elapsed);
}
}
//connect timer回调函数
void handle_connect_timer(const boost::system::error_code &error)
{
boost::mutex::scoped_lock lk(m_io_mutex);
if (m_socket.is_open() == true)
{
return;
}
else
{
close_tcp_session();
}
}
void handle_tcp_once_connect(const boost::system::error_code &error)
{
//确保回调函数不会冲突
boost::mutex::scoped_lock lk(m_io_mutex);
if (error)
{
//std::cout << "Connect failed: " << error.message() << std::endl;
// Check if the connect operation failed before the deadline expired.
//如果出现错误则close
close_tcp_session();
}
else
{
//成功连接
n_socket_status = SOCKET_STATUS_NORMAL;
m_last_action_time_sec.restart();
//timer task first start
handle_protocol_timer_first_start();
//调用用户函数
handle_protocol_connected();
}
}
void handle_tcp_iterator_connect(const boost::system::error_code &error,
boost::asio::ip::tcp::resolver::iterator endpoint_iter
)
{
//确保回调函数不会冲突
boost::mutex::scoped_lock lk(m_io_mutex);
if (n_socket_status == SOCKET_STATUS_CLOSED)
{
//std::cout << "Connect timed out" << std::endl;
return;
}
if (m_socket.is_open() == false)
{
//std::cout << "Connect failed. Try next endpoint" << std::endl;
// Try the next available endpoint.
async_start_connect(++endpoint_iter);
}
else if (error)
{
//std::cout << "Connect error: " << error.message() << std::endl;
// Check if the connect operation failed before the deadline expired.
//如果出现错误则close
close_tcp_session();
}
else
{
//成功连接
n_socket_status = SOCKET_STATUS_NORMAL;
m_last_action_time_sec.restart();
handle_protocol_connected();
handle_protocol_timer_first_start();
}
}
//async_recv回调函数
void handle_tcp_read(const boost::system::error_code &error,
boost::shared_ptr<char> data_buffer, size_t bytes_transferre)
{
//确保回调函数不会冲突
boost::mutex::scoped_lock lk(m_io_mutex);
m_last_action_time_sec.restart();
if (!error)
{
handle_protocol_read(data_buffer, bytes_transferre);
}
else
{
//std::cout << error << std::endl;
//如果出现错误则close
close_tcp_session();
}
}
//async_sender回调函数
void handle_tcp_write(const boost::system::error_code &error
, boost::shared_ptr<std::string> pstr, std::size_t bytes_transferred)
{
//确保回调函数不会冲突
boost::mutex::scoped_lock lk(m_io_mutex);
m_last_action_time_sec.restart();
if (!error)
{
handle_protocol_write(pstr, bytes_transferred);
}
else
{
//如果出现错误则close
close_tcp_session();
}
}
boost::shared_ptr<char> make_safe_buffer(size_t size,
bool b_fill_zero_to_memory = true)
{
//default_delete是STL中的默认删除器
size_t safe_size = size + 1;
boost::shared_ptr<char> ret_ptr(
new char[safe_size],
std::default_delete<char[]>()
);
if (b_fill_zero_to_memory == true)
{
memset(ret_ptr.get(), 0, safe_size * sizeof(char));
}
return ret_ptr;
}
boost::asio::io_service &m_io_service;
boost::asio::ip::tcp::resolver m_resolver;
boost::asio::ip::tcp::socket m_socket;
boost::mutex m_io_mutex;
boost::mutex m_close_mutex;
int m_timeouts;
TIMER_SEC m_last_action_time_sec;
boost::asio::deadline_timer m_io_timer;
boost::asio::deadline_timer m_connect_timer;
//enum { max_length = 1024 };
//char data_[max_length];
enum
{
SOCKET_STATUS_NORMAL = 0,
SOCKET_STATUS_CLOSED = 1
};
volatile int n_socket_status;
int m_buffer_size;
};
#endif