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