stream_descriptor.hpp 14 KB

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