basic_stream_handle.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // windows/basic_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_BASIC_STREAM_HANDLE_HPP
  11. #define BOOST_ASIO_WINDOWS_BASIC_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. #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  17. #if defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \
  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/windows/basic_handle.hpp>
  24. #include <boost/asio/windows/stream_handle_service.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace windows {
  29. /// Provides stream-oriented handle functionality.
  30. /**
  31. * The windows::basic_stream_handle class template provides asynchronous and
  32. * blocking stream-oriented handle 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 StreamHandleService = stream_handle_service>
  42. class basic_stream_handle
  43. : public basic_handle<StreamHandleService>
  44. {
  45. public:
  46. /// The native representation of a handle.
  47. typedef typename StreamHandleService::native_handle_type native_handle_type;
  48. /// Construct a basic_stream_handle without opening it.
  49. /**
  50. * This constructor creates a stream handle without opening it. The handle
  51. * needs to be opened and then connected or accepted before data can be sent
  52. * or received on it.
  53. *
  54. * @param io_context The io_context object that the stream handle will use to
  55. * dispatch handlers for any asynchronous operations performed on the handle.
  56. */
  57. explicit basic_stream_handle(boost::asio::io_context& io_context)
  58. : basic_handle<StreamHandleService>(io_context)
  59. {
  60. }
  61. /// Construct a basic_stream_handle on an existing native handle.
  62. /**
  63. * This constructor creates a stream handle object to hold an existing native
  64. * handle.
  65. *
  66. * @param io_context The io_context object that the stream handle will use to
  67. * dispatch handlers for any asynchronous operations performed on the handle.
  68. *
  69. * @param handle The new underlying handle implementation.
  70. *
  71. * @throws boost::system::system_error Thrown on failure.
  72. */
  73. basic_stream_handle(boost::asio::io_context& io_context,
  74. const native_handle_type& handle)
  75. : basic_handle<StreamHandleService>(io_context, handle)
  76. {
  77. }
  78. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  79. /// Move-construct a basic_stream_handle from another.
  80. /**
  81. * This constructor moves a stream handle from one object to another.
  82. *
  83. * @param other The other basic_stream_handle object from which the move
  84. * will occur.
  85. *
  86. * @note Following the move, the moved-from object is in the same state as if
  87. * constructed using the @c basic_stream_handle(io_context&) constructor.
  88. */
  89. basic_stream_handle(basic_stream_handle&& other)
  90. : basic_handle<StreamHandleService>(
  91. BOOST_ASIO_MOVE_CAST(basic_stream_handle)(other))
  92. {
  93. }
  94. /// Move-assign a basic_stream_handle from another.
  95. /**
  96. * This assignment operator moves a stream handle from one object to
  97. * another.
  98. *
  99. * @param other The other basic_stream_handle object from which the move
  100. * will occur.
  101. *
  102. * @note Following the move, the moved-from object is in the same state as if
  103. * constructed using the @c basic_stream_handle(io_context&) constructor.
  104. */
  105. basic_stream_handle& operator=(basic_stream_handle&& other)
  106. {
  107. basic_handle<StreamHandleService>::operator=(
  108. BOOST_ASIO_MOVE_CAST(basic_stream_handle)(other));
  109. return *this;
  110. }
  111. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  112. /// Write some data to the handle.
  113. /**
  114. * This function is used to write data to the stream handle. The function call
  115. * will block until one or more bytes of the data has been written
  116. * successfully, or until an error occurs.
  117. *
  118. * @param buffers One or more data buffers to be written to the handle.
  119. *
  120. * @returns The number of bytes written.
  121. *
  122. * @throws boost::system::system_error Thrown on failure. An error code of
  123. * boost::asio::error::eof indicates that the connection was closed by the
  124. * peer.
  125. *
  126. * @note The write_some operation may not transmit all of the data to the
  127. * peer. Consider using the @ref write function if you need to ensure that
  128. * all data is written before the blocking operation completes.
  129. *
  130. * @par Example
  131. * To write a single data buffer use the @ref buffer function as follows:
  132. * @code
  133. * handle.write_some(boost::asio::buffer(data, size));
  134. * @endcode
  135. * See the @ref buffer documentation for information on writing multiple
  136. * buffers in one go, and how to use it with arrays, boost::array or
  137. * std::vector.
  138. */
  139. template <typename ConstBufferSequence>
  140. std::size_t write_some(const ConstBufferSequence& buffers)
  141. {
  142. boost::system::error_code ec;
  143. std::size_t s = this->get_service().write_some(
  144. this->get_implementation(), buffers, ec);
  145. boost::asio::detail::throw_error(ec, "write_some");
  146. return s;
  147. }
  148. /// Write some data to the handle.
  149. /**
  150. * This function is used to write data to the stream handle. The function call
  151. * will block until one or more bytes of the data has been written
  152. * successfully, or until an error occurs.
  153. *
  154. * @param buffers One or more data buffers to be written to the handle.
  155. *
  156. * @param ec Set to indicate what error occurred, if any.
  157. *
  158. * @returns The number of bytes written. Returns 0 if an error occurred.
  159. *
  160. * @note The write_some operation may not transmit all of the data to the
  161. * peer. Consider using the @ref write function if you need to ensure that
  162. * all data is written before the blocking operation completes.
  163. */
  164. template <typename ConstBufferSequence>
  165. std::size_t write_some(const ConstBufferSequence& buffers,
  166. boost::system::error_code& ec)
  167. {
  168. return this->get_service().write_some(
  169. this->get_implementation(), buffers, ec);
  170. }
  171. /// Start an asynchronous write.
  172. /**
  173. * This function is used to asynchronously write data to the stream handle.
  174. * The function call always returns immediately.
  175. *
  176. * @param buffers One or more data buffers to be written to the handle.
  177. * Although the buffers object may be copied as necessary, ownership of the
  178. * underlying memory blocks is retained by the caller, which must guarantee
  179. * that they remain valid until the handler is called.
  180. *
  181. * @param handler The handler to be called when the write operation completes.
  182. * Copies will be made of the handler as required. The function signature of
  183. * the handler must be:
  184. * @code void handler(
  185. * const boost::system::error_code& error, // Result of operation.
  186. * std::size_t bytes_transferred // Number of bytes written.
  187. * ); @endcode
  188. * Regardless of whether the asynchronous operation completes immediately or
  189. * not, the handler will not be invoked from within this function. Invocation
  190. * of the handler will be performed in a manner equivalent to using
  191. * boost::asio::io_context::post().
  192. *
  193. * @note The write operation may not transmit all of the data to the peer.
  194. * Consider using the @ref async_write function if you need to ensure that all
  195. * data is written before the asynchronous operation completes.
  196. *
  197. * @par Example
  198. * To write a single data buffer use the @ref buffer function as follows:
  199. * @code
  200. * handle.async_write_some(boost::asio::buffer(data, size), handler);
  201. * @endcode
  202. * See the @ref buffer documentation for information on writing multiple
  203. * buffers in one go, and how to use it with arrays, boost::array or
  204. * std::vector.
  205. */
  206. template <typename ConstBufferSequence, typename WriteHandler>
  207. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  208. void (boost::system::error_code, std::size_t))
  209. async_write_some(const ConstBufferSequence& buffers,
  210. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  211. {
  212. // If you get an error on the following line it means that your handler does
  213. // not meet the documented type requirements for a WriteHandler.
  214. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  215. return this->get_service().async_write_some(this->get_implementation(),
  216. buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
  217. }
  218. /// Read some data from the handle.
  219. /**
  220. * This function is used to read data from the stream handle. 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. * handle.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 handle.
  256. /**
  257. * This function is used to read data from the stream handle. 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 handle.
  282. * 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. * handle.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. return this->get_service().async_read_some(this->get_implementation(),
  325. buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  326. }
  327. };
  328. } // namespace windows
  329. } // namespace asio
  330. } // namespace boost
  331. #include <boost/asio/detail/pop_options.hpp>
  332. #endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
  333. // || defined(GENERATING_DOCUMENTATION)
  334. #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  335. #endif // BOOST_ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP