basic_stream_handle.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. //
  2. // windows/basic_stream_handle.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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 ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include "asio/windows/basic_overlapped_handle.hpp"
  17. #if defined(ASIO_HAS_WINDOWS_STREAM_HANDLE) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include "asio/detail/push_options.hpp"
  20. namespace asio {
  21. namespace windows {
  22. /// Provides stream-oriented handle functionality.
  23. /**
  24. * The windows::basic_stream_handle class provides asynchronous and blocking
  25. * stream-oriented handle functionality.
  26. *
  27. * @par Thread Safety
  28. * @e Distinct @e objects: Safe.@n
  29. * @e Shared @e objects: Unsafe.
  30. *
  31. * @par Concepts:
  32. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  33. */
  34. template <typename Executor = any_io_executor>
  35. class basic_stream_handle
  36. : public basic_overlapped_handle<Executor>
  37. {
  38. public:
  39. /// The type of the executor associated with the object.
  40. typedef Executor executor_type;
  41. /// Rebinds the handle type to another executor.
  42. template <typename Executor1>
  43. struct rebind_executor
  44. {
  45. /// The handle type when rebound to the specified executor.
  46. typedef basic_stream_handle<Executor1> other;
  47. };
  48. /// The native representation of a handle.
  49. #if defined(GENERATING_DOCUMENTATION)
  50. typedef implementation_defined native_handle_type;
  51. #else
  52. typedef asio::detail::win_iocp_handle_service::native_handle_type
  53. native_handle_type;
  54. #endif
  55. /// Construct a stream handle without opening it.
  56. /**
  57. * This constructor creates a stream handle without opening it.
  58. *
  59. * @param ex The I/O executor that the stream handle will use, by default, to
  60. * dispatch handlers for any asynchronous operations performed on the stream
  61. * handle.
  62. */
  63. explicit basic_stream_handle(const executor_type& ex)
  64. : basic_overlapped_handle<Executor>(ex)
  65. {
  66. }
  67. /// Construct a stream handle without opening it.
  68. /**
  69. * This constructor creates a stream handle without opening it. The handle
  70. * needs to be opened or assigned before data can be written to or read from
  71. * it.
  72. *
  73. * @param context An execution context which provides the I/O executor that
  74. * the stream handle will use, by default, to dispatch handlers for any
  75. * asynchronous operations performed on the stream handle.
  76. */
  77. template <typename ExecutionContext>
  78. explicit basic_stream_handle(ExecutionContext& context,
  79. typename constraint<
  80. is_convertible<ExecutionContext&, execution_context&>::value,
  81. defaulted_constraint
  82. >::type = defaulted_constraint())
  83. : basic_overlapped_handle<Executor>(context)
  84. {
  85. }
  86. /// Construct a stream handle on an existing native handle.
  87. /**
  88. * This constructor creates a stream handle object to hold an existing native
  89. * handle.
  90. *
  91. * @param ex The I/O executor that the stream handle will use, by default, to
  92. * dispatch handlers for any asynchronous operations performed on the stream
  93. * handle.
  94. *
  95. * @param handle The new underlying handle implementation.
  96. *
  97. * @throws asio::system_error Thrown on failure.
  98. */
  99. basic_stream_handle(const executor_type& ex, const native_handle_type& handle)
  100. : basic_overlapped_handle<Executor>(ex, handle)
  101. {
  102. }
  103. /// Construct a stream handle on an existing native handle.
  104. /**
  105. * This constructor creates a stream handle object to hold an existing native
  106. * handle.
  107. *
  108. * @param context An execution context which provides the I/O executor that
  109. * the stream handle will use, by default, to dispatch handlers for any
  110. * asynchronous operations performed on the stream handle.
  111. *
  112. * @param handle The new underlying handle implementation.
  113. *
  114. * @throws asio::system_error Thrown on failure.
  115. */
  116. template <typename ExecutionContext>
  117. basic_stream_handle(ExecutionContext& context,
  118. const native_handle_type& handle,
  119. typename constraint<
  120. is_convertible<ExecutionContext&, execution_context&>::value
  121. >::type = 0)
  122. : basic_overlapped_handle<Executor>(context, handle)
  123. {
  124. }
  125. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  126. /// Move-construct a stream handle from another.
  127. /**
  128. * This constructor moves a stream handle from one object to another.
  129. *
  130. * @param other The other stream handle object from which the move
  131. * will occur.
  132. *
  133. * @note Following the move, the moved-from object is in the same state as if
  134. * constructed using the @c basic_stream_handle(const executor_type&)
  135. * constructor.
  136. */
  137. basic_stream_handle(basic_stream_handle&& other)
  138. : basic_overlapped_handle<Executor>(std::move(other))
  139. {
  140. }
  141. /// Move-assign a stream handle from another.
  142. /**
  143. * This assignment operator moves a stream handle from one object to
  144. * another.
  145. *
  146. * @param other The other stream handle object from which the move will occur.
  147. *
  148. * @note Following the move, the moved-from object is in the same state as if
  149. * constructed using the @c basic_stream_handle(const executor_type&)
  150. * constructor.
  151. */
  152. basic_stream_handle& operator=(basic_stream_handle&& other)
  153. {
  154. basic_overlapped_handle<Executor>::operator=(std::move(other));
  155. return *this;
  156. }
  157. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  158. /// Write some data to the handle.
  159. /**
  160. * This function is used to write data to the stream handle. The function call
  161. * will block until one or more bytes of the data has been written
  162. * successfully, or until an error occurs.
  163. *
  164. * @param buffers One or more data buffers to be written to the handle.
  165. *
  166. * @returns The number of bytes written.
  167. *
  168. * @throws asio::system_error Thrown on failure. An error code of
  169. * asio::error::eof indicates that the connection was closed by the
  170. * peer.
  171. *
  172. * @note The write_some operation may not transmit all of the data to the
  173. * peer. Consider using the @ref write function if you need to ensure that
  174. * all data is written before the blocking operation completes.
  175. *
  176. * @par Example
  177. * To write a single data buffer use the @ref buffer function as follows:
  178. * @code
  179. * handle.write_some(asio::buffer(data, size));
  180. * @endcode
  181. * See the @ref buffer documentation for information on writing multiple
  182. * buffers in one go, and how to use it with arrays, boost::array or
  183. * std::vector.
  184. */
  185. template <typename ConstBufferSequence>
  186. std::size_t write_some(const ConstBufferSequence& buffers)
  187. {
  188. asio::error_code ec;
  189. std::size_t s = this->impl_.get_service().write_some(
  190. this->impl_.get_implementation(), buffers, ec);
  191. asio::detail::throw_error(ec, "write_some");
  192. return s;
  193. }
  194. /// Write some data to the handle.
  195. /**
  196. * This function is used to write data to the stream handle. The function call
  197. * will block until one or more bytes of the data has been written
  198. * successfully, or until an error occurs.
  199. *
  200. * @param buffers One or more data buffers to be written to the handle.
  201. *
  202. * @param ec Set to indicate what error occurred, if any.
  203. *
  204. * @returns The number of bytes written. Returns 0 if an error occurred.
  205. *
  206. * @note The write_some operation may not transmit all of the data to the
  207. * peer. Consider using the @ref write function if you need to ensure that
  208. * all data is written before the blocking operation completes.
  209. */
  210. template <typename ConstBufferSequence>
  211. std::size_t write_some(const ConstBufferSequence& buffers,
  212. asio::error_code& ec)
  213. {
  214. return this->impl_.get_service().write_some(
  215. this->impl_.get_implementation(), buffers, ec);
  216. }
  217. /// Start an asynchronous write.
  218. /**
  219. * This function is used to asynchronously write data to the stream handle.
  220. * It is an initiating function for an @ref asynchronous_operation, and always
  221. * returns immediately.
  222. *
  223. * @param buffers One or more data buffers to be written to the handle.
  224. * Although the buffers object may be copied as necessary, ownership of the
  225. * underlying memory blocks is retained by the caller, which must guarantee
  226. * that they remain valid until the completion handler is called.
  227. *
  228. * @param token The @ref completion_token that will be used to produce a
  229. * completion handler, which will be called when the write completes.
  230. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  231. * @ref yield_context, or a function object with the correct completion
  232. * signature. The function signature of the completion handler must be:
  233. * @code void handler(
  234. * const asio::error_code& error, // Result of operation.
  235. * std::size_t bytes_transferred // Number of bytes written.
  236. * ); @endcode
  237. * Regardless of whether the asynchronous operation completes immediately or
  238. * not, the completion handler will not be invoked from within this function.
  239. * On immediate completion, invocation of the handler will be performed in a
  240. * manner equivalent to using asio::post().
  241. *
  242. * @par Completion Signature
  243. * @code void(asio::error_code, std::size_t) @endcode
  244. *
  245. * @note The write operation may not transmit all of the data to the peer.
  246. * Consider using the @ref async_write function if you need to ensure that all
  247. * data is written before the asynchronous operation completes.
  248. *
  249. * @par Example
  250. * To write a single data buffer use the @ref buffer function as follows:
  251. * @code
  252. * handle.async_write_some(asio::buffer(data, size), handler);
  253. * @endcode
  254. * See the @ref buffer documentation for information on writing multiple
  255. * buffers in one go, and how to use it with arrays, boost::array or
  256. * std::vector.
  257. *
  258. * @par Per-Operation Cancellation
  259. * This asynchronous operation supports cancellation for the following
  260. * asio::cancellation_type values:
  261. *
  262. * @li @c cancellation_type::terminal
  263. *
  264. * @li @c cancellation_type::partial
  265. *
  266. * @li @c cancellation_type::total
  267. */
  268. template <typename ConstBufferSequence,
  269. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  270. std::size_t)) WriteToken
  271. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  272. ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  273. void (asio::error_code, std::size_t))
  274. async_write_some(const ConstBufferSequence& buffers,
  275. ASIO_MOVE_ARG(WriteToken) token
  276. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  277. {
  278. return async_initiate<WriteToken,
  279. void (asio::error_code, std::size_t)>(
  280. initiate_async_write_some(this), token, buffers);
  281. }
  282. /// Read some data from the handle.
  283. /**
  284. * This function is used to read data from the stream handle. The function
  285. * call will block until one or more bytes of data has been read successfully,
  286. * or until an error occurs.
  287. *
  288. * @param buffers One or more buffers into which the data will be read.
  289. *
  290. * @returns The number of bytes read.
  291. *
  292. * @throws asio::system_error Thrown on failure. An error code of
  293. * asio::error::eof indicates that the connection was closed by the
  294. * peer.
  295. *
  296. * @note The read_some operation may not read all of the requested number of
  297. * bytes. Consider using the @ref read function if you need to ensure that
  298. * the requested amount of data is read before the blocking operation
  299. * completes.
  300. *
  301. * @par Example
  302. * To read into a single data buffer use the @ref buffer function as follows:
  303. * @code
  304. * handle.read_some(asio::buffer(data, size));
  305. * @endcode
  306. * See the @ref buffer documentation for information on reading into multiple
  307. * buffers in one go, and how to use it with arrays, boost::array or
  308. * std::vector.
  309. */
  310. template <typename MutableBufferSequence>
  311. std::size_t read_some(const MutableBufferSequence& buffers)
  312. {
  313. asio::error_code ec;
  314. std::size_t s = this->impl_.get_service().read_some(
  315. this->impl_.get_implementation(), buffers, ec);
  316. asio::detail::throw_error(ec, "read_some");
  317. return s;
  318. }
  319. /// Read some data from the handle.
  320. /**
  321. * This function is used to read data from the stream handle. The function
  322. * call will block until one or more bytes of data has been read successfully,
  323. * or until an error occurs.
  324. *
  325. * @param buffers One or more buffers into which the data will be read.
  326. *
  327. * @param ec Set to indicate what error occurred, if any.
  328. *
  329. * @returns The number of bytes read. Returns 0 if an error occurred.
  330. *
  331. * @note The read_some operation may not read all of the requested number of
  332. * bytes. Consider using the @ref read function if you need to ensure that
  333. * the requested amount of data is read before the blocking operation
  334. * completes.
  335. */
  336. template <typename MutableBufferSequence>
  337. std::size_t read_some(const MutableBufferSequence& buffers,
  338. asio::error_code& ec)
  339. {
  340. return this->impl_.get_service().read_some(
  341. this->impl_.get_implementation(), buffers, ec);
  342. }
  343. /// Start an asynchronous read.
  344. /**
  345. * This function is used to asynchronously read data from the stream handle.
  346. * It is an initiating function for an @ref asynchronous_operation, and always
  347. * returns immediately.
  348. *
  349. * @param buffers One or more buffers into which the data will be read.
  350. * Although the buffers object may be copied as necessary, ownership of the
  351. * underlying memory blocks is retained by the caller, which must guarantee
  352. * that they remain valid until the completion handler is called.
  353. *
  354. * @param token The @ref completion_token that will be used to produce a
  355. * completion handler, which will be called when the read completes.
  356. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  357. * @ref yield_context, or a function object with the correct completion
  358. * signature. The function signature of the completion handler must be:
  359. * @code void handler(
  360. * const asio::error_code& error, // Result of operation.
  361. * std::size_t bytes_transferred // Number of bytes read.
  362. * ); @endcode
  363. * Regardless of whether the asynchronous operation completes immediately or
  364. * not, the completion handler will not be invoked from within this function.
  365. * On immediate completion, invocation of the handler will be performed in a
  366. * manner equivalent to using asio::post().
  367. *
  368. * @par Completion Signature
  369. * @code void(asio::error_code, std::size_t) @endcode
  370. *
  371. * @note The read operation may not read all of the requested number of bytes.
  372. * Consider using the @ref async_read function if you need to ensure that the
  373. * requested amount of data is read before the asynchronous operation
  374. * completes.
  375. *
  376. * @par Example
  377. * To read into a single data buffer use the @ref buffer function as follows:
  378. * @code
  379. * handle.async_read_some(asio::buffer(data, size), handler);
  380. * @endcode
  381. * See the @ref buffer documentation for information on reading into multiple
  382. * buffers in one go, and how to use it with arrays, boost::array or
  383. * std::vector.
  384. *
  385. * @par Per-Operation Cancellation
  386. * This asynchronous operation supports cancellation for the following
  387. * asio::cancellation_type values:
  388. *
  389. * @li @c cancellation_type::terminal
  390. *
  391. * @li @c cancellation_type::partial
  392. *
  393. * @li @c cancellation_type::total
  394. */
  395. template <typename MutableBufferSequence,
  396. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  397. std::size_t)) ReadToken
  398. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  399. ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
  400. void (asio::error_code, std::size_t))
  401. async_read_some(const MutableBufferSequence& buffers,
  402. ASIO_MOVE_ARG(ReadToken) token
  403. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  404. {
  405. return async_initiate<ReadToken,
  406. void (asio::error_code, std::size_t)>(
  407. initiate_async_read_some(this), token, buffers);
  408. }
  409. private:
  410. class initiate_async_write_some
  411. {
  412. public:
  413. typedef Executor executor_type;
  414. explicit initiate_async_write_some(basic_stream_handle* self)
  415. : self_(self)
  416. {
  417. }
  418. executor_type get_executor() const ASIO_NOEXCEPT
  419. {
  420. return self_->get_executor();
  421. }
  422. template <typename WriteHandler, typename ConstBufferSequence>
  423. void operator()(ASIO_MOVE_ARG(WriteHandler) handler,
  424. const ConstBufferSequence& buffers) const
  425. {
  426. // If you get an error on the following line it means that your handler
  427. // does not meet the documented type requirements for a WriteHandler.
  428. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  429. detail::non_const_lvalue<WriteHandler> handler2(handler);
  430. self_->impl_.get_service().async_write_some(
  431. self_->impl_.get_implementation(), buffers,
  432. handler2.value, self_->impl_.get_executor());
  433. }
  434. private:
  435. basic_stream_handle* self_;
  436. };
  437. class initiate_async_read_some
  438. {
  439. public:
  440. typedef Executor executor_type;
  441. explicit initiate_async_read_some(basic_stream_handle* self)
  442. : self_(self)
  443. {
  444. }
  445. executor_type get_executor() const ASIO_NOEXCEPT
  446. {
  447. return self_->get_executor();
  448. }
  449. template <typename ReadHandler, typename MutableBufferSequence>
  450. void operator()(ASIO_MOVE_ARG(ReadHandler) handler,
  451. const MutableBufferSequence& buffers) const
  452. {
  453. // If you get an error on the following line it means that your handler
  454. // does not meet the documented type requirements for a ReadHandler.
  455. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  456. detail::non_const_lvalue<ReadHandler> handler2(handler);
  457. self_->impl_.get_service().async_read_some(
  458. self_->impl_.get_implementation(), buffers,
  459. handler2.value, self_->impl_.get_executor());
  460. }
  461. private:
  462. basic_stream_handle* self_;
  463. };
  464. };
  465. } // namespace windows
  466. } // namespace asio
  467. #include "asio/detail/pop_options.hpp"
  468. #endif // defined(ASIO_HAS_WINDOWS_STREAM_HANDLE)
  469. // || defined(GENERATING_DOCUMENTATION)
  470. #endif // ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP