basic_random_access_handle.hpp 21 KB

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