write.hpp 55 KB

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