read.hpp 61 KB

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