read_at.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. //
  2. // read_at.hpp
  3. // ~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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 <boost/cstdint.hpp>
  18. #include "asio/basic_streambuf_fwd.hpp"
  19. #include "asio/error.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. /**
  23. * @defgroup read_at asio::read_at
  24. *
  25. * @brief Attempt to read a certain amount of data at the specified offset
  26. * before returning.
  27. */
  28. /*@{*/
  29. /// Attempt to read a certain amount of data at the specified offset before
  30. /// returning.
  31. /**
  32. * This function is used to read a certain number of bytes of data from a
  33. * random access device at the specified offset. The call will block until one
  34. * of the following conditions is true:
  35. *
  36. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  37. * the sum of the buffer sizes.
  38. *
  39. * @li An error occurred.
  40. *
  41. * This operation is implemented in terms of zero or more calls to the device's
  42. * read_some_at function.
  43. *
  44. * @param d The device from which the data is to be read. The type must support
  45. * the SyncRandomAccessReadDevice concept.
  46. *
  47. * @param offset The offset at which the data will be read.
  48. *
  49. * @param buffers One or more buffers into which the data will be read. The sum
  50. * of the buffer sizes indicates the maximum number of bytes to read from the
  51. * device.
  52. *
  53. * @returns The number of bytes transferred.
  54. *
  55. * @throws asio::system_error Thrown on failure.
  56. *
  57. * @par Example
  58. * To read into a single data buffer use the @ref buffer function as follows:
  59. * @code asio::read_at(d, 42, asio::buffer(data, size)); @endcode
  60. * See the @ref buffer documentation for information on reading into multiple
  61. * buffers in one go, and how to use it with arrays, boost::array or
  62. * std::vector.
  63. *
  64. * @note This overload is equivalent to calling:
  65. * @code asio::read_at(
  66. * d, 42, buffers,
  67. * asio::transfer_all()); @endcode
  68. */
  69. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence>
  70. std::size_t read_at(SyncRandomAccessReadDevice& d,
  71. boost::uint64_t offset, const MutableBufferSequence& buffers);
  72. /// Attempt to read a certain amount of data at the specified offset before
  73. /// returning.
  74. /**
  75. * This function is used to read a certain number of bytes of data from a
  76. * random access device at the specified offset. The call will block until one
  77. * of the following conditions is true:
  78. *
  79. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  80. * the sum of the buffer sizes.
  81. *
  82. * @li The completion_condition function object returns 0.
  83. *
  84. * This operation is implemented in terms of zero or more calls to the device's
  85. * read_some_at function.
  86. *
  87. * @param d The device from which the data is to be read. The type must support
  88. * the SyncRandomAccessReadDevice concept.
  89. *
  90. * @param offset The offset at which the data will be read.
  91. *
  92. * @param buffers One or more buffers into which the data will be read. The sum
  93. * of the buffer sizes indicates the maximum number of bytes to read from the
  94. * device.
  95. *
  96. * @param completion_condition The function object to be called to determine
  97. * whether the read operation is complete. The signature of the function object
  98. * must be:
  99. * @code std::size_t completion_condition(
  100. * // Result of latest read_some_at operation.
  101. * const asio::error_code& error,
  102. *
  103. * // Number of bytes transferred so far.
  104. * std::size_t bytes_transferred
  105. * ); @endcode
  106. * A return value of 0 indicates that the read operation is complete. A non-zero
  107. * return value indicates the maximum number of bytes to be read on the next
  108. * call to the device's read_some_at function.
  109. *
  110. * @returns The number of bytes transferred.
  111. *
  112. * @throws asio::system_error Thrown on failure.
  113. *
  114. * @par Example
  115. * To read into a single data buffer use the @ref buffer function as follows:
  116. * @code asio::read_at(d, 42, asio::buffer(data, size),
  117. * asio::transfer_at_least(32)); @endcode
  118. * See the @ref buffer documentation for information on reading into multiple
  119. * buffers in one go, and how to use it with arrays, boost::array or
  120. * std::vector.
  121. */
  122. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
  123. typename CompletionCondition>
  124. std::size_t read_at(SyncRandomAccessReadDevice& d,
  125. boost::uint64_t offset, const MutableBufferSequence& buffers,
  126. CompletionCondition completion_condition);
  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. * @param ec Set to indicate what error occurred, if any.
  166. *
  167. * @returns The number of bytes read. If an error occurs, returns the total
  168. * number of bytes successfully transferred prior to the error.
  169. */
  170. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
  171. typename CompletionCondition>
  172. std::size_t read_at(SyncRandomAccessReadDevice& d,
  173. boost::uint64_t offset, const MutableBufferSequence& buffers,
  174. CompletionCondition completion_condition, asio::error_code& ec);
  175. #if !defined(BOOST_NO_IOSTREAM)
  176. /// Attempt to read a certain amount of data at the specified offset before
  177. /// returning.
  178. /**
  179. * This function is used to read a certain number of bytes of data from a
  180. * random access device at the specified offset. The call will block until one
  181. * of the following conditions is true:
  182. *
  183. * @li An error occurred.
  184. *
  185. * This operation is implemented in terms of zero or more calls to the device's
  186. * read_some_at function.
  187. *
  188. * @param d The device from which the data is to be read. The type must support
  189. * the SyncRandomAccessReadDevice concept.
  190. *
  191. * @param offset The offset at which the data will be read.
  192. *
  193. * @param b The basic_streambuf object into which the data will be read.
  194. *
  195. * @returns The number of bytes transferred.
  196. *
  197. * @throws asio::system_error Thrown on failure.
  198. *
  199. * @note This overload is equivalent to calling:
  200. * @code asio::read_at(
  201. * d, 42, b,
  202. * asio::transfer_all()); @endcode
  203. */
  204. template <typename SyncRandomAccessReadDevice, typename Allocator>
  205. std::size_t read_at(SyncRandomAccessReadDevice& d,
  206. boost::uint64_t offset, basic_streambuf<Allocator>& b);
  207. /// Attempt to read a certain amount of data at the specified offset before
  208. /// returning.
  209. /**
  210. * This function is used to read a certain number of bytes of data from a
  211. * random access device at the specified offset. The call will block until one
  212. * of the following conditions is true:
  213. *
  214. * @li The completion_condition function object returns 0.
  215. *
  216. * This operation is implemented in terms of zero or more calls to the device's
  217. * read_some_at function.
  218. *
  219. * @param d The device from which the data is to be read. The type must support
  220. * the SyncRandomAccessReadDevice concept.
  221. *
  222. * @param offset The offset at which the data will be read.
  223. *
  224. * @param b The basic_streambuf object into which the data will be read.
  225. *
  226. * @param completion_condition The function object to be called to determine
  227. * whether the read operation is complete. The signature of the function object
  228. * must be:
  229. * @code std::size_t completion_condition(
  230. * // Result of latest read_some_at operation.
  231. * const asio::error_code& error,
  232. *
  233. * // Number of bytes transferred so far.
  234. * std::size_t bytes_transferred
  235. * ); @endcode
  236. * A return value of 0 indicates that the read operation is complete. A non-zero
  237. * return value indicates the maximum number of bytes to be read on the next
  238. * call to the device's read_some_at function.
  239. *
  240. * @returns The number of bytes transferred.
  241. *
  242. * @throws asio::system_error Thrown on failure.
  243. */
  244. template <typename SyncRandomAccessReadDevice, typename Allocator,
  245. typename CompletionCondition>
  246. std::size_t read_at(SyncRandomAccessReadDevice& d,
  247. boost::uint64_t offset, basic_streambuf<Allocator>& b,
  248. CompletionCondition completion_condition);
  249. /// Attempt to read a certain amount of data at the specified offset before
  250. /// returning.
  251. /**
  252. * This function is used to read a certain number of bytes of data from a
  253. * random access device at the specified offset. The call will block until one
  254. * of the following conditions is true:
  255. *
  256. * @li The completion_condition function object returns 0.
  257. *
  258. * This operation is implemented in terms of zero or more calls to the device's
  259. * read_some_at function.
  260. *
  261. * @param d The device from which the data is to be read. The type must support
  262. * the SyncRandomAccessReadDevice concept.
  263. *
  264. * @param offset The offset at which the data will be read.
  265. *
  266. * @param b The basic_streambuf object into which the data will be read.
  267. *
  268. * @param completion_condition The function object to be called to determine
  269. * whether the read operation is complete. The signature of the function object
  270. * must be:
  271. * @code std::size_t completion_condition(
  272. * // Result of latest read_some_at operation.
  273. * const asio::error_code& error,
  274. *
  275. * // Number of bytes transferred so far.
  276. * std::size_t bytes_transferred
  277. * ); @endcode
  278. * A return value of 0 indicates that the read operation is complete. A non-zero
  279. * return value indicates the maximum number of bytes to be read on the next
  280. * call to the device's read_some_at function.
  281. *
  282. * @param ec Set to indicate what error occurred, if any.
  283. *
  284. * @returns The number of bytes read. If an error occurs, returns the total
  285. * number of bytes successfully transferred prior to the error.
  286. */
  287. template <typename SyncRandomAccessReadDevice, typename Allocator,
  288. typename CompletionCondition>
  289. std::size_t read_at(SyncRandomAccessReadDevice& d,
  290. boost::uint64_t offset, basic_streambuf<Allocator>& b,
  291. CompletionCondition completion_condition, asio::error_code& ec);
  292. #endif // !defined(BOOST_NO_IOSTREAM)
  293. /*@}*/
  294. /**
  295. * @defgroup async_read_at asio::async_read_at
  296. *
  297. * @brief Start an asynchronous operation to read a certain amount of data at
  298. * the specified offset.
  299. */
  300. /*@{*/
  301. /// Start an asynchronous operation to read a certain amount of data at the
  302. /// specified offset.
  303. /**
  304. * This function is used to asynchronously read a certain number of bytes of
  305. * data from a random access device at the specified offset. The function call
  306. * always returns immediately. The asynchronous operation will continue until
  307. * one of the following conditions is true:
  308. *
  309. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  310. * the sum of the buffer sizes.
  311. *
  312. * @li An error occurred.
  313. *
  314. * This operation is implemented in terms of zero or more calls to the device's
  315. * async_read_some_at function.
  316. *
  317. * @param d The device from which the data is to be read. The type must support
  318. * the AsyncRandomAccessReadDevice concept.
  319. *
  320. * @param offset The offset at which the data will be read.
  321. *
  322. * @param buffers One or more buffers into which the data will be read. The sum
  323. * of the buffer sizes indicates the maximum number of bytes to read from the
  324. * device. Although the buffers object may be copied as necessary, ownership of
  325. * the underlying memory blocks is retained by the caller, which must guarantee
  326. * that they remain valid until the handler is called.
  327. *
  328. * @param handler The handler to be called when the read operation completes.
  329. * Copies will be made of the handler as required. The function signature of the
  330. * handler must be:
  331. * @code void handler(
  332. * // Result of operation.
  333. * const asio::error_code& error,
  334. *
  335. * // Number of bytes copied into the buffers. If an error
  336. * // occurred, this will be the number of bytes successfully
  337. * // transferred prior to the error.
  338. * std::size_t bytes_transferred
  339. * ); @endcode
  340. * Regardless of whether the asynchronous operation completes immediately or
  341. * not, the handler will not be invoked from within this function. Invocation of
  342. * the handler will be performed in a manner equivalent to using
  343. * asio::io_service::post().
  344. *
  345. * @par Example
  346. * To read into a single data buffer use the @ref buffer function as follows:
  347. * @code
  348. * asio::async_read_at(d, 42, asio::buffer(data, size), handler);
  349. * @endcode
  350. * See the @ref buffer documentation for information on reading into multiple
  351. * buffers in one go, and how to use it with arrays, boost::array or
  352. * std::vector.
  353. *
  354. * @note This overload is equivalent to calling:
  355. * @code asio::async_read_at(
  356. * d, 42, buffers,
  357. * asio::transfer_all(),
  358. * handler); @endcode
  359. */
  360. template <typename AsyncRandomAccessReadDevice, typename MutableBufferSequence,
  361. typename ReadHandler>
  362. void async_read_at(AsyncRandomAccessReadDevice& d, boost::uint64_t offset,
  363. const MutableBufferSequence& buffers, ReadHandler handler);
  364. /// Start an asynchronous operation to read a certain amount of data at the
  365. /// specified offset.
  366. /**
  367. * This function is used to asynchronously read a certain number of bytes of
  368. * data from a random access device at the specified offset. The function call
  369. * always returns immediately. The asynchronous operation will continue until
  370. * one of the following conditions is true:
  371. *
  372. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  373. * the sum of the buffer sizes.
  374. *
  375. * @li The completion_condition function object returns 0.
  376. *
  377. * @param d The device from which the data is to be read. The type must support
  378. * the AsyncRandomAccessReadDevice concept.
  379. *
  380. * @param offset The offset at which the data will be read.
  381. *
  382. * @param buffers One or more buffers into which the data will be read. The sum
  383. * of the buffer sizes indicates the maximum number of bytes to read from the
  384. * device. Although the buffers object may be copied as necessary, ownership of
  385. * the underlying memory blocks is retained by the caller, which must guarantee
  386. * that they remain valid until the handler is called.
  387. *
  388. * @param completion_condition The function object to be called to determine
  389. * whether the read operation is complete. The signature of the function object
  390. * must be:
  391. * @code std::size_t completion_condition(
  392. * // Result of latest async_read_some_at operation.
  393. * const asio::error_code& error,
  394. *
  395. * // Number of bytes transferred so far.
  396. * std::size_t bytes_transferred
  397. * ); @endcode
  398. * A return value of 0 indicates that the read operation is complete. A non-zero
  399. * return value indicates the maximum number of bytes to be read on the next
  400. * call to the device's async_read_some_at function.
  401. *
  402. * @param handler The handler to be called when the read operation completes.
  403. * Copies will be made of the handler as required. The function signature of the
  404. * handler must be:
  405. * @code void handler(
  406. * // Result of operation.
  407. * const asio::error_code& error,
  408. *
  409. * // Number of bytes copied into the buffers. If an error
  410. * // occurred, this will be the number of bytes successfully
  411. * // transferred prior to the error.
  412. * std::size_t bytes_transferred
  413. * ); @endcode
  414. * Regardless of whether the asynchronous operation completes immediately or
  415. * not, the handler will not be invoked from within this function. Invocation of
  416. * the handler will be performed in a manner equivalent to using
  417. * asio::io_service::post().
  418. *
  419. * @par Example
  420. * To read into a single data buffer use the @ref buffer function as follows:
  421. * @code asio::async_read_at(d, 42,
  422. * asio::buffer(data, size),
  423. * asio::transfer_at_least(32),
  424. * handler); @endcode
  425. * See the @ref buffer documentation for information on reading into multiple
  426. * buffers in one go, and how to use it with arrays, boost::array or
  427. * std::vector.
  428. */
  429. template <typename AsyncRandomAccessReadDevice, typename MutableBufferSequence,
  430. typename CompletionCondition, typename ReadHandler>
  431. void async_read_at(AsyncRandomAccessReadDevice& d,
  432. boost::uint64_t offset, const MutableBufferSequence& buffers,
  433. CompletionCondition completion_condition, ReadHandler handler);
  434. #if !defined(BOOST_NO_IOSTREAM)
  435. /// Start an asynchronous operation to read a certain amount of data at the
  436. /// specified offset.
  437. /**
  438. * This function is used to asynchronously read a certain number of bytes of
  439. * data from a random access device at the specified offset. The function call
  440. * always returns immediately. The asynchronous operation will continue until
  441. * one of the following conditions is true:
  442. *
  443. * @li An error occurred.
  444. *
  445. * This operation is implemented in terms of zero or more calls to the device's
  446. * async_read_some_at function.
  447. *
  448. * @param d The device from which the data is to be read. The type must support
  449. * the AsyncRandomAccessReadDevice concept.
  450. *
  451. * @param offset The offset at which the data will be read.
  452. *
  453. * @param b A basic_streambuf object into which the data will be read. Ownership
  454. * of the streambuf is retained by the caller, which must guarantee that it
  455. * remains valid until the handler is called.
  456. *
  457. * @param handler The handler to be called when the read operation completes.
  458. * Copies will be made of the handler as required. The function signature of the
  459. * handler must be:
  460. * @code void handler(
  461. * // Result of operation.
  462. * const asio::error_code& error,
  463. *
  464. * // Number of bytes copied into the buffers. If an error
  465. * // occurred, this will be the number of bytes successfully
  466. * // transferred prior to the error.
  467. * std::size_t bytes_transferred
  468. * ); @endcode
  469. * Regardless of whether the asynchronous operation completes immediately or
  470. * not, the handler will not be invoked from within this function. Invocation of
  471. * the handler will be performed in a manner equivalent to using
  472. * asio::io_service::post().
  473. *
  474. * @note This overload is equivalent to calling:
  475. * @code asio::async_read_at(
  476. * d, 42, b,
  477. * asio::transfer_all(),
  478. * handler); @endcode
  479. */
  480. template <typename AsyncRandomAccessReadDevice, typename Allocator,
  481. typename ReadHandler>
  482. void async_read_at(AsyncRandomAccessReadDevice& d, boost::uint64_t offset,
  483. basic_streambuf<Allocator>& b, ReadHandler handler);
  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. The function call
  489. * always returns immediately. The asynchronous operation will continue until
  490. * one of the following conditions is true:
  491. *
  492. * @li The completion_condition function object returns 0.
  493. *
  494. * This operation is implemented in terms of zero or more calls to the device's
  495. * async_read_some_at function.
  496. *
  497. * @param d The device from which the data is to be read. The type must support
  498. * the AsyncRandomAccessReadDevice concept.
  499. *
  500. * @param offset The offset at which the data will be read.
  501. *
  502. * @param b A basic_streambuf object into which the data will be read. Ownership
  503. * of the streambuf is retained by the caller, which must guarantee that it
  504. * remains valid until the handler is called.
  505. *
  506. * @param completion_condition The function object to be called to determine
  507. * whether the read operation is complete. The signature of the function object
  508. * must be:
  509. * @code std::size_t completion_condition(
  510. * // Result of latest async_read_some_at operation.
  511. * const asio::error_code& error,
  512. *
  513. * // Number of bytes transferred so far.
  514. * std::size_t bytes_transferred
  515. * ); @endcode
  516. * A return value of 0 indicates that the read operation is complete. A non-zero
  517. * return value indicates the maximum number of bytes to be read on the next
  518. * call to the device's async_read_some_at function.
  519. *
  520. * @param handler The handler to be called when the read operation completes.
  521. * Copies will be made of the handler as required. The function signature of the
  522. * 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 handler will not be invoked from within this function. Invocation of
  534. * the handler will be performed in a manner equivalent to using
  535. * asio::io_service::post().
  536. */
  537. template <typename AsyncRandomAccessReadDevice, typename Allocator,
  538. typename CompletionCondition, typename ReadHandler>
  539. void async_read_at(AsyncRandomAccessReadDevice& d,
  540. boost::uint64_t offset, basic_streambuf<Allocator>& b,
  541. CompletionCondition completion_condition, ReadHandler handler);
  542. #endif // !defined(BOOST_NO_IOSTREAM)
  543. /*@}*/
  544. } // namespace asio
  545. #include "asio/detail/pop_options.hpp"
  546. #include "asio/impl/read_at.hpp"
  547. #endif // ASIO_READ_AT_HPP