write.hpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. //
  2. // write.hpp
  3. // ~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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/error.hpp>
  20. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  21. # include <boost/asio/basic_streambuf_fwd.hpp>
  22. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. /**
  27. * @defgroup write boost::asio::write
  28. *
  29. * @brief Write a certain amount of data to a stream before returning.
  30. */
  31. /*@{*/
  32. /// Write all of the supplied data to a stream before returning.
  33. /**
  34. * This function is used to write a certain number of bytes of data to a stream.
  35. * The call will block until one of the following conditions is true:
  36. *
  37. * @li All of the data in the supplied buffers has been written. That is, the
  38. * bytes transferred is equal to the sum of the buffer sizes.
  39. *
  40. * @li An error occurred.
  41. *
  42. * This operation is implemented in terms of zero or more calls to the stream's
  43. * write_some function.
  44. *
  45. * @param s The stream to which the data is to be written. The type must support
  46. * the SyncWriteStream concept.
  47. *
  48. * @param buffers One or more buffers containing the data to be written. The sum
  49. * of the buffer sizes indicates the maximum number of bytes to write to the
  50. * stream.
  51. *
  52. * @returns The number of bytes transferred.
  53. *
  54. * @throws boost::system::system_error Thrown on failure.
  55. *
  56. * @par Example
  57. * To write a single data buffer use the @ref buffer function as follows:
  58. * @code boost::asio::write(s, boost::asio::buffer(data, size)); @endcode
  59. * See the @ref buffer documentation for information on writing multiple
  60. * buffers in one go, and how to use it with arrays, boost::array or
  61. * std::vector.
  62. *
  63. * @note This overload is equivalent to calling:
  64. * @code boost::asio::write(
  65. * s, buffers,
  66. * boost::asio::transfer_all()); @endcode
  67. */
  68. template <typename SyncWriteStream, typename ConstBufferSequence>
  69. std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
  70. typename enable_if<
  71. is_const_buffer_sequence<ConstBufferSequence>::value
  72. >::type* = 0);
  73. /// Write all of the supplied data to a stream before returning.
  74. /**
  75. * This function is used to write a certain number of bytes of data to a stream.
  76. * The call will block until one of the following conditions is true:
  77. *
  78. * @li All of the data in the supplied buffers has been written. That is, the
  79. * bytes transferred is equal to the sum of the buffer sizes.
  80. *
  81. * @li An error occurred.
  82. *
  83. * This operation is implemented in terms of zero or more calls to the stream's
  84. * write_some function.
  85. *
  86. * @param s The stream to which the data is to be written. The type must support
  87. * the SyncWriteStream concept.
  88. *
  89. * @param buffers One or more buffers containing the data to be written. The sum
  90. * of the buffer sizes indicates the maximum number of bytes to write to the
  91. * stream.
  92. *
  93. * @param ec Set to indicate what error occurred, if any.
  94. *
  95. * @returns The number of bytes transferred.
  96. *
  97. * @par Example
  98. * To write a single data buffer use the @ref buffer function as follows:
  99. * @code boost::asio::write(s, boost::asio::buffer(data, size), ec); @endcode
  100. * See the @ref buffer documentation for information on writing multiple
  101. * buffers in one go, and how to use it with arrays, boost::array or
  102. * std::vector.
  103. *
  104. * @note This overload is equivalent to calling:
  105. * @code boost::asio::write(
  106. * s, buffers,
  107. * boost::asio::transfer_all(), ec); @endcode
  108. */
  109. template <typename SyncWriteStream, typename ConstBufferSequence>
  110. std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
  111. boost::system::error_code& ec,
  112. typename enable_if<
  113. is_const_buffer_sequence<ConstBufferSequence>::value
  114. >::type* = 0);
  115. /// Write a certain amount of data to a stream before returning.
  116. /**
  117. * This function is used to write a certain number of bytes of data to a stream.
  118. * The call will block until one of the following conditions is true:
  119. *
  120. * @li All of the data in the supplied buffers has been written. That is, the
  121. * bytes transferred is equal to the sum of the buffer sizes.
  122. *
  123. * @li The completion_condition function object returns 0.
  124. *
  125. * This operation is implemented in terms of zero or more calls to the stream's
  126. * write_some function.
  127. *
  128. * @param s The stream to which the data is to be written. The type must support
  129. * the SyncWriteStream concept.
  130. *
  131. * @param buffers One or more buffers containing the data to be written. The sum
  132. * of the buffer sizes indicates the maximum number of bytes to write to the
  133. * stream.
  134. *
  135. * @param completion_condition The function object to be called to determine
  136. * whether the write operation is complete. The signature of the function object
  137. * must be:
  138. * @code std::size_t completion_condition(
  139. * // Result of latest write_some operation.
  140. * const boost::system::error_code& error,
  141. *
  142. * // Number of bytes transferred so far.
  143. * std::size_t bytes_transferred
  144. * ); @endcode
  145. * A return value of 0 indicates that the write operation is complete. A
  146. * non-zero return value indicates the maximum number of bytes to be written on
  147. * the next call to the stream's write_some function.
  148. *
  149. * @returns The number of bytes transferred.
  150. *
  151. * @throws boost::system::system_error Thrown on failure.
  152. *
  153. * @par Example
  154. * To write a single data buffer use the @ref buffer function as follows:
  155. * @code boost::asio::write(s, boost::asio::buffer(data, size),
  156. * boost::asio::transfer_at_least(32)); @endcode
  157. * See the @ref buffer documentation for information on writing multiple
  158. * buffers in one go, and how to use it with arrays, boost::array or
  159. * std::vector.
  160. */
  161. template <typename SyncWriteStream, typename ConstBufferSequence,
  162. typename CompletionCondition>
  163. std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
  164. CompletionCondition completion_condition,
  165. typename enable_if<
  166. is_const_buffer_sequence<ConstBufferSequence>::value
  167. >::type* = 0);
  168. /// Write a certain amount of data to a stream before returning.
  169. /**
  170. * This function is used to write a certain number of bytes of data to a stream.
  171. * The call will block until one of the following conditions is true:
  172. *
  173. * @li All of the data in the supplied buffers has been written. That is, the
  174. * bytes transferred is equal to the sum of the buffer sizes.
  175. *
  176. * @li The completion_condition function object returns 0.
  177. *
  178. * This operation is implemented in terms of zero or more calls to the stream's
  179. * write_some function.
  180. *
  181. * @param s The stream to which the data is to be written. The type must support
  182. * the SyncWriteStream concept.
  183. *
  184. * @param buffers One or more buffers containing the data to be written. The sum
  185. * of the buffer sizes indicates the maximum number of bytes to write to the
  186. * stream.
  187. *
  188. * @param completion_condition The function object to be called to determine
  189. * whether the write operation is complete. The signature of the function object
  190. * must be:
  191. * @code std::size_t completion_condition(
  192. * // Result of latest write_some operation.
  193. * const boost::system::error_code& error,
  194. *
  195. * // Number of bytes transferred so far.
  196. * std::size_t bytes_transferred
  197. * ); @endcode
  198. * A return value of 0 indicates that the write operation is complete. A
  199. * non-zero return value indicates the maximum number of bytes to be written on
  200. * the next call to the stream's write_some function.
  201. *
  202. * @param ec Set to indicate what error occurred, if any.
  203. *
  204. * @returns The number of bytes written. If an error occurs, returns the total
  205. * number of bytes successfully transferred prior to the error.
  206. */
  207. template <typename SyncWriteStream, typename ConstBufferSequence,
  208. typename CompletionCondition>
  209. std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
  210. CompletionCondition completion_condition, boost::system::error_code& ec,
  211. typename enable_if<
  212. is_const_buffer_sequence<ConstBufferSequence>::value
  213. >::type* = 0);
  214. /// Write all of the supplied data to a stream before returning.
  215. /**
  216. * This function is used to write a certain number of bytes of data to a stream.
  217. * The call will block until one of the following conditions is true:
  218. *
  219. * @li All of the data in the supplied dynamic buffer sequence has been written.
  220. *
  221. * @li An error occurred.
  222. *
  223. * This operation is implemented in terms of zero or more calls to the stream's
  224. * write_some function.
  225. *
  226. * @param s The stream to which the data is to be written. The type must support
  227. * the SyncWriteStream concept.
  228. *
  229. * @param buffers The dynamic buffer sequence from which data will be written.
  230. * Successfully written data is automatically consumed from the buffers.
  231. *
  232. * @returns The number of bytes transferred.
  233. *
  234. * @throws boost::system::system_error Thrown on failure.
  235. *
  236. * @note This overload is equivalent to calling:
  237. * @code boost::asio::write(
  238. * s, buffers,
  239. * boost::asio::transfer_all()); @endcode
  240. */
  241. template <typename SyncWriteStream, typename DynamicBuffer>
  242. std::size_t write(SyncWriteStream& s,
  243. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  244. typename enable_if<
  245. is_dynamic_buffer<typename decay<DynamicBuffer>::type>::value
  246. >::type* = 0);
  247. /// Write all of the supplied data to a stream before returning.
  248. /**
  249. * This function is used to write a certain number of bytes of data to a stream.
  250. * The call will block until one of the following conditions is true:
  251. *
  252. * @li All of the data in the supplied dynamic buffer sequence has been written.
  253. *
  254. * @li An error occurred.
  255. *
  256. * This operation is implemented in terms of zero or more calls to the stream's
  257. * write_some function.
  258. *
  259. * @param s The stream to which the data is to be written. The type must support
  260. * the SyncWriteStream concept.
  261. *
  262. * @param buffers The dynamic buffer sequence from which data will be written.
  263. * Successfully written data is automatically consumed from the buffers.
  264. *
  265. * @param ec Set to indicate what error occurred, if any.
  266. *
  267. * @returns The number of bytes transferred.
  268. *
  269. * @note This overload is equivalent to calling:
  270. * @code boost::asio::write(
  271. * s, buffers,
  272. * boost::asio::transfer_all(), ec); @endcode
  273. */
  274. template <typename SyncWriteStream, typename DynamicBuffer>
  275. std::size_t write(SyncWriteStream& s,
  276. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  277. boost::system::error_code& ec,
  278. typename enable_if<
  279. is_dynamic_buffer<typename decay<DynamicBuffer>::type>::value
  280. >::type* = 0);
  281. /// Write a certain amount of data to a stream before returning.
  282. /**
  283. * This function is used to write a certain number of bytes of data to a stream.
  284. * The call will block until one of the following conditions is true:
  285. *
  286. * @li All of the data in the supplied dynamic buffer sequence has been written.
  287. *
  288. * @li The completion_condition function object returns 0.
  289. *
  290. * This operation is implemented in terms of zero or more calls to the stream's
  291. * write_some function.
  292. *
  293. * @param s The stream to which the data is to be written. The type must support
  294. * the SyncWriteStream concept.
  295. *
  296. * @param buffers The dynamic buffer sequence from which data will be written.
  297. * Successfully written data is automatically consumed from the buffers.
  298. *
  299. * @param completion_condition The function object to be called to determine
  300. * whether the write operation is complete. The signature of the function object
  301. * must be:
  302. * @code std::size_t completion_condition(
  303. * // Result of latest write_some operation.
  304. * const boost::system::error_code& error,
  305. *
  306. * // Number of bytes transferred so far.
  307. * std::size_t bytes_transferred
  308. * ); @endcode
  309. * A return value of 0 indicates that the write operation is complete. A
  310. * non-zero return value indicates the maximum number of bytes to be written on
  311. * the next call to the stream's write_some function.
  312. *
  313. * @returns The number of bytes transferred.
  314. *
  315. * @throws boost::system::system_error Thrown on failure.
  316. */
  317. template <typename SyncWriteStream, typename DynamicBuffer,
  318. typename CompletionCondition>
  319. std::size_t write(SyncWriteStream& s,
  320. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  321. CompletionCondition completion_condition,
  322. typename enable_if<
  323. is_dynamic_buffer<typename decay<DynamicBuffer>::type>::value
  324. >::type* = 0);
  325. /// Write a certain amount of data to a stream before returning.
  326. /**
  327. * This function is used to write a certain number of bytes of data to a stream.
  328. * The call will block until one of the following conditions is true:
  329. *
  330. * @li All of the data in the supplied dynamic buffer sequence has been written.
  331. *
  332. * @li The completion_condition function object returns 0.
  333. *
  334. * This operation is implemented in terms of zero or more calls to the stream's
  335. * write_some function.
  336. *
  337. * @param s The stream to which the data is to be written. The type must support
  338. * the SyncWriteStream concept.
  339. *
  340. * @param buffers The dynamic buffer sequence from which data will be written.
  341. * Successfully written data is automatically consumed from the buffers.
  342. *
  343. * @param completion_condition The function object to be called to determine
  344. * whether the write operation is complete. The signature of the function object
  345. * must be:
  346. * @code std::size_t completion_condition(
  347. * // Result of latest write_some operation.
  348. * const boost::system::error_code& error,
  349. *
  350. * // Number of bytes transferred so far.
  351. * std::size_t bytes_transferred
  352. * ); @endcode
  353. * A return value of 0 indicates that the write operation is complete. A
  354. * non-zero return value indicates the maximum number of bytes to be written on
  355. * the next call to the stream's write_some function.
  356. *
  357. * @param ec Set to indicate what error occurred, if any.
  358. *
  359. * @returns The number of bytes written. If an error occurs, returns the total
  360. * number of bytes successfully transferred prior to the error.
  361. */
  362. template <typename SyncWriteStream, typename DynamicBuffer,
  363. typename CompletionCondition>
  364. std::size_t write(SyncWriteStream& s,
  365. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  366. CompletionCondition completion_condition, boost::system::error_code& ec,
  367. typename enable_if<
  368. is_dynamic_buffer<typename decay<DynamicBuffer>::type>::value
  369. >::type* = 0);
  370. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  371. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  372. /// Write all of the supplied data to a stream before returning.
  373. /**
  374. * This function is used to write a certain number of bytes of data to a stream.
  375. * The call will block until one of the following conditions is true:
  376. *
  377. * @li All of the data in the supplied basic_streambuf has been written.
  378. *
  379. * @li An error occurred.
  380. *
  381. * This operation is implemented in terms of zero or more calls to the stream's
  382. * write_some function.
  383. *
  384. * @param s The stream to which the data is to be written. The type must support
  385. * the SyncWriteStream concept.
  386. *
  387. * @param b The basic_streambuf object from which data will be written.
  388. *
  389. * @returns The number of bytes transferred.
  390. *
  391. * @throws boost::system::system_error Thrown on failure.
  392. *
  393. * @note This overload is equivalent to calling:
  394. * @code boost::asio::write(
  395. * s, b,
  396. * boost::asio::transfer_all()); @endcode
  397. */
  398. template <typename SyncWriteStream, typename Allocator>
  399. std::size_t write(SyncWriteStream& s, basic_streambuf<Allocator>& b);
  400. /// Write all of the supplied data to a stream before returning.
  401. /**
  402. * This function is used to write a certain number of bytes of data to a stream.
  403. * The call will block until one of the following conditions is true:
  404. *
  405. * @li All of the data in the supplied basic_streambuf has been written.
  406. *
  407. * @li An error occurred.
  408. *
  409. * This operation is implemented in terms of zero or more calls to the stream's
  410. * write_some function.
  411. *
  412. * @param s The stream to which the data is to be written. The type must support
  413. * the SyncWriteStream concept.
  414. *
  415. * @param b The basic_streambuf object from which data will be written.
  416. *
  417. * @param ec Set to indicate what error occurred, if any.
  418. *
  419. * @returns The number of bytes transferred.
  420. *
  421. * @note This overload is equivalent to calling:
  422. * @code boost::asio::write(
  423. * s, b,
  424. * boost::asio::transfer_all(), ec); @endcode
  425. */
  426. template <typename SyncWriteStream, typename Allocator>
  427. std::size_t write(SyncWriteStream& s, basic_streambuf<Allocator>& b,
  428. boost::system::error_code& ec);
  429. /// Write a certain amount of data to a stream before returning.
  430. /**
  431. * This function is used to write a certain number of bytes of data to a stream.
  432. * The call will block until one of the following conditions is true:
  433. *
  434. * @li All of the data in the supplied basic_streambuf has been written.
  435. *
  436. * @li The completion_condition function object returns 0.
  437. *
  438. * This operation is implemented in terms of zero or more calls to the stream's
  439. * write_some function.
  440. *
  441. * @param s The stream to which the data is to be written. The type must support
  442. * the SyncWriteStream concept.
  443. *
  444. * @param b The basic_streambuf object from which data will be written.
  445. *
  446. * @param completion_condition The function object to be called to determine
  447. * whether the write operation is complete. The signature of the function object
  448. * must be:
  449. * @code std::size_t completion_condition(
  450. * // Result of latest write_some operation.
  451. * const boost::system::error_code& error,
  452. *
  453. * // Number of bytes transferred so far.
  454. * std::size_t bytes_transferred
  455. * ); @endcode
  456. * A return value of 0 indicates that the write operation is complete. A
  457. * non-zero return value indicates the maximum number of bytes to be written on
  458. * the next call to the stream's write_some function.
  459. *
  460. * @returns The number of bytes transferred.
  461. *
  462. * @throws boost::system::system_error Thrown on failure.
  463. */
  464. template <typename SyncWriteStream, typename Allocator,
  465. typename CompletionCondition>
  466. std::size_t write(SyncWriteStream& s, basic_streambuf<Allocator>& b,
  467. CompletionCondition completion_condition);
  468. /// Write a certain amount of data to a stream before returning.
  469. /**
  470. * This function is used to write a certain number of bytes of data to a stream.
  471. * The call will block until one of the following conditions is true:
  472. *
  473. * @li All of the data in the supplied basic_streambuf has been written.
  474. *
  475. * @li The completion_condition function object returns 0.
  476. *
  477. * This operation is implemented in terms of zero or more calls to the stream's
  478. * write_some function.
  479. *
  480. * @param s The stream to which the data is to be written. The type must support
  481. * the SyncWriteStream concept.
  482. *
  483. * @param b The basic_streambuf object from which data will be written.
  484. *
  485. * @param completion_condition The function object to be called to determine
  486. * whether the write operation is complete. The signature of the function object
  487. * must be:
  488. * @code std::size_t completion_condition(
  489. * // Result of latest write_some operation.
  490. * const boost::system::error_code& error,
  491. *
  492. * // Number of bytes transferred so far.
  493. * std::size_t bytes_transferred
  494. * ); @endcode
  495. * A return value of 0 indicates that the write operation is complete. A
  496. * non-zero return value indicates the maximum number of bytes to be written on
  497. * the next call to the stream's write_some function.
  498. *
  499. * @param ec Set to indicate what error occurred, if any.
  500. *
  501. * @returns The number of bytes written. If an error occurs, returns the total
  502. * number of bytes successfully transferred prior to the error.
  503. */
  504. template <typename SyncWriteStream, typename Allocator,
  505. typename CompletionCondition>
  506. std::size_t write(SyncWriteStream& s, basic_streambuf<Allocator>& b,
  507. CompletionCondition completion_condition, boost::system::error_code& ec);
  508. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  509. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  510. /*@}*/
  511. /**
  512. * @defgroup async_write boost::asio::async_write
  513. *
  514. * @brief Start an asynchronous operation to write a certain amount of data to a
  515. * stream.
  516. */
  517. /*@{*/
  518. /// Start an asynchronous operation to write all of the supplied data to a
  519. /// stream.
  520. /**
  521. * This function is used to asynchronously write a certain number of bytes of
  522. * data to a stream. The function call always returns immediately. The
  523. * asynchronous operation will continue until one of the following conditions
  524. * is true:
  525. *
  526. * @li All of the data in the supplied buffers has been written. That is, the
  527. * bytes transferred is equal to the sum of the buffer sizes.
  528. *
  529. * @li An error occurred.
  530. *
  531. * This operation is implemented in terms of zero or more calls to the stream's
  532. * async_write_some function, and is known as a <em>composed operation</em>. The
  533. * program must ensure that the stream performs no other write operations (such
  534. * as async_write, the stream's async_write_some function, or any other composed
  535. * operations that perform writes) until this operation completes.
  536. *
  537. * @param s The stream to which the data is to be written. The type must support
  538. * the AsyncWriteStream concept.
  539. *
  540. * @param buffers One or more buffers containing the data to be written.
  541. * Although the buffers object may be copied as necessary, ownership of the
  542. * underlying memory blocks is retained by the caller, which must guarantee
  543. * that they remain valid until the handler is called.
  544. *
  545. * @param handler The handler to be called when the write operation completes.
  546. * Copies will be made of the handler as required. The function signature of
  547. * the handler must be:
  548. * @code void handler(
  549. * const boost::system::error_code& error, // Result of operation.
  550. *
  551. * std::size_t bytes_transferred // Number of bytes written from the
  552. * // buffers. If an error occurred,
  553. * // this will be less than the sum
  554. * // of the buffer sizes.
  555. * ); @endcode
  556. * Regardless of whether the asynchronous operation completes immediately or
  557. * not, the handler will not be invoked from within this function. Invocation of
  558. * the handler will be performed in a manner equivalent to using
  559. * boost::asio::io_context::post().
  560. *
  561. * @par Example
  562. * To write a single data buffer use the @ref buffer function as follows:
  563. * @code
  564. * boost::asio::async_write(s, boost::asio::buffer(data, size), handler);
  565. * @endcode
  566. * See the @ref buffer documentation for information on writing multiple
  567. * buffers in one go, and how to use it with arrays, boost::array or
  568. * std::vector.
  569. */
  570. template <typename AsyncWriteStream, typename ConstBufferSequence,
  571. typename WriteHandler>
  572. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  573. void (boost::system::error_code, std::size_t))
  574. async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
  575. BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  576. typename enable_if<
  577. is_const_buffer_sequence<ConstBufferSequence>::value
  578. >::type* = 0);
  579. /// Start an asynchronous operation to write a certain amount of data to a
  580. /// stream.
  581. /**
  582. * This function is used to asynchronously write a certain number of bytes of
  583. * data to a stream. The function call always returns immediately. The
  584. * asynchronous operation will continue until one of the following conditions
  585. * is true:
  586. *
  587. * @li All of the data in the supplied buffers has been written. That is, the
  588. * bytes transferred is equal to the sum of the buffer sizes.
  589. *
  590. * @li The completion_condition function object returns 0.
  591. *
  592. * This operation is implemented in terms of zero or more calls to the stream's
  593. * async_write_some function, and is known as a <em>composed operation</em>. The
  594. * program must ensure that the stream performs no other write operations (such
  595. * as async_write, the stream's async_write_some function, or any other composed
  596. * operations that perform writes) until this operation completes.
  597. *
  598. * @param s The stream to which the data is to be written. The type must support
  599. * the AsyncWriteStream concept.
  600. *
  601. * @param buffers One or more buffers containing the data to be written.
  602. * Although the buffers object may be copied as necessary, ownership of the
  603. * underlying memory blocks is retained by the caller, which must guarantee
  604. * that they remain valid until the handler is called.
  605. *
  606. * @param completion_condition The function object to be called to determine
  607. * whether the write operation is complete. The signature of the function object
  608. * must be:
  609. * @code std::size_t completion_condition(
  610. * // Result of latest async_write_some operation.
  611. * const boost::system::error_code& error,
  612. *
  613. * // Number of bytes transferred so far.
  614. * std::size_t bytes_transferred
  615. * ); @endcode
  616. * A return value of 0 indicates that the write operation is complete. A
  617. * non-zero return value indicates the maximum number of bytes to be written on
  618. * the next call to the stream's async_write_some function.
  619. *
  620. * @param handler The handler to be called when the write operation completes.
  621. * Copies will be made of the handler as required. The function signature of the
  622. * handler must be:
  623. * @code void handler(
  624. * const boost::system::error_code& error, // Result of operation.
  625. *
  626. * std::size_t bytes_transferred // Number of bytes written from the
  627. * // buffers. If an error occurred,
  628. * // this will be less than the sum
  629. * // of the buffer sizes.
  630. * ); @endcode
  631. * Regardless of whether the asynchronous operation completes immediately or
  632. * not, the handler will not be invoked from within this function. Invocation of
  633. * the handler will be performed in a manner equivalent to using
  634. * boost::asio::io_context::post().
  635. *
  636. * @par Example
  637. * To write a single data buffer use the @ref buffer function as follows:
  638. * @code boost::asio::async_write(s,
  639. * boost::asio::buffer(data, size),
  640. * boost::asio::transfer_at_least(32),
  641. * handler); @endcode
  642. * See the @ref buffer documentation for information on writing multiple
  643. * buffers in one go, and how to use it with arrays, boost::array or
  644. * std::vector.
  645. */
  646. template <typename AsyncWriteStream, typename ConstBufferSequence,
  647. typename CompletionCondition, typename WriteHandler>
  648. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  649. void (boost::system::error_code, std::size_t))
  650. async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
  651. CompletionCondition completion_condition,
  652. BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  653. typename enable_if<
  654. is_const_buffer_sequence<ConstBufferSequence>::value
  655. >::type* = 0);
  656. /// Start an asynchronous operation to write all of the supplied data to a
  657. /// stream.
  658. /**
  659. * This function is used to asynchronously write a certain number of bytes of
  660. * data to a stream. The function call always returns immediately. The
  661. * asynchronous operation will continue until one of the following conditions
  662. * is true:
  663. *
  664. * @li All of the data in the supplied dynamic buffer sequence has been written.
  665. *
  666. * @li An error occurred.
  667. *
  668. * This operation is implemented in terms of zero or more calls to the stream's
  669. * async_write_some function, and is known as a <em>composed operation</em>. The
  670. * program must ensure that the stream performs no other write operations (such
  671. * as async_write, the stream's async_write_some function, or any other composed
  672. * operations that perform writes) until this operation completes.
  673. *
  674. * @param s The stream to which the data is to be written. The type must support
  675. * the AsyncWriteStream concept.
  676. *
  677. * @param buffers The dynamic buffer sequence from which data will be written.
  678. * Although the buffers object may be copied as necessary, ownership of the
  679. * underlying memory blocks is retained by the caller, which must guarantee
  680. * that they remain valid until the handler is called. Successfully written
  681. * data is automatically consumed from the buffers.
  682. *
  683. * @param handler The handler to be called when the write operation completes.
  684. * Copies will be made of the handler as required. The function signature of the
  685. * handler must be:
  686. * @code void handler(
  687. * const boost::system::error_code& error, // Result of operation.
  688. *
  689. * std::size_t bytes_transferred // Number of bytes written from the
  690. * // buffers. If an error occurred,
  691. * // this will be less than the sum
  692. * // of the buffer sizes.
  693. * ); @endcode
  694. * Regardless of whether the asynchronous operation completes immediately or
  695. * not, the handler will not be invoked from within this function. Invocation of
  696. * the handler will be performed in a manner equivalent to using
  697. * boost::asio::io_context::post().
  698. */
  699. template <typename AsyncWriteStream,
  700. typename DynamicBuffer, typename WriteHandler>
  701. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  702. void (boost::system::error_code, std::size_t))
  703. async_write(AsyncWriteStream& s,
  704. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  705. BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  706. typename enable_if<
  707. is_dynamic_buffer<typename decay<DynamicBuffer>::type>::value
  708. >::type* = 0);
  709. /// Start an asynchronous operation to write a certain amount of data to a
  710. /// stream.
  711. /**
  712. * This function is used to asynchronously write a certain number of bytes of
  713. * data to a stream. The function call always returns immediately. The
  714. * asynchronous operation will continue until one of the following conditions
  715. * is true:
  716. *
  717. * @li All of the data in the supplied dynamic buffer sequence has been written.
  718. *
  719. * @li The completion_condition function object returns 0.
  720. *
  721. * This operation is implemented in terms of zero or more calls to the stream's
  722. * async_write_some function, and is known as a <em>composed operation</em>. The
  723. * program must ensure that the stream performs no other write operations (such
  724. * as async_write, the stream's async_write_some function, or any other composed
  725. * operations that perform writes) until this operation completes.
  726. *
  727. * @param s The stream to which the data is to be written. The type must support
  728. * the AsyncWriteStream concept.
  729. *
  730. * @param buffers The dynamic buffer sequence from which data will be written.
  731. * Although the buffers object may be copied as necessary, ownership of the
  732. * underlying memory blocks is retained by the caller, which must guarantee
  733. * that they remain valid until the handler is called. Successfully written
  734. * data is automatically consumed from the buffers.
  735. *
  736. * @param completion_condition The function object to be called to determine
  737. * whether the write operation is complete. The signature of the function object
  738. * must be:
  739. * @code std::size_t completion_condition(
  740. * // Result of latest async_write_some operation.
  741. * const boost::system::error_code& error,
  742. *
  743. * // Number of bytes transferred so far.
  744. * std::size_t bytes_transferred
  745. * ); @endcode
  746. * A return value of 0 indicates that the write operation is complete. A
  747. * non-zero return value indicates the maximum number of bytes to be written on
  748. * the next call to the stream's async_write_some function.
  749. *
  750. * @param handler The handler to be called when the write operation completes.
  751. * Copies will be made of the handler as required. The function signature of the
  752. * handler must be:
  753. * @code void handler(
  754. * const boost::system::error_code& error, // Result of operation.
  755. *
  756. * std::size_t bytes_transferred // Number of bytes written from the
  757. * // buffers. If an error occurred,
  758. * // this will be less than the sum
  759. * // of the buffer sizes.
  760. * ); @endcode
  761. * Regardless of whether the asynchronous operation completes immediately or
  762. * not, the handler will not be invoked from within this function. Invocation of
  763. * the handler will be performed in a manner equivalent to using
  764. * boost::asio::io_context::post().
  765. */
  766. template <typename AsyncWriteStream, typename DynamicBuffer,
  767. typename CompletionCondition, typename WriteHandler>
  768. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  769. void (boost::system::error_code, std::size_t))
  770. async_write(AsyncWriteStream& s,
  771. BOOST_ASIO_MOVE_ARG(DynamicBuffer) buffers,
  772. CompletionCondition completion_condition,
  773. BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  774. typename enable_if<
  775. is_dynamic_buffer<typename decay<DynamicBuffer>::type>::value
  776. >::type* = 0);
  777. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  778. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  779. /// Start an asynchronous operation to write all of the supplied data to a
  780. /// stream.
  781. /**
  782. * This function is used to asynchronously write a certain number of bytes of
  783. * data to a stream. The function call always returns immediately. The
  784. * asynchronous operation will continue until one of the following conditions
  785. * is true:
  786. *
  787. * @li All of the data in the supplied basic_streambuf has been written.
  788. *
  789. * @li An error occurred.
  790. *
  791. * This operation is implemented in terms of zero or more calls to the stream's
  792. * async_write_some function, and is known as a <em>composed operation</em>. The
  793. * program must ensure that the stream performs no other write operations (such
  794. * as async_write, the stream's async_write_some function, or any other composed
  795. * operations that perform writes) until this operation completes.
  796. *
  797. * @param s The stream to which the data is to be written. The type must support
  798. * the AsyncWriteStream concept.
  799. *
  800. * @param b A basic_streambuf object from which data will be written. Ownership
  801. * of the streambuf is retained by the caller, which must guarantee that it
  802. * remains valid until the handler is called.
  803. *
  804. * @param handler The handler to be called when the write operation completes.
  805. * Copies will be made of the handler as required. The function signature of the
  806. * handler must be:
  807. * @code void handler(
  808. * const boost::system::error_code& error, // Result of operation.
  809. *
  810. * std::size_t bytes_transferred // Number of bytes written from the
  811. * // buffers. If an error occurred,
  812. * // this will be less than the sum
  813. * // of the buffer sizes.
  814. * ); @endcode
  815. * Regardless of whether the asynchronous operation completes immediately or
  816. * not, the handler will not be invoked from within this function. Invocation of
  817. * the handler will be performed in a manner equivalent to using
  818. * boost::asio::io_context::post().
  819. */
  820. template <typename AsyncWriteStream, typename Allocator, typename WriteHandler>
  821. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  822. void (boost::system::error_code, std::size_t))
  823. async_write(AsyncWriteStream& s, basic_streambuf<Allocator>& b,
  824. BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
  825. /// Start an asynchronous operation to write a certain amount of data to a
  826. /// stream.
  827. /**
  828. * This function is used to asynchronously write a certain number of bytes of
  829. * data to a stream. The function call always returns immediately. The
  830. * asynchronous operation will continue until one of the following conditions
  831. * is true:
  832. *
  833. * @li All of the data in the supplied basic_streambuf has been written.
  834. *
  835. * @li The completion_condition function object returns 0.
  836. *
  837. * This operation is implemented in terms of zero or more calls to the stream's
  838. * async_write_some function, and is known as a <em>composed operation</em>. The
  839. * program must ensure that the stream performs no other write operations (such
  840. * as async_write, the stream's async_write_some function, or any other composed
  841. * operations that perform writes) until this operation completes.
  842. *
  843. * @param s The stream to which the data is to be written. The type must support
  844. * the AsyncWriteStream concept.
  845. *
  846. * @param b A basic_streambuf object from which data will be written. Ownership
  847. * of the streambuf is retained by the caller, which must guarantee that it
  848. * remains valid until the handler is called.
  849. *
  850. * @param completion_condition The function object to be called to determine
  851. * whether the write operation is complete. The signature of the function object
  852. * must be:
  853. * @code std::size_t completion_condition(
  854. * // Result of latest async_write_some operation.
  855. * const boost::system::error_code& error,
  856. *
  857. * // Number of bytes transferred so far.
  858. * std::size_t bytes_transferred
  859. * ); @endcode
  860. * A return value of 0 indicates that the write operation is complete. A
  861. * non-zero return value indicates the maximum number of bytes to be written on
  862. * the next call to the stream's async_write_some function.
  863. *
  864. * @param handler The handler to be called when the write operation completes.
  865. * Copies will be made of the handler as required. The function signature of the
  866. * handler must be:
  867. * @code void handler(
  868. * const boost::system::error_code& error, // Result of operation.
  869. *
  870. * std::size_t bytes_transferred // Number of bytes written from the
  871. * // buffers. If an error occurred,
  872. * // this will be less than the sum
  873. * // of the buffer sizes.
  874. * ); @endcode
  875. * Regardless of whether the asynchronous operation completes immediately or
  876. * not, the handler will not be invoked from within this function. Invocation of
  877. * the handler will be performed in a manner equivalent to using
  878. * boost::asio::io_context::post().
  879. */
  880. template <typename AsyncWriteStream, typename Allocator,
  881. typename CompletionCondition, typename WriteHandler>
  882. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  883. void (boost::system::error_code, std::size_t))
  884. async_write(AsyncWriteStream& s, basic_streambuf<Allocator>& b,
  885. CompletionCondition completion_condition,
  886. BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
  887. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  888. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  889. /*@}*/
  890. } // namespace asio
  891. } // namespace boost
  892. #include <boost/asio/detail/pop_options.hpp>
  893. #include <boost/asio/impl/write.hpp>
  894. #endif // BOOST_ASIO_WRITE_HPP