basic_random_access_handle.hpp 19 KB

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