read.hpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. //
  2. // read.hpp
  3. // ~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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/completion_condition.hpp>
  20. #include <boost/asio/error.hpp>
  21. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  22. # include <boost/asio/basic_streambuf_fwd.hpp>
  23. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. template <typename> class initiate_async_read;
  29. #if !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  30. template <typename> class initiate_async_read_dynbuf_v1;
  31. #endif // !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  32. template <typename> class initiate_async_read_dynbuf_v2;
  33. } // namespace detail
  34. /**
  35. * @defgroup read boost::asio::read
  36. *
  37. * @brief The @c read function is a composed operation that reads a certain
  38. * amount of data from a stream before returning.
  39. */
  40. /*@{*/
  41. /// Attempt to read a certain amount of data from a stream before returning.
  42. /**
  43. * This function is used to read a certain number of bytes of data from a
  44. * stream. The call will block until one of the following conditions is true:
  45. *
  46. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  47. * the sum of the buffer sizes.
  48. *
  49. * @li An error occurred.
  50. *
  51. * This operation is implemented in terms of zero or more calls to the stream's
  52. * read_some function.
  53. *
  54. * @param s The stream from which the data is to be read. The type must support
  55. * the SyncReadStream concept.
  56. *
  57. * @param buffers One or more buffers into which the data will be read. The sum
  58. * of the buffer sizes indicates the maximum number of bytes to read from the
  59. * stream.
  60. *
  61. * @returns The number of bytes transferred.
  62. *
  63. * @throws boost::system::system_error Thrown on failure.
  64. *
  65. * @par Example
  66. * To read into a single data buffer use the @ref buffer function as follows:
  67. * @code boost::asio::read(s, boost::asio::buffer(data, size)); @endcode
  68. * See the @ref buffer documentation for information on reading into multiple
  69. * buffers in one go, and how to use it with arrays, boost::array or
  70. * std::vector.
  71. *
  72. * @note This overload is equivalent to calling:
  73. * @code boost::asio::read(
  74. * s, buffers,
  75. * boost::asio::transfer_all()); @endcode
  76. */
  77. template <typename SyncReadStream, typename MutableBufferSequence>
  78. std::size_t read(SyncReadStream& s, const MutableBufferSequence& buffers,
  79. constraint_t<
  80. is_mutable_buffer_sequence<MutableBufferSequence>::value
  81. > = 0);
  82. /// Attempt to read a certain amount of data from a stream before returning.
  83. /**
  84. * This function is used to read a certain number of bytes of data from a
  85. * stream. The call will block until one of the following conditions is true:
  86. *
  87. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  88. * the sum of the buffer sizes.
  89. *
  90. * @li An error occurred.
  91. *
  92. * This operation is implemented in terms of zero or more calls to the stream's
  93. * read_some function.
  94. *
  95. * @param s The stream from which the data is to be read. The type must support
  96. * the SyncReadStream concept.
  97. *
  98. * @param buffers One or more buffers into which the data will be read. The sum
  99. * of the buffer sizes indicates the maximum number of bytes to read from the
  100. * stream.
  101. *
  102. * @param ec Set to indicate what error occurred, if any.
  103. *
  104. * @returns The number of bytes transferred.
  105. *
  106. * @par Example
  107. * To read into a single data buffer use the @ref buffer function as follows:
  108. * @code boost::asio::read(s, boost::asio::buffer(data, size), ec); @endcode
  109. * See the @ref buffer documentation for information on reading into multiple
  110. * buffers in one go, and how to use it with arrays, boost::array or
  111. * std::vector.
  112. *
  113. * @note This overload is equivalent to calling:
  114. * @code boost::asio::read(
  115. * s, buffers,
  116. * boost::asio::transfer_all(), ec); @endcode
  117. */
  118. template <typename SyncReadStream, typename MutableBufferSequence>
  119. std::size_t read(SyncReadStream& s, const MutableBufferSequence& buffers,
  120. boost::system::error_code& ec,
  121. constraint_t<
  122. is_mutable_buffer_sequence<MutableBufferSequence>::value
  123. > = 0);
  124. /// Attempt to read a certain amount of data from a stream before returning.
  125. /**
  126. * This function is used to read a certain number of bytes of data from a
  127. * stream. The call will block until one of the following conditions is true:
  128. *
  129. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  130. * the sum of the buffer sizes.
  131. *
  132. * @li The completion_condition function object returns 0.
  133. *
  134. * This operation is implemented in terms of zero or more calls to the stream's
  135. * read_some function.
  136. *
  137. * @param s The stream from which the data is to be read. The type must support
  138. * the SyncReadStream concept.
  139. *
  140. * @param buffers One or more buffers into which the data will be read. The sum
  141. * of the buffer sizes indicates the maximum number of bytes to read from the
  142. * stream.
  143. *
  144. * @param completion_condition The function object to be called to determine
  145. * whether the read operation is complete. The signature of the function object
  146. * must be:
  147. * @code std::size_t completion_condition(
  148. * // Result of latest read_some operation.
  149. * const boost::system::error_code& error,
  150. *
  151. * // Number of bytes transferred so far.
  152. * std::size_t bytes_transferred
  153. * ); @endcode
  154. * A return value of 0 indicates that the read operation is complete. A non-zero
  155. * return value indicates the maximum number of bytes to be read on the next
  156. * call to the stream's read_some function.
  157. *
  158. * @returns The number of bytes transferred.
  159. *
  160. * @throws boost::system::system_error Thrown on failure.
  161. *
  162. * @par Example
  163. * To read into a single data buffer use the @ref buffer function as follows:
  164. * @code boost::asio::read(s, boost::asio::buffer(data, size),
  165. * boost::asio::transfer_at_least(32)); @endcode
  166. * See the @ref buffer documentation for information on reading into multiple
  167. * buffers in one go, and how to use it with arrays, boost::array or
  168. * std::vector.
  169. */
  170. template <typename SyncReadStream, typename MutableBufferSequence,
  171. typename CompletionCondition>
  172. std::size_t read(SyncReadStream& s, const MutableBufferSequence& buffers,
  173. CompletionCondition completion_condition,
  174. constraint_t<
  175. is_mutable_buffer_sequence<MutableBufferSequence>::value
  176. > = 0);
  177. /// Attempt to read a certain amount of data from a stream before returning.
  178. /**
  179. * This function is used to read a certain number of bytes of data from a
  180. * stream. The call will block until one of the following conditions is true:
  181. *
  182. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  183. * the sum of the buffer sizes.
  184. *
  185. * @li The completion_condition function object returns 0.
  186. *
  187. * This operation is implemented in terms of zero or more calls to the stream's
  188. * read_some function.
  189. *
  190. * @param s The stream from which the data is to be read. The type must support
  191. * the SyncReadStream concept.
  192. *
  193. * @param buffers One or more buffers into which the data will be read. The sum
  194. * of the buffer sizes indicates the maximum number of bytes to read from the
  195. * stream.
  196. *
  197. * @param completion_condition The function object to be called to determine
  198. * whether the read operation is complete. The signature of the function object
  199. * must be:
  200. * @code std::size_t completion_condition(
  201. * // Result of latest read_some operation.
  202. * const boost::system::error_code& error,
  203. *
  204. * // Number of bytes transferred so far.
  205. * std::size_t bytes_transferred
  206. * ); @endcode
  207. * A return value of 0 indicates that the read operation is complete. A non-zero
  208. * return value indicates the maximum number of bytes to be read on the next
  209. * call to the stream's read_some function.
  210. *
  211. * @param ec Set to indicate what error occurred, if any.
  212. *
  213. * @returns The number of bytes read. If an error occurs, returns the total
  214. * number of bytes successfully transferred prior to the error.
  215. */
  216. template <typename SyncReadStream, typename MutableBufferSequence,
  217. typename CompletionCondition>
  218. std::size_t read(SyncReadStream& s, const MutableBufferSequence& buffers,
  219. CompletionCondition completion_condition, boost::system::error_code& ec,
  220. constraint_t<
  221. is_mutable_buffer_sequence<MutableBufferSequence>::value
  222. > = 0);
  223. #if !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  224. /// Attempt to read a certain amount of data from a stream before returning.
  225. /**
  226. * This function is used to read a certain number of bytes of data from a
  227. * stream. The call will block until one of the following conditions is true:
  228. *
  229. * @li The specified dynamic buffer sequence is full (that is, it has reached
  230. * maximum size).
  231. *
  232. * @li An error occurred.
  233. *
  234. * This operation is implemented in terms of zero or more calls to the stream's
  235. * read_some function.
  236. *
  237. * @param s The stream from which the data is to be read. The type must support
  238. * the SyncReadStream concept.
  239. *
  240. * @param buffers The dynamic buffer sequence into which the data will be read.
  241. *
  242. * @returns The number of bytes transferred.
  243. *
  244. * @throws boost::system::system_error Thrown on failure.
  245. *
  246. * @note This overload is equivalent to calling:
  247. * @code boost::asio::read(
  248. * s, buffers,
  249. * boost::asio::transfer_all()); @endcode
  250. */
  251. template <typename SyncReadStream, typename DynamicBuffer_v1>
  252. std::size_t read(SyncReadStream& s,
  253. DynamicBuffer_v1&& buffers,
  254. constraint_t<
  255. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  256. > = 0,
  257. constraint_t<
  258. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  259. > = 0);
  260. /// Attempt to read a certain amount of data from a stream before returning.
  261. /**
  262. * This function is used to read a certain number of bytes of data from a
  263. * stream. The call will block until one of the following conditions is true:
  264. *
  265. * @li The supplied buffer is full (that is, it has reached maximum size).
  266. *
  267. * @li An error occurred.
  268. *
  269. * This operation is implemented in terms of zero or more calls to the stream's
  270. * read_some function.
  271. *
  272. * @param s The stream from which the data is to be read. The type must support
  273. * the SyncReadStream concept.
  274. *
  275. * @param buffers The dynamic buffer sequence into which the data will be read.
  276. *
  277. * @param ec Set to indicate what error occurred, if any.
  278. *
  279. * @returns The number of bytes transferred.
  280. *
  281. * @note This overload is equivalent to calling:
  282. * @code boost::asio::read(
  283. * s, buffers,
  284. * boost::asio::transfer_all(), ec); @endcode
  285. */
  286. template <typename SyncReadStream, typename DynamicBuffer_v1>
  287. std::size_t read(SyncReadStream& s,
  288. DynamicBuffer_v1&& buffers,
  289. boost::system::error_code& ec,
  290. constraint_t<
  291. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  292. > = 0,
  293. constraint_t<
  294. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  295. > = 0);
  296. /// Attempt to read a certain amount of data from a stream before returning.
  297. /**
  298. * This function is used to read a certain number of bytes of data from a
  299. * stream. The call will block until one of the following conditions is true:
  300. *
  301. * @li The specified dynamic buffer sequence is full (that is, it has reached
  302. * maximum size).
  303. *
  304. * @li The completion_condition function object returns 0.
  305. *
  306. * This operation is implemented in terms of zero or more calls to the stream's
  307. * read_some function.
  308. *
  309. * @param s The stream from which the data is to be read. The type must support
  310. * the SyncReadStream concept.
  311. *
  312. * @param buffers The dynamic buffer sequence into which the data will be read.
  313. *
  314. * @param completion_condition The function object to be called to determine
  315. * whether the read operation is complete. The signature of the function object
  316. * must be:
  317. * @code std::size_t completion_condition(
  318. * // Result of latest read_some operation.
  319. * const boost::system::error_code& error,
  320. *
  321. * // Number of bytes transferred so far.
  322. * std::size_t bytes_transferred
  323. * ); @endcode
  324. * A return value of 0 indicates that the read operation is complete. A non-zero
  325. * return value indicates the maximum number of bytes to be read on the next
  326. * call to the stream's read_some function.
  327. *
  328. * @returns The number of bytes transferred.
  329. *
  330. * @throws boost::system::system_error Thrown on failure.
  331. */
  332. template <typename SyncReadStream, typename DynamicBuffer_v1,
  333. typename CompletionCondition>
  334. std::size_t read(SyncReadStream& s,
  335. DynamicBuffer_v1&& buffers,
  336. CompletionCondition completion_condition,
  337. constraint_t<
  338. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  339. > = 0,
  340. constraint_t<
  341. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  342. > = 0);
  343. /// Attempt to read a certain amount of data from a stream before returning.
  344. /**
  345. * This function is used to read a certain number of bytes of data from a
  346. * stream. The call will block until one of the following conditions is true:
  347. *
  348. * @li The specified dynamic buffer sequence is full (that is, it has reached
  349. * maximum size).
  350. *
  351. * @li The completion_condition function object returns 0.
  352. *
  353. * This operation is implemented in terms of zero or more calls to the stream's
  354. * read_some function.
  355. *
  356. * @param s The stream from which the data is to be read. The type must support
  357. * the SyncReadStream concept.
  358. *
  359. * @param buffers The dynamic buffer sequence into which the data will be read.
  360. *
  361. * @param completion_condition The function object to be called to determine
  362. * whether the read operation is complete. The signature of the function object
  363. * must be:
  364. * @code std::size_t completion_condition(
  365. * // Result of latest read_some operation.
  366. * const boost::system::error_code& error,
  367. *
  368. * // Number of bytes transferred so far.
  369. * std::size_t bytes_transferred
  370. * ); @endcode
  371. * A return value of 0 indicates that the read operation is complete. A non-zero
  372. * return value indicates the maximum number of bytes to be read on the next
  373. * call to the stream's read_some function.
  374. *
  375. * @param ec Set to indicate what error occurred, if any.
  376. *
  377. * @returns The number of bytes read. If an error occurs, returns the total
  378. * number of bytes successfully transferred prior to the error.
  379. */
  380. template <typename SyncReadStream, typename DynamicBuffer_v1,
  381. typename CompletionCondition>
  382. std::size_t read(SyncReadStream& s,
  383. DynamicBuffer_v1&& buffers,
  384. CompletionCondition completion_condition, boost::system::error_code& ec,
  385. constraint_t<
  386. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  387. > = 0,
  388. constraint_t<
  389. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  390. > = 0);
  391. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  392. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  393. /// Attempt to read a certain amount of data from a stream before returning.
  394. /**
  395. * This function is used to read a certain number of bytes of data from a
  396. * stream. The call will block until one of the following conditions is true:
  397. *
  398. * @li The supplied buffer is full (that is, it has reached maximum size).
  399. *
  400. * @li An error occurred.
  401. *
  402. * This operation is implemented in terms of zero or more calls to the stream's
  403. * read_some function.
  404. *
  405. * @param s The stream from which the data is to be read. The type must support
  406. * the SyncReadStream concept.
  407. *
  408. * @param b The basic_streambuf object into which the data will be read.
  409. *
  410. * @returns The number of bytes transferred.
  411. *
  412. * @throws boost::system::system_error Thrown on failure.
  413. *
  414. * @note This overload is equivalent to calling:
  415. * @code boost::asio::read(
  416. * s, b,
  417. * boost::asio::transfer_all()); @endcode
  418. */
  419. template <typename SyncReadStream, typename Allocator>
  420. std::size_t read(SyncReadStream& s, basic_streambuf<Allocator>& b);
  421. /// Attempt to read a certain amount of data from a stream before returning.
  422. /**
  423. * This function is used to read a certain number of bytes of data from a
  424. * stream. The call will block until one of the following conditions is true:
  425. *
  426. * @li The supplied buffer is full (that is, it has reached maximum size).
  427. *
  428. * @li An error occurred.
  429. *
  430. * This operation is implemented in terms of zero or more calls to the stream's
  431. * read_some function.
  432. *
  433. * @param s The stream from which the data is to be read. The type must support
  434. * the SyncReadStream concept.
  435. *
  436. * @param b The basic_streambuf object into which the data will be read.
  437. *
  438. * @param ec Set to indicate what error occurred, if any.
  439. *
  440. * @returns The number of bytes transferred.
  441. *
  442. * @note This overload is equivalent to calling:
  443. * @code boost::asio::read(
  444. * s, b,
  445. * boost::asio::transfer_all(), ec); @endcode
  446. */
  447. template <typename SyncReadStream, typename Allocator>
  448. std::size_t read(SyncReadStream& s, basic_streambuf<Allocator>& b,
  449. boost::system::error_code& ec);
  450. /// Attempt to read a certain amount of data from a stream before returning.
  451. /**
  452. * This function is used to read a certain number of bytes of data from a
  453. * stream. The call will block until one of the following conditions is true:
  454. *
  455. * @li The supplied buffer is full (that is, it has reached maximum size).
  456. *
  457. * @li The completion_condition function object returns 0.
  458. *
  459. * This operation is implemented in terms of zero or more calls to the stream's
  460. * read_some function.
  461. *
  462. * @param s The stream from which the data is to be read. The type must support
  463. * the SyncReadStream concept.
  464. *
  465. * @param b The basic_streambuf object into which the data will be read.
  466. *
  467. * @param completion_condition The function object to be called to determine
  468. * whether the read operation is complete. The signature of the function object
  469. * must be:
  470. * @code std::size_t completion_condition(
  471. * // Result of latest read_some operation.
  472. * const boost::system::error_code& error,
  473. *
  474. * // Number of bytes transferred so far.
  475. * std::size_t bytes_transferred
  476. * ); @endcode
  477. * A return value of 0 indicates that the read operation is complete. A non-zero
  478. * return value indicates the maximum number of bytes to be read on the next
  479. * call to the stream's read_some function.
  480. *
  481. * @returns The number of bytes transferred.
  482. *
  483. * @throws boost::system::system_error Thrown on failure.
  484. */
  485. template <typename SyncReadStream, typename Allocator,
  486. typename CompletionCondition>
  487. std::size_t read(SyncReadStream& s, basic_streambuf<Allocator>& b,
  488. CompletionCondition completion_condition);
  489. /// Attempt to read a certain amount of data from a stream before returning.
  490. /**
  491. * This function is used to read a certain number of bytes of data from a
  492. * stream. The call will block until one of the following conditions is true:
  493. *
  494. * @li The supplied buffer is full (that is, it has reached maximum size).
  495. *
  496. * @li The completion_condition function object returns 0.
  497. *
  498. * This operation is implemented in terms of zero or more calls to the stream's
  499. * read_some function.
  500. *
  501. * @param s The stream from which the data is to be read. The type must support
  502. * the SyncReadStream concept.
  503. *
  504. * @param b The basic_streambuf object into which the data will be read.
  505. *
  506. * @param completion_condition The function object to be called to determine
  507. * whether the read operation is complete. The signature of the function object
  508. * must be:
  509. * @code std::size_t completion_condition(
  510. * // Result of latest read_some operation.
  511. * const boost::system::error_code& error,
  512. *
  513. * // Number of bytes transferred so far.
  514. * std::size_t bytes_transferred
  515. * ); @endcode
  516. * A return value of 0 indicates that the read operation is complete. A non-zero
  517. * return value indicates the maximum number of bytes to be read on the next
  518. * call to the stream's read_some function.
  519. *
  520. * @param ec Set to indicate what error occurred, if any.
  521. *
  522. * @returns The number of bytes read. If an error occurs, returns the total
  523. * number of bytes successfully transferred prior to the error.
  524. */
  525. template <typename SyncReadStream, typename Allocator,
  526. typename CompletionCondition>
  527. std::size_t read(SyncReadStream& s, basic_streambuf<Allocator>& b,
  528. CompletionCondition completion_condition, boost::system::error_code& ec);
  529. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  530. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  531. #endif // !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  532. /// Attempt to read a certain amount of data from a stream before returning.
  533. /**
  534. * This function is used to read a certain number of bytes of data from a
  535. * stream. The call will block until one of the following conditions is true:
  536. *
  537. * @li The specified dynamic buffer sequence is full (that is, it has reached
  538. * maximum size).
  539. *
  540. * @li An error occurred.
  541. *
  542. * This operation is implemented in terms of zero or more calls to the stream's
  543. * read_some function.
  544. *
  545. * @param s The stream from which the data is to be read. The type must support
  546. * the SyncReadStream concept.
  547. *
  548. * @param buffers The dynamic buffer sequence into which the data will be read.
  549. *
  550. * @returns The number of bytes transferred.
  551. *
  552. * @throws boost::system::system_error Thrown on failure.
  553. *
  554. * @note This overload is equivalent to calling:
  555. * @code boost::asio::read(
  556. * s, buffers,
  557. * boost::asio::transfer_all()); @endcode
  558. */
  559. template <typename SyncReadStream, typename DynamicBuffer_v2>
  560. std::size_t read(SyncReadStream& s, DynamicBuffer_v2 buffers,
  561. constraint_t<
  562. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  563. > = 0);
  564. /// Attempt to read a certain amount of data from a stream before returning.
  565. /**
  566. * This function is used to read a certain number of bytes of data from a
  567. * stream. The call will block until one of the following conditions is true:
  568. *
  569. * @li The supplied buffer is full (that is, it has reached maximum size).
  570. *
  571. * @li An error occurred.
  572. *
  573. * This operation is implemented in terms of zero or more calls to the stream's
  574. * read_some function.
  575. *
  576. * @param s The stream from which the data is to be read. The type must support
  577. * the SyncReadStream concept.
  578. *
  579. * @param buffers The dynamic buffer sequence into which the data will be read.
  580. *
  581. * @param ec Set to indicate what error occurred, if any.
  582. *
  583. * @returns The number of bytes transferred.
  584. *
  585. * @note This overload is equivalent to calling:
  586. * @code boost::asio::read(
  587. * s, buffers,
  588. * boost::asio::transfer_all(), ec); @endcode
  589. */
  590. template <typename SyncReadStream, typename DynamicBuffer_v2>
  591. std::size_t read(SyncReadStream& s, DynamicBuffer_v2 buffers,
  592. boost::system::error_code& ec,
  593. constraint_t<
  594. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  595. > = 0);
  596. /// Attempt to read a certain amount of data from a stream before returning.
  597. /**
  598. * This function is used to read a certain number of bytes of data from a
  599. * stream. The call will block until one of the following conditions is true:
  600. *
  601. * @li The specified dynamic buffer sequence is full (that is, it has reached
  602. * maximum size).
  603. *
  604. * @li The completion_condition function object returns 0.
  605. *
  606. * This operation is implemented in terms of zero or more calls to the stream's
  607. * read_some function.
  608. *
  609. * @param s The stream from which the data is to be read. The type must support
  610. * the SyncReadStream concept.
  611. *
  612. * @param buffers The dynamic buffer sequence into which the data will be read.
  613. *
  614. * @param completion_condition The function object to be called to determine
  615. * whether the read operation is complete. The signature of the function object
  616. * must be:
  617. * @code std::size_t completion_condition(
  618. * // Result of latest read_some operation.
  619. * const boost::system::error_code& error,
  620. *
  621. * // Number of bytes transferred so far.
  622. * std::size_t bytes_transferred
  623. * ); @endcode
  624. * A return value of 0 indicates that the read operation is complete. A non-zero
  625. * return value indicates the maximum number of bytes to be read on the next
  626. * call to the stream's read_some function.
  627. *
  628. * @returns The number of bytes transferred.
  629. *
  630. * @throws boost::system::system_error Thrown on failure.
  631. */
  632. template <typename SyncReadStream, typename DynamicBuffer_v2,
  633. typename CompletionCondition>
  634. std::size_t read(SyncReadStream& s, DynamicBuffer_v2 buffers,
  635. CompletionCondition completion_condition,
  636. constraint_t<
  637. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  638. > = 0);
  639. /// Attempt to read a certain amount of data from a stream before returning.
  640. /**
  641. * This function is used to read a certain number of bytes of data from a
  642. * stream. The call will block until one of the following conditions is true:
  643. *
  644. * @li The specified dynamic buffer sequence is full (that is, it has reached
  645. * maximum size).
  646. *
  647. * @li The completion_condition function object returns 0.
  648. *
  649. * This operation is implemented in terms of zero or more calls to the stream's
  650. * read_some function.
  651. *
  652. * @param s The stream from which the data is to be read. The type must support
  653. * the SyncReadStream concept.
  654. *
  655. * @param buffers The dynamic buffer sequence into which the data will be read.
  656. *
  657. * @param completion_condition The function object to be called to determine
  658. * whether the read operation is complete. The signature of the function object
  659. * must be:
  660. * @code std::size_t completion_condition(
  661. * // Result of latest read_some operation.
  662. * const boost::system::error_code& error,
  663. *
  664. * // Number of bytes transferred so far.
  665. * std::size_t bytes_transferred
  666. * ); @endcode
  667. * A return value of 0 indicates that the read operation is complete. A non-zero
  668. * return value indicates the maximum number of bytes to be read on the next
  669. * call to the stream's read_some function.
  670. *
  671. * @param ec Set to indicate what error occurred, if any.
  672. *
  673. * @returns The number of bytes read. If an error occurs, returns the total
  674. * number of bytes successfully transferred prior to the error.
  675. */
  676. template <typename SyncReadStream, typename DynamicBuffer_v2,
  677. typename CompletionCondition>
  678. std::size_t read(SyncReadStream& s, DynamicBuffer_v2 buffers,
  679. CompletionCondition completion_condition, boost::system::error_code& ec,
  680. constraint_t<
  681. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  682. > = 0);
  683. /*@}*/
  684. /**
  685. * @defgroup async_read boost::asio::async_read
  686. *
  687. * @brief The @c async_read function is a composed asynchronous operation that
  688. * reads a certain amount of data from a stream before completion.
  689. */
  690. /*@{*/
  691. /// Start an asynchronous operation to read a certain amount of data from a
  692. /// stream.
  693. /**
  694. * This function is used to asynchronously read a certain number of bytes of
  695. * data from a stream. It is an initiating function for an @ref
  696. * asynchronous_operation, and always returns immediately. The asynchronous
  697. * operation will continue until one of the following conditions is true:
  698. *
  699. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  700. * the sum of the buffer sizes.
  701. *
  702. * @li An error occurred.
  703. *
  704. * This operation is implemented in terms of zero or more calls to the stream's
  705. * async_read_some function, and is known as a <em>composed operation</em>. The
  706. * program must ensure that the stream performs no other read operations (such
  707. * as async_read, the stream's async_read_some function, or any other composed
  708. * operations that perform reads) until this operation completes.
  709. *
  710. * @param s The stream from which the data is to be read. The type must support
  711. * the AsyncReadStream concept.
  712. *
  713. * @param buffers One or more buffers into which the data will be read. The sum
  714. * of the buffer sizes indicates the maximum number of bytes to read from the
  715. * stream. Although the buffers object may be copied as necessary, ownership of
  716. * the underlying memory blocks is retained by the caller, which must guarantee
  717. * that they remain valid until the completion handler is called.
  718. *
  719. * @param token The @ref completion_token that will be used to produce a
  720. * completion handler, which will be called when the read completes.
  721. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  722. * @ref yield_context, or a function object with the correct completion
  723. * signature. The function signature of the completion handler must be:
  724. * @code void handler(
  725. * // Result of operation.
  726. * const boost::system::error_code& error,
  727. *
  728. * // Number of bytes copied into the buffers. If an error
  729. * // occurred, this will be the number of bytes successfully
  730. * // transferred prior to the error.
  731. * std::size_t bytes_transferred
  732. * ); @endcode
  733. * Regardless of whether the asynchronous operation completes immediately or
  734. * not, the completion handler will not be invoked from within this function.
  735. * On immediate completion, invocation of the handler will be performed in a
  736. * manner equivalent to using boost::asio::post().
  737. *
  738. * @par Completion Signature
  739. * @code void(boost::system::error_code, std::size_t) @endcode
  740. *
  741. * @par Example
  742. * To read into a single data buffer use the @ref buffer function as follows:
  743. * @code
  744. * boost::asio::async_read(s, boost::asio::buffer(data, size), handler);
  745. * @endcode
  746. * See the @ref buffer documentation for information on reading into multiple
  747. * buffers in one go, and how to use it with arrays, boost::array or
  748. * std::vector.
  749. *
  750. * @note This overload is equivalent to calling:
  751. * @code boost::asio::async_read(
  752. * s, buffers,
  753. * boost::asio::transfer_all(),
  754. * handler); @endcode
  755. *
  756. * @par Per-Operation Cancellation
  757. * This asynchronous operation supports cancellation for the following
  758. * boost::asio::cancellation_type values:
  759. *
  760. * @li @c cancellation_type::terminal
  761. *
  762. * @li @c cancellation_type::partial
  763. *
  764. * if they are also supported by the @c AsyncReadStream type's
  765. * @c async_read_some operation.
  766. */
  767. template <typename AsyncReadStream, typename MutableBufferSequence,
  768. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  769. std::size_t)) ReadToken = default_completion_token_t<
  770. typename AsyncReadStream::executor_type>>
  771. auto async_read(AsyncReadStream& s, const MutableBufferSequence& buffers,
  772. ReadToken&& token = default_completion_token_t<
  773. typename AsyncReadStream::executor_type>(),
  774. constraint_t<
  775. is_mutable_buffer_sequence<MutableBufferSequence>::value
  776. > = 0)
  777. -> decltype(
  778. async_initiate<ReadToken,
  779. void (boost::system::error_code, std::size_t)>(
  780. declval<detail::initiate_async_read<AsyncReadStream>>(),
  781. token, buffers, transfer_all()));
  782. /// Start an asynchronous operation to read a certain amount of data from a
  783. /// stream.
  784. /**
  785. * This function is used to asynchronously read a certain number of bytes of
  786. * data from a stream. It is an initiating function for an @ref
  787. * asynchronous_operation, and always returns immediately. The asynchronous
  788. * operation will continue until one of the following conditions is true:
  789. *
  790. * @li The supplied buffers are full. That is, the bytes transferred is equal to
  791. * the sum of the buffer sizes.
  792. *
  793. * @li The completion_condition function object returns 0.
  794. *
  795. * @param s The stream from which the data is to be read. The type must support
  796. * the AsyncReadStream concept.
  797. *
  798. * @param buffers One or more buffers into which the data will be read. The sum
  799. * of the buffer sizes indicates the maximum number of bytes to read from the
  800. * stream. Although the buffers object may be copied as necessary, ownership of
  801. * the underlying memory blocks is retained by the caller, which must guarantee
  802. * that they remain valid until the completion handler is called.
  803. *
  804. * @param completion_condition The function object to be called to determine
  805. * whether the read operation is complete. The signature of the function object
  806. * must be:
  807. * @code std::size_t completion_condition(
  808. * // Result of latest async_read_some operation.
  809. * const boost::system::error_code& error,
  810. *
  811. * // Number of bytes transferred so far.
  812. * std::size_t bytes_transferred
  813. * ); @endcode
  814. * A return value of 0 indicates that the read operation is complete. A non-zero
  815. * return value indicates the maximum number of bytes to be read on the next
  816. * call to the stream's async_read_some function.
  817. *
  818. * @param token The @ref completion_token that will be used to produce a
  819. * completion handler, which will be called when the read completes.
  820. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  821. * @ref yield_context, or a function object with the correct completion
  822. * signature. The function signature of the completion handler must be:
  823. * @code void handler(
  824. * // Result of operation.
  825. * const boost::system::error_code& error,
  826. *
  827. * // Number of bytes copied into the buffers. If an error
  828. * // occurred, this will be the number of bytes successfully
  829. * // transferred prior to the error.
  830. * std::size_t bytes_transferred
  831. * ); @endcode
  832. * Regardless of whether the asynchronous operation completes immediately or
  833. * not, the completion handler will not be invoked from within this function.
  834. * On immediate completion, invocation of the handler will be performed in a
  835. * manner equivalent to using boost::asio::post().
  836. *
  837. * @par Completion Signature
  838. * @code void(boost::system::error_code, std::size_t) @endcode
  839. *
  840. * @par Example
  841. * To read into a single data buffer use the @ref buffer function as follows:
  842. * @code boost::asio::async_read(s,
  843. * boost::asio::buffer(data, size),
  844. * boost::asio::transfer_at_least(32),
  845. * handler); @endcode
  846. * See the @ref buffer documentation for information on reading into multiple
  847. * buffers in one go, and how to use it with arrays, boost::array or
  848. * std::vector.
  849. *
  850. * @par Per-Operation Cancellation
  851. * This asynchronous operation supports cancellation for the following
  852. * boost::asio::cancellation_type values:
  853. *
  854. * @li @c cancellation_type::terminal
  855. *
  856. * @li @c cancellation_type::partial
  857. *
  858. * if they are also supported by the @c AsyncReadStream type's
  859. * @c async_read_some operation.
  860. */
  861. template <typename AsyncReadStream,
  862. typename MutableBufferSequence, typename CompletionCondition,
  863. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  864. std::size_t)) ReadToken = default_completion_token_t<
  865. typename AsyncReadStream::executor_type>>
  866. auto async_read(AsyncReadStream& s, const MutableBufferSequence& buffers,
  867. CompletionCondition completion_condition,
  868. ReadToken&& token = default_completion_token_t<
  869. typename AsyncReadStream::executor_type>(),
  870. constraint_t<
  871. is_mutable_buffer_sequence<MutableBufferSequence>::value
  872. > = 0)
  873. -> decltype(
  874. async_initiate<ReadToken,
  875. void (boost::system::error_code, std::size_t)>(
  876. declval<detail::initiate_async_read<AsyncReadStream>>(),
  877. token, buffers,
  878. static_cast<CompletionCondition&&>(completion_condition)));
  879. #if !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  880. /// Start an asynchronous operation to read a certain amount of data from a
  881. /// stream.
  882. /**
  883. * This function is used to asynchronously read a certain number of bytes of
  884. * data from a stream. It is an initiating function for an @ref
  885. * asynchronous_operation, and always returns immediately. The asynchronous
  886. * operation will continue until one of the following conditions is true:
  887. *
  888. * @li The specified dynamic buffer sequence is full (that is, it has reached
  889. * maximum size).
  890. *
  891. * @li An error occurred.
  892. *
  893. * This operation is implemented in terms of zero or more calls to the stream's
  894. * async_read_some function, and is known as a <em>composed operation</em>. The
  895. * program must ensure that the stream performs no other read operations (such
  896. * as async_read, the stream's async_read_some function, or any other composed
  897. * operations that perform reads) until this operation completes.
  898. *
  899. * @param s The stream from which the data is to be read. The type must support
  900. * the AsyncReadStream concept.
  901. *
  902. * @param buffers The dynamic buffer sequence into which the data will be read.
  903. * Although the buffers object may be copied as necessary, ownership of the
  904. * underlying memory blocks is retained by the caller, which must guarantee
  905. * that they remain valid until the completion handler is called.
  906. *
  907. * @param token The @ref completion_token that will be used to produce a
  908. * completion handler, which will be called when the read completes.
  909. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  910. * @ref yield_context, or a function object with the correct completion
  911. * signature. The function signature of the completion handler must be:
  912. * @code void handler(
  913. * // Result of operation.
  914. * const boost::system::error_code& error,
  915. *
  916. * // Number of bytes copied into the buffers. If an error
  917. * // occurred, this will be the number of bytes successfully
  918. * // transferred prior to the error.
  919. * std::size_t bytes_transferred
  920. * ); @endcode
  921. * Regardless of whether the asynchronous operation completes immediately or
  922. * not, the completion handler will not be invoked from within this function.
  923. * On immediate completion, invocation of the handler will be performed in a
  924. * manner equivalent to using boost::asio::post().
  925. *
  926. * @par Completion Signature
  927. * @code void(boost::system::error_code, std::size_t) @endcode
  928. *
  929. * @note This overload is equivalent to calling:
  930. * @code boost::asio::async_read(
  931. * s, buffers,
  932. * boost::asio::transfer_all(),
  933. * handler); @endcode
  934. *
  935. * @par Per-Operation Cancellation
  936. * This asynchronous operation supports cancellation for the following
  937. * boost::asio::cancellation_type values:
  938. *
  939. * @li @c cancellation_type::terminal
  940. *
  941. * @li @c cancellation_type::partial
  942. *
  943. * if they are also supported by the @c AsyncReadStream type's
  944. * @c async_read_some operation.
  945. */
  946. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  947. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  948. std::size_t)) ReadToken = default_completion_token_t<
  949. typename AsyncReadStream::executor_type>>
  950. auto async_read(AsyncReadStream& s, DynamicBuffer_v1&& buffers,
  951. ReadToken&& token = default_completion_token_t<
  952. typename AsyncReadStream::executor_type>(),
  953. constraint_t<
  954. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  955. > = 0,
  956. constraint_t<
  957. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  958. > = 0)
  959. -> decltype(
  960. async_initiate<ReadToken,
  961. void (boost::system::error_code, std::size_t)>(
  962. declval<detail::initiate_async_read_dynbuf_v1<AsyncReadStream>>(),
  963. token, static_cast<DynamicBuffer_v1&&>(buffers), transfer_all()));
  964. /// Start an asynchronous operation to read a certain amount of data from a
  965. /// stream.
  966. /**
  967. * This function is used to asynchronously read a certain number of bytes of
  968. * data from a stream. It is an initiating function for an @ref
  969. * asynchronous_operation, and always returns immediately. The asynchronous
  970. * operation will continue until one of the following conditions is true:
  971. *
  972. * @li The specified dynamic buffer sequence is full (that is, it has reached
  973. * maximum size).
  974. *
  975. * @li The completion_condition function object returns 0.
  976. *
  977. * This operation is implemented in terms of zero or more calls to the stream's
  978. * async_read_some function, and is known as a <em>composed operation</em>. The
  979. * program must ensure that the stream performs no other read operations (such
  980. * as async_read, the stream's async_read_some function, or any other composed
  981. * operations that perform reads) until this operation completes.
  982. *
  983. * @param s The stream from which the data is to be read. The type must support
  984. * the AsyncReadStream concept.
  985. *
  986. * @param buffers The dynamic buffer sequence into which the data will be read.
  987. * Although the buffers object may be copied as necessary, ownership of the
  988. * underlying memory blocks is retained by the caller, which must guarantee
  989. * that they remain valid until the completion handler is called.
  990. *
  991. * @param completion_condition The function object to be called to determine
  992. * whether the read operation is complete. The signature of the function object
  993. * must be:
  994. * @code std::size_t completion_condition(
  995. * // Result of latest async_read_some operation.
  996. * const boost::system::error_code& error,
  997. *
  998. * // Number of bytes transferred so far.
  999. * std::size_t bytes_transferred
  1000. * ); @endcode
  1001. * A return value of 0 indicates that the read operation is complete. A non-zero
  1002. * return value indicates the maximum number of bytes to be read on the next
  1003. * call to the stream's async_read_some function.
  1004. *
  1005. * @param token The @ref completion_token that will be used to produce a
  1006. * completion handler, which will be called when the read completes.
  1007. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1008. * @ref yield_context, or a function object with the correct completion
  1009. * signature. The function signature of the completion handler must be:
  1010. * @code void handler(
  1011. * // Result of operation.
  1012. * const boost::system::error_code& error,
  1013. *
  1014. * // Number of bytes copied into the buffers. If an error
  1015. * // occurred, this will be the number of bytes successfully
  1016. * // transferred prior to the error.
  1017. * std::size_t bytes_transferred
  1018. * ); @endcode
  1019. * Regardless of whether the asynchronous operation completes immediately or
  1020. * not, the completion handler will not be invoked from within this function.
  1021. * On immediate completion, invocation of the handler will be performed in a
  1022. * manner equivalent to using boost::asio::post().
  1023. *
  1024. * @par Completion Signature
  1025. * @code void(boost::system::error_code, std::size_t) @endcode
  1026. *
  1027. * @par Per-Operation Cancellation
  1028. * This asynchronous operation supports cancellation for the following
  1029. * boost::asio::cancellation_type values:
  1030. *
  1031. * @li @c cancellation_type::terminal
  1032. *
  1033. * @li @c cancellation_type::partial
  1034. *
  1035. * if they are also supported by the @c AsyncReadStream type's
  1036. * @c async_read_some operation.
  1037. */
  1038. template <typename AsyncReadStream,
  1039. typename DynamicBuffer_v1, typename CompletionCondition,
  1040. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1041. std::size_t)) ReadToken = default_completion_token_t<
  1042. typename AsyncReadStream::executor_type>>
  1043. auto async_read(AsyncReadStream& s, DynamicBuffer_v1&& buffers,
  1044. CompletionCondition completion_condition,
  1045. ReadToken&& token = default_completion_token_t<
  1046. typename AsyncReadStream::executor_type>(),
  1047. constraint_t<
  1048. is_dynamic_buffer_v1<decay_t<DynamicBuffer_v1>>::value
  1049. > = 0,
  1050. constraint_t<
  1051. !is_dynamic_buffer_v2<decay_t<DynamicBuffer_v1>>::value
  1052. > = 0)
  1053. -> decltype(
  1054. async_initiate<ReadToken,
  1055. void (boost::system::error_code, std::size_t)>(
  1056. declval<detail::initiate_async_read_dynbuf_v1<AsyncReadStream>>(),
  1057. token, static_cast<DynamicBuffer_v1&&>(buffers),
  1058. static_cast<CompletionCondition&&>(completion_condition)));
  1059. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  1060. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  1061. /// Start an asynchronous operation to read a certain amount of data from a
  1062. /// stream.
  1063. /**
  1064. * This function is used to asynchronously read a certain number of bytes of
  1065. * data from a stream. It is an initiating function for an @ref
  1066. * asynchronous_operation, and always returns immediately. The asynchronous
  1067. * operation will continue until one of the following conditions is true:
  1068. *
  1069. * @li The supplied buffer is full (that is, it has reached maximum size).
  1070. *
  1071. * @li An error occurred.
  1072. *
  1073. * This operation is implemented in terms of zero or more calls to the stream's
  1074. * async_read_some function, and is known as a <em>composed operation</em>. The
  1075. * program must ensure that the stream performs no other read operations (such
  1076. * as async_read, the stream's async_read_some function, or any other composed
  1077. * operations that perform reads) until this operation completes.
  1078. *
  1079. * @param s The stream from which the data is to be read. The type must support
  1080. * the AsyncReadStream concept.
  1081. *
  1082. * @param b A basic_streambuf object into which the data will be read. Ownership
  1083. * of the streambuf is retained by the caller, which must guarantee that it
  1084. * remains valid until the completion handler is called.
  1085. *
  1086. * @param token The @ref completion_token that will be used to produce a
  1087. * completion handler, which will be called when the read completes.
  1088. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1089. * @ref yield_context, or a function object with the correct completion
  1090. * signature. The function signature of the completion handler must be:
  1091. * @code void handler(
  1092. * // Result of operation.
  1093. * const boost::system::error_code& error,
  1094. *
  1095. * // Number of bytes copied into the buffers. If an error
  1096. * // occurred, this will be the number of bytes successfully
  1097. * // transferred prior to the error.
  1098. * std::size_t bytes_transferred
  1099. * ); @endcode
  1100. * Regardless of whether the asynchronous operation completes immediately or
  1101. * not, the completion handler will not be invoked from within this function.
  1102. * On immediate completion, invocation of the handler will be performed in a
  1103. * manner equivalent to using boost::asio::post().
  1104. *
  1105. * @par Completion Signature
  1106. * @code void(boost::system::error_code, std::size_t) @endcode
  1107. *
  1108. * @note This overload is equivalent to calling:
  1109. * @code boost::asio::async_read(
  1110. * s, b,
  1111. * boost::asio::transfer_all(),
  1112. * handler); @endcode
  1113. *
  1114. * @par Per-Operation Cancellation
  1115. * This asynchronous operation supports cancellation for the following
  1116. * boost::asio::cancellation_type values:
  1117. *
  1118. * @li @c cancellation_type::terminal
  1119. *
  1120. * @li @c cancellation_type::partial
  1121. *
  1122. * if they are also supported by the @c AsyncReadStream type's
  1123. * @c async_read_some operation.
  1124. */
  1125. template <typename AsyncReadStream, typename Allocator,
  1126. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1127. std::size_t)) ReadToken = default_completion_token_t<
  1128. typename AsyncReadStream::executor_type>>
  1129. auto async_read(AsyncReadStream& s, basic_streambuf<Allocator>& b,
  1130. ReadToken&& token = default_completion_token_t<
  1131. typename AsyncReadStream::executor_type>())
  1132. -> decltype(
  1133. async_initiate<ReadToken,
  1134. void (boost::system::error_code, std::size_t)>(
  1135. declval<detail::initiate_async_read_dynbuf_v1<AsyncReadStream>>(),
  1136. token, basic_streambuf_ref<Allocator>(b), transfer_all()));
  1137. /// Start an asynchronous operation to read a certain amount of data from a
  1138. /// stream.
  1139. /**
  1140. * This function is used to asynchronously read a certain number of bytes of
  1141. * data from a stream. It is an initiating function for an @ref
  1142. * asynchronous_operation, and always returns immediately. The asynchronous
  1143. * operation will continue until one of the following conditions is true:
  1144. *
  1145. * @li The supplied buffer is full (that is, it has reached maximum size).
  1146. *
  1147. * @li The completion_condition function object returns 0.
  1148. *
  1149. * This operation is implemented in terms of zero or more calls to the stream's
  1150. * async_read_some function, and is known as a <em>composed operation</em>. The
  1151. * program must ensure that the stream performs no other read operations (such
  1152. * as async_read, the stream's async_read_some function, or any other composed
  1153. * operations that perform reads) until this operation completes.
  1154. *
  1155. * @param s The stream from which the data is to be read. The type must support
  1156. * the AsyncReadStream concept.
  1157. *
  1158. * @param b A basic_streambuf object into which the data will be read. Ownership
  1159. * of the streambuf is retained by the caller, which must guarantee that it
  1160. * remains valid until the completion handler is called.
  1161. *
  1162. * @param completion_condition The function object to be called to determine
  1163. * whether the read operation is complete. The signature of the function object
  1164. * must be:
  1165. * @code std::size_t completion_condition(
  1166. * // Result of latest async_read_some operation.
  1167. * const boost::system::error_code& error,
  1168. *
  1169. * // Number of bytes transferred so far.
  1170. * std::size_t bytes_transferred
  1171. * ); @endcode
  1172. * A return value of 0 indicates that the read operation is complete. A non-zero
  1173. * return value indicates the maximum number of bytes to be read on the next
  1174. * call to the stream's async_read_some function.
  1175. *
  1176. * @param token The @ref completion_token that will be used to produce a
  1177. * completion handler, which will be called when the read completes.
  1178. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1179. * @ref yield_context, or a function object with the correct completion
  1180. * signature. The function signature of the completion handler must be:
  1181. * @code void handler(
  1182. * // Result of operation.
  1183. * const boost::system::error_code& error,
  1184. *
  1185. * // Number of bytes copied into the buffers. If an error
  1186. * // occurred, this will be the number of bytes successfully
  1187. * // transferred prior to the error.
  1188. * std::size_t bytes_transferred
  1189. * ); @endcode
  1190. * Regardless of whether the asynchronous operation completes immediately or
  1191. * not, the completion handler will not be invoked from within this function.
  1192. * On immediate completion, invocation of the handler will be performed in a
  1193. * manner equivalent to using boost::asio::post().
  1194. *
  1195. * @par Completion Signature
  1196. * @code void(boost::system::error_code, std::size_t) @endcode
  1197. *
  1198. * @par Per-Operation Cancellation
  1199. * This asynchronous operation supports cancellation for the following
  1200. * boost::asio::cancellation_type values:
  1201. *
  1202. * @li @c cancellation_type::terminal
  1203. *
  1204. * @li @c cancellation_type::partial
  1205. *
  1206. * if they are also supported by the @c AsyncReadStream type's
  1207. * @c async_read_some operation.
  1208. */
  1209. template <typename AsyncReadStream,
  1210. typename Allocator, typename CompletionCondition,
  1211. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1212. std::size_t)) ReadToken = default_completion_token_t<
  1213. typename AsyncReadStream::executor_type>>
  1214. auto async_read(AsyncReadStream& s, basic_streambuf<Allocator>& b,
  1215. CompletionCondition completion_condition,
  1216. ReadToken&& token = default_completion_token_t<
  1217. typename AsyncReadStream::executor_type>())
  1218. -> decltype(
  1219. async_initiate<ReadToken,
  1220. void (boost::system::error_code, std::size_t)>(
  1221. declval<detail::initiate_async_read_dynbuf_v1<AsyncReadStream>>(),
  1222. token, basic_streambuf_ref<Allocator>(b),
  1223. static_cast<CompletionCondition&&>(completion_condition)));
  1224. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  1225. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  1226. #endif // !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  1227. /// Start an asynchronous operation to read a certain amount of data from a
  1228. /// stream.
  1229. /**
  1230. * This function is used to asynchronously read a certain number of bytes of
  1231. * data from a stream. It is an initiating function for an @ref
  1232. * asynchronous_operation, and always returns immediately. The asynchronous
  1233. * operation will continue until one of the following conditions is true:
  1234. *
  1235. * @li The specified dynamic buffer sequence is full (that is, it has reached
  1236. * maximum size).
  1237. *
  1238. * @li An error occurred.
  1239. *
  1240. * This operation is implemented in terms of zero or more calls to the stream's
  1241. * async_read_some function, and is known as a <em>composed operation</em>. The
  1242. * program must ensure that the stream performs no other read operations (such
  1243. * as async_read, the stream's async_read_some function, or any other composed
  1244. * operations that perform reads) until this operation completes.
  1245. *
  1246. * @param s The stream from which the data is to be read. The type must support
  1247. * the AsyncReadStream concept.
  1248. *
  1249. * @param buffers The dynamic buffer sequence into which the data will be read.
  1250. * Although the buffers object may be copied as necessary, ownership of the
  1251. * underlying memory blocks is retained by the caller, which must guarantee
  1252. * that they remain valid until the completion handler is called.
  1253. *
  1254. * @param token The @ref completion_token that will be used to produce a
  1255. * completion handler, which will be called when the read completes.
  1256. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1257. * @ref yield_context, or a function object with the correct completion
  1258. * signature. The function signature of the completion handler must be:
  1259. * @code void handler(
  1260. * // Result of operation.
  1261. * const boost::system::error_code& error,
  1262. *
  1263. * // Number of bytes copied into the buffers. If an error
  1264. * // occurred, this will be the number of bytes successfully
  1265. * // transferred prior to the error.
  1266. * std::size_t bytes_transferred
  1267. * ); @endcode
  1268. * Regardless of whether the asynchronous operation completes immediately or
  1269. * not, the completion handler will not be invoked from within this function.
  1270. * On immediate completion, invocation of the handler will be performed in a
  1271. * manner equivalent to using boost::asio::post().
  1272. *
  1273. * @par Completion Signature
  1274. * @code void(boost::system::error_code, std::size_t) @endcode
  1275. *
  1276. * @note This overload is equivalent to calling:
  1277. * @code boost::asio::async_read(
  1278. * s, buffers,
  1279. * boost::asio::transfer_all(),
  1280. * handler); @endcode
  1281. *
  1282. * @par Per-Operation Cancellation
  1283. * This asynchronous operation supports cancellation for the following
  1284. * boost::asio::cancellation_type values:
  1285. *
  1286. * @li @c cancellation_type::terminal
  1287. *
  1288. * @li @c cancellation_type::partial
  1289. *
  1290. * if they are also supported by the @c AsyncReadStream type's
  1291. * @c async_read_some operation.
  1292. */
  1293. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  1294. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1295. std::size_t)) ReadToken = default_completion_token_t<
  1296. typename AsyncReadStream::executor_type>>
  1297. auto async_read(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  1298. ReadToken&& token = default_completion_token_t<
  1299. typename AsyncReadStream::executor_type>(),
  1300. constraint_t<
  1301. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1302. > = 0)
  1303. -> decltype(
  1304. async_initiate<ReadToken,
  1305. void (boost::system::error_code, std::size_t)>(
  1306. declval<detail::initiate_async_read_dynbuf_v2<AsyncReadStream>>(),
  1307. token, static_cast<DynamicBuffer_v2&&>(buffers), transfer_all()));
  1308. /// Start an asynchronous operation to read a certain amount of data from a
  1309. /// stream.
  1310. /**
  1311. * This function is used to asynchronously read a certain number of bytes of
  1312. * data from a stream. It is an initiating function for an @ref
  1313. * asynchronous_operation, and always returns immediately. The asynchronous
  1314. * operation will continue until one of the following conditions is true:
  1315. *
  1316. * @li The specified dynamic buffer sequence is full (that is, it has reached
  1317. * maximum size).
  1318. *
  1319. * @li The completion_condition function object returns 0.
  1320. *
  1321. * This operation is implemented in terms of zero or more calls to the stream's
  1322. * async_read_some function, and is known as a <em>composed operation</em>. The
  1323. * program must ensure that the stream performs no other read operations (such
  1324. * as async_read, the stream's async_read_some function, or any other composed
  1325. * operations that perform reads) until this operation completes.
  1326. *
  1327. * @param s The stream from which the data is to be read. The type must support
  1328. * the AsyncReadStream concept.
  1329. *
  1330. * @param buffers The dynamic buffer sequence into which the data will be read.
  1331. * Although the buffers object may be copied as necessary, ownership of the
  1332. * underlying memory blocks is retained by the caller, which must guarantee
  1333. * that they remain valid until the completion handler is called.
  1334. *
  1335. * @param completion_condition The function object to be called to determine
  1336. * whether the read operation is complete. The signature of the function object
  1337. * must be:
  1338. * @code std::size_t completion_condition(
  1339. * // Result of latest async_read_some operation.
  1340. * const boost::system::error_code& error,
  1341. *
  1342. * // Number of bytes transferred so far.
  1343. * std::size_t bytes_transferred
  1344. * ); @endcode
  1345. * A return value of 0 indicates that the read operation is complete. A non-zero
  1346. * return value indicates the maximum number of bytes to be read on the next
  1347. * call to the stream's async_read_some function.
  1348. *
  1349. * @param token The @ref completion_token that will be used to produce a
  1350. * completion handler, which will be called when the read completes.
  1351. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1352. * @ref yield_context, or a function object with the correct completion
  1353. * signature. The function signature of the completion handler must be:
  1354. * @code void handler(
  1355. * // Result of operation.
  1356. * const boost::system::error_code& error,
  1357. *
  1358. * // Number of bytes copied into the buffers. If an error
  1359. * // occurred, this will be the number of bytes successfully
  1360. * // transferred prior to the error.
  1361. * std::size_t bytes_transferred
  1362. * ); @endcode
  1363. * Regardless of whether the asynchronous operation completes immediately or
  1364. * not, the completion handler will not be invoked from within this function.
  1365. * On immediate completion, invocation of the handler will be performed in a
  1366. * manner equivalent to using boost::asio::post().
  1367. *
  1368. * @par Completion Signature
  1369. * @code void(boost::system::error_code, std::size_t) @endcode
  1370. *
  1371. * @par Per-Operation Cancellation
  1372. * This asynchronous operation supports cancellation for the following
  1373. * boost::asio::cancellation_type values:
  1374. *
  1375. * @li @c cancellation_type::terminal
  1376. *
  1377. * @li @c cancellation_type::partial
  1378. *
  1379. * if they are also supported by the @c AsyncReadStream type's
  1380. * @c async_read_some operation.
  1381. */
  1382. template <typename AsyncReadStream,
  1383. typename DynamicBuffer_v2, typename CompletionCondition,
  1384. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1385. std::size_t)) ReadToken = default_completion_token_t<
  1386. typename AsyncReadStream::executor_type>>
  1387. auto async_read(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  1388. CompletionCondition completion_condition,
  1389. ReadToken&& token = default_completion_token_t<
  1390. typename AsyncReadStream::executor_type>(),
  1391. constraint_t<
  1392. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1393. > = 0)
  1394. -> decltype(
  1395. async_initiate<ReadToken,
  1396. void (boost::system::error_code, std::size_t)>(
  1397. declval<detail::initiate_async_read_dynbuf_v2<AsyncReadStream>>(),
  1398. token, static_cast<DynamicBuffer_v2&&>(buffers),
  1399. static_cast<CompletionCondition&&>(completion_condition)));
  1400. /*@}*/
  1401. } // namespace asio
  1402. } // namespace boost
  1403. #include <boost/asio/detail/pop_options.hpp>
  1404. #include <boost/asio/impl/read.hpp>
  1405. #endif // BOOST_ASIO_READ_HPP