read.hpp 55 KB

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