write.hpp 57 KB

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