read_at.hpp 32 KB

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