basic_descriptor.hpp 17 KB

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