-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_repeating_timer.h
211 lines (182 loc) · 7.05 KB
/
basic_repeating_timer.h
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
#ifndef BOOST_ASIO_REPEATING_TIMER_H_INCLUDED
#define BOOST_ASIO_REPEATING_TIMER_H_INCLUDED
// Developed 2007 by David C. Wyles (http:///www.codegorilla.co.uk)
// released for public consumption under the same
// licensing policy as the boost library http://www.boost.org/LICENSE_1_0.txt
//
// most people will use the repeating_timer typedef for working with posix time
//
// Is is based on the basic_deadline_timer
//
#include <boost/bind.hpp>
#include <boost/noncopyable.hpp>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/weak_ptr.hpp>
namespace boost
{
namespace asio
{
// basic repeating timer with start stop semantics.
template <typename Time,
typename TimeTraits = boost::asio::time_traits<Time>,
typename TimerService = boost::asio::deadline_timer_service<Time, TimeTraits> >
class basic_repeating_timer : public boost::asio::basic_io_object<TimerService>
{
public:
typedef boost::asio::basic_deadline_timer<Time> timer_type;
explicit basic_repeating_timer( boost::asio::io_service& io_service )
: boost::asio::basic_io_object<TimerService>(io_service)
{
}
~basic_repeating_timer()
{
stop();
}
template <typename WaitHandler>
void start( typename timer_type::duration_type const & repeat_interval, WaitHandler handler )
{
boost::recursive_mutex::scoped_lock guard(lock_);
{
// cleanup code, cancel any existing timer
handler_.reset();
if( timer_ )
{
timer_->cancel();
}
timer_.reset();
}
// create new handler.
handler_.reset( new handler_impl<WaitHandler>( handler ) );
// create new timer
timer_ = internal_timer::create( this->get_io_service(), repeat_interval, handler_ );
}
void stop()
{
boost::recursive_mutex::scoped_lock guard(lock_);
{
// cleanup code.
handler_.reset();
if( timer_ )
{
timer_->cancel();
}
timer_.reset();
}
}
void cancel() {stop();}
// changes the interval the next time the timer is fired.
void change_interval( typename timer_type::duration_type const & repeat_interval )
{
boost::recursive_mutex::scoped_lock guard(lock_);
if( timer_ )
{
timer_->change_interval( repeat_interval );
}
}
private:
boost::recursive_mutex lock_;
class internal_timer;
typedef boost::shared_ptr<internal_timer> internal_timer_ptr;
internal_timer_ptr timer_;
class handler_base;
boost::shared_ptr<handler_base> handler_;
class handler_base
{
public:
virtual ~handler_base() {}
virtual void handler( boost::system::error_code const & ) = 0;
};
template <typename HandlerFunc>
class handler_impl : public handler_base
{
public:
handler_impl( HandlerFunc func ) : handler_func_(func) {}
virtual void handler( boost::system::error_code const & result )
{
handler_func_(result);
}
HandlerFunc handler_func_;
};
class internal_timer : public boost::enable_shared_from_this<internal_timer>
{
public:
static internal_timer_ptr create( boost::asio::io_service& io_service,
typename timer_type::duration_type const & repeat_interval,
boost::shared_ptr<handler_base> const & handler )
{
internal_timer_ptr timer( new internal_timer( io_service ) );
timer->start( repeat_interval, handler );
return timer;
}
void cancel()
{
boost::recursive_mutex::scoped_lock guard( lock_ );
timer_.cancel();
}
void change_interval( typename timer_type::duration_type const & repeat_interval )
{
boost::recursive_mutex::scoped_lock guard( lock_ );
interval_ = repeat_interval;
}
private:
timer_type timer_;
boost::weak_ptr<handler_base> handler_;
typename timer_type::duration_type interval_;
boost::recursive_mutex lock_;
internal_timer( boost::asio::io_service& io_service )
:timer_(io_service)
{
}
void start( typename timer_type::duration_type const & repeat_interval,
boost::shared_ptr<handler_base> const & handler )
{
// only EVER called once, via create
interval_ = repeat_interval;
handler_ = handler;
timer_.expires_from_now( interval_ );
timer_.async_wait( boost::bind( &internal_timer::handle_timeout, this->shared_from_this(), boost::asio::placeholders::error ) );
}
void handle_timeout(boost::system::error_code const& error)
{
// we lock in the timeout to block the cancel operation until this timeout completes
boost::recursive_mutex::scoped_lock guard( lock_ );
{
// do the fire.
boost::shared_ptr<handler_base> Handler = handler_.lock();
if( Handler )
{
try
{
Handler->handler(error);
}
catch( std::exception const & e )
{
// consume for now, no much else we can do, we don't want to damage the
// io_service thread
(void)e;
}
}
}
if( !error )
{
// check if we need to reschedule.
boost::shared_ptr<handler_base> Handler = handler_.lock();
if( Handler )
{
timer_.expires_from_now( interval_ );
//m_Timer.expires_at( m_Timer.expires_at() + m_Repeat_Interval );
timer_.async_wait( boost::bind( &internal_timer::handle_timeout, this->shared_from_this(), boost::asio::placeholders::error ) );
}
}
}
};
};
typedef basic_repeating_timer<boost::posix_time::ptime> repeating_timer;
}
}
#endif