basic_deadline_timer.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. //
  2. // basic_deadline_timer.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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 ASIO_BASIC_DEADLINE_TIMER_HPP
  11. #define ASIO_BASIC_DEADLINE_TIMER_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include "asio/detail/pop_options.hpp"
  20. #include "asio/basic_io_object.hpp"
  21. #include "asio/deadline_timer_service.hpp"
  22. #include "asio/error.hpp"
  23. #include "asio/detail/throw_error.hpp"
  24. namespace asio {
  25. /// Provides waitable timer functionality.
  26. /**
  27. * The basic_deadline_timer class template provides the ability to perform a
  28. * blocking or asynchronous wait for a timer to expire.
  29. *
  30. * A deadline timer is always in one of two states: "expired" or "not expired".
  31. * If the wait() or async_wait() function is called on an expired timer, the
  32. * wait operation will complete immediately.
  33. *
  34. * Most applications will use the asio::deadline_timer typedef.
  35. *
  36. * @par Thread Safety
  37. * @e Distinct @e objects: Safe.@n
  38. * @e Shared @e objects: Unsafe.
  39. *
  40. * @par Examples
  41. * Performing a blocking wait:
  42. * @code
  43. * // Construct a timer without setting an expiry time.
  44. * asio::deadline_timer timer(io_service);
  45. *
  46. * // Set an expiry time relative to now.
  47. * timer.expires_from_now(boost::posix_time::seconds(5));
  48. *
  49. * // Wait for the timer to expire.
  50. * timer.wait();
  51. * @endcode
  52. *
  53. * @par
  54. * Performing an asynchronous wait:
  55. * @code
  56. * void handler(const asio::error_code& error)
  57. * {
  58. * if (!error)
  59. * {
  60. * // Timer expired.
  61. * }
  62. * }
  63. *
  64. * ...
  65. *
  66. * // Construct a timer with an absolute expiry time.
  67. * asio::deadline_timer timer(io_service,
  68. * boost::posix_time::time_from_string("2005-12-07 23:59:59.000"));
  69. *
  70. * // Start an asynchronous wait.
  71. * timer.async_wait(handler);
  72. * @endcode
  73. *
  74. * @par Changing an active deadline_timer's expiry time
  75. *
  76. * Changing the expiry time of a timer while there are pending asynchronous
  77. * waits causes those wait operations to be cancelled. To ensure that the action
  78. * associated with the timer is performed only once, use something like this:
  79. * used:
  80. *
  81. * @code
  82. * void on_some_event()
  83. * {
  84. * if (my_timer.expires_from_now(seconds(5)) > 0)
  85. * {
  86. * // We managed to cancel the timer. Start new asynchronous wait.
  87. * my_timer.async_wait(on_timeout);
  88. * }
  89. * else
  90. * {
  91. * // Too late, timer has already expired!
  92. * }
  93. * }
  94. *
  95. * void on_timeout(const asio::error_code& e)
  96. * {
  97. * if (e != asio::error::operation_aborted)
  98. * {
  99. * // Timer was not cancelled, take necessary action.
  100. * }
  101. * }
  102. * @endcode
  103. *
  104. * @li The asio::basic_deadline_timer::expires_from_now() function
  105. * cancels any pending asynchronous waits, and returns the number of
  106. * asynchronous waits that were cancelled. If it returns 0 then you were too
  107. * late and the wait handler has already been executed, or will soon be
  108. * executed. If it returns 1 then the wait handler was successfully cancelled.
  109. *
  110. * @li If a wait handler is cancelled, the asio::error_code passed to
  111. * it contains the value asio::error::operation_aborted.
  112. */
  113. template <typename Time,
  114. typename TimeTraits = asio::time_traits<Time>,
  115. typename TimerService = deadline_timer_service<Time, TimeTraits> >
  116. class basic_deadline_timer
  117. : public basic_io_object<TimerService>
  118. {
  119. public:
  120. /// The time traits type.
  121. typedef TimeTraits traits_type;
  122. /// The time type.
  123. typedef typename traits_type::time_type time_type;
  124. /// The duration type.
  125. typedef typename traits_type::duration_type duration_type;
  126. /// Constructor.
  127. /**
  128. * This constructor creates a timer without setting an expiry time. The
  129. * expires_at() or expires_from_now() functions must be called to set an
  130. * expiry time before the timer can be waited on.
  131. *
  132. * @param io_service The io_service object that the timer will use to dispatch
  133. * handlers for any asynchronous operations performed on the timer.
  134. */
  135. explicit basic_deadline_timer(asio::io_service& io_service)
  136. : basic_io_object<TimerService>(io_service)
  137. {
  138. }
  139. /// Constructor to set a particular expiry time as an absolute time.
  140. /**
  141. * This constructor creates a timer and sets the expiry time.
  142. *
  143. * @param io_service The io_service object that the timer will use to dispatch
  144. * handlers for any asynchronous operations performed on the timer.
  145. *
  146. * @param expiry_time The expiry time to be used for the timer, expressed
  147. * as an absolute time.
  148. */
  149. basic_deadline_timer(asio::io_service& io_service,
  150. const time_type& expiry_time)
  151. : basic_io_object<TimerService>(io_service)
  152. {
  153. asio::error_code ec;
  154. this->service.expires_at(this->implementation, expiry_time, ec);
  155. asio::detail::throw_error(ec);
  156. }
  157. /// Constructor to set a particular expiry time relative to now.
  158. /**
  159. * This constructor creates a timer and sets the expiry time.
  160. *
  161. * @param io_service The io_service object that the timer will use to dispatch
  162. * handlers for any asynchronous operations performed on the timer.
  163. *
  164. * @param expiry_time The expiry time to be used for the timer, relative to
  165. * now.
  166. */
  167. basic_deadline_timer(asio::io_service& io_service,
  168. const duration_type& expiry_time)
  169. : basic_io_object<TimerService>(io_service)
  170. {
  171. asio::error_code ec;
  172. this->service.expires_from_now(this->implementation, expiry_time, ec);
  173. asio::detail::throw_error(ec);
  174. }
  175. /// Cancel any asynchronous operations that are waiting on the timer.
  176. /**
  177. * This function forces the completion of any pending asynchronous wait
  178. * operations against the timer. The handler for each cancelled operation will
  179. * be invoked with the asio::error::operation_aborted error code.
  180. *
  181. * Cancelling the timer does not change the expiry time.
  182. *
  183. * @return The number of asynchronous operations that were cancelled.
  184. *
  185. * @throws asio::system_error Thrown on failure.
  186. *
  187. * @note If the timer has already expired when cancel() is called, then the
  188. * handlers for asynchronous wait operations will:
  189. *
  190. * @li have already been invoked; or
  191. *
  192. * @li have been queued for invocation in the near future.
  193. *
  194. * These handlers can no longer be cancelled, and therefore are passed an
  195. * error code that indicates the successful completion of the wait operation.
  196. */
  197. std::size_t cancel()
  198. {
  199. asio::error_code ec;
  200. std::size_t s = this->service.cancel(this->implementation, ec);
  201. asio::detail::throw_error(ec);
  202. return s;
  203. }
  204. /// Cancel any asynchronous operations that are waiting on the timer.
  205. /**
  206. * This function forces the completion of any pending asynchronous wait
  207. * operations against the timer. The handler for each cancelled operation will
  208. * be invoked with the asio::error::operation_aborted error code.
  209. *
  210. * Cancelling the timer does not change the expiry time.
  211. *
  212. * @param ec Set to indicate what error occurred, if any.
  213. *
  214. * @return The number of asynchronous operations that were cancelled.
  215. *
  216. * @note If the timer has already expired when cancel() is called, then the
  217. * handlers for asynchronous wait operations will:
  218. *
  219. * @li have already been invoked; or
  220. *
  221. * @li have been queued for invocation in the near future.
  222. *
  223. * These handlers can no longer be cancelled, and therefore are passed an
  224. * error code that indicates the successful completion of the wait operation.
  225. */
  226. std::size_t cancel(asio::error_code& ec)
  227. {
  228. return this->service.cancel(this->implementation, ec);
  229. }
  230. /// Get the timer's expiry time as an absolute time.
  231. /**
  232. * This function may be used to obtain the timer's current expiry time.
  233. * Whether the timer has expired or not does not affect this value.
  234. */
  235. time_type expires_at() const
  236. {
  237. return this->service.expires_at(this->implementation);
  238. }
  239. /// Set the timer's expiry time as an absolute time.
  240. /**
  241. * This function sets the expiry time. Any pending asynchronous wait
  242. * operations will be cancelled. The handler for each cancelled operation will
  243. * be invoked with the asio::error::operation_aborted error code.
  244. *
  245. * @param expiry_time The expiry time to be used for the timer.
  246. *
  247. * @return The number of asynchronous operations that were cancelled.
  248. *
  249. * @throws asio::system_error Thrown on failure.
  250. *
  251. * @note If the timer has already expired when expires_at() is called, then
  252. * the handlers for asynchronous wait operations will:
  253. *
  254. * @li have already been invoked; or
  255. *
  256. * @li have been queued for invocation in the near future.
  257. *
  258. * These handlers can no longer be cancelled, and therefore are passed an
  259. * error code that indicates the successful completion of the wait operation.
  260. */
  261. std::size_t expires_at(const time_type& expiry_time)
  262. {
  263. asio::error_code ec;
  264. std::size_t s = this->service.expires_at(
  265. this->implementation, expiry_time, ec);
  266. asio::detail::throw_error(ec);
  267. return s;
  268. }
  269. /// Set the timer's expiry time as an absolute time.
  270. /**
  271. * This function sets the expiry time. Any pending asynchronous wait
  272. * operations will be cancelled. The handler for each cancelled operation will
  273. * be invoked with the asio::error::operation_aborted error code.
  274. *
  275. * @param expiry_time The expiry time to be used for the timer.
  276. *
  277. * @param ec Set to indicate what error occurred, if any.
  278. *
  279. * @return The number of asynchronous operations that were cancelled.
  280. *
  281. * @note If the timer has already expired when expires_at() is called, then
  282. * the handlers for asynchronous wait operations will:
  283. *
  284. * @li have already been invoked; or
  285. *
  286. * @li have been queued for invocation in the near future.
  287. *
  288. * These handlers can no longer be cancelled, and therefore are passed an
  289. * error code that indicates the successful completion of the wait operation.
  290. */
  291. std::size_t expires_at(const time_type& expiry_time,
  292. asio::error_code& ec)
  293. {
  294. return this->service.expires_at(this->implementation, expiry_time, ec);
  295. }
  296. /// Get the timer's expiry time relative to now.
  297. /**
  298. * This function may be used to obtain the timer's current expiry time.
  299. * Whether the timer has expired or not does not affect this value.
  300. */
  301. duration_type expires_from_now() const
  302. {
  303. return this->service.expires_from_now(this->implementation);
  304. }
  305. /// Set the timer's expiry time relative to now.
  306. /**
  307. * This function sets the expiry time. Any pending asynchronous wait
  308. * operations will be cancelled. The handler for each cancelled operation will
  309. * be invoked with the asio::error::operation_aborted error code.
  310. *
  311. * @param expiry_time The expiry time to be used for the timer.
  312. *
  313. * @return The number of asynchronous operations that were cancelled.
  314. *
  315. * @throws asio::system_error Thrown on failure.
  316. *
  317. * @note If the timer has already expired when expires_from_now() is called,
  318. * then the handlers for asynchronous wait operations will:
  319. *
  320. * @li have already been invoked; or
  321. *
  322. * @li have been queued for invocation in the near future.
  323. *
  324. * These handlers can no longer be cancelled, and therefore are passed an
  325. * error code that indicates the successful completion of the wait operation.
  326. */
  327. std::size_t expires_from_now(const duration_type& expiry_time)
  328. {
  329. asio::error_code ec;
  330. std::size_t s = this->service.expires_from_now(
  331. this->implementation, expiry_time, ec);
  332. asio::detail::throw_error(ec);
  333. return s;
  334. }
  335. /// Set the timer's expiry time relative to now.
  336. /**
  337. * This function sets the expiry time. Any pending asynchronous wait
  338. * operations will be cancelled. The handler for each cancelled operation will
  339. * be invoked with the asio::error::operation_aborted error code.
  340. *
  341. * @param expiry_time The expiry time to be used for the timer.
  342. *
  343. * @param ec Set to indicate what error occurred, if any.
  344. *
  345. * @return The number of asynchronous operations that were cancelled.
  346. *
  347. * @note If the timer has already expired when expires_from_now() is called,
  348. * then the handlers for asynchronous wait operations will:
  349. *
  350. * @li have already been invoked; or
  351. *
  352. * @li have been queued for invocation in the near future.
  353. *
  354. * These handlers can no longer be cancelled, and therefore are passed an
  355. * error code that indicates the successful completion of the wait operation.
  356. */
  357. std::size_t expires_from_now(const duration_type& expiry_time,
  358. asio::error_code& ec)
  359. {
  360. return this->service.expires_from_now(
  361. this->implementation, expiry_time, ec);
  362. }
  363. /// Perform a blocking wait on the timer.
  364. /**
  365. * This function is used to wait for the timer to expire. This function
  366. * blocks and does not return until the timer has expired.
  367. *
  368. * @throws asio::system_error Thrown on failure.
  369. */
  370. void wait()
  371. {
  372. asio::error_code ec;
  373. this->service.wait(this->implementation, ec);
  374. asio::detail::throw_error(ec);
  375. }
  376. /// Perform a blocking wait on the timer.
  377. /**
  378. * This function is used to wait for the timer to expire. This function
  379. * blocks and does not return until the timer has expired.
  380. *
  381. * @param ec Set to indicate what error occurred, if any.
  382. */
  383. void wait(asio::error_code& ec)
  384. {
  385. this->service.wait(this->implementation, ec);
  386. }
  387. /// Start an asynchronous wait on the timer.
  388. /**
  389. * This function may be used to initiate an asynchronous wait against the
  390. * timer. It always returns immediately.
  391. *
  392. * For each call to async_wait(), the supplied handler will be called exactly
  393. * once. The handler will be called when:
  394. *
  395. * @li The timer has expired.
  396. *
  397. * @li The timer was cancelled, in which case the handler is passed the error
  398. * code asio::error::operation_aborted.
  399. *
  400. * @param handler The handler to be called when the timer expires. Copies
  401. * will be made of the handler as required. The function signature of the
  402. * handler must be:
  403. * @code void handler(
  404. * const asio::error_code& error // Result of operation.
  405. * ); @endcode
  406. * Regardless of whether the asynchronous operation completes immediately or
  407. * not, the handler will not be invoked from within this function. Invocation
  408. * of the handler will be performed in a manner equivalent to using
  409. * asio::io_service::post().
  410. */
  411. template <typename WaitHandler>
  412. void async_wait(WaitHandler handler)
  413. {
  414. this->service.async_wait(this->implementation, handler);
  415. }
  416. };
  417. } // namespace asio
  418. #include "asio/detail/pop_options.hpp"
  419. #endif // ASIO_BASIC_DEADLINE_TIMER_HPP