write.hpp 56 KB

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