write.hpp 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  1. //
  2. // write.hpp
  3. // ~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_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