read.hpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. //
  2. // read.hpp
  3. // ~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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/async_result.hpp"
  18. #include "asio/buffer.hpp"
  19. #include "asio/error.hpp"
  20. #if !defined(ASIO_NO_EXTENSIONS)
  21. # include "asio/basic_streambuf_fwd.hpp"
  22. #endif // !defined(ASIO_NO_EXTENSIONS)
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. /**
  26. * @defgroup read asio::read
  27. *
  28. * @brief The @c read function is a composed operation that reads a certain
  29. * amount of data from a stream before returning.
  30. */
  31. /*@{*/
  32. /// Attempt to read a certain amount of data from a stream before returning.
  33. /**
  34. * This function is used to read a certain number of bytes of data from a
  35. * stream. The call will block until one of the following conditions is true:
  36. *
  37. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  38. * the sum of the buffer sizes.
  39. *
  40. * @li An error occurred.
  41. *
  42. * This operation is implemented in terms of zero or more calls to the stream's
  43. * read_some function.
  44. *
  45. * @param s The stream from which the data is to be read. The type must support
  46. * the SyncReadStream concept.
  47. *
  48. * @param buffers One or more buffers into which the data will be read. The sum
  49. * of the buffer sizes indicates the maximum number of bytes to read from the
  50. * stream.
  51. *
  52. * @returns The number of bytes transferred.
  53. *
  54. * @throws asio::system_error Thrown on failure.
  55. *
  56. * @par Example
  57. * To read into a single data buffer use the @ref buffer function as follows:
  58. * @code asio::read(s, asio::buffer(data, size)); @endcode
  59. * See the @ref buffer documentation for information on reading into multiple
  60. * buffers in one go, and how to use it with arrays, boost::array or
  61. * std::vector.
  62. *
  63. * @note This overload is equivalent to calling:
  64. * @code asio::read(
  65. * s, buffers,
  66. * asio::transfer_all()); @endcode
  67. */
  68. template <typename SyncReadStream, typename MutableBufferSequence>
  69. std::size_t read(SyncReadStream& s, const MutableBufferSequence& buffers,
  70. typename enable_if<
  71. is_mutable_buffer_sequence<MutableBufferSequence>::value
  72. >::type* = 0);
  73. /// Attempt to read a certain amount of data from a stream before returning.
  74. /**
  75. * This function is used to read a certain number of bytes of data from a
  76. * stream. The call will block until one of the following conditions is true:
  77. *
  78. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  79. * the sum of the buffer sizes.
  80. *
  81. * @li An error occurred.
  82. *
  83. * This operation is implemented in terms of zero or more calls to the stream's
  84. * read_some function.
  85. *
  86. * @param s The stream from which the data is to be read. The type must support
  87. * the SyncReadStream concept.
  88. *
  89. * @param buffers One or more buffers into which the data will be read. The sum
  90. * of the buffer sizes indicates the maximum number of bytes to read from the
  91. * stream.
  92. *
  93. * @param ec Set to indicate what error occurred, if any.
  94. *
  95. * @returns The number of bytes transferred.
  96. *
  97. * @par Example
  98. * To read into a single data buffer use the @ref buffer function as follows:
  99. * @code asio::read(s, asio::buffer(data, size), ec); @endcode
  100. * See the @ref buffer documentation for information on reading into multiple
  101. * buffers in one go, and how to use it with arrays, boost::array or
  102. * std::vector.
  103. *
  104. * @note This overload is equivalent to calling:
  105. * @code asio::read(
  106. * s, buffers,
  107. * asio::transfer_all(), ec); @endcode
  108. */
  109. template <typename SyncReadStream, typename MutableBufferSequence>
  110. std::size_t read(SyncReadStream& s, const MutableBufferSequence& buffers,
  111. asio::error_code& ec,
  112. typename enable_if<
  113. is_mutable_buffer_sequence<MutableBufferSequence>::value
  114. >::type* = 0);
  115. /// Attempt to read a certain amount of data from a stream before returning.
  116. /**
  117. * This function is used to read a certain number of bytes of data from a
  118. * stream. The call will block until one of the following conditions is true:
  119. *
  120. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  121. * the sum of the buffer sizes.
  122. *
  123. * @li The completion_condition function object returns 0.
  124. *
  125. * This operation is implemented in terms of zero or more calls to the stream's
  126. * read_some function.
  127. *
  128. * @param s The stream from which the data is to be read. The type must support
  129. * the SyncReadStream concept.
  130. *
  131. * @param buffers One or more buffers into which the data will be read. The sum
  132. * of the buffer sizes indicates the maximum number of bytes to read from the
  133. * stream.
  134. *
  135. * @param completion_condition The function object to be called to determine
  136. * whether the read operation is complete. The signature of the function object
  137. * must be:
  138. * @code std::size_t completion_condition(
  139. * // Result of latest read_some operation.
  140. * const asio::error_code& error,
  141. *
  142. * // Number of bytes transferred so far.
  143. * std::size_t bytes_transferred
  144. * ); @endcode
  145. * A return value of 0 indicates that the read operation is complete. A non-zero
  146. * return value indicates the maximum number of bytes to be read on the next
  147. * call to the stream's read_some function.
  148. *
  149. * @returns The number of bytes transferred.
  150. *
  151. * @throws asio::system_error Thrown on failure.
  152. *
  153. * @par Example
  154. * To read into a single data buffer use the @ref buffer function as follows:
  155. * @code asio::read(s, asio::buffer(data, size),
  156. * asio::transfer_at_least(32)); @endcode
  157. * See the @ref buffer documentation for information on reading into multiple
  158. * buffers in one go, and how to use it with arrays, boost::array or
  159. * std::vector.
  160. */
  161. template <typename SyncReadStream, typename MutableBufferSequence,
  162. typename CompletionCondition>
  163. std::size_t read(SyncReadStream& s, const MutableBufferSequence& buffers,
  164. CompletionCondition completion_condition,
  165. typename enable_if<
  166. is_mutable_buffer_sequence<MutableBufferSequence>::value
  167. >::type* = 0);
  168. /// Attempt to read a certain amount of data from a stream before returning.
  169. /**
  170. * This function is used to read a certain number of bytes of data from a
  171. * stream. The call will block until one of the following conditions is true:
  172. *
  173. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  174. * the sum of the buffer sizes.
  175. *
  176. * @li The completion_condition function object returns 0.
  177. *
  178. * This operation is implemented in terms of zero or more calls to the stream's
  179. * read_some function.
  180. *
  181. * @param s The stream from which the data is to be read. The type must support
  182. * the SyncReadStream concept.
  183. *
  184. * @param buffers One or more buffers into which the data will be read. The sum
  185. * of the buffer sizes indicates the maximum number of bytes to read from the
  186. * stream.
  187. *
  188. * @param completion_condition The function object to be called to determine
  189. * whether the read operation is complete. The signature of the function object
  190. * must be:
  191. * @code std::size_t completion_condition(
  192. * // Result of latest read_some operation.
  193. * const asio::error_code& error,
  194. *
  195. * // Number of bytes transferred so far.
  196. * std::size_t bytes_transferred
  197. * ); @endcode
  198. * A return value of 0 indicates that the read operation is complete. A non-zero
  199. * return value indicates the maximum number of bytes to be read on the next
  200. * call to the stream's read_some function.
  201. *
  202. * @param ec Set to indicate what error occurred, if any.
  203. *
  204. * @returns The number of bytes read. If an error occurs, returns the total
  205. * number of bytes successfully transferred prior to the error.
  206. */
  207. template <typename SyncReadStream, typename MutableBufferSequence,
  208. typename CompletionCondition>
  209. std::size_t read(SyncReadStream& s, const MutableBufferSequence& buffers,
  210. CompletionCondition completion_condition, asio::error_code& ec,
  211. typename enable_if<
  212. is_mutable_buffer_sequence<MutableBufferSequence>::value
  213. >::type* = 0);
  214. /// Attempt to read a certain amount of data from a stream before returning.
  215. /**
  216. * This function is used to read a certain number of bytes of data from a
  217. * stream. The call will block until one of the following conditions is true:
  218. *
  219. * @li The specified dynamic buffer sequence is full (that is, it has reached
  220. * maximum size).
  221. *
  222. * @li An error occurred.
  223. *
  224. * This operation is implemented in terms of zero or more calls to the stream's
  225. * read_some function.
  226. *
  227. * @param s The stream from which the data is to be read. The type must support
  228. * the SyncReadStream concept.
  229. *
  230. * @param buffers The dynamic buffer sequence into which the data will be read.
  231. *
  232. * @returns The number of bytes transferred.
  233. *
  234. * @throws asio::system_error Thrown on failure.
  235. *
  236. * @note This overload is equivalent to calling:
  237. * @code asio::read(
  238. * s, buffers,
  239. * asio::transfer_all()); @endcode
  240. */
  241. template <typename SyncReadStream, typename DynamicBuffer>
  242. std::size_t read(SyncReadStream& s,
  243. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  244. typename enable_if<
  245. is_dynamic_buffer<typename decay<DynamicBuffer>::type>::value
  246. >::type* = 0);
  247. /// Attempt to read a certain amount of data from a stream before returning.
  248. /**
  249. * This function is used to read a certain number of bytes of data from a
  250. * stream. The call will block until one of the following conditions is true:
  251. *
  252. * @li The supplied buffer is full (that is, it has reached maximum size).
  253. *
  254. * @li An error occurred.
  255. *
  256. * This operation is implemented in terms of zero or more calls to the stream's
  257. * read_some function.
  258. *
  259. * @param s The stream from which the data is to be read. The type must support
  260. * the SyncReadStream concept.
  261. *
  262. * @param buffers The dynamic buffer sequence into which the data will be read.
  263. *
  264. * @param ec Set to indicate what error occurred, if any.
  265. *
  266. * @returns The number of bytes transferred.
  267. *
  268. * @note This overload is equivalent to calling:
  269. * @code asio::read(
  270. * s, buffers,
  271. * asio::transfer_all(), ec); @endcode
  272. */
  273. template <typename SyncReadStream, typename DynamicBuffer>
  274. std::size_t read(SyncReadStream& s,
  275. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  276. asio::error_code& ec,
  277. typename enable_if<
  278. is_dynamic_buffer<typename decay<DynamicBuffer>::type>::value
  279. >::type* = 0);
  280. /// Attempt to read a certain amount of data from a stream before returning.
  281. /**
  282. * This function is used to read a certain number of bytes of data from a
  283. * stream. The call will block until one of the following conditions is true:
  284. *
  285. * @li The specified dynamic buffer sequence is full (that is, it has reached
  286. * maximum size).
  287. *
  288. * @li The completion_condition function object returns 0.
  289. *
  290. * This operation is implemented in terms of zero or more calls to the stream's
  291. * read_some function.
  292. *
  293. * @param s The stream from which the data is to be read. The type must support
  294. * the SyncReadStream concept.
  295. *
  296. * @param buffers The dynamic buffer sequence into which the data will be read.
  297. *
  298. * @param completion_condition The function object to be called to determine
  299. * whether the read operation is complete. The signature of the function object
  300. * must be:
  301. * @code std::size_t completion_condition(
  302. * // Result of latest read_some operation.
  303. * const asio::error_code& error,
  304. *
  305. * // Number of bytes transferred so far.
  306. * std::size_t bytes_transferred
  307. * ); @endcode
  308. * A return value of 0 indicates that the read operation is complete. A non-zero
  309. * return value indicates the maximum number of bytes to be read on the next
  310. * call to the stream's read_some function.
  311. *
  312. * @returns The number of bytes transferred.
  313. *
  314. * @throws asio::system_error Thrown on failure.
  315. */
  316. template <typename SyncReadStream, typename DynamicBuffer,
  317. typename CompletionCondition>
  318. std::size_t read(SyncReadStream& s,
  319. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  320. CompletionCondition completion_condition,
  321. typename enable_if<
  322. is_dynamic_buffer<typename decay<DynamicBuffer>::type>::value
  323. >::type* = 0);
  324. /// Attempt to read a certain amount of data from a stream before returning.
  325. /**
  326. * This function is used to read a certain number of bytes of data from a
  327. * stream. The call will block until one of the following conditions is true:
  328. *
  329. * @li The specified dynamic buffer sequence is full (that is, it has reached
  330. * maximum size).
  331. *
  332. * @li The completion_condition function object returns 0.
  333. *
  334. * This operation is implemented in terms of zero or more calls to the stream's
  335. * read_some function.
  336. *
  337. * @param s The stream from which the data is to be read. The type must support
  338. * the SyncReadStream concept.
  339. *
  340. * @param buffers The dynamic buffer sequence into which the data will be read.
  341. *
  342. * @param completion_condition The function object to be called to determine
  343. * whether the read operation is complete. The signature of the function object
  344. * must be:
  345. * @code std::size_t completion_condition(
  346. * // Result of latest read_some operation.
  347. * const asio::error_code& error,
  348. *
  349. * // Number of bytes transferred so far.
  350. * std::size_t bytes_transferred
  351. * ); @endcode
  352. * A return value of 0 indicates that the read operation is complete. A non-zero
  353. * return value indicates the maximum number of bytes to be read on the next
  354. * call to the stream's read_some function.
  355. *
  356. * @param ec Set to indicate what error occurred, if any.
  357. *
  358. * @returns The number of bytes read. If an error occurs, returns the total
  359. * number of bytes successfully transferred prior to the error.
  360. */
  361. template <typename SyncReadStream, typename DynamicBuffer,
  362. typename CompletionCondition>
  363. std::size_t read(SyncReadStream& s,
  364. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  365. CompletionCondition completion_condition, asio::error_code& ec,
  366. typename enable_if<
  367. is_dynamic_buffer<typename decay<DynamicBuffer>::type>::value
  368. >::type* = 0);
  369. #if !defined(ASIO_NO_EXTENSIONS)
  370. #if !defined(ASIO_NO_IOSTREAM)
  371. /// Attempt to read a certain amount of data from a stream before returning.
  372. /**
  373. * This function is used to read a certain number of bytes of data from a
  374. * stream. The call will block until one of the following conditions is true:
  375. *
  376. * @li The supplied buffer is full (that is, it has reached maximum size).
  377. *
  378. * @li An error occurred.
  379. *
  380. * This operation is implemented in terms of zero or more calls to the stream's
  381. * read_some function.
  382. *
  383. * @param s The stream from which the data is to be read. The type must support
  384. * the SyncReadStream concept.
  385. *
  386. * @param b The basic_streambuf object into which the data will be read.
  387. *
  388. * @returns The number of bytes transferred.
  389. *
  390. * @throws asio::system_error Thrown on failure.
  391. *
  392. * @note This overload is equivalent to calling:
  393. * @code asio::read(
  394. * s, b,
  395. * asio::transfer_all()); @endcode
  396. */
  397. template <typename SyncReadStream, typename Allocator>
  398. std::size_t read(SyncReadStream& s, basic_streambuf<Allocator>& b);
  399. /// Attempt to read a certain amount of data from a stream before returning.
  400. /**
  401. * This function is used to read a certain number of bytes of data from a
  402. * stream. The call will block until one of the following conditions is true:
  403. *
  404. * @li The supplied buffer is full (that is, it has reached maximum size).
  405. *
  406. * @li An error occurred.
  407. *
  408. * This operation is implemented in terms of zero or more calls to the stream's
  409. * read_some function.
  410. *
  411. * @param s The stream from which the data is to be read. The type must support
  412. * the SyncReadStream concept.
  413. *
  414. * @param b The basic_streambuf object into which the data will be read.
  415. *
  416. * @param ec Set to indicate what error occurred, if any.
  417. *
  418. * @returns The number of bytes transferred.
  419. *
  420. * @note This overload is equivalent to calling:
  421. * @code asio::read(
  422. * s, b,
  423. * asio::transfer_all(), ec); @endcode
  424. */
  425. template <typename SyncReadStream, typename Allocator>
  426. std::size_t read(SyncReadStream& s, basic_streambuf<Allocator>& b,
  427. asio::error_code& ec);
  428. /// Attempt to read a certain amount of data from a stream before returning.
  429. /**
  430. * This function is used to read a certain number of bytes of data from a
  431. * stream. The call will block until one of the following conditions is true:
  432. *
  433. * @li The supplied buffer is full (that is, it has reached maximum size).
  434. *
  435. * @li The completion_condition function object returns 0.
  436. *
  437. * This operation is implemented in terms of zero or more calls to the stream's
  438. * read_some function.
  439. *
  440. * @param s The stream from which the data is to be read. The type must support
  441. * the SyncReadStream concept.
  442. *
  443. * @param b The basic_streambuf object into which the data will be read.
  444. *
  445. * @param completion_condition The function object to be called to determine
  446. * whether the read operation is complete. The signature of the function object
  447. * must be:
  448. * @code std::size_t completion_condition(
  449. * // Result of latest read_some operation.
  450. * const asio::error_code& error,
  451. *
  452. * // Number of bytes transferred so far.
  453. * std::size_t bytes_transferred
  454. * ); @endcode
  455. * A return value of 0 indicates that the read operation is complete. A non-zero
  456. * return value indicates the maximum number of bytes to be read on the next
  457. * call to the stream's read_some function.
  458. *
  459. * @returns The number of bytes transferred.
  460. *
  461. * @throws asio::system_error Thrown on failure.
  462. */
  463. template <typename SyncReadStream, typename Allocator,
  464. typename CompletionCondition>
  465. std::size_t read(SyncReadStream& s, basic_streambuf<Allocator>& b,
  466. CompletionCondition completion_condition);
  467. /// Attempt to read a certain amount of data from a stream before returning.
  468. /**
  469. * This function is used to read a certain number of bytes of data from a
  470. * stream. The call will block until one of the following conditions is true:
  471. *
  472. * @li The supplied buffer is full (that is, it has reached maximum size).
  473. *
  474. * @li The completion_condition function object returns 0.
  475. *
  476. * This operation is implemented in terms of zero or more calls to the stream's
  477. * read_some function.
  478. *
  479. * @param s The stream from which the data is to be read. The type must support
  480. * the SyncReadStream concept.
  481. *
  482. * @param b The basic_streambuf object into which the data will be read.
  483. *
  484. * @param completion_condition The function object to be called to determine
  485. * whether the read operation is complete. The signature of the function object
  486. * must be:
  487. * @code std::size_t completion_condition(
  488. * // Result of latest read_some operation.
  489. * const asio::error_code& error,
  490. *
  491. * // Number of bytes transferred so far.
  492. * std::size_t bytes_transferred
  493. * ); @endcode
  494. * A return value of 0 indicates that the read operation is complete. A non-zero
  495. * return value indicates the maximum number of bytes to be read on the next
  496. * call to the stream's read_some function.
  497. *
  498. * @param ec Set to indicate what error occurred, if any.
  499. *
  500. * @returns The number of bytes read. If an error occurs, returns the total
  501. * number of bytes successfully transferred prior to the error.
  502. */
  503. template <typename SyncReadStream, typename Allocator,
  504. typename CompletionCondition>
  505. std::size_t read(SyncReadStream& s, basic_streambuf<Allocator>& b,
  506. CompletionCondition completion_condition, asio::error_code& ec);
  507. #endif // !defined(ASIO_NO_IOSTREAM)
  508. #endif // !defined(ASIO_NO_EXTENSIONS)
  509. /*@}*/
  510. /**
  511. * @defgroup async_read asio::async_read
  512. *
  513. * @brief The @c async_read function is a composed asynchronous operation that
  514. * reads a certain amount of data from a stream before completion.
  515. */
  516. /*@{*/
  517. /// Start an asynchronous operation to read a certain amount of data from a
  518. /// stream.
  519. /**
  520. * This function is used to asynchronously read a certain number of bytes of
  521. * data from a stream. The function call always returns immediately. The
  522. * asynchronous operation will continue until one of the following conditions is
  523. * true:
  524. *
  525. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  526. * the sum of the buffer sizes.
  527. *
  528. * @li An error occurred.
  529. *
  530. * This operation is implemented in terms of zero or more calls to the stream's
  531. * async_read_some function, and is known as a <em>composed operation</em>. The
  532. * program must ensure that the stream performs no other read operations (such
  533. * as async_read, the stream's async_read_some function, or any other composed
  534. * operations that perform reads) until this operation completes.
  535. *
  536. * @param s The stream from which the data is to be read. The type must support
  537. * the AsyncReadStream concept.
  538. *
  539. * @param buffers One or more buffers into which the data will be read. The sum
  540. * of the buffer sizes indicates the maximum number of bytes to read from the
  541. * stream. Although the buffers object may be copied as necessary, ownership of
  542. * the underlying memory blocks is retained by the caller, which must guarantee
  543. * that they remain valid until the handler is called.
  544. *
  545. * @param handler The handler to be called when the read operation completes.
  546. * Copies will be made of the handler as required. The function signature of the
  547. * handler must be:
  548. * @code void handler(
  549. * const asio::error_code& error, // Result of operation.
  550. *
  551. * std::size_t bytes_transferred // Number of bytes copied into the
  552. * // buffers. If an error occurred,
  553. * // this will be the number of
  554. * // bytes successfully transferred
  555. * // prior to the error.
  556. * ); @endcode
  557. * Regardless of whether the asynchronous operation completes immediately or
  558. * not, the handler will not be invoked from within this function. Invocation of
  559. * the handler will be performed in a manner equivalent to using
  560. * asio::io_context::post().
  561. *
  562. * @par Example
  563. * To read into a single data buffer use the @ref buffer function as follows:
  564. * @code
  565. * asio::async_read(s, asio::buffer(data, size), handler);
  566. * @endcode
  567. * See the @ref buffer documentation for information on reading into multiple
  568. * buffers in one go, and how to use it with arrays, boost::array or
  569. * std::vector.
  570. *
  571. * @note This overload is equivalent to calling:
  572. * @code asio::async_read(
  573. * s, buffers,
  574. * asio::transfer_all(),
  575. * handler); @endcode
  576. */
  577. template <typename AsyncReadStream, typename MutableBufferSequence,
  578. typename ReadHandler>
  579. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  580. void (asio::error_code, std::size_t))
  581. async_read(AsyncReadStream& s, const MutableBufferSequence& buffers,
  582. ASIO_MOVE_ARG(ReadHandler) handler,
  583. typename enable_if<
  584. is_mutable_buffer_sequence<MutableBufferSequence>::value
  585. >::type* = 0);
  586. /// Start an asynchronous operation to read a certain amount of data from a
  587. /// stream.
  588. /**
  589. * This function is used to asynchronously read a certain number of bytes of
  590. * data from a stream. The function call always returns immediately. The
  591. * asynchronous operation will continue until one of the following conditions is
  592. * true:
  593. *
  594. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  595. * the sum of the buffer sizes.
  596. *
  597. * @li The completion_condition function object returns 0.
  598. *
  599. * @param s The stream from which the data is to be read. The type must support
  600. * the AsyncReadStream concept.
  601. *
  602. * @param buffers One or more buffers into which the data will be read. The sum
  603. * of the buffer sizes indicates the maximum number of bytes to read from the
  604. * stream. Although the buffers object may be copied as necessary, ownership of
  605. * the underlying memory blocks is retained by the caller, which must guarantee
  606. * that they remain valid until the handler is called.
  607. *
  608. * @param completion_condition The function object to be called to determine
  609. * whether the read operation is complete. The signature of the function object
  610. * must be:
  611. * @code std::size_t completion_condition(
  612. * // Result of latest async_read_some operation.
  613. * const asio::error_code& error,
  614. *
  615. * // Number of bytes transferred so far.
  616. * std::size_t bytes_transferred
  617. * ); @endcode
  618. * A return value of 0 indicates that the read operation is complete. A non-zero
  619. * return value indicates the maximum number of bytes to be read on the next
  620. * call to the stream's async_read_some function.
  621. *
  622. * @param handler The handler to be called when the read operation completes.
  623. * Copies will be made of the handler as required. The function signature of the
  624. * handler must be:
  625. * @code void handler(
  626. * const asio::error_code& error, // Result of operation.
  627. *
  628. * std::size_t bytes_transferred // Number of bytes copied into the
  629. * // buffers. If an error occurred,
  630. * // this will be the number of
  631. * // bytes successfully transferred
  632. * // prior to the error.
  633. * ); @endcode
  634. * Regardless of whether the asynchronous operation completes immediately or
  635. * not, the handler will not be invoked from within this function. Invocation of
  636. * the handler will be performed in a manner equivalent to using
  637. * asio::io_context::post().
  638. *
  639. * @par Example
  640. * To read into a single data buffer use the @ref buffer function as follows:
  641. * @code asio::async_read(s,
  642. * asio::buffer(data, size),
  643. * asio::transfer_at_least(32),
  644. * handler); @endcode
  645. * See the @ref buffer documentation for information on reading into multiple
  646. * buffers in one go, and how to use it with arrays, boost::array or
  647. * std::vector.
  648. */
  649. template <typename AsyncReadStream, typename MutableBufferSequence,
  650. typename CompletionCondition, typename ReadHandler>
  651. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  652. void (asio::error_code, std::size_t))
  653. async_read(AsyncReadStream& s, const MutableBufferSequence& buffers,
  654. CompletionCondition completion_condition,
  655. ASIO_MOVE_ARG(ReadHandler) handler,
  656. typename enable_if<
  657. is_mutable_buffer_sequence<MutableBufferSequence>::value
  658. >::type* = 0);
  659. /// Start an asynchronous operation to read a certain amount of data from a
  660. /// stream.
  661. /**
  662. * This function is used to asynchronously read a certain number of bytes of
  663. * data from a stream. The function call always returns immediately. The
  664. * asynchronous operation will continue until one of the following conditions is
  665. * true:
  666. *
  667. * @li The specified dynamic buffer sequence is full (that is, it has reached
  668. * maximum size).
  669. *
  670. * @li An error occurred.
  671. *
  672. * This operation is implemented in terms of zero or more calls to the stream's
  673. * async_read_some function, and is known as a <em>composed operation</em>. The
  674. * program must ensure that the stream performs no other read operations (such
  675. * as async_read, the stream's async_read_some function, or any other composed
  676. * operations that perform reads) until this operation completes.
  677. *
  678. * @param s The stream from which the data is to be read. The type must support
  679. * the AsyncReadStream concept.
  680. *
  681. * @param buffers The dynamic buffer sequence into which the data will be read.
  682. * Although the buffers object may be copied as necessary, ownership of the
  683. * underlying memory blocks is retained by the caller, which must guarantee
  684. * that they remain valid until the handler is called.
  685. *
  686. * @param handler The handler to be called when the read operation completes.
  687. * Copies will be made of the handler as required. The function signature of the
  688. * handler must be:
  689. * @code void handler(
  690. * const asio::error_code& error, // Result of operation.
  691. *
  692. * std::size_t bytes_transferred // Number of bytes copied into the
  693. * // buffers. If an error occurred,
  694. * // this will be the number of
  695. * // bytes successfully transferred
  696. * // prior to the error.
  697. * ); @endcode
  698. * Regardless of whether the asynchronous operation completes immediately or
  699. * not, the handler will not be invoked from within this function. Invocation of
  700. * the handler will be performed in a manner equivalent to using
  701. * asio::io_context::post().
  702. *
  703. * @note This overload is equivalent to calling:
  704. * @code asio::async_read(
  705. * s, buffers,
  706. * asio::transfer_all(),
  707. * handler); @endcode
  708. */
  709. template <typename AsyncReadStream,
  710. typename DynamicBuffer, typename ReadHandler>
  711. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  712. void (asio::error_code, std::size_t))
  713. async_read(AsyncReadStream& s,
  714. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  715. ASIO_MOVE_ARG(ReadHandler) handler,
  716. typename enable_if<
  717. is_dynamic_buffer<typename decay<DynamicBuffer>::type>::value
  718. >::type* = 0);
  719. /// Start an asynchronous operation to read a certain amount of data from a
  720. /// stream.
  721. /**
  722. * This function is used to asynchronously read a certain number of bytes of
  723. * data from a stream. The function call always returns immediately. The
  724. * asynchronous operation will continue until one of the following conditions is
  725. * true:
  726. *
  727. * @li The specified dynamic buffer sequence is full (that is, it has reached
  728. * maximum size).
  729. *
  730. * @li The completion_condition function object returns 0.
  731. *
  732. * This operation is implemented in terms of zero or more calls to the stream's
  733. * async_read_some function, and is known as a <em>composed operation</em>. The
  734. * program must ensure that the stream performs no other read operations (such
  735. * as async_read, the stream's async_read_some function, or any other composed
  736. * operations that perform reads) until this operation completes.
  737. *
  738. * @param s The stream from which the data is to be read. The type must support
  739. * the AsyncReadStream concept.
  740. *
  741. * @param buffers The dynamic buffer sequence into which the data will be read.
  742. * Although the buffers object may be copied as necessary, ownership of the
  743. * underlying memory blocks is retained by the caller, which must guarantee
  744. * that they remain valid until the handler is called.
  745. *
  746. * @param completion_condition The function object to be called to determine
  747. * whether the read operation is complete. The signature of the function object
  748. * must be:
  749. * @code std::size_t completion_condition(
  750. * // Result of latest async_read_some operation.
  751. * const asio::error_code& error,
  752. *
  753. * // Number of bytes transferred so far.
  754. * std::size_t bytes_transferred
  755. * ); @endcode
  756. * A return value of 0 indicates that the read operation is complete. A non-zero
  757. * return value indicates the maximum number of bytes to be read on the next
  758. * call to the stream's async_read_some function.
  759. *
  760. * @param handler The handler to be called when the read operation completes.
  761. * Copies will be made of the handler as required. The function signature of the
  762. * handler must be:
  763. * @code void handler(
  764. * const asio::error_code& error, // Result of operation.
  765. *
  766. * std::size_t bytes_transferred // Number of bytes copied into the
  767. * // buffers. If an error occurred,
  768. * // this will be the number of
  769. * // bytes successfully transferred
  770. * // prior to the error.
  771. * ); @endcode
  772. * Regardless of whether the asynchronous operation completes immediately or
  773. * not, the handler will not be invoked from within this function. Invocation of
  774. * the handler will be performed in a manner equivalent to using
  775. * asio::io_context::post().
  776. */
  777. template <typename AsyncReadStream, typename DynamicBuffer,
  778. typename CompletionCondition, typename ReadHandler>
  779. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  780. void (asio::error_code, std::size_t))
  781. async_read(AsyncReadStream& s,
  782. ASIO_MOVE_ARG(DynamicBuffer) buffers,
  783. CompletionCondition completion_condition,
  784. ASIO_MOVE_ARG(ReadHandler) handler,
  785. typename enable_if<
  786. is_dynamic_buffer<typename decay<DynamicBuffer>::type>::value
  787. >::type* = 0);
  788. #if !defined(ASIO_NO_EXTENSIONS)
  789. #if !defined(ASIO_NO_IOSTREAM)
  790. /// Start an asynchronous operation to read a certain amount of data from a
  791. /// stream.
  792. /**
  793. * This function is used to asynchronously read a certain number of bytes of
  794. * data from a stream. The function call always returns immediately. The
  795. * asynchronous operation will continue until one of the following conditions is
  796. * true:
  797. *
  798. * @li The supplied buffer is full (that is, it has reached maximum size).
  799. *
  800. * @li An error occurred.
  801. *
  802. * This operation is implemented in terms of zero or more calls to the stream's
  803. * async_read_some function, and is known as a <em>composed operation</em>. The
  804. * program must ensure that the stream performs no other read operations (such
  805. * as async_read, the stream's async_read_some function, or any other composed
  806. * operations that perform reads) until this operation completes.
  807. *
  808. * @param s The stream from which the data is to be read. The type must support
  809. * the AsyncReadStream concept.
  810. *
  811. * @param b A basic_streambuf object into which the data will be read. Ownership
  812. * of the streambuf is retained by the caller, which must guarantee that it
  813. * remains valid until the handler is called.
  814. *
  815. * @param handler The handler to be called when the read operation completes.
  816. * Copies will be made of the handler as required. The function signature of the
  817. * handler must be:
  818. * @code void handler(
  819. * const asio::error_code& error, // Result of operation.
  820. *
  821. * std::size_t bytes_transferred // Number of bytes copied into the
  822. * // buffers. If an error occurred,
  823. * // this will be the number of
  824. * // bytes successfully transferred
  825. * // prior to the error.
  826. * ); @endcode
  827. * Regardless of whether the asynchronous operation completes immediately or
  828. * not, the handler will not be invoked from within this function. Invocation of
  829. * the handler will be performed in a manner equivalent to using
  830. * asio::io_context::post().
  831. *
  832. * @note This overload is equivalent to calling:
  833. * @code asio::async_read(
  834. * s, b,
  835. * asio::transfer_all(),
  836. * handler); @endcode
  837. */
  838. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  839. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  840. void (asio::error_code, std::size_t))
  841. async_read(AsyncReadStream& s, basic_streambuf<Allocator>& b,
  842. ASIO_MOVE_ARG(ReadHandler) handler);
  843. /// Start an asynchronous operation to read a certain amount of data from a
  844. /// stream.
  845. /**
  846. * This function is used to asynchronously read a certain number of bytes of
  847. * data from a stream. The function call always returns immediately. The
  848. * asynchronous operation will continue until one of the following conditions is
  849. * true:
  850. *
  851. * @li The supplied buffer is full (that is, it has reached maximum size).
  852. *
  853. * @li The completion_condition function object returns 0.
  854. *
  855. * This operation is implemented in terms of zero or more calls to the stream's
  856. * async_read_some function, and is known as a <em>composed operation</em>. The
  857. * program must ensure that the stream performs no other read operations (such
  858. * as async_read, the stream's async_read_some function, or any other composed
  859. * operations that perform reads) until this operation completes.
  860. *
  861. * @param s The stream from which the data is to be read. The type must support
  862. * the AsyncReadStream concept.
  863. *
  864. * @param b A basic_streambuf object into which the data will be read. Ownership
  865. * of the streambuf is retained by the caller, which must guarantee that it
  866. * remains valid until the handler is called.
  867. *
  868. * @param completion_condition The function object to be called to determine
  869. * whether the read operation is complete. The signature of the function object
  870. * must be:
  871. * @code std::size_t completion_condition(
  872. * // Result of latest async_read_some operation.
  873. * const asio::error_code& error,
  874. *
  875. * // Number of bytes transferred so far.
  876. * std::size_t bytes_transferred
  877. * ); @endcode
  878. * A return value of 0 indicates that the read operation is complete. A non-zero
  879. * return value indicates the maximum number of bytes to be read on the next
  880. * call to the stream's async_read_some function.
  881. *
  882. * @param handler The handler to be called when the read operation completes.
  883. * Copies will be made of the handler as required. The function signature of the
  884. * handler must be:
  885. * @code void handler(
  886. * const asio::error_code& error, // Result of operation.
  887. *
  888. * std::size_t bytes_transferred // Number of bytes copied into the
  889. * // buffers. If an error occurred,
  890. * // this will be the number of
  891. * // bytes successfully transferred
  892. * // prior to the error.
  893. * ); @endcode
  894. * Regardless of whether the asynchronous operation completes immediately or
  895. * not, the handler will not be invoked from within this function. Invocation of
  896. * the handler will be performed in a manner equivalent to using
  897. * asio::io_context::post().
  898. */
  899. template <typename AsyncReadStream, typename Allocator,
  900. typename CompletionCondition, typename ReadHandler>
  901. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  902. void (asio::error_code, std::size_t))
  903. async_read(AsyncReadStream& s, basic_streambuf<Allocator>& b,
  904. CompletionCondition completion_condition,
  905. ASIO_MOVE_ARG(ReadHandler) handler);
  906. #endif // !defined(ASIO_NO_IOSTREAM)
  907. #endif // !defined(ASIO_NO_EXTENSIONS)
  908. /*@}*/
  909. } // namespace asio
  910. #include "asio/detail/pop_options.hpp"
  911. #include "asio/impl/read.hpp"
  912. #endif // ASIO_READ_HPP