read.hpp 56 KB

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