read.hpp 21 KB

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