deadline_timer_service.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // deadline_timer_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include <boost/date_time/posix_time/posix_time_types.hpp>
  20. #include <boost/asio/detail/pop_options.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/io_service.hpp>
  23. #include <boost/asio/detail/bind_handler.hpp>
  24. #include <boost/asio/detail/handler_base_from_member.hpp>
  25. #include <boost/asio/detail/noncopyable.hpp>
  26. #include <boost/asio/detail/service_base.hpp>
  27. #include <boost/asio/detail/socket_ops.hpp>
  28. #include <boost/asio/detail/socket_types.hpp>
  29. #include <boost/asio/detail/timer_queue.hpp>
  30. namespace boost {
  31. namespace asio {
  32. namespace detail {
  33. template <typename Time_Traits, typename Timer_Scheduler>
  34. class deadline_timer_service
  35. : public boost::asio::detail::service_base<
  36. deadline_timer_service<Time_Traits, Timer_Scheduler> >
  37. {
  38. public:
  39. // The time type.
  40. typedef typename Time_Traits::time_type time_type;
  41. // The duration type.
  42. typedef typename Time_Traits::duration_type duration_type;
  43. // The implementation type of the timer. This type is dependent on the
  44. // underlying implementation of the timer service.
  45. struct implementation_type
  46. : private boost::asio::detail::noncopyable
  47. {
  48. time_type expiry;
  49. bool might_have_pending_waits;
  50. };
  51. // Constructor.
  52. deadline_timer_service(boost::asio::io_service& io_service)
  53. : boost::asio::detail::service_base<
  54. deadline_timer_service<Time_Traits, Timer_Scheduler> >(io_service),
  55. scheduler_(boost::asio::use_service<Timer_Scheduler>(io_service))
  56. {
  57. scheduler_.init_task();
  58. scheduler_.add_timer_queue(timer_queue_);
  59. }
  60. // Destructor.
  61. ~deadline_timer_service()
  62. {
  63. scheduler_.remove_timer_queue(timer_queue_);
  64. }
  65. // Destroy all user-defined handler objects owned by the service.
  66. void shutdown_service()
  67. {
  68. }
  69. // Construct a new timer implementation.
  70. void construct(implementation_type& impl)
  71. {
  72. impl.expiry = time_type();
  73. impl.might_have_pending_waits = false;
  74. }
  75. // Destroy a timer implementation.
  76. void destroy(implementation_type& impl)
  77. {
  78. boost::system::error_code ec;
  79. cancel(impl, ec);
  80. }
  81. // Cancel any asynchronous wait operations associated with the timer.
  82. std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
  83. {
  84. if (!impl.might_have_pending_waits)
  85. {
  86. ec = boost::system::error_code();
  87. return 0;
  88. }
  89. std::size_t count = scheduler_.cancel_timer(timer_queue_, &impl);
  90. impl.might_have_pending_waits = false;
  91. ec = boost::system::error_code();
  92. return count;
  93. }
  94. // Get the expiry time for the timer as an absolute time.
  95. time_type expires_at(const implementation_type& impl) const
  96. {
  97. return impl.expiry;
  98. }
  99. // Set the expiry time for the timer as an absolute time.
  100. std::size_t expires_at(implementation_type& impl,
  101. const time_type& expiry_time, boost::system::error_code& ec)
  102. {
  103. std::size_t count = cancel(impl, ec);
  104. impl.expiry = expiry_time;
  105. ec = boost::system::error_code();
  106. return count;
  107. }
  108. // Get the expiry time for the timer relative to now.
  109. duration_type expires_from_now(const implementation_type& impl) const
  110. {
  111. return Time_Traits::subtract(expires_at(impl), Time_Traits::now());
  112. }
  113. // Set the expiry time for the timer relative to now.
  114. std::size_t expires_from_now(implementation_type& impl,
  115. const duration_type& expiry_time, boost::system::error_code& ec)
  116. {
  117. return expires_at(impl,
  118. Time_Traits::add(Time_Traits::now(), expiry_time), ec);
  119. }
  120. // Perform a blocking wait on the timer.
  121. void wait(implementation_type& impl, boost::system::error_code& ec)
  122. {
  123. time_type now = Time_Traits::now();
  124. while (Time_Traits::less_than(now, impl.expiry))
  125. {
  126. boost::posix_time::time_duration timeout =
  127. Time_Traits::to_posix_duration(Time_Traits::subtract(impl.expiry, now));
  128. ::timeval tv;
  129. tv.tv_sec = timeout.total_seconds();
  130. tv.tv_usec = timeout.total_microseconds() % 1000000;
  131. boost::system::error_code ec;
  132. socket_ops::select(0, 0, 0, 0, &tv, ec);
  133. now = Time_Traits::now();
  134. }
  135. ec = boost::system::error_code();
  136. }
  137. template <typename Handler>
  138. class wait_handler :
  139. public handler_base_from_member<Handler>
  140. {
  141. public:
  142. wait_handler(boost::asio::io_service& io_service, Handler handler)
  143. : handler_base_from_member<Handler>(handler),
  144. io_service_(io_service),
  145. work_(io_service)
  146. {
  147. }
  148. void operator()(const boost::system::error_code& result)
  149. {
  150. io_service_.post(detail::bind_handler(this->handler_, result));
  151. }
  152. private:
  153. boost::asio::io_service& io_service_;
  154. boost::asio::io_service::work work_;
  155. };
  156. // Start an asynchronous wait on the timer.
  157. template <typename Handler>
  158. void async_wait(implementation_type& impl, Handler handler)
  159. {
  160. impl.might_have_pending_waits = true;
  161. scheduler_.schedule_timer(timer_queue_, impl.expiry,
  162. wait_handler<Handler>(this->get_io_service(), handler), &impl);
  163. }
  164. private:
  165. // The queue of timers.
  166. timer_queue<Time_Traits> timer_queue_;
  167. // The object that schedules and executes timers. Usually a reactor.
  168. Timer_Scheduler& scheduler_;
  169. };
  170. } // namespace detail
  171. } // namespace asio
  172. } // namespace boost
  173. #include <boost/asio/detail/pop_options.hpp>
  174. #endif // BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP