write.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. //
  2. // write.hpp
  3. // ~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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/basic_streambuf_fwd.hpp"
  18. #include "asio/error.hpp"
  19. #include "asio/detail/push_options.hpp"
  20. namespace asio {
  21. /**
  22. * @defgroup write asio::write
  23. *
  24. * @brief Write a certain amount of data to a stream before returning.
  25. */
  26. /*@{*/
  27. /// Write all of the supplied data to a stream before returning.
  28. /**
  29. * This function is used to write a certain number of bytes of data to a stream.
  30. * The call will block until one of the following conditions is true:
  31. *
  32. * @li All of the data in the supplied buffers has been written. That is, the
  33. * bytes transferred is equal to the sum of the buffer sizes.
  34. *
  35. * @li An error occurred.
  36. *
  37. * This operation is implemented in terms of zero or more calls to the stream's
  38. * write_some function.
  39. *
  40. * @param s The stream to which the data is to be written. The type must support
  41. * the SyncWriteStream concept.
  42. *
  43. * @param buffers One or more buffers containing the data to be written. The sum
  44. * of the buffer sizes indicates the maximum number of bytes to write to the
  45. * stream.
  46. *
  47. * @returns The number of bytes transferred.
  48. *
  49. * @throws asio::system_error Thrown on failure.
  50. *
  51. * @par Example
  52. * To write a single data buffer use the @ref buffer function as follows:
  53. * @code asio::write(s, asio::buffer(data, size)); @endcode
  54. * See the @ref buffer documentation for information on writing multiple
  55. * buffers in one go, and how to use it with arrays, boost::array or
  56. * std::vector.
  57. *
  58. * @note This overload is equivalent to calling:
  59. * @code asio::write(
  60. * s, buffers,
  61. * asio::transfer_all()); @endcode
  62. */
  63. template <typename SyncWriteStream, typename ConstBufferSequence>
  64. std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers);
  65. /// Write a certain amount of data to a stream before returning.
  66. /**
  67. * This function is used to write a certain number of bytes of data to a stream.
  68. * The call will block until one of the following conditions is true:
  69. *
  70. * @li All of the data in the supplied buffers has been written. That is, the
  71. * bytes transferred is equal to the sum of the buffer sizes.
  72. *
  73. * @li The completion_condition function object returns 0.
  74. *
  75. * This operation is implemented in terms of zero or more calls to the stream's
  76. * write_some function.
  77. *
  78. * @param s The stream to which the data is to be written. The type must support
  79. * the SyncWriteStream concept.
  80. *
  81. * @param buffers One or more buffers containing the data to be written. The sum
  82. * of the buffer sizes indicates the maximum number of bytes to write to the
  83. * stream.
  84. *
  85. * @param completion_condition The function object to be called to determine
  86. * whether the write operation is complete. The signature of the function object
  87. * must be:
  88. * @code std::size_t completion_condition(
  89. * // Result of latest write_some operation.
  90. * const asio::error_code& error,
  91. *
  92. * // Number of bytes transferred so far.
  93. * std::size_t bytes_transferred
  94. * ); @endcode
  95. * A return value of 0 indicates that the write operation is complete. A
  96. * non-zero return value indicates the maximum number of bytes to be written on
  97. * the next call to the stream's write_some function.
  98. *
  99. * @returns The number of bytes transferred.
  100. *
  101. * @throws asio::system_error Thrown on failure.
  102. *
  103. * @par Example
  104. * To write a single data buffer use the @ref buffer function as follows:
  105. * @code asio::write(s, asio::buffer(data, size),
  106. * asio::transfer_at_least(32)); @endcode
  107. * See the @ref buffer documentation for information on writing multiple
  108. * buffers in one go, and how to use it with arrays, boost::array or
  109. * std::vector.
  110. */
  111. template <typename SyncWriteStream, typename ConstBufferSequence,
  112. typename CompletionCondition>
  113. std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
  114. CompletionCondition completion_condition);
  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 asio::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. * @param ec Set to indicate what error occurred, if any.
  150. *
  151. * @returns The number of bytes written. If an error occurs, returns the total
  152. * number of bytes successfully transferred prior to the error.
  153. */
  154. template <typename SyncWriteStream, typename ConstBufferSequence,
  155. typename CompletionCondition>
  156. std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
  157. CompletionCondition completion_condition, asio::error_code& ec);
  158. #if !defined(BOOST_NO_IOSTREAM)
  159. /// Write all of the supplied data to a stream before returning.
  160. /**
  161. * This function is used to write a certain number of bytes of data to a stream.
  162. * The call will block until one of the following conditions is true:
  163. *
  164. * @li All of the data in the supplied basic_streambuf has been written.
  165. *
  166. * @li An error occurred.
  167. *
  168. * This operation is implemented in terms of zero or more calls to the stream's
  169. * write_some function.
  170. *
  171. * @param s The stream to which the data is to be written. The type must support
  172. * the SyncWriteStream concept.
  173. *
  174. * @param b The basic_streambuf object from which data will be written.
  175. *
  176. * @returns The number of bytes transferred.
  177. *
  178. * @throws asio::system_error Thrown on failure.
  179. *
  180. * @note This overload is equivalent to calling:
  181. * @code asio::write(
  182. * s, b,
  183. * asio::transfer_all()); @endcode
  184. */
  185. template <typename SyncWriteStream, typename Allocator>
  186. std::size_t write(SyncWriteStream& s, basic_streambuf<Allocator>& b);
  187. /// Write a certain amount of data to a stream before returning.
  188. /**
  189. * This function is used to write a certain number of bytes of data to a stream.
  190. * The call will block until one of the following conditions is true:
  191. *
  192. * @li All of the data in the supplied basic_streambuf has been written.
  193. *
  194. * @li The completion_condition function object returns 0.
  195. *
  196. * This operation is implemented in terms of zero or more calls to the stream's
  197. * write_some function.
  198. *
  199. * @param s The stream to which the data is to be written. The type must support
  200. * the SyncWriteStream concept.
  201. *
  202. * @param b The basic_streambuf object from which data will be written.
  203. *
  204. * @param completion_condition The function object to be called to determine
  205. * whether the write operation is complete. The signature of the function object
  206. * must be:
  207. * @code std::size_t completion_condition(
  208. * // Result of latest write_some operation.
  209. * const asio::error_code& error,
  210. *
  211. * // Number of bytes transferred so far.
  212. * std::size_t bytes_transferred
  213. * ); @endcode
  214. * A return value of 0 indicates that the write operation is complete. A
  215. * non-zero return value indicates the maximum number of bytes to be written on
  216. * the next call to the stream's write_some function.
  217. *
  218. * @returns The number of bytes transferred.
  219. *
  220. * @throws asio::system_error Thrown on failure.
  221. */
  222. template <typename SyncWriteStream, typename Allocator,
  223. typename CompletionCondition>
  224. std::size_t write(SyncWriteStream& s, basic_streambuf<Allocator>& b,
  225. CompletionCondition completion_condition);
  226. /// Write a certain amount of data to a stream before returning.
  227. /**
  228. * This function is used to write a certain number of bytes of data to a stream.
  229. * The call will block until one of the following conditions is true:
  230. *
  231. * @li All of the data in the supplied basic_streambuf has been written.
  232. *
  233. * @li The completion_condition function object returns 0.
  234. *
  235. * This operation is implemented in terms of zero or more calls to the stream's
  236. * write_some function.
  237. *
  238. * @param s The stream to which the data is to be written. The type must support
  239. * the SyncWriteStream concept.
  240. *
  241. * @param b The basic_streambuf object from which data will be written.
  242. *
  243. * @param completion_condition The function object to be called to determine
  244. * whether the write operation is complete. The signature of the function object
  245. * must be:
  246. * @code std::size_t completion_condition(
  247. * // Result of latest write_some operation.
  248. * const asio::error_code& error,
  249. *
  250. * // Number of bytes transferred so far.
  251. * std::size_t bytes_transferred
  252. * ); @endcode
  253. * A return value of 0 indicates that the write operation is complete. A
  254. * non-zero return value indicates the maximum number of bytes to be written on
  255. * the next call to the stream's write_some function.
  256. *
  257. * @param ec Set to indicate what error occurred, if any.
  258. *
  259. * @returns The number of bytes written. If an error occurs, returns the total
  260. * number of bytes successfully transferred prior to the error.
  261. */
  262. template <typename SyncWriteStream, typename Allocator,
  263. typename CompletionCondition>
  264. std::size_t write(SyncWriteStream& s, basic_streambuf<Allocator>& b,
  265. CompletionCondition completion_condition, asio::error_code& ec);
  266. #endif // !defined(BOOST_NO_IOSTREAM)
  267. /*@}*/
  268. /**
  269. * @defgroup async_write asio::async_write
  270. *
  271. * @brief Start an asynchronous operation to write a certain amount of data to a
  272. * stream.
  273. */
  274. /*@{*/
  275. /// Start an asynchronous operation to write all of the supplied data to a
  276. /// stream.
  277. /**
  278. * This function is used to asynchronously write a certain number of bytes of
  279. * data to a stream. The function call always returns immediately. The
  280. * asynchronous operation will continue until one of the following conditions
  281. * is true:
  282. *
  283. * @li All of the data in the supplied buffers has been written. That is, the
  284. * bytes transferred is equal to the sum of the buffer sizes.
  285. *
  286. * @li An error occurred.
  287. *
  288. * This operation is implemented in terms of zero or more calls to the stream's
  289. * async_write_some function, and is known as a <em>composed operation</em>. The
  290. * program must ensure that the stream performs no other write operations (such
  291. * as async_write, the stream's async_write_some function, or any other composed
  292. * operations that perform writes) until this operation completes.
  293. *
  294. * @param s The stream to which the data is to be written. The type must support
  295. * the AsyncWriteStream concept.
  296. *
  297. * @param buffers One or more buffers containing the data to be written.
  298. * Although the buffers object may be copied as necessary, ownership of the
  299. * underlying memory blocks is retained by the caller, which must guarantee
  300. * that they remain valid until the handler is called.
  301. *
  302. * @param handler The handler to be called when the write operation completes.
  303. * Copies will be made of the handler as required. The function signature of
  304. * the handler must be:
  305. * @code void handler(
  306. * const asio::error_code& error, // Result of operation.
  307. *
  308. * std::size_t bytes_transferred // Number of bytes written from the
  309. * // buffers. If an error occurred,
  310. * // this will be less than the sum
  311. * // of the buffer sizes.
  312. * ); @endcode
  313. * Regardless of whether the asynchronous operation completes immediately or
  314. * not, the handler will not be invoked from within this function. Invocation of
  315. * the handler will be performed in a manner equivalent to using
  316. * asio::io_service::post().
  317. *
  318. * @par Example
  319. * To write a single data buffer use the @ref buffer function as follows:
  320. * @code
  321. * asio::async_write(s, asio::buffer(data, size), handler);
  322. * @endcode
  323. * See the @ref buffer documentation for information on writing multiple
  324. * buffers in one go, and how to use it with arrays, boost::array or
  325. * std::vector.
  326. */
  327. template <typename AsyncWriteStream, typename ConstBufferSequence,
  328. typename WriteHandler>
  329. void async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
  330. WriteHandler handler);
  331. /// Start an asynchronous operation to write a certain amount of data to a
  332. /// stream.
  333. /**
  334. * This function is used to asynchronously write a certain number of bytes of
  335. * data to a stream. The function call always returns immediately. The
  336. * asynchronous operation will continue until one of the following conditions
  337. * is true:
  338. *
  339. * @li All of the data in the supplied buffers has been written. That is, the
  340. * bytes transferred is equal to the sum of the buffer sizes.
  341. *
  342. * @li The completion_condition function object returns 0.
  343. *
  344. * This operation is implemented in terms of zero or more calls to the stream's
  345. * async_write_some function, and is known as a <em>composed operation</em>. The
  346. * program must ensure that the stream performs no other write operations (such
  347. * as async_write, the stream's async_write_some function, or any other composed
  348. * operations that perform writes) until this operation completes.
  349. *
  350. * @param s The stream to which the data is to be written. The type must support
  351. * the AsyncWriteStream concept.
  352. *
  353. * @param buffers One or more buffers containing the data to be written.
  354. * Although the buffers object may be copied as necessary, ownership of the
  355. * underlying memory blocks is retained by the caller, which must guarantee
  356. * that they remain valid until the handler is called.
  357. *
  358. * @param completion_condition The function object to be called to determine
  359. * whether the write operation is complete. The signature of the function object
  360. * must be:
  361. * @code std::size_t completion_condition(
  362. * // Result of latest async_write_some operation.
  363. * const asio::error_code& error,
  364. *
  365. * // Number of bytes transferred so far.
  366. * std::size_t bytes_transferred
  367. * ); @endcode
  368. * A return value of 0 indicates that the write operation is complete. A
  369. * non-zero return value indicates the maximum number of bytes to be written on
  370. * the next call to the stream's async_write_some function.
  371. *
  372. * @param handler The handler to be called when the write operation completes.
  373. * Copies will be made of the handler as required. The function signature of the
  374. * handler must be:
  375. * @code void handler(
  376. * const asio::error_code& error, // Result of operation.
  377. *
  378. * std::size_t bytes_transferred // Number of bytes written from the
  379. * // buffers. If an error occurred,
  380. * // this will be less than the sum
  381. * // of the buffer sizes.
  382. * ); @endcode
  383. * Regardless of whether the asynchronous operation completes immediately or
  384. * not, the handler will not be invoked from within this function. Invocation of
  385. * the handler will be performed in a manner equivalent to using
  386. * asio::io_service::post().
  387. *
  388. * @par Example
  389. * To write a single data buffer use the @ref buffer function as follows:
  390. * @code asio::async_write(s,
  391. * asio::buffer(data, size),
  392. * asio::transfer_at_least(32),
  393. * handler); @endcode
  394. * See the @ref buffer documentation for information on writing multiple
  395. * buffers in one go, and how to use it with arrays, boost::array or
  396. * std::vector.
  397. */
  398. template <typename AsyncWriteStream, typename ConstBufferSequence,
  399. typename CompletionCondition, typename WriteHandler>
  400. void async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
  401. CompletionCondition completion_condition, WriteHandler handler);
  402. #if !defined(BOOST_NO_IOSTREAM)
  403. /// Start an asynchronous operation to write all of the supplied data to a
  404. /// stream.
  405. /**
  406. * This function is used to asynchronously write a certain number of bytes of
  407. * data to a stream. The function call always returns immediately. The
  408. * asynchronous operation will continue until one of the following conditions
  409. * 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. * async_write_some function, and is known as a <em>composed operation</em>. The
  417. * program must ensure that the stream performs no other write operations (such
  418. * as async_write, the stream's async_write_some function, or any other composed
  419. * operations that perform writes) until this operation completes.
  420. *
  421. * @param s The stream to which the data is to be written. The type must support
  422. * the AsyncWriteStream concept.
  423. *
  424. * @param b A basic_streambuf object from which data will be written. Ownership
  425. * of the streambuf is retained by the caller, which must guarantee that it
  426. * remains valid until the handler is called.
  427. *
  428. * @param handler The handler to be called when the write operation completes.
  429. * Copies will be made of the handler as required. The function signature of the
  430. * handler must be:
  431. * @code void handler(
  432. * const asio::error_code& error, // Result of operation.
  433. *
  434. * std::size_t bytes_transferred // Number of bytes written from the
  435. * // buffers. If an error occurred,
  436. * // this will be less than the sum
  437. * // of the buffer sizes.
  438. * ); @endcode
  439. * Regardless of whether the asynchronous operation completes immediately or
  440. * not, the handler will not be invoked from within this function. Invocation of
  441. * the handler will be performed in a manner equivalent to using
  442. * asio::io_service::post().
  443. */
  444. template <typename AsyncWriteStream, typename Allocator, typename WriteHandler>
  445. void async_write(AsyncWriteStream& s, basic_streambuf<Allocator>& b,
  446. WriteHandler handler);
  447. /// Start an asynchronous operation to write a certain amount of data to a
  448. /// stream.
  449. /**
  450. * This function is used to asynchronously write a certain number of bytes of
  451. * data to a stream. The function call always returns immediately. The
  452. * asynchronous operation will continue until one of the following conditions
  453. * is true:
  454. *
  455. * @li All of the data in the supplied basic_streambuf has been written.
  456. *
  457. * @li The completion_condition function object returns 0.
  458. *
  459. * This operation is implemented in terms of zero or more calls to the stream's
  460. * async_write_some function, and is known as a <em>composed operation</em>. The
  461. * program must ensure that the stream performs no other write operations (such
  462. * as async_write, the stream's async_write_some function, or any other composed
  463. * operations that perform writes) until this operation completes.
  464. *
  465. * @param s The stream to which the data is to be written. The type must support
  466. * the AsyncWriteStream concept.
  467. *
  468. * @param b A basic_streambuf object from which data will be written. Ownership
  469. * of the streambuf is retained by the caller, which must guarantee that it
  470. * remains valid until the handler is called.
  471. *
  472. * @param completion_condition The function object to be called to determine
  473. * whether the write operation is complete. The signature of the function object
  474. * must be:
  475. * @code std::size_t completion_condition(
  476. * // Result of latest async_write_some operation.
  477. * const asio::error_code& error,
  478. *
  479. * // Number of bytes transferred so far.
  480. * std::size_t bytes_transferred
  481. * ); @endcode
  482. * A return value of 0 indicates that the write operation is complete. A
  483. * non-zero return value indicates the maximum number of bytes to be written on
  484. * the next call to the stream's async_write_some function.
  485. *
  486. * @param handler The handler to be called when the write operation completes.
  487. * Copies will be made of the handler as required. The function signature of the
  488. * handler must be:
  489. * @code void handler(
  490. * const asio::error_code& error, // Result of operation.
  491. *
  492. * std::size_t bytes_transferred // Number of bytes written from the
  493. * // buffers. If an error occurred,
  494. * // this will be less than the sum
  495. * // of the buffer sizes.
  496. * ); @endcode
  497. * Regardless of whether the asynchronous operation completes immediately or
  498. * not, the handler will not be invoked from within this function. Invocation of
  499. * the handler will be performed in a manner equivalent to using
  500. * asio::io_service::post().
  501. */
  502. template <typename AsyncWriteStream, typename Allocator,
  503. typename CompletionCondition, typename WriteHandler>
  504. void async_write(AsyncWriteStream& s, basic_streambuf<Allocator>& b,
  505. CompletionCondition completion_condition, WriteHandler handler);
  506. #endif // !defined(BOOST_NO_IOSTREAM)
  507. /*@}*/
  508. } // namespace asio
  509. #include "asio/detail/pop_options.hpp"
  510. #include "asio/impl/write.hpp"
  511. #endif // ASIO_WRITE_HPP