basic_stream_descriptor.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //
  2. // posix/basic_stream_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_STREAM_DESCRIPTOR_HPP
  11. #define BOOST_ASIO_POSIX_BASIC_STREAM_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. #include <boost/asio/posix/descriptor.hpp>
  17. #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. namespace boost {
  20. namespace asio {
  21. namespace posix {
  22. /// Provides stream-oriented descriptor functionality.
  23. /**
  24. * The posix::basic_stream_descriptor class template provides asynchronous and
  25. * blocking stream-oriented descriptor functionality.
  26. *
  27. * @par Thread Safety
  28. * @e Distinct @e objects: Safe.@n
  29. * @e Shared @e objects: Unsafe.
  30. *
  31. * @par Concepts:
  32. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  33. */
  34. template <typename Executor = executor>
  35. class basic_stream_descriptor
  36. : public basic_descriptor<Executor>
  37. {
  38. public:
  39. /// The type of the executor associated with the object.
  40. typedef Executor executor_type;
  41. /// The native representation of a descriptor.
  42. typedef typename basic_descriptor<Executor>::native_handle_type
  43. native_handle_type;
  44. /// Construct a stream descriptor without opening it.
  45. /**
  46. * This constructor creates a stream descriptor without opening it. The
  47. * descriptor needs to be opened and then connected or accepted before data
  48. * can be sent or received on it.
  49. *
  50. * @param ex The I/O executor that the descriptor will use, by default, to
  51. * dispatch handlers for any asynchronous operations performed on the
  52. * descriptor.
  53. */
  54. explicit basic_stream_descriptor(const executor_type& ex)
  55. : basic_descriptor<Executor>(ex)
  56. {
  57. }
  58. /// Construct a stream descriptor without opening it.
  59. /**
  60. * This constructor creates a stream descriptor without opening it. The
  61. * descriptor needs to be opened and then connected or accepted before data
  62. * can be sent or received on it.
  63. *
  64. * @param context An execution context which provides the I/O executor that
  65. * the descriptor will use, by default, to dispatch handlers for any
  66. * asynchronous operations performed on the descriptor.
  67. */
  68. template <typename ExecutionContext>
  69. explicit basic_stream_descriptor(ExecutionContext& context,
  70. typename enable_if<
  71. is_convertible<ExecutionContext&, execution_context&>::value
  72. >::type* = 0)
  73. : basic_descriptor<Executor>(context)
  74. {
  75. }
  76. /// Construct a stream descriptor on an existing native descriptor.
  77. /**
  78. * This constructor creates a stream descriptor object to hold an existing
  79. * native descriptor.
  80. *
  81. * @param ex The I/O executor that the descriptor will use, by default, to
  82. * dispatch handlers for any asynchronous operations performed on the
  83. * descriptor.
  84. *
  85. * @param native_descriptor The new underlying descriptor implementation.
  86. *
  87. * @throws boost::system::system_error Thrown on failure.
  88. */
  89. basic_stream_descriptor(const executor_type& ex,
  90. const native_handle_type& native_descriptor)
  91. : basic_descriptor<Executor>(ex, native_descriptor)
  92. {
  93. }
  94. /// Construct a stream descriptor on an existing native descriptor.
  95. /**
  96. * This constructor creates a stream descriptor object to hold an existing
  97. * native descriptor.
  98. *
  99. * @param context An execution context which provides the I/O executor that
  100. * the descriptor will use, by default, to dispatch handlers for any
  101. * asynchronous operations performed on the descriptor.
  102. *
  103. * @param native_descriptor The new underlying descriptor implementation.
  104. *
  105. * @throws boost::system::system_error Thrown on failure.
  106. */
  107. template <typename ExecutionContext>
  108. basic_stream_descriptor(ExecutionContext& context,
  109. const native_handle_type& native_descriptor,
  110. typename enable_if<
  111. is_convertible<ExecutionContext&, execution_context&>::value
  112. >::type* = 0)
  113. : basic_descriptor<Executor>(context, native_descriptor)
  114. {
  115. }
  116. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  117. /// Move-construct a stream descriptor from another.
  118. /**
  119. * This constructor moves a stream descriptor from one object to another.
  120. *
  121. * @param other The other stream descriptor object from which the move
  122. * will occur.
  123. *
  124. * @note Following the move, the moved-from object is in the same state as if
  125. * constructed using the @c basic_stream_descriptor(const executor_type&)
  126. * constructor.
  127. */
  128. basic_stream_descriptor(basic_stream_descriptor&& other)
  129. : descriptor(std::move(other))
  130. {
  131. }
  132. /// Move-assign a stream descriptor from another.
  133. /**
  134. * This assignment operator moves a stream descriptor from one object to
  135. * another.
  136. *
  137. * @param other The other stream descriptor object from which the move
  138. * will occur.
  139. *
  140. * @note Following the move, the moved-from object is in the same state as if
  141. * constructed using the @c basic_stream_descriptor(const executor_type&)
  142. * constructor.
  143. */
  144. basic_stream_descriptor& operator=(basic_stream_descriptor&& other)
  145. {
  146. descriptor::operator=(std::move(other));
  147. return *this;
  148. }
  149. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  150. /// Write some data to the descriptor.
  151. /**
  152. * This function is used to write data to the stream descriptor. The function
  153. * call will block until one or more bytes of the data has been written
  154. * successfully, or until an error occurs.
  155. *
  156. * @param buffers One or more data buffers to be written to the descriptor.
  157. *
  158. * @returns The number of bytes written.
  159. *
  160. * @throws boost::system::system_error Thrown on failure. An error code of
  161. * boost::asio::error::eof indicates that the connection was closed by the
  162. * peer.
  163. *
  164. * @note The write_some operation may not transmit all of the data to the
  165. * peer. Consider using the @ref write function if you need to ensure that
  166. * all data is written before the blocking operation completes.
  167. *
  168. * @par Example
  169. * To write a single data buffer use the @ref buffer function as follows:
  170. * @code
  171. * descriptor.write_some(boost::asio::buffer(data, size));
  172. * @endcode
  173. * See the @ref buffer documentation for information on writing multiple
  174. * buffers in one go, and how to use it with arrays, boost::array or
  175. * std::vector.
  176. */
  177. template <typename ConstBufferSequence>
  178. std::size_t write_some(const ConstBufferSequence& buffers)
  179. {
  180. boost::system::error_code ec;
  181. std::size_t s = this->impl_.get_service().write_some(
  182. this->impl_.get_implementation(), buffers, ec);
  183. boost::asio::detail::throw_error(ec, "write_some");
  184. return s;
  185. }
  186. /// Write some data to the descriptor.
  187. /**
  188. * This function is used to write data to the stream descriptor. The function
  189. * call will block until one or more bytes of the data has been written
  190. * successfully, or until an error occurs.
  191. *
  192. * @param buffers One or more data buffers to be written to the descriptor.
  193. *
  194. * @param ec Set to indicate what error occurred, if any.
  195. *
  196. * @returns The number of bytes written. Returns 0 if an error occurred.
  197. *
  198. * @note The write_some operation may not transmit all of the data to the
  199. * peer. Consider using the @ref write function if you need to ensure that
  200. * all data is written before the blocking operation completes.
  201. */
  202. template <typename ConstBufferSequence>
  203. std::size_t write_some(const ConstBufferSequence& buffers,
  204. boost::system::error_code& ec)
  205. {
  206. return this->impl_.get_service().write_some(
  207. this->impl_.get_implementation(), buffers, ec);
  208. }
  209. /// Start an asynchronous write.
  210. /**
  211. * This function is used to asynchronously write data to the stream
  212. * descriptor. The function call always returns immediately.
  213. *
  214. * @param buffers One or more data buffers to be written to the descriptor.
  215. * Although the buffers object may be copied as necessary, ownership of the
  216. * underlying memory blocks is retained by the caller, which must guarantee
  217. * that they remain valid until the handler is called.
  218. *
  219. * @param handler The handler to be called when the write operation completes.
  220. * Copies will be made of the handler as required. The function signature of
  221. * the handler must be:
  222. * @code void handler(
  223. * const boost::system::error_code& error, // Result of operation.
  224. * std::size_t bytes_transferred // Number of bytes written.
  225. * ); @endcode
  226. * Regardless of whether the asynchronous operation completes immediately or
  227. * not, the handler will not be invoked from within this function. On
  228. * immediate completion, invocation of the handler will be performed in a
  229. * manner equivalent to using boost::asio::post().
  230. *
  231. * @note The write operation may not transmit all of the data to the peer.
  232. * Consider using the @ref async_write function if you need to ensure that all
  233. * data is written before the asynchronous operation completes.
  234. *
  235. * @par Example
  236. * To write a single data buffer use the @ref buffer function as follows:
  237. * @code
  238. * descriptor.async_write_some(boost::asio::buffer(data, size), handler);
  239. * @endcode
  240. * See the @ref buffer documentation for information on writing multiple
  241. * buffers in one go, and how to use it with arrays, boost::array or
  242. * std::vector.
  243. */
  244. template <typename ConstBufferSequence, typename WriteHandler>
  245. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  246. void (boost::system::error_code, std::size_t))
  247. async_write_some(const ConstBufferSequence& buffers,
  248. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  249. {
  250. return async_initiate<WriteHandler,
  251. void (boost::system::error_code, std::size_t)>(
  252. initiate_async_write_some(), handler, this, buffers);
  253. }
  254. /// Read some data from the descriptor.
  255. /**
  256. * This function is used to read data from the stream descriptor. The function
  257. * call will block until one or more bytes of data has been read successfully,
  258. * or until an error occurs.
  259. *
  260. * @param buffers One or more buffers into which the data will be read.
  261. *
  262. * @returns The number of bytes read.
  263. *
  264. * @throws boost::system::system_error Thrown on failure. An error code of
  265. * boost::asio::error::eof indicates that the connection was closed by the
  266. * peer.
  267. *
  268. * @note The read_some operation may not read all of the requested number of
  269. * bytes. Consider using the @ref read function if you need to ensure that
  270. * the requested amount of data is read before the blocking operation
  271. * completes.
  272. *
  273. * @par Example
  274. * To read into a single data buffer use the @ref buffer function as follows:
  275. * @code
  276. * descriptor.read_some(boost::asio::buffer(data, size));
  277. * @endcode
  278. * See the @ref buffer documentation for information on reading into multiple
  279. * buffers in one go, and how to use it with arrays, boost::array or
  280. * std::vector.
  281. */
  282. template <typename MutableBufferSequence>
  283. std::size_t read_some(const MutableBufferSequence& buffers)
  284. {
  285. boost::system::error_code ec;
  286. std::size_t s = this->impl_.get_service().read_some(
  287. this->impl_.get_implementation(), buffers, ec);
  288. boost::asio::detail::throw_error(ec, "read_some");
  289. return s;
  290. }
  291. /// Read some data from the descriptor.
  292. /**
  293. * This function is used to read data from the stream descriptor. The function
  294. * call will block until one or more bytes of data has been read successfully,
  295. * or until an error occurs.
  296. *
  297. * @param buffers One or more buffers into which the data will be read.
  298. *
  299. * @param ec Set to indicate what error occurred, if any.
  300. *
  301. * @returns The number of bytes read. Returns 0 if an error occurred.
  302. *
  303. * @note The read_some operation may not read all of the requested number of
  304. * bytes. Consider using the @ref read function if you need to ensure that
  305. * the requested amount of data is read before the blocking operation
  306. * completes.
  307. */
  308. template <typename MutableBufferSequence>
  309. std::size_t read_some(const MutableBufferSequence& buffers,
  310. boost::system::error_code& ec)
  311. {
  312. return this->impl_.get_service().read_some(
  313. this->impl_.get_implementation(), buffers, ec);
  314. }
  315. /// Start an asynchronous read.
  316. /**
  317. * This function is used to asynchronously read data from the stream
  318. * descriptor. The function call always returns immediately.
  319. *
  320. * @param buffers One or more buffers into which the data will be read.
  321. * Although the buffers object may be copied as necessary, ownership of the
  322. * underlying memory blocks is retained by the caller, which must guarantee
  323. * that they remain valid until the handler is called.
  324. *
  325. * @param handler The handler to be called when the read operation completes.
  326. * Copies will be made of the handler as required. The function signature of
  327. * the handler must be:
  328. * @code void handler(
  329. * const boost::system::error_code& error, // Result of operation.
  330. * std::size_t bytes_transferred // Number of bytes read.
  331. * ); @endcode
  332. * Regardless of whether the asynchronous operation completes immediately or
  333. * not, the handler will not be invoked from within this function. On
  334. * immediate completion, invocation of the handler will be performed in a
  335. * manner equivalent to using boost::asio::post().
  336. *
  337. * @note The read operation may not read all of the requested number of bytes.
  338. * Consider using the @ref async_read function if you need to ensure that the
  339. * requested amount of data is read before the asynchronous operation
  340. * completes.
  341. *
  342. * @par Example
  343. * To read into a single data buffer use the @ref buffer function as follows:
  344. * @code
  345. * descriptor.async_read_some(boost::asio::buffer(data, size), handler);
  346. * @endcode
  347. * See the @ref buffer documentation for information on reading into multiple
  348. * buffers in one go, and how to use it with arrays, boost::array or
  349. * std::vector.
  350. */
  351. template <typename MutableBufferSequence, typename ReadHandler>
  352. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  353. void (boost::system::error_code, std::size_t))
  354. async_read_some(const MutableBufferSequence& buffers,
  355. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  356. {
  357. return async_initiate<ReadHandler,
  358. void (boost::system::error_code, std::size_t)>(
  359. initiate_async_read_some(), handler, this, buffers);
  360. }
  361. private:
  362. struct initiate_async_write_some
  363. {
  364. template <typename WriteHandler, typename ConstBufferSequence>
  365. void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  366. basic_stream_descriptor* self,
  367. const ConstBufferSequence& buffers) const
  368. {
  369. // If you get an error on the following line it means that your handler
  370. // does not meet the documented type requirements for a WriteHandler.
  371. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  372. detail::non_const_lvalue<WriteHandler> handler2(handler);
  373. self->impl_.get_service().async_write_some(
  374. self->impl_.get_implementation(), buffers, handler2.value,
  375. self->impl_.get_implementation_executor());
  376. }
  377. };
  378. struct initiate_async_read_some
  379. {
  380. template <typename ReadHandler, typename MutableBufferSequence>
  381. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  382. basic_stream_descriptor* self,
  383. const MutableBufferSequence& buffers) const
  384. {
  385. // If you get an error on the following line it means that your handler
  386. // does not meet the documented type requirements for a ReadHandler.
  387. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  388. detail::non_const_lvalue<ReadHandler> handler2(handler);
  389. self->impl_.get_service().async_read_some(
  390. self->impl_.get_implementation(), buffers, handler2.value,
  391. self->impl_.get_implementation_executor());
  392. }
  393. };
  394. };
  395. } // namespace posix
  396. } // namespace asio
  397. } // namespace boost
  398. #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
  399. // || defined(GENERATING_DOCUMENTATION)
  400. #endif // BOOST_ASIO_POSIX_BASIC_STREAM_DESCRIPTOR_HPP