1 #ifndef BOOST_ASIO_REPEATING_TIMER_H_INCLUDED 2 #define BOOST_ASIO_REPEATING_TIMER_H_INCLUDED 14 #include <boost/bind.hpp> 15 #include <boost/noncopyable.hpp> 16 #include <boost/asio.hpp> 17 #include <boost/date_time/posix_time/posix_time.hpp> 18 #include <boost/thread/recursive_mutex.hpp> 19 #include <boost/thread/condition.hpp> 20 #include <boost/shared_ptr.hpp> 21 #include <boost/enable_shared_from_this.hpp> 22 #include <boost/weak_ptr.hpp> 32 template <
typename Time,
33 typename TimeTraits = boost::asio::time_traits<Time>,
34 typename TimerService = boost::asio::detail::deadline_timer_service<TimeTraits> >
38 typedef boost::asio::basic_deadline_timer<Time>
timer_type;
42 :
boost::asio::basic_io_object<TimerService>(io_service)
53 template <
typename WaitHandler>
54 void start(
typename timer_type::duration_type
const & repeat_interval, WaitHandler handler )
56 boost::recursive_mutex::scoped_lock guard(lock_);
69 handler_.reset(
new handler_impl<WaitHandler>( handler ) );
71 timer_ = internal_timer::create( this->get_io_service(), repeat_interval, handler_ );
76 boost::recursive_mutex::scoped_lock guard(lock_);
93 void change_interval(
typename timer_type::duration_type
const & repeat_interval )
95 boost::recursive_mutex::scoped_lock guard(lock_);
98 timer_->change_interval( repeat_interval );
106 boost::recursive_mutex lock_;
107 class internal_timer;
108 typedef boost::shared_ptr<internal_timer> internal_timer_ptr;
109 internal_timer_ptr timer_;
112 boost::shared_ptr<handler_base> handler_;
119 virtual ~handler_base() {}
120 virtual void handler( boost::system::error_code
const & ) = 0;
124 template <
typename HandlerFunc>
125 class handler_impl :
public handler_base
128 handler_impl( HandlerFunc func ) : handler_func_(func) {}
129 virtual void handler( boost::system::error_code
const & result )
131 handler_func_(result);
133 HandlerFunc handler_func_;
137 class internal_timer :
public boost::enable_shared_from_this<internal_timer>
140 static internal_timer_ptr create( boost::asio::io_service& io_service,
141 typename timer_type::duration_type
const & repeat_interval,
142 boost::shared_ptr<handler_base>
const & handler )
144 internal_timer_ptr timer(
new internal_timer( io_service ) );
145 timer->start( repeat_interval, handler );
152 boost::recursive_mutex::scoped_lock guard( lock_ );
157 void change_interval(
typename timer_type::duration_type
const & repeat_interval )
159 boost::recursive_mutex::scoped_lock guard( lock_ );
160 interval_ = repeat_interval;
166 boost::weak_ptr<handler_base> handler_;
167 typename timer_type::duration_type interval_;
168 boost::recursive_mutex lock_;
171 internal_timer( boost::asio::io_service& io_service )
177 void start(
typename timer_type::duration_type
const & repeat_interval,
178 boost::shared_ptr<handler_base>
const & handler )
181 interval_ = repeat_interval;
185 timer_.expires_from_now( interval_ );
186 timer_.async_wait( boost::bind( &internal_timer::handle_timeout, this->shared_from_this(), boost::asio::placeholders::error ) );
190 void handle_timeout(boost::system::error_code
const& error)
193 boost::recursive_mutex::scoped_lock guard( lock_ );
196 boost::shared_ptr<handler_base> Handler = handler_.lock();
201 Handler->handler(error);
203 catch( std::exception
const & e )
215 boost::shared_ptr<handler_base> Handler = handler_.lock();
218 timer_.expires_from_now( interval_ );
220 timer_.async_wait( boost::bind( &internal_timer::handle_timeout, this->shared_from_this(), boost::asio::placeholders::error ) );
Definition: basic_repeating_timer.h:25
void change_interval(typename timer_type::duration_type const &repeat_interval)
Definition: basic_repeating_timer.h:93
~basic_repeating_timer()
Definition: basic_repeating_timer.h:47
void cancel()
Definition: basic_repeating_timer.h:89
basic_repeating_timer(boost::asio::io_service &io_service)
Definition: basic_repeating_timer.h:41
boost::asio::basic_deadline_timer< Time > timer_type
Definition: basic_repeating_timer.h:38
void start(typename timer_type::duration_type const &repeat_interval, WaitHandler handler)
Definition: basic_repeating_timer.h:54
basic_repeating_timer< boost::posix_time::ptime > repeating_timer
Definition: basic_repeating_timer.h:228
Definition: basic_repeating_timer.h:35
void stop()
Definition: basic_repeating_timer.h:74