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