basic_stream_handle.hpp 15 KB

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