read_at.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. //
  2. // read_at.hpp
  3. // ~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include <boost/cstdint.hpp>
  20. #include <boost/asio/detail/pop_options.hpp>
  21. #include <boost/asio/basic_streambuf.hpp>
  22. #include <boost/asio/error.hpp>
  23. namespace boost {
  24. namespace asio {
  25. /**
  26. * @defgroup read_at boost::asio::read_at
  27. *
  28. * @brief Attempt to read a certain amount of data at the specified offset
  29. * before returning.
  30. */
  31. /*@{*/
  32. /// Attempt to read a certain amount of data at the specified offset before
  33. /// returning.
  34. /**
  35. * This function is used to read a certain number of bytes of data from a
  36. * random access device at the specified offset. The call will block until one
  37. * of the following conditions is true:
  38. *
  39. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  40. * the sum of the buffer sizes.
  41. *
  42. * @li An error occurred.
  43. *
  44. * This operation is implemented in terms of zero or more calls to the device's
  45. * read_some_at function.
  46. *
  47. * @param d The device from which the data is to be read. The type must support
  48. * the SyncRandomAccessReadDevice concept.
  49. *
  50. * @param offset The offset at which the data will be read.
  51. *
  52. * @param buffers One or more buffers into which the data will be read. The sum
  53. * of the buffer sizes indicates the maximum number of bytes to read from the
  54. * device.
  55. *
  56. * @returns The number of bytes transferred.
  57. *
  58. * @throws boost::system::system_error Thrown on failure.
  59. *
  60. * @par Example
  61. * To read into a single data buffer use the @ref buffer function as follows:
  62. * @code boost::asio::read_at(d, 42, boost::asio::buffer(data, size)); @endcode
  63. * See the @ref buffer documentation for information on reading into multiple
  64. * buffers in one go, and how to use it with arrays, boost::array or
  65. * std::vector.
  66. *
  67. * @note This overload is equivalent to calling:
  68. * @code boost::asio::read_at(
  69. * d, 42, buffers,
  70. * boost::asio::transfer_all()); @endcode
  71. */
  72. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence>
  73. std::size_t read_at(SyncRandomAccessReadDevice& d,
  74. boost::uint64_t offset, const MutableBufferSequence& buffers);
  75. /// Attempt to read a certain amount of data at the specified offset before
  76. /// returning.
  77. /**
  78. * This function is used to read a certain number of bytes of data from a
  79. * random access device at the specified offset. The call will block until one
  80. * of the following conditions is true:
  81. *
  82. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  83. * the sum of the buffer sizes.
  84. *
  85. * @li The completion_condition function object returns 0.
  86. *
  87. * This operation is implemented in terms of zero or more calls to the device's
  88. * read_some_at function.
  89. *
  90. * @param d The device from which the data is to be read. The type must support
  91. * the SyncRandomAccessReadDevice concept.
  92. *
  93. * @param offset The offset at which the data will be read.
  94. *
  95. * @param buffers One or more buffers into which the data will be read. The sum
  96. * of the buffer sizes indicates the maximum number of bytes to read from the
  97. * device.
  98. *
  99. * @param completion_condition The function object to be called to determine
  100. * whether the read operation is complete. The signature of the function object
  101. * must be:
  102. * @code std::size_t completion_condition(
  103. * // Result of latest read_some_at operation.
  104. * const boost::system::error_code& error,
  105. *
  106. * // Number of bytes transferred so far.
  107. * std::size_t bytes_transferred
  108. * ); @endcode
  109. * A return value of 0 indicates that the read operation is complete. A non-zero
  110. * return value indicates the maximum number of bytes to be read on the next
  111. * call to the device's read_some_at function.
  112. *
  113. * @returns The number of bytes transferred.
  114. *
  115. * @throws boost::system::system_error Thrown on failure.
  116. *
  117. * @par Example
  118. * To read into a single data buffer use the @ref buffer function as follows:
  119. * @code boost::asio::read_at(d, 42, boost::asio::buffer(data, size),
  120. * boost::asio::transfer_at_least(32)); @endcode
  121. * See the @ref buffer documentation for information on reading into multiple
  122. * buffers in one go, and how to use it with arrays, boost::array or
  123. * std::vector.
  124. */
  125. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
  126. typename CompletionCondition>
  127. std::size_t read_at(SyncRandomAccessReadDevice& d,
  128. boost::uint64_t offset, const MutableBufferSequence& buffers,
  129. CompletionCondition completion_condition);
  130. /// Attempt to read a certain amount of data at the specified offset before
  131. /// returning.
  132. /**
  133. * This function is used to read a certain number of bytes of data from a
  134. * random access device at the specified offset. The call will block until one
  135. * of the following conditions is true:
  136. *
  137. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  138. * the sum of the buffer sizes.
  139. *
  140. * @li The completion_condition function object returns 0.
  141. *
  142. * This operation is implemented in terms of zero or more calls to the device's
  143. * read_some_at function.
  144. *
  145. * @param d The device from which the data is to be read. The type must support
  146. * the SyncRandomAccessReadDevice concept.
  147. *
  148. * @param offset The offset at which the data will be read.
  149. *
  150. * @param buffers One or more buffers into which the data will be read. The sum
  151. * of the buffer sizes indicates the maximum number of bytes to read from the
  152. * device.
  153. *
  154. * @param completion_condition The function object to be called to determine
  155. * whether the read operation is complete. The signature of the function object
  156. * must be:
  157. * @code std::size_t completion_condition(
  158. * // Result of latest read_some_at operation.
  159. * const boost::system::error_code& error,
  160. *
  161. * // Number of bytes transferred so far.
  162. * std::size_t bytes_transferred
  163. * ); @endcode
  164. * A return value of 0 indicates that the read operation is complete. A non-zero
  165. * return value indicates the maximum number of bytes to be read on the next
  166. * call to the device's read_some_at function.
  167. *
  168. * @param ec Set to indicate what error occurred, if any.
  169. *
  170. * @returns The number of bytes read. If an error occurs, returns the total
  171. * number of bytes successfully transferred prior to the error.
  172. */
  173. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
  174. typename CompletionCondition>
  175. std::size_t read_at(SyncRandomAccessReadDevice& d,
  176. boost::uint64_t offset, const MutableBufferSequence& buffers,
  177. CompletionCondition completion_condition, boost::system::error_code& ec);
  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 boost::system::system_error Thrown on failure.
  200. *
  201. * @note This overload is equivalent to calling:
  202. * @code boost::asio::read_at(
  203. * d, 42, b,
  204. * boost::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 boost::system::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 boost::system::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 boost::system::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, boost::system::error_code& ec);
  294. /*@}*/
  295. /**
  296. * @defgroup async_read_at boost::asio::async_read_at
  297. *
  298. * @brief Start an asynchronous operation to read a certain amount of data at
  299. * the specified offset.
  300. */
  301. /*@{*/
  302. /// Start an asynchronous operation to read a certain amount of data at the
  303. /// specified offset.
  304. /**
  305. * This function is used to asynchronously read a certain number of bytes of
  306. * data from a random access device at the specified offset. The function call
  307. * always returns immediately. The asynchronous operation will continue until
  308. * one of the following conditions is true:
  309. *
  310. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  311. * the sum of the buffer sizes.
  312. *
  313. * @li An error occurred.
  314. *
  315. * This operation is implemented in terms of zero or more calls to the device's
  316. * async_read_some_at function.
  317. *
  318. * @param d The device from which the data is to be read. The type must support
  319. * the AsyncRandomAccessReadDevice concept.
  320. *
  321. * @param offset The offset at which the data will be read.
  322. *
  323. * @param buffers One or more buffers into which the data will be read. The sum
  324. * of the buffer sizes indicates the maximum number of bytes to read from the
  325. * device. Although the buffers object may be copied as necessary, ownership of
  326. * the underlying memory blocks is retained by the caller, which must guarantee
  327. * that they remain valid until the handler is called.
  328. *
  329. * @param handler The handler to be called when the read operation completes.
  330. * Copies will be made of the handler as required. The function signature of the
  331. * handler must be:
  332. * @code void handler(
  333. * // Result of operation.
  334. * const boost::system::error_code& error,
  335. *
  336. * // Number of bytes copied into the buffers. If an error
  337. * // occurred, this will be the number of bytes successfully
  338. * // transferred prior to the error.
  339. * std::size_t bytes_transferred
  340. * ); @endcode
  341. * Regardless of whether the asynchronous operation completes immediately or
  342. * not, the handler will not be invoked from within this function. Invocation of
  343. * the handler will be performed in a manner equivalent to using
  344. * boost::asio::io_service::post().
  345. *
  346. * @par Example
  347. * To read into a single data buffer use the @ref buffer function as follows:
  348. * @code
  349. * boost::asio::async_read_at(d, 42, boost::asio::buffer(data, size), handler);
  350. * @endcode
  351. * See the @ref buffer documentation for information on reading into multiple
  352. * buffers in one go, and how to use it with arrays, boost::array or
  353. * std::vector.
  354. *
  355. * @note This overload is equivalent to calling:
  356. * @code boost::asio::async_read_at(
  357. * d, 42, buffers,
  358. * boost::asio::transfer_all(),
  359. * handler); @endcode
  360. */
  361. template <typename AsyncRandomAccessReadDevice, typename MutableBufferSequence,
  362. typename ReadHandler>
  363. void async_read_at(AsyncRandomAccessReadDevice& d, boost::uint64_t offset,
  364. const MutableBufferSequence& buffers, ReadHandler handler);
  365. /// Start an asynchronous operation to read a certain amount of data at the
  366. /// specified offset.
  367. /**
  368. * This function is used to asynchronously read a certain number of bytes of
  369. * data from a random access device at the specified offset. The function call
  370. * always returns immediately. The asynchronous operation will continue until
  371. * one of the following conditions is true:
  372. *
  373. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  374. * the sum of the buffer sizes.
  375. *
  376. * @li The completion_condition function object returns 0.
  377. *
  378. * @param d The device from which the data is to be read. The type must support
  379. * the AsyncRandomAccessReadDevice concept.
  380. *
  381. * @param offset The offset at which the data will be read.
  382. *
  383. * @param buffers One or more buffers into which the data will be read. The sum
  384. * of the buffer sizes indicates the maximum number of bytes to read from the
  385. * device. Although the buffers object may be copied as necessary, ownership of
  386. * the underlying memory blocks is retained by the caller, which must guarantee
  387. * that they remain valid until the handler is called.
  388. *
  389. * @param completion_condition The function object to be called to determine
  390. * whether the read operation is complete. The signature of the function object
  391. * must be:
  392. * @code std::size_t completion_condition(
  393. * // Result of latest async_read_some_at operation.
  394. * const boost::system::error_code& error,
  395. *
  396. * // Number of bytes transferred so far.
  397. * std::size_t bytes_transferred
  398. * ); @endcode
  399. * A return value of 0 indicates that the read operation is complete. A non-zero
  400. * return value indicates the maximum number of bytes to be read on the next
  401. * call to the device's async_read_some_at function.
  402. *
  403. * @param handler The handler to be called when the read operation completes.
  404. * Copies will be made of the handler as required. The function signature of the
  405. * handler must be:
  406. * @code void handler(
  407. * // Result of operation.
  408. * const boost::system::error_code& error,
  409. *
  410. * // Number of bytes copied into the buffers. If an error
  411. * // occurred, this will be the number of bytes successfully
  412. * // transferred prior to the error.
  413. * std::size_t bytes_transferred
  414. * ); @endcode
  415. * Regardless of whether the asynchronous operation completes immediately or
  416. * not, the handler will not be invoked from within this function. Invocation of
  417. * the handler will be performed in a manner equivalent to using
  418. * boost::asio::io_service::post().
  419. *
  420. * @par Example
  421. * To read into a single data buffer use the @ref buffer function as follows:
  422. * @code boost::asio::async_read_at(d, 42,
  423. * boost::asio::buffer(data, size),
  424. * boost::asio::transfer_at_least(32),
  425. * handler); @endcode
  426. * See the @ref buffer documentation for information on reading into multiple
  427. * buffers in one go, and how to use it with arrays, boost::array or
  428. * std::vector.
  429. */
  430. template <typename AsyncRandomAccessReadDevice, typename MutableBufferSequence,
  431. typename CompletionCondition, typename ReadHandler>
  432. void async_read_at(AsyncRandomAccessReadDevice& d,
  433. boost::uint64_t offset, const MutableBufferSequence& buffers,
  434. CompletionCondition completion_condition, ReadHandler handler);
  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 boost::system::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. * boost::asio::io_service::post().
  473. *
  474. * @note This overload is equivalent to calling:
  475. * @code boost::asio::async_read_at(
  476. * d, 42, b,
  477. * boost::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 boost::system::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 boost::system::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. * boost::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. /*@}*/
  543. } // namespace asio
  544. } // namespace boost
  545. #include <boost/asio/impl/read_at.ipp>
  546. #include <boost/asio/detail/pop_options.hpp>
  547. #endif // BOOST_ASIO_READ_AT_HPP