deadline_timer_service.hpp 5.1 KB

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