read_at.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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 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. /// Attempt to read a certain amount of data at the specified offset before
  184. /// returning.
  185. /**
  186. * This function is used to read a certain number of bytes of data from a
  187. * random access device at the specified offset. The call will block until one
  188. * of the following conditions is true:
  189. *
  190. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  191. * the sum of the buffer sizes.
  192. *
  193. * @li The completion_condition function object returns 0.
  194. *
  195. * This operation is implemented in terms of zero or more calls to the device's
  196. * read_some_at function.
  197. *
  198. * @param d The device from which the data is to be read. The type must support
  199. * the SyncRandomAccessReadDevice concept.
  200. *
  201. * @param offset The offset at which the data will be read.
  202. *
  203. * @param buffers One or more buffers into which the data will be read. The sum
  204. * of the buffer sizes indicates the maximum number of bytes to read from the
  205. * device.
  206. *
  207. * @param completion_condition The function object to be called to determine
  208. * whether the read operation is complete. The signature of the function object
  209. * must be:
  210. * @code std::size_t completion_condition(
  211. * // Result of latest read_some_at operation.
  212. * const boost::system::error_code& error,
  213. *
  214. * // Number of bytes transferred so far.
  215. * std::size_t bytes_transferred
  216. * ); @endcode
  217. * A return value of 0 indicates that the read operation is complete. A non-zero
  218. * return value indicates the maximum number of bytes to be read on the next
  219. * call to the device's read_some_at function.
  220. *
  221. * @param ec Set to indicate what error occurred, if any.
  222. *
  223. * @returns The number of bytes read. If an error occurs, returns the total
  224. * number of bytes successfully transferred prior to the error.
  225. */
  226. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
  227. typename CompletionCondition>
  228. std::size_t read_at(SyncRandomAccessReadDevice& d,
  229. uint64_t offset, const MutableBufferSequence& buffers,
  230. CompletionCondition completion_condition, boost::system::error_code& ec);
  231. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  232. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  233. /// Attempt to read a certain amount of data at the specified offset before
  234. /// returning.
  235. /**
  236. * This function is used to read a certain number of bytes of data from a
  237. * random access device at the specified offset. The call will block until one
  238. * of the following conditions is true:
  239. *
  240. * @li An error occurred.
  241. *
  242. * This operation is implemented in terms of zero or more calls to the device's
  243. * read_some_at function.
  244. *
  245. * @param d The device from which the data is to be read. The type must support
  246. * the SyncRandomAccessReadDevice concept.
  247. *
  248. * @param offset The offset at which the data will be read.
  249. *
  250. * @param b The basic_streambuf object into which the data will be read.
  251. *
  252. * @returns The number of bytes transferred.
  253. *
  254. * @throws boost::system::system_error Thrown on failure.
  255. *
  256. * @note This overload is equivalent to calling:
  257. * @code boost::asio::read_at(
  258. * d, 42, b,
  259. * boost::asio::transfer_all()); @endcode
  260. */
  261. template <typename SyncRandomAccessReadDevice, typename Allocator>
  262. std::size_t read_at(SyncRandomAccessReadDevice& d,
  263. uint64_t offset, basic_streambuf<Allocator>& b);
  264. /// Attempt to read a certain amount of data at the specified offset before
  265. /// returning.
  266. /**
  267. * This function is used to read a certain number of bytes of data from a
  268. * random access device at the specified offset. The call will block until one
  269. * of the following conditions is true:
  270. *
  271. * @li An error occurred.
  272. *
  273. * This operation is implemented in terms of zero or more calls to the device's
  274. * read_some_at function.
  275. *
  276. * @param d The device from which the data is to be read. The type must support
  277. * the SyncRandomAccessReadDevice concept.
  278. *
  279. * @param offset The offset at which the data will be read.
  280. *
  281. * @param b The basic_streambuf object into which the data will be read.
  282. *
  283. * @param ec Set to indicate what error occurred, if any.
  284. *
  285. * @returns The number of bytes transferred.
  286. *
  287. * @note This overload is equivalent to calling:
  288. * @code boost::asio::read_at(
  289. * d, 42, b,
  290. * boost::asio::transfer_all(), ec); @endcode
  291. */
  292. template <typename SyncRandomAccessReadDevice, typename Allocator>
  293. std::size_t read_at(SyncRandomAccessReadDevice& d,
  294. uint64_t offset, basic_streambuf<Allocator>& b,
  295. boost::system::error_code& ec);
  296. /// Attempt to read a certain amount of data at the specified offset before
  297. /// returning.
  298. /**
  299. * This function is used to read a certain number of bytes of data from a
  300. * random access device at the specified offset. The call will block until one
  301. * of the following conditions is true:
  302. *
  303. * @li The completion_condition function object returns 0.
  304. *
  305. * This operation is implemented in terms of zero or more calls to the device's
  306. * read_some_at function.
  307. *
  308. * @param d The device from which the data is to be read. The type must support
  309. * the SyncRandomAccessReadDevice concept.
  310. *
  311. * @param offset The offset at which the data will be read.
  312. *
  313. * @param b The basic_streambuf object into which the data will be read.
  314. *
  315. * @param completion_condition The function object to be called to determine
  316. * whether the read operation is complete. The signature of the function object
  317. * must be:
  318. * @code std::size_t completion_condition(
  319. * // Result of latest read_some_at operation.
  320. * const boost::system::error_code& error,
  321. *
  322. * // Number of bytes transferred so far.
  323. * std::size_t bytes_transferred
  324. * ); @endcode
  325. * A return value of 0 indicates that the read operation is complete. A non-zero
  326. * return value indicates the maximum number of bytes to be read on the next
  327. * call to the device's read_some_at function.
  328. *
  329. * @returns The number of bytes transferred.
  330. *
  331. * @throws boost::system::system_error Thrown on failure.
  332. */
  333. template <typename SyncRandomAccessReadDevice, typename Allocator,
  334. typename CompletionCondition>
  335. std::size_t read_at(SyncRandomAccessReadDevice& d,
  336. uint64_t offset, basic_streambuf<Allocator>& b,
  337. CompletionCondition completion_condition);
  338. /// Attempt to read a certain amount of data at the specified offset before
  339. /// returning.
  340. /**
  341. * This function is used to read a certain number of bytes of data from a
  342. * random access device at the specified offset. The call will block until one
  343. * of the following conditions is true:
  344. *
  345. * @li The completion_condition function object returns 0.
  346. *
  347. * This operation is implemented in terms of zero or more calls to the device's
  348. * read_some_at function.
  349. *
  350. * @param d The device from which the data is to be read. The type must support
  351. * the SyncRandomAccessReadDevice concept.
  352. *
  353. * @param offset The offset at which the data will be read.
  354. *
  355. * @param b The basic_streambuf object into which the data will be read.
  356. *
  357. * @param completion_condition The function object to be called to determine
  358. * whether the read operation is complete. The signature of the function object
  359. * must be:
  360. * @code std::size_t completion_condition(
  361. * // Result of latest read_some_at operation.
  362. * const boost::system::error_code& error,
  363. *
  364. * // Number of bytes transferred so far.
  365. * std::size_t bytes_transferred
  366. * ); @endcode
  367. * A return value of 0 indicates that the read operation is complete. A non-zero
  368. * return value indicates the maximum number of bytes to be read on the next
  369. * call to the device's read_some_at function.
  370. *
  371. * @param ec Set to indicate what error occurred, if any.
  372. *
  373. * @returns The number of bytes read. If an error occurs, returns the total
  374. * number of bytes successfully transferred prior to the error.
  375. */
  376. template <typename SyncRandomAccessReadDevice, typename Allocator,
  377. typename CompletionCondition>
  378. std::size_t read_at(SyncRandomAccessReadDevice& d,
  379. uint64_t offset, basic_streambuf<Allocator>& b,
  380. CompletionCondition completion_condition, boost::system::error_code& ec);
  381. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  382. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  383. /*@}*/
  384. /**
  385. * @defgroup async_read_at boost::asio::async_read_at
  386. *
  387. * @brief The @c async_read_at function is a composed asynchronous operation
  388. * that reads a certain amount of data at the specified offset.
  389. */
  390. /*@{*/
  391. /// Start an asynchronous operation to read a certain amount of data at the
  392. /// specified offset.
  393. /**
  394. * This function is used to asynchronously read a certain number of bytes of
  395. * data from a random access device at the specified offset. It is an
  396. * initiating function for an @ref asynchronous_operation, and always returns
  397. * immediately. The asynchronous operation will continue until one of the
  398. * following conditions is true:
  399. *
  400. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  401. * the sum of the buffer sizes.
  402. *
  403. * @li An error occurred.
  404. *
  405. * This operation is implemented in terms of zero or more calls to the device's
  406. * async_read_some_at function.
  407. *
  408. * @param d The device from which the data is to be read. The type must support
  409. * the AsyncRandomAccessReadDevice concept.
  410. *
  411. * @param offset The offset at which the data will be read.
  412. *
  413. * @param buffers One or more buffers into which the data will be read. The sum
  414. * of the buffer sizes indicates the maximum number of bytes to read from the
  415. * device. Although the buffers object may be copied as necessary, ownership of
  416. * the underlying memory blocks is retained by the caller, which must guarantee
  417. * that they remain valid until the completion handler is called.
  418. *
  419. * @param token The @ref completion_token that will be used to produce a
  420. * completion handler, which will be called when the read completes.
  421. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  422. * @ref yield_context, or a function object with the correct completion
  423. * signature. The function signature of the completion handler must be:
  424. * @code void handler(
  425. * // Result of operation.
  426. * const boost::system::error_code& error,
  427. *
  428. * // Number of bytes copied into the buffers. If an error
  429. * // occurred, this will be the number of bytes successfully
  430. * // transferred prior to the error.
  431. * std::size_t bytes_transferred
  432. * ); @endcode
  433. * Regardless of whether the asynchronous operation completes immediately or
  434. * not, the completion handler will not be invoked from within this function.
  435. * On immediate completion, invocation of the handler will be performed in a
  436. * manner equivalent to using boost::asio::post().
  437. *
  438. * @par Completion Signature
  439. * @code void(boost::system::error_code, std::size_t) @endcode
  440. *
  441. * @par Example
  442. * To read into a single data buffer use the @ref buffer function as follows:
  443. * @code
  444. * boost::asio::async_read_at(d, 42, boost::asio::buffer(data, size), handler);
  445. * @endcode
  446. * See the @ref buffer documentation for information on reading into multiple
  447. * buffers in one go, and how to use it with arrays, boost::array or
  448. * std::vector.
  449. *
  450. * @note This overload is equivalent to calling:
  451. * @code boost::asio::async_read_at(
  452. * d, 42, buffers,
  453. * boost::asio::transfer_all(),
  454. * handler); @endcode
  455. *
  456. * @par Per-Operation Cancellation
  457. * This asynchronous operation supports cancellation for the following
  458. * boost::asio::cancellation_type values:
  459. *
  460. * @li @c cancellation_type::terminal
  461. *
  462. * @li @c cancellation_type::partial
  463. *
  464. * if they are also supported by the @c AsyncRandomAccessReadDevice type's
  465. * async_read_some_at operation.
  466. */
  467. template <typename AsyncRandomAccessReadDevice, typename MutableBufferSequence,
  468. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  469. std::size_t)) ReadToken
  470. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  471. typename AsyncRandomAccessReadDevice::executor_type)>
  472. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(ReadToken,
  473. void (boost::system::error_code, std::size_t))
  474. async_read_at(AsyncRandomAccessReadDevice& d, uint64_t offset,
  475. const MutableBufferSequence& buffers,
  476. BOOST_ASIO_MOVE_ARG(ReadToken) token
  477. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  478. typename AsyncRandomAccessReadDevice::executor_type))
  479. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  480. async_initiate<ReadToken,
  481. void (boost::system::error_code, std::size_t)>(
  482. declval<detail::initiate_async_read_at<AsyncRandomAccessReadDevice> >(),
  483. token, offset, buffers, transfer_all())));
  484. /// Start an asynchronous operation to read a certain amount of data at the
  485. /// specified offset.
  486. /**
  487. * This function is used to asynchronously read a certain number of bytes of
  488. * data from a random access device at the specified offset. It is an
  489. * initiating function for an @ref asynchronous_operation, and always returns
  490. * immediately. The asynchronous operation will continue until one of the
  491. * following conditions is true:
  492. *
  493. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  494. * the sum of the buffer sizes.
  495. *
  496. * @li The completion_condition function object returns 0.
  497. *
  498. * @param d The device from which the data is to be read. The type must support
  499. * the AsyncRandomAccessReadDevice concept.
  500. *
  501. * @param offset The offset at which the data will be read.
  502. *
  503. * @param buffers One or more buffers into which the data will be read. The sum
  504. * of the buffer sizes indicates the maximum number of bytes to read from the
  505. * device. Although the buffers object may be copied as necessary, ownership of
  506. * the underlying memory blocks is retained by the caller, which must guarantee
  507. * that they remain valid until the completion handler is called.
  508. *
  509. * @param completion_condition The function object to be called to determine
  510. * whether the read operation is complete. The signature of the function object
  511. * must be:
  512. * @code std::size_t completion_condition(
  513. * // Result of latest async_read_some_at operation.
  514. * const boost::system::error_code& error,
  515. *
  516. * // Number of bytes transferred so far.
  517. * std::size_t bytes_transferred
  518. * ); @endcode
  519. * A return value of 0 indicates that the read operation is complete. A non-zero
  520. * return value indicates the maximum number of bytes to be read on the next
  521. * call to the device's async_read_some_at function.
  522. *
  523. * @param token The @ref completion_token that will be used to produce a
  524. * completion handler, which will be called when the read completes.
  525. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  526. * @ref yield_context, or a function object with the correct completion
  527. * signature. The function signature of the completion handler must be:
  528. * @code void handler(
  529. * // Result of operation.
  530. * const boost::system::error_code& error,
  531. *
  532. * // Number of bytes copied into the buffers. If an error
  533. * // occurred, this will be the number of bytes successfully
  534. * // transferred prior to the error.
  535. * std::size_t bytes_transferred
  536. * ); @endcode
  537. * Regardless of whether the asynchronous operation completes immediately or
  538. * not, the completion handler will not be invoked from within this function.
  539. * On immediate completion, invocation of the handler will be performed in a
  540. * manner equivalent to using boost::asio::post().
  541. *
  542. * @par Completion Signature
  543. * @code void(boost::system::error_code, std::size_t) @endcode
  544. *
  545. * @par Example
  546. * To read into a single data buffer use the @ref buffer function as follows:
  547. * @code boost::asio::async_read_at(d, 42,
  548. * boost::asio::buffer(data, size),
  549. * boost::asio::transfer_at_least(32),
  550. * handler); @endcode
  551. * See the @ref buffer documentation for information on reading into multiple
  552. * buffers in one go, and how to use it with arrays, boost::array or
  553. * std::vector.
  554. *
  555. * @par Per-Operation Cancellation
  556. * This asynchronous operation supports cancellation for the following
  557. * boost::asio::cancellation_type values:
  558. *
  559. * @li @c cancellation_type::terminal
  560. *
  561. * @li @c cancellation_type::partial
  562. *
  563. * if they are also supported by the @c AsyncRandomAccessReadDevice type's
  564. * async_read_some_at operation.
  565. */
  566. template <typename AsyncRandomAccessReadDevice,
  567. typename MutableBufferSequence, typename CompletionCondition,
  568. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  569. std::size_t)) ReadToken
  570. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  571. typename AsyncRandomAccessReadDevice::executor_type)>
  572. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(ReadToken,
  573. void (boost::system::error_code, std::size_t))
  574. async_read_at(AsyncRandomAccessReadDevice& d,
  575. uint64_t offset, const MutableBufferSequence& buffers,
  576. CompletionCondition completion_condition,
  577. BOOST_ASIO_MOVE_ARG(ReadToken) token
  578. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  579. typename AsyncRandomAccessReadDevice::executor_type))
  580. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  581. async_initiate<ReadToken,
  582. void (boost::system::error_code, std::size_t)>(
  583. declval<detail::initiate_async_read_at<AsyncRandomAccessReadDevice> >(),
  584. token, offset, buffers,
  585. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition))));
  586. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  587. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  588. /// Start an asynchronous operation to read a certain amount of data at the
  589. /// specified offset.
  590. /**
  591. * This function is used to asynchronously read a certain number of bytes of
  592. * data from a random access device at the specified offset. It is an
  593. * initiating function for an @ref asynchronous_operation, and always returns
  594. * immediately. The asynchronous operation will continue until one of the
  595. * following conditions is true:
  596. *
  597. * @li An error occurred.
  598. *
  599. * This operation is implemented in terms of zero or more calls to the device's
  600. * async_read_some_at function.
  601. *
  602. * @param d The device from which the data is to be read. The type must support
  603. * the AsyncRandomAccessReadDevice concept.
  604. *
  605. * @param offset The offset at which the data will be read.
  606. *
  607. * @param b A basic_streambuf object into which the data will be read. Ownership
  608. * of the streambuf is retained by the caller, which must guarantee that it
  609. * remains valid until the completion handler is called.
  610. *
  611. * @param token The @ref completion_token that will be used to produce a
  612. * completion handler, which will be called when the read completes.
  613. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  614. * @ref yield_context, or a function object with the correct completion
  615. * signature. The function signature of the completion handler must be:
  616. * @code void handler(
  617. * // Result of operation.
  618. * const boost::system::error_code& error,
  619. *
  620. * // Number of bytes copied into the buffers. If an error
  621. * // occurred, this will be the number of bytes successfully
  622. * // transferred prior to the error.
  623. * std::size_t bytes_transferred
  624. * ); @endcode
  625. * Regardless of whether the asynchronous operation completes immediately or
  626. * not, the completion handler will not be invoked from within this function.
  627. * On immediate completion, invocation of the handler will be performed in a
  628. * manner equivalent to using boost::asio::post().
  629. *
  630. * @par Completion Signature
  631. * @code void(boost::system::error_code, std::size_t) @endcode
  632. *
  633. * @note This overload is equivalent to calling:
  634. * @code boost::asio::async_read_at(
  635. * d, 42, b,
  636. * boost::asio::transfer_all(),
  637. * handler); @endcode
  638. *
  639. * @par Per-Operation Cancellation
  640. * This asynchronous operation supports cancellation for the following
  641. * boost::asio::cancellation_type values:
  642. *
  643. * @li @c cancellation_type::terminal
  644. *
  645. * @li @c cancellation_type::partial
  646. *
  647. * if they are also supported by the @c AsyncRandomAccessReadDevice type's
  648. * async_read_some_at operation.
  649. */
  650. template <typename AsyncRandomAccessReadDevice, typename Allocator,
  651. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  652. std::size_t)) ReadToken
  653. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  654. typename AsyncRandomAccessReadDevice::executor_type)>
  655. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(ReadToken,
  656. void (boost::system::error_code, std::size_t))
  657. async_read_at(AsyncRandomAccessReadDevice& d,
  658. uint64_t offset, basic_streambuf<Allocator>& b,
  659. BOOST_ASIO_MOVE_ARG(ReadToken) token
  660. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  661. typename AsyncRandomAccessReadDevice::executor_type))
  662. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  663. async_initiate<ReadToken,
  664. void (boost::system::error_code, std::size_t)>(
  665. declval<detail::initiate_async_read_at_streambuf<
  666. AsyncRandomAccessReadDevice> >(),
  667. token, offset, &b, transfer_all())));
  668. /// Start an asynchronous operation to read a certain amount of data at the
  669. /// specified offset.
  670. /**
  671. * This function is used to asynchronously read a certain number of bytes of
  672. * data from a random access device at the specified offset. It is an
  673. * initiating function for an @ref asynchronous_operation, and always returns
  674. * immediately. The asynchronous operation will continue until one of the
  675. * following conditions is true:
  676. *
  677. * @li The completion_condition function object returns 0.
  678. *
  679. * This operation is implemented in terms of zero or more calls to the device's
  680. * async_read_some_at function.
  681. *
  682. * @param d The device from which the data is to be read. The type must support
  683. * the AsyncRandomAccessReadDevice concept.
  684. *
  685. * @param offset The offset at which the data will be read.
  686. *
  687. * @param b A basic_streambuf object into which the data will be read. Ownership
  688. * of the streambuf is retained by the caller, which must guarantee that it
  689. * remains valid until the completion handler is called.
  690. *
  691. * @param completion_condition The function object to be called to determine
  692. * whether the read operation is complete. The signature of the function object
  693. * must be:
  694. * @code std::size_t completion_condition(
  695. * // Result of latest async_read_some_at operation.
  696. * const boost::system::error_code& error,
  697. *
  698. * // Number of bytes transferred so far.
  699. * std::size_t bytes_transferred
  700. * ); @endcode
  701. * A return value of 0 indicates that the read operation is complete. A non-zero
  702. * return value indicates the maximum number of bytes to be read on the next
  703. * call to the device's async_read_some_at function.
  704. *
  705. * @param token The @ref completion_token that will be used to produce a
  706. * completion handler, which will be called when the read completes.
  707. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  708. * @ref yield_context, or a function object with the correct completion
  709. * signature. The function signature of the completion handler must be:
  710. * @code void handler(
  711. * // Result of operation.
  712. * const boost::system::error_code& error,
  713. *
  714. * // Number of bytes copied into the buffers. If an error
  715. * // occurred, this will be the number of bytes successfully
  716. * // transferred prior to the error.
  717. * std::size_t bytes_transferred
  718. * ); @endcode
  719. * Regardless of whether the asynchronous operation completes immediately or
  720. * not, the completion handler will not be invoked from within this function.
  721. * On immediate completion, invocation of the handler will be performed in a
  722. * manner equivalent to using boost::asio::post().
  723. *
  724. * @par Completion Signature
  725. * @code void(boost::system::error_code, std::size_t) @endcode
  726. *
  727. * @par Per-Operation Cancellation
  728. * This asynchronous operation supports cancellation for the following
  729. * boost::asio::cancellation_type values:
  730. *
  731. * @li @c cancellation_type::terminal
  732. *
  733. * @li @c cancellation_type::partial
  734. *
  735. * if they are also supported by the @c AsyncRandomAccessReadDevice type's
  736. * async_read_some_at operation.
  737. */
  738. template <typename AsyncRandomAccessReadDevice,
  739. typename Allocator, typename CompletionCondition,
  740. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  741. std::size_t)) ReadToken
  742. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  743. typename AsyncRandomAccessReadDevice::executor_type)>
  744. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(ReadToken,
  745. void (boost::system::error_code, std::size_t))
  746. async_read_at(AsyncRandomAccessReadDevice& d,
  747. uint64_t offset, basic_streambuf<Allocator>& b,
  748. CompletionCondition completion_condition,
  749. BOOST_ASIO_MOVE_ARG(ReadToken) token
  750. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  751. typename AsyncRandomAccessReadDevice::executor_type))
  752. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  753. async_initiate<ReadToken,
  754. void (boost::system::error_code, std::size_t)>(
  755. declval<detail::initiate_async_read_at_streambuf<
  756. AsyncRandomAccessReadDevice> >(),
  757. token, offset, &b,
  758. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition))));
  759. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  760. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  761. /*@}*/
  762. } // namespace asio
  763. } // namespace boost
  764. #include <boost/asio/detail/pop_options.hpp>
  765. #include <boost/asio/impl/read_at.hpp>
  766. #endif // BOOST_ASIO_READ_AT_HPP