-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy path_test_boost_asio_timer.cpp
130 lines (109 loc) · 2.92 KB
/
_test_boost_asio_timer.cpp
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
/**----------------------------------------------------------------------------
* _test_boost_asio_timer.cpp
*-----------------------------------------------------------------------------
*
*-----------------------------------------------------------------------------
* All rights reserved by Noh,Yonghwan ([email protected], [email protected])
*-----------------------------------------------------------------------------
* 2014:12:27 17:44 created
**---------------------------------------------------------------------------*/
#include "stdafx.h"
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
void asio_on_timer(const boost::system::error_code& error);
bool test_boost_asio_timer_1();
bool test_boost_asio_timer_2();
void asio_on_timer_3(const boost::system::error_code& error, boost::asio::steady_timer& timer);
void set_timer(boost::asio::steady_timer& timer);
bool test_boost_asio_timer_3();
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool test_boost_asio_timer()
{
test_boost_asio_timer_1();
test_boost_asio_timer_3();
return true;
}
/**
* @brief
**/
void asio_on_timer( const boost::system::error_code& error )
{
if (!error) // true if no error
{
con_info "1st timer called." con_end
}
else
{
con_err "error code = %d, msg = %s", error.value(), error.message().c_str() con_end
}
}
/**
* @brief version 1 - 콜백 함수 이용
**/
bool test_boost_asio_timer_1()
{
boost::asio::io_service io_service;
boost::asio::steady_timer timer(io_service);
timer.expires_from_now( boost::chrono::seconds(1) );
timer.async_wait( asio_on_timer );
io_service.run();
return true;
}
/**
* @brief v2 - lambda 를 이용한 타이머
**/
bool test_boost_asio_timer_2()
{
boost::asio::io_service io_service;
boost::asio::steady_timer timer(io_service);
timer.expires_from_now( boost::chrono::seconds(1) );
timer.async_wait(
[](const boost::system::error_code& error)
{
if (!error) // true if no error
{
con_info "%s, timer expired. ", time(NULL) con_end
}
else
{
con_err "error code = %d, msg = %s", error.value(), error.message().c_str() con_end
}
});
io_service.run();
return true;
}
/**
* @brief v3 - lamda + 타이머이벤트 계속 발생하기, 취소
**/
void print(const boost::system::error_code& e, boost::asio::steady_timer& timer, int& count)
{
if (count < 5)
{
std::cout << count << "\n";
++(count);
timer.expires_from_now(boost::chrono::seconds(1));
timer.async_wait(boost::bind(print, boost::asio::placeholders::error, boost::ref(timer), boost::ref(count)));
}
}
bool test_boost_asio_timer_3()
{
boost::asio::io_service io_service;
int count = 0;
boost::asio::steady_timer timer(io_service);
timer.async_wait(boost::bind(
print,
boost::asio::placeholders::error,
boost::ref(timer),
boost::ref(count)));
io_service.run();
std::cout << "count = " << count << std::endl;
return true;
}