basic_deadline_timer.hpp 15 KB

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