stream_handle.hpp 14 KB

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