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