read_at.hpp 22 KB

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