deadline_timer_service.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // deadline_timer_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_DEADLINE_TIMER_SERVICE_HPP
  11. #define BOOST_ASIO_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/config.hpp>
  16. #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  17. #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <cstddef>
  20. #include <boost/asio/async_result.hpp>
  21. #include <boost/asio/detail/deadline_timer_service.hpp>
  22. #include <boost/asio/io_context.hpp>
  23. #include <boost/asio/time_traits.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. /// Default service implementation for a timer.
  28. template <typename TimeType,
  29. typename TimeTraits = boost::asio::time_traits<TimeType> >
  30. class deadline_timer_service
  31. #if defined(GENERATING_DOCUMENTATION)
  32. : public boost::asio::io_context::service
  33. #else
  34. : public boost::asio::detail::service_base<
  35. deadline_timer_service<TimeType, TimeTraits> >
  36. #endif
  37. {
  38. public:
  39. #if defined(GENERATING_DOCUMENTATION)
  40. /// The unique service identifier.
  41. static boost::asio::io_context::id id;
  42. #endif
  43. /// The time traits type.
  44. typedef TimeTraits traits_type;
  45. /// The time type.
  46. typedef typename traits_type::time_type time_type;
  47. /// The duration type.
  48. typedef typename traits_type::duration_type duration_type;
  49. private:
  50. // The type of the platform-specific implementation.
  51. typedef detail::deadline_timer_service<traits_type> service_impl_type;
  52. public:
  53. /// The implementation type of the deadline timer.
  54. #if defined(GENERATING_DOCUMENTATION)
  55. typedef implementation_defined implementation_type;
  56. #else
  57. typedef typename service_impl_type::implementation_type implementation_type;
  58. #endif
  59. /// Construct a new timer service for the specified io_context.
  60. explicit deadline_timer_service(boost::asio::io_context& io_context)
  61. : boost::asio::detail::service_base<
  62. deadline_timer_service<TimeType, TimeTraits> >(io_context),
  63. service_impl_(io_context)
  64. {
  65. }
  66. /// Construct a new timer implementation.
  67. void construct(implementation_type& impl)
  68. {
  69. service_impl_.construct(impl);
  70. }
  71. /// Destroy a timer implementation.
  72. void destroy(implementation_type& impl)
  73. {
  74. service_impl_.destroy(impl);
  75. }
  76. /// Cancel any asynchronous wait operations associated with the timer.
  77. std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
  78. {
  79. return service_impl_.cancel(impl, ec);
  80. }
  81. /// Cancels one asynchronous wait operation associated with the timer.
  82. std::size_t cancel_one(implementation_type& impl,
  83. boost::system::error_code& ec)
  84. {
  85. return service_impl_.cancel_one(impl, ec);
  86. }
  87. /// Get the expiry time for the timer as an absolute time.
  88. time_type expires_at(const implementation_type& impl) const
  89. {
  90. return service_impl_.expiry(impl);
  91. }
  92. /// Set the expiry time for the timer as an absolute time.
  93. std::size_t expires_at(implementation_type& impl,
  94. const time_type& expiry_time, boost::system::error_code& ec)
  95. {
  96. return service_impl_.expires_at(impl, expiry_time, ec);
  97. }
  98. /// Get the expiry time for the timer relative to now.
  99. duration_type expires_from_now(const implementation_type& impl) const
  100. {
  101. return TimeTraits::subtract(service_impl_.expiry(impl), TimeTraits::now());
  102. }
  103. /// Set the expiry time for the timer relative to now.
  104. std::size_t expires_from_now(implementation_type& impl,
  105. const duration_type& expiry_time, boost::system::error_code& ec)
  106. {
  107. return service_impl_.expires_after(impl, expiry_time, ec);
  108. }
  109. // Perform a blocking wait on the timer.
  110. void wait(implementation_type& impl, boost::system::error_code& ec)
  111. {
  112. service_impl_.wait(impl, ec);
  113. }
  114. // Start an asynchronous wait on the timer.
  115. template <typename WaitHandler>
  116. BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
  117. void (boost::system::error_code))
  118. async_wait(implementation_type& impl,
  119. BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
  120. {
  121. async_completion<WaitHandler,
  122. void (boost::system::error_code)> init(handler);
  123. service_impl_.async_wait(impl, init.completion_handler);
  124. return init.result.get();
  125. }
  126. private:
  127. // Destroy all user-defined handler objects owned by the service.
  128. void shutdown()
  129. {
  130. service_impl_.shutdown();
  131. }
  132. // The platform-specific implementation.
  133. service_impl_type service_impl_;
  134. };
  135. } // namespace asio
  136. } // namespace boost
  137. #include <boost/asio/detail/pop_options.hpp>
  138. #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  139. // || defined(GENERATING_DOCUMENTATION)
  140. #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  141. #endif // BOOST_ASIO_DEADLINE_TIMER_SERVICE_HPP