write.hpp 22 KB

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