basic_stream_descriptor.hpp 14 KB

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