Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

921 lines
37KB

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