basic_descriptor.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. //
  2. // posix/basic_descriptor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_POSIX_BASIC_DESCRIPTOR_HPP
  11. #define BOOST_ASIO_POSIX_BASIC_DESCRIPTOR_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_POSIX_STREAM_DESCRIPTOR) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #include <boost/asio/async_result.hpp>
  19. #include <boost/asio/detail/handler_type_requirements.hpp>
  20. #include <boost/asio/detail/io_object_impl.hpp>
  21. #include <boost/asio/detail/non_const_lvalue.hpp>
  22. #include <boost/asio/detail/reactive_descriptor_service.hpp>
  23. #include <boost/asio/detail/throw_error.hpp>
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/execution_context.hpp>
  26. #include <boost/asio/executor.hpp>
  27. #include <boost/asio/posix/descriptor_base.hpp>
  28. #if defined(BOOST_ASIO_HAS_MOVE)
  29. # include <utility>
  30. #endif // defined(BOOST_ASIO_HAS_MOVE)
  31. #include <boost/asio/detail/push_options.hpp>
  32. namespace boost {
  33. namespace asio {
  34. namespace posix {
  35. /// Provides POSIX descriptor functionality.
  36. /**
  37. * The posix::basic_descriptor class template provides the ability to wrap a
  38. * POSIX descriptor.
  39. *
  40. * @par Thread Safety
  41. * @e Distinct @e objects: Safe.@n
  42. * @e Shared @e objects: Unsafe.
  43. */
  44. template <typename Executor = executor>
  45. class basic_descriptor
  46. : public descriptor_base
  47. {
  48. public:
  49. /// The type of the executor associated with the object.
  50. typedef Executor executor_type;
  51. /// Rebinds the descriptor type to another executor.
  52. template <typename Executor1>
  53. struct rebind_executor
  54. {
  55. /// The descriptor type when rebound to the specified executor.
  56. typedef basic_descriptor<Executor1> other;
  57. };
  58. /// The native representation of a descriptor.
  59. #if defined(GENERATING_DOCUMENTATION)
  60. typedef implementation_defined native_handle_type;
  61. #else
  62. typedef detail::reactive_descriptor_service::native_handle_type
  63. native_handle_type;
  64. #endif
  65. /// A descriptor is always the lowest layer.
  66. typedef basic_descriptor lowest_layer_type;
  67. /// Construct a descriptor without opening it.
  68. /**
  69. * This constructor creates a descriptor without opening it.
  70. *
  71. * @param ex The I/O executor that the descriptor will use, by default, to
  72. * dispatch handlers for any asynchronous operations performed on the
  73. * descriptor.
  74. */
  75. explicit basic_descriptor(const executor_type& ex)
  76. : impl_(ex)
  77. {
  78. }
  79. /// Construct a descriptor without opening it.
  80. /**
  81. * This constructor creates a descriptor without opening it.
  82. *
  83. * @param context An execution context which provides the I/O executor that
  84. * the descriptor will use, by default, to dispatch handlers for any
  85. * asynchronous operations performed on the descriptor.
  86. */
  87. template <typename ExecutionContext>
  88. explicit basic_descriptor(ExecutionContext& context,
  89. typename enable_if<
  90. is_convertible<ExecutionContext&, execution_context&>::value
  91. >::type* = 0)
  92. : impl_(context)
  93. {
  94. }
  95. /// Construct a descriptor on an existing native descriptor.
  96. /**
  97. * This constructor creates a descriptor object to hold an existing native
  98. * descriptor.
  99. *
  100. * @param ex The I/O executor that the descriptor will use, by default, to
  101. * dispatch handlers for any asynchronous operations performed on the
  102. * descriptor.
  103. *
  104. * @param native_descriptor A native descriptor.
  105. *
  106. * @throws boost::system::system_error Thrown on failure.
  107. */
  108. basic_descriptor(const executor_type& ex,
  109. const native_handle_type& native_descriptor)
  110. : impl_(ex)
  111. {
  112. boost::system::error_code ec;
  113. impl_.get_service().assign(impl_.get_implementation(),
  114. native_descriptor, ec);
  115. boost::asio::detail::throw_error(ec, "assign");
  116. }
  117. /// Construct a descriptor on an existing native descriptor.
  118. /**
  119. * This constructor creates a descriptor object to hold an existing native
  120. * descriptor.
  121. *
  122. * @param context An execution context which provides the I/O executor that
  123. * the descriptor will use, by default, to dispatch handlers for any
  124. * asynchronous operations performed on the descriptor.
  125. *
  126. * @param native_descriptor A native descriptor.
  127. *
  128. * @throws boost::system::system_error Thrown on failure.
  129. */
  130. template <typename ExecutionContext>
  131. basic_descriptor(ExecutionContext& context,
  132. const native_handle_type& native_descriptor,
  133. typename enable_if<
  134. is_convertible<ExecutionContext&, execution_context&>::value
  135. >::type* = 0)
  136. : impl_(context)
  137. {
  138. boost::system::error_code ec;
  139. impl_.get_service().assign(impl_.get_implementation(),
  140. native_descriptor, ec);
  141. boost::asio::detail::throw_error(ec, "assign");
  142. }
  143. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  144. /// Move-construct a descriptor from another.
  145. /**
  146. * This constructor moves a descriptor from one object to another.
  147. *
  148. * @param other The other descriptor object from which the move will
  149. * occur.
  150. *
  151. * @note Following the move, the moved-from object is in the same state as if
  152. * constructed using the @c basic_descriptor(const executor_type&)
  153. * constructor.
  154. */
  155. basic_descriptor(basic_descriptor&& other)
  156. : impl_(std::move(other.impl_))
  157. {
  158. }
  159. /// Move-assign a descriptor from another.
  160. /**
  161. * This assignment operator moves a descriptor from one object to another.
  162. *
  163. * @param other The other descriptor object from which the move will
  164. * occur.
  165. *
  166. * @note Following the move, the moved-from object is in the same state as if
  167. * constructed using the @c basic_descriptor(const executor_type&)
  168. * constructor.
  169. */
  170. basic_descriptor& operator=(basic_descriptor&& other)
  171. {
  172. impl_ = std::move(other.impl_);
  173. return *this;
  174. }
  175. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  176. /// Get the executor associated with the object.
  177. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  178. {
  179. return impl_.get_executor();
  180. }
  181. /// Get a reference to the lowest layer.
  182. /**
  183. * This function returns a reference to the lowest layer in a stack of
  184. * layers. Since a descriptor cannot contain any further layers, it
  185. * simply returns a reference to itself.
  186. *
  187. * @return A reference to the lowest layer in the stack of layers. Ownership
  188. * is not transferred to the caller.
  189. */
  190. lowest_layer_type& lowest_layer()
  191. {
  192. return *this;
  193. }
  194. /// Get a const reference to the lowest layer.
  195. /**
  196. * This function returns a const reference to the lowest layer in a stack of
  197. * layers. Since a descriptor cannot contain any further layers, it
  198. * simply returns a reference to itself.
  199. *
  200. * @return A const reference to the lowest layer in the stack of layers.
  201. * Ownership is not transferred to the caller.
  202. */
  203. const lowest_layer_type& lowest_layer() const
  204. {
  205. return *this;
  206. }
  207. /// Assign an existing native descriptor to the descriptor.
  208. /*
  209. * This function opens the descriptor to hold an existing native descriptor.
  210. *
  211. * @param native_descriptor A native descriptor.
  212. *
  213. * @throws boost::system::system_error Thrown on failure.
  214. */
  215. void assign(const native_handle_type& native_descriptor)
  216. {
  217. boost::system::error_code ec;
  218. impl_.get_service().assign(impl_.get_implementation(),
  219. native_descriptor, ec);
  220. boost::asio::detail::throw_error(ec, "assign");
  221. }
  222. /// Assign an existing native descriptor to the descriptor.
  223. /*
  224. * This function opens the descriptor to hold an existing native descriptor.
  225. *
  226. * @param native_descriptor A native descriptor.
  227. *
  228. * @param ec Set to indicate what error occurred, if any.
  229. */
  230. BOOST_ASIO_SYNC_OP_VOID assign(const native_handle_type& native_descriptor,
  231. boost::system::error_code& ec)
  232. {
  233. impl_.get_service().assign(
  234. impl_.get_implementation(), native_descriptor, ec);
  235. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  236. }
  237. /// Determine whether the descriptor is open.
  238. bool is_open() const
  239. {
  240. return impl_.get_service().is_open(impl_.get_implementation());
  241. }
  242. /// Close the descriptor.
  243. /**
  244. * This function is used to close the descriptor. Any asynchronous read or
  245. * write operations will be cancelled immediately, and will complete with the
  246. * boost::asio::error::operation_aborted error.
  247. *
  248. * @throws boost::system::system_error Thrown on failure. Note that, even if
  249. * the function indicates an error, the underlying descriptor is closed.
  250. */
  251. void close()
  252. {
  253. boost::system::error_code ec;
  254. impl_.get_service().close(impl_.get_implementation(), ec);
  255. boost::asio::detail::throw_error(ec, "close");
  256. }
  257. /// Close the descriptor.
  258. /**
  259. * This function is used to close the descriptor. Any asynchronous read or
  260. * write operations will be cancelled immediately, and will complete with the
  261. * boost::asio::error::operation_aborted error.
  262. *
  263. * @param ec Set to indicate what error occurred, if any. Note that, even if
  264. * the function indicates an error, the underlying descriptor is closed.
  265. */
  266. BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
  267. {
  268. impl_.get_service().close(impl_.get_implementation(), ec);
  269. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  270. }
  271. /// Get the native descriptor representation.
  272. /**
  273. * This function may be used to obtain the underlying representation of the
  274. * descriptor. This is intended to allow access to native descriptor
  275. * functionality that is not otherwise provided.
  276. */
  277. native_handle_type native_handle()
  278. {
  279. return impl_.get_service().native_handle(impl_.get_implementation());
  280. }
  281. /// Release ownership of the native descriptor implementation.
  282. /**
  283. * This function may be used to obtain the underlying representation of the
  284. * descriptor. After calling this function, @c is_open() returns false. The
  285. * caller is responsible for closing the descriptor.
  286. *
  287. * All outstanding asynchronous read or write operations will finish
  288. * immediately, and the handlers for cancelled operations will be passed the
  289. * boost::asio::error::operation_aborted error.
  290. */
  291. native_handle_type release()
  292. {
  293. return impl_.get_service().release(impl_.get_implementation());
  294. }
  295. /// Cancel all asynchronous operations associated with the descriptor.
  296. /**
  297. * This function causes all outstanding asynchronous read or write operations
  298. * to finish immediately, and the handlers for cancelled operations will be
  299. * passed the boost::asio::error::operation_aborted error.
  300. *
  301. * @throws boost::system::system_error Thrown on failure.
  302. */
  303. void cancel()
  304. {
  305. boost::system::error_code ec;
  306. impl_.get_service().cancel(impl_.get_implementation(), ec);
  307. boost::asio::detail::throw_error(ec, "cancel");
  308. }
  309. /// Cancel all asynchronous operations associated with the descriptor.
  310. /**
  311. * This function causes all outstanding asynchronous read or write operations
  312. * to finish immediately, and the handlers for cancelled operations will be
  313. * passed the boost::asio::error::operation_aborted error.
  314. *
  315. * @param ec Set to indicate what error occurred, if any.
  316. */
  317. BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
  318. {
  319. impl_.get_service().cancel(impl_.get_implementation(), ec);
  320. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  321. }
  322. /// Perform an IO control command on the descriptor.
  323. /**
  324. * This function is used to execute an IO control command on the descriptor.
  325. *
  326. * @param command The IO control command to be performed on the descriptor.
  327. *
  328. * @throws boost::system::system_error Thrown on failure.
  329. *
  330. * @sa IoControlCommand @n
  331. * boost::asio::posix::descriptor_base::bytes_readable @n
  332. * boost::asio::posix::descriptor_base::non_blocking_io
  333. *
  334. * @par Example
  335. * Getting the number of bytes ready to read:
  336. * @code
  337. * boost::asio::posix::stream_descriptor descriptor(my_context);
  338. * ...
  339. * boost::asio::posix::stream_descriptor::bytes_readable command;
  340. * descriptor.io_control(command);
  341. * std::size_t bytes_readable = command.get();
  342. * @endcode
  343. */
  344. template <typename IoControlCommand>
  345. void io_control(IoControlCommand& command)
  346. {
  347. boost::system::error_code ec;
  348. impl_.get_service().io_control(impl_.get_implementation(), command, ec);
  349. boost::asio::detail::throw_error(ec, "io_control");
  350. }
  351. /// Perform an IO control command on the descriptor.
  352. /**
  353. * This function is used to execute an IO control command on the descriptor.
  354. *
  355. * @param command The IO control command to be performed on the descriptor.
  356. *
  357. * @param ec Set to indicate what error occurred, if any.
  358. *
  359. * @sa IoControlCommand @n
  360. * boost::asio::posix::descriptor_base::bytes_readable @n
  361. * boost::asio::posix::descriptor_base::non_blocking_io
  362. *
  363. * @par Example
  364. * Getting the number of bytes ready to read:
  365. * @code
  366. * boost::asio::posix::stream_descriptor descriptor(my_context);
  367. * ...
  368. * boost::asio::posix::stream_descriptor::bytes_readable command;
  369. * boost::system::error_code ec;
  370. * descriptor.io_control(command, ec);
  371. * if (ec)
  372. * {
  373. * // An error occurred.
  374. * }
  375. * std::size_t bytes_readable = command.get();
  376. * @endcode
  377. */
  378. template <typename IoControlCommand>
  379. BOOST_ASIO_SYNC_OP_VOID io_control(IoControlCommand& command,
  380. boost::system::error_code& ec)
  381. {
  382. impl_.get_service().io_control(impl_.get_implementation(), command, ec);
  383. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  384. }
  385. /// Gets the non-blocking mode of the descriptor.
  386. /**
  387. * @returns @c true if the descriptor's synchronous operations will fail with
  388. * boost::asio::error::would_block if they are unable to perform the requested
  389. * operation immediately. If @c false, synchronous operations will block
  390. * until complete.
  391. *
  392. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  393. * operations. Asynchronous operations will never fail with the error
  394. * boost::asio::error::would_block.
  395. */
  396. bool non_blocking() const
  397. {
  398. return impl_.get_service().non_blocking(impl_.get_implementation());
  399. }
  400. /// Sets the non-blocking mode of the descriptor.
  401. /**
  402. * @param mode If @c true, the descriptor's synchronous operations will fail
  403. * with boost::asio::error::would_block if they are unable to perform the
  404. * requested operation immediately. If @c false, synchronous operations will
  405. * block until complete.
  406. *
  407. * @throws boost::system::system_error Thrown on failure.
  408. *
  409. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  410. * operations. Asynchronous operations will never fail with the error
  411. * boost::asio::error::would_block.
  412. */
  413. void non_blocking(bool mode)
  414. {
  415. boost::system::error_code ec;
  416. impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
  417. boost::asio::detail::throw_error(ec, "non_blocking");
  418. }
  419. /// Sets the non-blocking mode of the descriptor.
  420. /**
  421. * @param mode If @c true, the descriptor's synchronous operations will fail
  422. * with boost::asio::error::would_block if they are unable to perform the
  423. * requested operation immediately. If @c false, synchronous operations will
  424. * block until complete.
  425. *
  426. * @param ec Set to indicate what error occurred, if any.
  427. *
  428. * @note The non-blocking mode has no effect on the behaviour of asynchronous
  429. * operations. Asynchronous operations will never fail with the error
  430. * boost::asio::error::would_block.
  431. */
  432. BOOST_ASIO_SYNC_OP_VOID non_blocking(
  433. bool mode, boost::system::error_code& ec)
  434. {
  435. impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
  436. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  437. }
  438. /// Gets the non-blocking mode of the native descriptor implementation.
  439. /**
  440. * This function is used to retrieve the non-blocking mode of the underlying
  441. * native descriptor. This mode has no effect on the behaviour of the
  442. * descriptor object's synchronous operations.
  443. *
  444. * @returns @c true if the underlying descriptor is in non-blocking mode and
  445. * direct system calls may fail with boost::asio::error::would_block (or the
  446. * equivalent system error).
  447. *
  448. * @note The current non-blocking mode is cached by the descriptor object.
  449. * Consequently, the return value may be incorrect if the non-blocking mode
  450. * was set directly on the native descriptor.
  451. */
  452. bool native_non_blocking() const
  453. {
  454. return impl_.get_service().native_non_blocking(
  455. impl_.get_implementation());
  456. }
  457. /// Sets the non-blocking mode of the native descriptor implementation.
  458. /**
  459. * This function is used to modify the non-blocking mode of the underlying
  460. * native descriptor. It has no effect on the behaviour of the descriptor
  461. * object's synchronous operations.
  462. *
  463. * @param mode If @c true, the underlying descriptor is put into non-blocking
  464. * mode and direct system calls may fail with boost::asio::error::would_block
  465. * (or the equivalent system error).
  466. *
  467. * @throws boost::system::system_error Thrown on failure. If the @c mode is
  468. * @c false, but the current value of @c non_blocking() is @c true, this
  469. * function fails with boost::asio::error::invalid_argument, as the
  470. * combination does not make sense.
  471. */
  472. void native_non_blocking(bool mode)
  473. {
  474. boost::system::error_code ec;
  475. impl_.get_service().native_non_blocking(
  476. impl_.get_implementation(), mode, ec);
  477. boost::asio::detail::throw_error(ec, "native_non_blocking");
  478. }
  479. /// Sets the non-blocking mode of the native descriptor implementation.
  480. /**
  481. * This function is used to modify the non-blocking mode of the underlying
  482. * native descriptor. It has no effect on the behaviour of the descriptor
  483. * object's synchronous operations.
  484. *
  485. * @param mode If @c true, the underlying descriptor is put into non-blocking
  486. * mode and direct system calls may fail with boost::asio::error::would_block
  487. * (or the equivalent system error).
  488. *
  489. * @param ec Set to indicate what error occurred, if any. If the @c mode is
  490. * @c false, but the current value of @c non_blocking() is @c true, this
  491. * function fails with boost::asio::error::invalid_argument, as the
  492. * combination does not make sense.
  493. */
  494. BOOST_ASIO_SYNC_OP_VOID native_non_blocking(
  495. bool mode, boost::system::error_code& ec)
  496. {
  497. impl_.get_service().native_non_blocking(
  498. impl_.get_implementation(), mode, ec);
  499. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  500. }
  501. /// Wait for the descriptor to become ready to read, ready to write, or to
  502. /// have pending error conditions.
  503. /**
  504. * This function is used to perform a blocking wait for a descriptor to enter
  505. * a ready to read, write or error condition state.
  506. *
  507. * @param w Specifies the desired descriptor state.
  508. *
  509. * @par Example
  510. * Waiting for a descriptor to become readable.
  511. * @code
  512. * boost::asio::posix::stream_descriptor descriptor(my_context);
  513. * ...
  514. * descriptor.wait(boost::asio::posix::stream_descriptor::wait_read);
  515. * @endcode
  516. */
  517. void wait(wait_type w)
  518. {
  519. boost::system::error_code ec;
  520. impl_.get_service().wait(impl_.get_implementation(), w, ec);
  521. boost::asio::detail::throw_error(ec, "wait");
  522. }
  523. /// Wait for the descriptor to become ready to read, ready to write, or to
  524. /// have pending error conditions.
  525. /**
  526. * This function is used to perform a blocking wait for a descriptor to enter
  527. * a ready to read, write or error condition state.
  528. *
  529. * @param w Specifies the desired descriptor state.
  530. *
  531. * @param ec Set to indicate what error occurred, if any.
  532. *
  533. * @par Example
  534. * Waiting for a descriptor to become readable.
  535. * @code
  536. * boost::asio::posix::stream_descriptor descriptor(my_context);
  537. * ...
  538. * boost::system::error_code ec;
  539. * descriptor.wait(boost::asio::posix::stream_descriptor::wait_read, ec);
  540. * @endcode
  541. */
  542. BOOST_ASIO_SYNC_OP_VOID wait(wait_type w, boost::system::error_code& ec)
  543. {
  544. impl_.get_service().wait(impl_.get_implementation(), w, ec);
  545. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  546. }
  547. /// Asynchronously wait for the descriptor to become ready to read, ready to
  548. /// write, or to have pending error conditions.
  549. /**
  550. * This function is used to perform an asynchronous wait for a descriptor to
  551. * enter a ready to read, write or error condition state.
  552. *
  553. * @param w Specifies the desired descriptor state.
  554. *
  555. * @param handler The handler to be called when the wait operation completes.
  556. * Copies will be made of the handler as required. The function signature of
  557. * the handler must be:
  558. * @code void handler(
  559. * const boost::system::error_code& error // Result of operation
  560. * ); @endcode
  561. * Regardless of whether the asynchronous operation completes immediately or
  562. * not, the handler will not be invoked from within this function. On
  563. * immediate completion, invocation of the handler will be performed in a
  564. * manner equivalent to using boost::asio::post().
  565. *
  566. * @par Example
  567. * @code
  568. * void wait_handler(const boost::system::error_code& error)
  569. * {
  570. * if (!error)
  571. * {
  572. * // Wait succeeded.
  573. * }
  574. * }
  575. *
  576. * ...
  577. *
  578. * boost::asio::posix::stream_descriptor descriptor(my_context);
  579. * ...
  580. * descriptor.async_wait(
  581. * boost::asio::posix::stream_descriptor::wait_read,
  582. * wait_handler);
  583. * @endcode
  584. */
  585. template <
  586. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  587. WaitHandler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  588. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WaitHandler,
  589. void (boost::system::error_code))
  590. async_wait(wait_type w,
  591. BOOST_ASIO_MOVE_ARG(WaitHandler) handler
  592. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  593. {
  594. return async_initiate<WaitHandler, void (boost::system::error_code)>(
  595. initiate_async_wait(this), handler, w);
  596. }
  597. protected:
  598. /// Protected destructor to prevent deletion through this type.
  599. /**
  600. * This function destroys the descriptor, cancelling any outstanding
  601. * asynchronous wait operations associated with the descriptor as if by
  602. * calling @c cancel.
  603. */
  604. ~basic_descriptor()
  605. {
  606. }
  607. detail::io_object_impl<detail::reactive_descriptor_service, Executor> impl_;
  608. private:
  609. // Disallow copying and assignment.
  610. basic_descriptor(const basic_descriptor&) BOOST_ASIO_DELETED;
  611. basic_descriptor& operator=(const basic_descriptor&) BOOST_ASIO_DELETED;
  612. class initiate_async_wait
  613. {
  614. public:
  615. typedef Executor executor_type;
  616. explicit initiate_async_wait(basic_descriptor* self)
  617. : self_(self)
  618. {
  619. }
  620. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  621. {
  622. return self_->get_executor();
  623. }
  624. template <typename WaitHandler>
  625. void operator()(BOOST_ASIO_MOVE_ARG(WaitHandler) handler, wait_type w) const
  626. {
  627. // If you get an error on the following line it means that your handler
  628. // does not meet the documented type requirements for a WaitHandler.
  629. BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  630. detail::non_const_lvalue<WaitHandler> handler2(handler);
  631. self_->impl_.get_service().async_wait(
  632. self_->impl_.get_implementation(), w, handler2.value,
  633. self_->impl_.get_implementation_executor());
  634. }
  635. private:
  636. basic_descriptor* self_;
  637. };
  638. };
  639. } // namespace posix
  640. } // namespace asio
  641. } // namespace boost
  642. #include <boost/asio/detail/pop_options.hpp>
  643. #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
  644. // || defined(GENERATING_DOCUMENTATION)
  645. #endif // BOOST_ASIO_POSIX_BASIC_DESCRIPTOR_HPP