basic_descriptor.hpp 20 KB

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