read_at.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. //
  2. // read_at.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_READ_AT_HPP
  11. #define ASIO_READ_AT_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 <cstddef>
  17. #include "asio/async_result.hpp"
  18. #include "asio/completion_condition.hpp"
  19. #include "asio/detail/cstdint.hpp"
  20. #include "asio/error.hpp"
  21. #if !defined(ASIO_NO_EXTENSIONS)
  22. # include "asio/basic_streambuf_fwd.hpp"
  23. #endif // !defined(ASIO_NO_EXTENSIONS)
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. namespace detail {
  27. template <typename> class initiate_async_read_at;
  28. #if !defined(ASIO_NO_IOSTREAM)
  29. template <typename> class initiate_async_read_at_streambuf;
  30. #endif // !defined(ASIO_NO_IOSTREAM)
  31. } // namespace detail
  32. /**
  33. * @defgroup read_at asio::read_at
  34. *
  35. * @brief The @c read_at function is a composed operation that reads a certain
  36. * amount of data at the specified offset before returning.
  37. */
  38. /*@{*/
  39. /// Attempt to read a certain amount of data at the specified offset before
  40. /// returning.
  41. /**
  42. * This function is used to read a certain number of bytes of data from a
  43. * random access device at the specified offset. The call will block until one
  44. * of the following conditions is true:
  45. *
  46. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  47. * the sum of the buffer sizes.
  48. *
  49. * @li An error occurred.
  50. *
  51. * This operation is implemented in terms of zero or more calls to the device's
  52. * read_some_at function.
  53. *
  54. * @param d The device from which the data is to be read. The type must support
  55. * the SyncRandomAccessReadDevice concept.
  56. *
  57. * @param offset The offset at which the data will be read.
  58. *
  59. * @param buffers One or more buffers into which the data will be read. The sum
  60. * of the buffer sizes indicates the maximum number of bytes to read from the
  61. * device.
  62. *
  63. * @returns The number of bytes transferred.
  64. *
  65. * @throws asio::system_error Thrown on failure.
  66. *
  67. * @par Example
  68. * To read into a single data buffer use the @ref buffer function as follows:
  69. * @code asio::read_at(d, 42, asio::buffer(data, size)); @endcode
  70. * See the @ref buffer documentation for information on reading into multiple
  71. * buffers in one go, and how to use it with arrays, boost::array or
  72. * std::vector.
  73. *
  74. * @note This overload is equivalent to calling:
  75. * @code asio::read_at(
  76. * d, 42, buffers,
  77. * asio::transfer_all()); @endcode
  78. */
  79. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence>
  80. std::size_t read_at(SyncRandomAccessReadDevice& d,
  81. uint64_t offset, const MutableBufferSequence& buffers);
  82. /// Attempt to read a certain amount of data at the specified offset before
  83. /// returning.
  84. /**
  85. * This function is used to read a certain number of bytes of data from a
  86. * random access device at the specified offset. The call will block until one
  87. * of the following conditions is true:
  88. *
  89. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  90. * the sum of the buffer sizes.
  91. *
  92. * @li An error occurred.
  93. *
  94. * This operation is implemented in terms of zero or more calls to the device's
  95. * read_some_at function.
  96. *
  97. * @param d The device from which the data is to be read. The type must support
  98. * the SyncRandomAccessReadDevice concept.
  99. *
  100. * @param offset The offset at which the data will be read.
  101. *
  102. * @param buffers One or more buffers into which the data will be read. The sum
  103. * of the buffer sizes indicates the maximum number of bytes to read from the
  104. * device.
  105. *
  106. * @param ec Set to indicate what error occurred, if any.
  107. *
  108. * @returns The number of bytes transferred.
  109. *
  110. * @par Example
  111. * To read into a single data buffer use the @ref buffer function as follows:
  112. * @code asio::read_at(d, 42,
  113. * asio::buffer(data, size), ec); @endcode
  114. * See the @ref buffer documentation for information on reading into multiple
  115. * buffers in one go, and how to use it with arrays, boost::array or
  116. * std::vector.
  117. *
  118. * @note This overload is equivalent to calling:
  119. * @code asio::read_at(
  120. * d, 42, buffers,
  121. * asio::transfer_all(), ec); @endcode
  122. */
  123. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence>
  124. std::size_t read_at(SyncRandomAccessReadDevice& d,
  125. uint64_t offset, const MutableBufferSequence& buffers,
  126. asio::error_code& ec);
  127. /// Attempt to read a certain amount of data at the specified offset before
  128. /// returning.
  129. /**
  130. * This function is used to read a certain number of bytes of data from a
  131. * random access device at the specified offset. The call will block until one
  132. * of the following conditions is true:
  133. *
  134. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  135. * the sum of the buffer sizes.
  136. *
  137. * @li The completion_condition function object returns 0.
  138. *
  139. * This operation is implemented in terms of zero or more calls to the device's
  140. * read_some_at function.
  141. *
  142. * @param d The device from which the data is to be read. The type must support
  143. * the SyncRandomAccessReadDevice concept.
  144. *
  145. * @param offset The offset at which the data will be read.
  146. *
  147. * @param buffers One or more buffers into which the data will be read. The sum
  148. * of the buffer sizes indicates the maximum number of bytes to read from the
  149. * device.
  150. *
  151. * @param completion_condition The function object to be called to determine
  152. * whether the read operation is complete. The signature of the function object
  153. * must be:
  154. * @code std::size_t completion_condition(
  155. * // Result of latest read_some_at operation.
  156. * const asio::error_code& error,
  157. *
  158. * // Number of bytes transferred so far.
  159. * std::size_t bytes_transferred
  160. * ); @endcode
  161. * A return value of 0 indicates that the read operation is complete. A non-zero
  162. * return value indicates the maximum number of bytes to be read on the next
  163. * call to the device's read_some_at function.
  164. *
  165. * @returns The number of bytes transferred.
  166. *
  167. * @throws asio::system_error Thrown on failure.
  168. *
  169. * @par Example
  170. * To read into a single data buffer use the @ref buffer function as follows:
  171. * @code asio::read_at(d, 42, asio::buffer(data, size),
  172. * asio::transfer_at_least(32)); @endcode
  173. * See the @ref buffer documentation for information on reading into multiple
  174. * buffers in one go, and how to use it with arrays, boost::array or
  175. * std::vector.
  176. */
  177. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
  178. typename CompletionCondition>
  179. std::size_t read_at(SyncRandomAccessReadDevice& d,
  180. uint64_t offset, const MutableBufferSequence& buffers,
  181. CompletionCondition completion_condition);
  182. /// Attempt to read a certain amount of data at the specified offset before
  183. /// returning.
  184. /**
  185. * This function is used to read a certain number of bytes of data from a
  186. * random access device at the specified offset. The call will block until one
  187. * of the following conditions is true:
  188. *
  189. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  190. * the sum of the buffer sizes.
  191. *
  192. * @li The completion_condition function object returns 0.
  193. *
  194. * This operation is implemented in terms of zero or more calls to the device's
  195. * read_some_at function.
  196. *
  197. * @param d The device from which the data is to be read. The type must support
  198. * the SyncRandomAccessReadDevice concept.
  199. *
  200. * @param offset The offset at which the data will be read.
  201. *
  202. * @param buffers One or more buffers into which the data will be read. The sum
  203. * of the buffer sizes indicates the maximum number of bytes to read from the
  204. * device.
  205. *
  206. * @param completion_condition The function object to be called to determine
  207. * whether the read operation is complete. The signature of the function object
  208. * must be:
  209. * @code std::size_t completion_condition(
  210. * // Result of latest read_some_at operation.
  211. * const asio::error_code& error,
  212. *
  213. * // Number of bytes transferred so far.
  214. * std::size_t bytes_transferred
  215. * ); @endcode
  216. * A return value of 0 indicates that the read operation is complete. A non-zero
  217. * return value indicates the maximum number of bytes to be read on the next
  218. * call to the device's read_some_at function.
  219. *
  220. * @param ec Set to indicate what error occurred, if any.
  221. *
  222. * @returns The number of bytes read. If an error occurs, returns the total
  223. * number of bytes successfully transferred prior to the error.
  224. */
  225. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
  226. typename CompletionCondition>
  227. std::size_t read_at(SyncRandomAccessReadDevice& d,
  228. uint64_t offset, const MutableBufferSequence& buffers,
  229. CompletionCondition completion_condition, asio::error_code& ec);
  230. #if !defined(ASIO_NO_EXTENSIONS)
  231. #if !defined(ASIO_NO_IOSTREAM)
  232. /// Attempt to read a certain amount of data at the specified offset before
  233. /// returning.
  234. /**
  235. * This function is used to read a certain number of bytes of data from a
  236. * random access device at the specified offset. The call will block until one
  237. * of the following conditions is true:
  238. *
  239. * @li An error occurred.
  240. *
  241. * This operation is implemented in terms of zero or more calls to the device's
  242. * read_some_at function.
  243. *
  244. * @param d The device from which the data is to be read. The type must support
  245. * the SyncRandomAccessReadDevice concept.
  246. *
  247. * @param offset The offset at which the data will be read.
  248. *
  249. * @param b The basic_streambuf object into which the data will be read.
  250. *
  251. * @returns The number of bytes transferred.
  252. *
  253. * @throws asio::system_error Thrown on failure.
  254. *
  255. * @note This overload is equivalent to calling:
  256. * @code asio::read_at(
  257. * d, 42, b,
  258. * asio::transfer_all()); @endcode
  259. */
  260. template <typename SyncRandomAccessReadDevice, typename Allocator>
  261. std::size_t read_at(SyncRandomAccessReadDevice& d,
  262. uint64_t offset, basic_streambuf<Allocator>& b);
  263. /// Attempt to read a certain amount of data at the specified offset before
  264. /// returning.
  265. /**
  266. * This function is used to read a certain number of bytes of data from a
  267. * random access device at the specified offset. The call will block until one
  268. * of the following conditions is true:
  269. *
  270. * @li An error occurred.
  271. *
  272. * This operation is implemented in terms of zero or more calls to the device's
  273. * read_some_at function.
  274. *
  275. * @param d The device from which the data is to be read. The type must support
  276. * the SyncRandomAccessReadDevice concept.
  277. *
  278. * @param offset The offset at which the data will be read.
  279. *
  280. * @param b The basic_streambuf object into which the data will be read.
  281. *
  282. * @param ec Set to indicate what error occurred, if any.
  283. *
  284. * @returns The number of bytes transferred.
  285. *
  286. * @note This overload is equivalent to calling:
  287. * @code asio::read_at(
  288. * d, 42, b,
  289. * asio::transfer_all(), ec); @endcode
  290. */
  291. template <typename SyncRandomAccessReadDevice, typename Allocator>
  292. std::size_t read_at(SyncRandomAccessReadDevice& d,
  293. uint64_t offset, basic_streambuf<Allocator>& b,
  294. asio::error_code& ec);
  295. /// Attempt to read a certain amount of data at the specified offset before
  296. /// returning.
  297. /**
  298. * This function is used to read a certain number of bytes of data from a
  299. * random access device at the specified offset. The call will block until one
  300. * of the following conditions is true:
  301. *
  302. * @li The completion_condition function object returns 0.
  303. *
  304. * This operation is implemented in terms of zero or more calls to the device's
  305. * read_some_at function.
  306. *
  307. * @param d The device from which the data is to be read. The type must support
  308. * the SyncRandomAccessReadDevice concept.
  309. *
  310. * @param offset The offset at which the data will be read.
  311. *
  312. * @param b The basic_streambuf object into which the data will be read.
  313. *
  314. * @param completion_condition The function object to be called to determine
  315. * whether the read operation is complete. The signature of the function object
  316. * must be:
  317. * @code std::size_t completion_condition(
  318. * // Result of latest read_some_at operation.
  319. * const asio::error_code& error,
  320. *
  321. * // Number of bytes transferred so far.
  322. * std::size_t bytes_transferred
  323. * ); @endcode
  324. * A return value of 0 indicates that the read operation is complete. A non-zero
  325. * return value indicates the maximum number of bytes to be read on the next
  326. * call to the device's read_some_at function.
  327. *
  328. * @returns The number of bytes transferred.
  329. *
  330. * @throws asio::system_error Thrown on failure.
  331. */
  332. template <typename SyncRandomAccessReadDevice, typename Allocator,
  333. typename CompletionCondition>
  334. std::size_t read_at(SyncRandomAccessReadDevice& d,
  335. uint64_t offset, basic_streambuf<Allocator>& b,
  336. CompletionCondition completion_condition);
  337. /// Attempt to read a certain amount of data at the specified offset before
  338. /// returning.
  339. /**
  340. * This function is used to read a certain number of bytes of data from a
  341. * random access device at the specified offset. The call will block until one
  342. * of the following conditions is true:
  343. *
  344. * @li The completion_condition function object returns 0.
  345. *
  346. * This operation is implemented in terms of zero or more calls to the device's
  347. * read_some_at function.
  348. *
  349. * @param d The device from which the data is to be read. The type must support
  350. * the SyncRandomAccessReadDevice concept.
  351. *
  352. * @param offset The offset at which the data will be read.
  353. *
  354. * @param b The basic_streambuf object into which the data will be read.
  355. *
  356. * @param completion_condition The function object to be called to determine
  357. * whether the read operation is complete. The signature of the function object
  358. * must be:
  359. * @code std::size_t completion_condition(
  360. * // Result of latest read_some_at operation.
  361. * const asio::error_code& error,
  362. *
  363. * // Number of bytes transferred so far.
  364. * std::size_t bytes_transferred
  365. * ); @endcode
  366. * A return value of 0 indicates that the read operation is complete. A non-zero
  367. * return value indicates the maximum number of bytes to be read on the next
  368. * call to the device's read_some_at function.
  369. *
  370. * @param ec Set to indicate what error occurred, if any.
  371. *
  372. * @returns The number of bytes read. If an error occurs, returns the total
  373. * number of bytes successfully transferred prior to the error.
  374. */
  375. template <typename SyncRandomAccessReadDevice, typename Allocator,
  376. typename CompletionCondition>
  377. std::size_t read_at(SyncRandomAccessReadDevice& d,
  378. uint64_t offset, basic_streambuf<Allocator>& b,
  379. CompletionCondition completion_condition, asio::error_code& ec);
  380. #endif // !defined(ASIO_NO_IOSTREAM)
  381. #endif // !defined(ASIO_NO_EXTENSIONS)
  382. /*@}*/
  383. /**
  384. * @defgroup async_read_at asio::async_read_at
  385. *
  386. * @brief The @c async_read_at function is a composed asynchronous operation
  387. * that reads a certain amount of data at the specified offset.
  388. */
  389. /*@{*/
  390. /// Start an asynchronous operation to read a certain amount of data at the
  391. /// specified offset.
  392. /**
  393. * This function is used to asynchronously read a certain number of bytes of
  394. * data from a random access device at the specified offset. It is an
  395. * initiating function for an @ref asynchronous_operation, and always returns
  396. * immediately. The asynchronous operation will continue until one of the
  397. * following conditions is true:
  398. *
  399. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  400. * the sum of the buffer sizes.
  401. *
  402. * @li An error occurred.
  403. *
  404. * This operation is implemented in terms of zero or more calls to the device's
  405. * async_read_some_at function.
  406. *
  407. * @param d The device from which the data is to be read. The type must support
  408. * the AsyncRandomAccessReadDevice concept.
  409. *
  410. * @param offset The offset at which the data will be read.
  411. *
  412. * @param buffers One or more buffers into which the data will be read. The sum
  413. * of the buffer sizes indicates the maximum number of bytes to read from the
  414. * device. Although the buffers object may be copied as necessary, ownership of
  415. * the underlying memory blocks is retained by the caller, which must guarantee
  416. * that they remain valid until the completion handler is called.
  417. *
  418. * @param token The @ref completion_token that will be used to produce a
  419. * completion handler, which will be called when the read completes.
  420. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  421. * @ref yield_context, or a function object with the correct completion
  422. * signature. The function signature of the completion handler must be:
  423. * @code void handler(
  424. * // Result of operation.
  425. * const asio::error_code& error,
  426. *
  427. * // Number of bytes copied into the buffers. If an error
  428. * // occurred, this will be the number of bytes successfully
  429. * // transferred prior to the error.
  430. * std::size_t bytes_transferred
  431. * ); @endcode
  432. * Regardless of whether the asynchronous operation completes immediately or
  433. * not, the completion handler will not be invoked from within this function.
  434. * On immediate completion, invocation of the handler will be performed in a
  435. * manner equivalent to using asio::post().
  436. *
  437. * @par Completion Signature
  438. * @code void(asio::error_code, std::size_t) @endcode
  439. *
  440. * @par Example
  441. * To read into a single data buffer use the @ref buffer function as follows:
  442. * @code
  443. * asio::async_read_at(d, 42, asio::buffer(data, size), handler);
  444. * @endcode
  445. * See the @ref buffer documentation for information on reading into multiple
  446. * buffers in one go, and how to use it with arrays, boost::array or
  447. * std::vector.
  448. *
  449. * @note This overload is equivalent to calling:
  450. * @code asio::async_read_at(
  451. * d, 42, buffers,
  452. * asio::transfer_all(),
  453. * handler); @endcode
  454. *
  455. * @par Per-Operation Cancellation
  456. * This asynchronous operation supports cancellation for the following
  457. * asio::cancellation_type values:
  458. *
  459. * @li @c cancellation_type::terminal
  460. *
  461. * @li @c cancellation_type::partial
  462. *
  463. * if they are also supported by the @c AsyncRandomAccessReadDevice type's
  464. * async_read_some_at operation.
  465. */
  466. template <typename AsyncRandomAccessReadDevice, typename MutableBufferSequence,
  467. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  468. std::size_t)) ReadToken = default_completion_token_t<
  469. typename AsyncRandomAccessReadDevice::executor_type>>
  470. auto async_read_at(AsyncRandomAccessReadDevice& d,
  471. uint64_t offset, const MutableBufferSequence& buffers,
  472. ReadToken&& token = default_completion_token_t<
  473. typename AsyncRandomAccessReadDevice::executor_type>())
  474. -> decltype(
  475. async_initiate<ReadToken,
  476. void (asio::error_code, std::size_t)>(
  477. declval<detail::initiate_async_read_at<AsyncRandomAccessReadDevice>>(),
  478. token, offset, buffers, transfer_all()));
  479. /// Start an asynchronous operation to read a certain amount of data at the
  480. /// specified offset.
  481. /**
  482. * This function is used to asynchronously read a certain number of bytes of
  483. * data from a random access device at the specified offset. It is an
  484. * initiating function for an @ref asynchronous_operation, and always returns
  485. * immediately. The asynchronous operation will continue until one of the
  486. * following conditions is true:
  487. *
  488. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  489. * the sum of the buffer sizes.
  490. *
  491. * @li The completion_condition function object returns 0.
  492. *
  493. * @param d The device from which the data is to be read. The type must support
  494. * the AsyncRandomAccessReadDevice concept.
  495. *
  496. * @param offset The offset at which the data will be read.
  497. *
  498. * @param buffers One or more buffers into which the data will be read. The sum
  499. * of the buffer sizes indicates the maximum number of bytes to read from the
  500. * device. Although the buffers object may be copied as necessary, ownership of
  501. * the underlying memory blocks is retained by the caller, which must guarantee
  502. * that they remain valid until the completion handler is called.
  503. *
  504. * @param completion_condition The function object to be called to determine
  505. * whether the read operation is complete. The signature of the function object
  506. * must be:
  507. * @code std::size_t completion_condition(
  508. * // Result of latest async_read_some_at operation.
  509. * const asio::error_code& error,
  510. *
  511. * // Number of bytes transferred so far.
  512. * std::size_t bytes_transferred
  513. * ); @endcode
  514. * A return value of 0 indicates that the read operation is complete. A non-zero
  515. * return value indicates the maximum number of bytes to be read on the next
  516. * call to the device's async_read_some_at function.
  517. *
  518. * @param token The @ref completion_token that will be used to produce a
  519. * completion handler, which will be called when the read completes.
  520. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  521. * @ref yield_context, or a function object with the correct completion
  522. * signature. The function signature of the completion handler must be:
  523. * @code void handler(
  524. * // Result of operation.
  525. * const asio::error_code& error,
  526. *
  527. * // Number of bytes copied into the buffers. If an error
  528. * // occurred, this will be the number of bytes successfully
  529. * // transferred prior to the error.
  530. * std::size_t bytes_transferred
  531. * ); @endcode
  532. * Regardless of whether the asynchronous operation completes immediately or
  533. * not, the completion handler will not be invoked from within this function.
  534. * On immediate completion, invocation of the handler will be performed in a
  535. * manner equivalent to using asio::post().
  536. *
  537. * @par Completion Signature
  538. * @code void(asio::error_code, std::size_t) @endcode
  539. *
  540. * @par Example
  541. * To read into a single data buffer use the @ref buffer function as follows:
  542. * @code asio::async_read_at(d, 42,
  543. * asio::buffer(data, size),
  544. * asio::transfer_at_least(32),
  545. * handler); @endcode
  546. * See the @ref buffer documentation for information on reading into multiple
  547. * buffers in one go, and how to use it with arrays, boost::array or
  548. * std::vector.
  549. *
  550. * @par Per-Operation Cancellation
  551. * This asynchronous operation supports cancellation for the following
  552. * asio::cancellation_type values:
  553. *
  554. * @li @c cancellation_type::terminal
  555. *
  556. * @li @c cancellation_type::partial
  557. *
  558. * if they are also supported by the @c AsyncRandomAccessReadDevice type's
  559. * async_read_some_at operation.
  560. */
  561. template <typename AsyncRandomAccessReadDevice,
  562. typename MutableBufferSequence, typename CompletionCondition,
  563. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  564. std::size_t)) ReadToken = default_completion_token_t<
  565. typename AsyncRandomAccessReadDevice::executor_type>>
  566. auto async_read_at(AsyncRandomAccessReadDevice& d,
  567. uint64_t offset, const MutableBufferSequence& buffers,
  568. CompletionCondition completion_condition,
  569. ReadToken&& token = default_completion_token_t<
  570. typename AsyncRandomAccessReadDevice::executor_type>())
  571. -> decltype(
  572. async_initiate<ReadToken,
  573. void (asio::error_code, std::size_t)>(
  574. declval<detail::initiate_async_read_at<AsyncRandomAccessReadDevice>>(),
  575. token, offset, buffers,
  576. static_cast<CompletionCondition&&>(completion_condition)));
  577. #if !defined(ASIO_NO_EXTENSIONS)
  578. #if !defined(ASIO_NO_IOSTREAM)
  579. /// Start an asynchronous operation to read a certain amount of data at the
  580. /// specified offset.
  581. /**
  582. * This function is used to asynchronously read a certain number of bytes of
  583. * data from a random access device at the specified offset. It is an
  584. * initiating function for an @ref asynchronous_operation, and always returns
  585. * immediately. The asynchronous operation will continue until one of the
  586. * following conditions is true:
  587. *
  588. * @li An error occurred.
  589. *
  590. * This operation is implemented in terms of zero or more calls to the device's
  591. * async_read_some_at function.
  592. *
  593. * @param d The device from which the data is to be read. The type must support
  594. * the AsyncRandomAccessReadDevice concept.
  595. *
  596. * @param offset The offset at which the data will be read.
  597. *
  598. * @param b A basic_streambuf object into which the data will be read. Ownership
  599. * of the streambuf is retained by the caller, which must guarantee that it
  600. * remains valid until the completion handler is called.
  601. *
  602. * @param token The @ref completion_token that will be used to produce a
  603. * completion handler, which will be called when the read completes.
  604. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  605. * @ref yield_context, or a function object with the correct completion
  606. * signature. The function signature of the completion handler must be:
  607. * @code void handler(
  608. * // Result of operation.
  609. * const asio::error_code& error,
  610. *
  611. * // Number of bytes copied into the buffers. If an error
  612. * // occurred, this will be the number of bytes successfully
  613. * // transferred prior to the error.
  614. * std::size_t bytes_transferred
  615. * ); @endcode
  616. * Regardless of whether the asynchronous operation completes immediately or
  617. * not, the completion handler will not be invoked from within this function.
  618. * On immediate completion, invocation of the handler will be performed in a
  619. * manner equivalent to using asio::post().
  620. *
  621. * @par Completion Signature
  622. * @code void(asio::error_code, std::size_t) @endcode
  623. *
  624. * @note This overload is equivalent to calling:
  625. * @code asio::async_read_at(
  626. * d, 42, b,
  627. * asio::transfer_all(),
  628. * handler); @endcode
  629. *
  630. * @par Per-Operation Cancellation
  631. * This asynchronous operation supports cancellation for the following
  632. * asio::cancellation_type values:
  633. *
  634. * @li @c cancellation_type::terminal
  635. *
  636. * @li @c cancellation_type::partial
  637. *
  638. * if they are also supported by the @c AsyncRandomAccessReadDevice type's
  639. * async_read_some_at operation.
  640. */
  641. template <typename AsyncRandomAccessReadDevice, typename Allocator,
  642. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  643. std::size_t)) ReadToken = default_completion_token_t<
  644. typename AsyncRandomAccessReadDevice::executor_type>>
  645. auto async_read_at(AsyncRandomAccessReadDevice& d,
  646. uint64_t offset, basic_streambuf<Allocator>& b,
  647. ReadToken&& token = default_completion_token_t<
  648. typename AsyncRandomAccessReadDevice::executor_type>())
  649. -> decltype(
  650. async_initiate<ReadToken,
  651. void (asio::error_code, std::size_t)>(
  652. declval<detail::initiate_async_read_at_streambuf<
  653. AsyncRandomAccessReadDevice>>(),
  654. token, offset, &b, transfer_all()));
  655. /// Start an asynchronous operation to read a certain amount of data at the
  656. /// specified offset.
  657. /**
  658. * This function is used to asynchronously read a certain number of bytes of
  659. * data from a random access device at the specified offset. It is an
  660. * initiating function for an @ref asynchronous_operation, and always returns
  661. * immediately. The asynchronous operation will continue until one of the
  662. * following conditions is true:
  663. *
  664. * @li The completion_condition function object returns 0.
  665. *
  666. * This operation is implemented in terms of zero or more calls to the device's
  667. * async_read_some_at function.
  668. *
  669. * @param d The device from which the data is to be read. The type must support
  670. * the AsyncRandomAccessReadDevice concept.
  671. *
  672. * @param offset The offset at which the data will be read.
  673. *
  674. * @param b A basic_streambuf object into which the data will be read. Ownership
  675. * of the streambuf is retained by the caller, which must guarantee that it
  676. * remains valid until the completion handler is called.
  677. *
  678. * @param completion_condition The function object to be called to determine
  679. * whether the read operation is complete. The signature of the function object
  680. * must be:
  681. * @code std::size_t completion_condition(
  682. * // Result of latest async_read_some_at operation.
  683. * const asio::error_code& error,
  684. *
  685. * // Number of bytes transferred so far.
  686. * std::size_t bytes_transferred
  687. * ); @endcode
  688. * A return value of 0 indicates that the read operation is complete. A non-zero
  689. * return value indicates the maximum number of bytes to be read on the next
  690. * call to the device's async_read_some_at function.
  691. *
  692. * @param token The @ref completion_token that will be used to produce a
  693. * completion handler, which will be called when the read completes.
  694. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  695. * @ref yield_context, or a function object with the correct completion
  696. * signature. The function signature of the completion handler must be:
  697. * @code void handler(
  698. * // Result of operation.
  699. * const asio::error_code& error,
  700. *
  701. * // Number of bytes copied into the buffers. If an error
  702. * // occurred, this will be the number of bytes successfully
  703. * // transferred prior to the error.
  704. * std::size_t bytes_transferred
  705. * ); @endcode
  706. * Regardless of whether the asynchronous operation completes immediately or
  707. * not, the completion handler will not be invoked from within this function.
  708. * On immediate completion, invocation of the handler will be performed in a
  709. * manner equivalent to using asio::post().
  710. *
  711. * @par Completion Signature
  712. * @code void(asio::error_code, std::size_t) @endcode
  713. *
  714. * @par Per-Operation Cancellation
  715. * This asynchronous operation supports cancellation for the following
  716. * asio::cancellation_type values:
  717. *
  718. * @li @c cancellation_type::terminal
  719. *
  720. * @li @c cancellation_type::partial
  721. *
  722. * if they are also supported by the @c AsyncRandomAccessReadDevice type's
  723. * async_read_some_at operation.
  724. */
  725. template <typename AsyncRandomAccessReadDevice,
  726. typename Allocator, typename CompletionCondition,
  727. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  728. std::size_t)) ReadToken = default_completion_token_t<
  729. typename AsyncRandomAccessReadDevice::executor_type>>
  730. auto async_read_at(AsyncRandomAccessReadDevice& d, uint64_t offset,
  731. basic_streambuf<Allocator>& b, CompletionCondition completion_condition,
  732. ReadToken&& token = default_completion_token_t<
  733. typename AsyncRandomAccessReadDevice::executor_type>())
  734. -> decltype(
  735. async_initiate<ReadToken,
  736. void (asio::error_code, std::size_t)>(
  737. declval<detail::initiate_async_read_at_streambuf<
  738. AsyncRandomAccessReadDevice>>(),
  739. token, offset, &b,
  740. static_cast<CompletionCondition&&>(completion_condition)));
  741. #endif // !defined(ASIO_NO_IOSTREAM)
  742. #endif // !defined(ASIO_NO_EXTENSIONS)
  743. /*@}*/
  744. } // namespace asio
  745. #include "asio/detail/pop_options.hpp"
  746. #include "asio/impl/read_at.hpp"
  747. #endif // ASIO_READ_AT_HPP