basic_deadline_timer.hpp 18 KB

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