read.hpp 61 KB

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