write.hpp 61 KB

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