read.hpp 38 KB

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