basic_random_access_handle.hpp 21 KB

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