write.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. //
  2. // write.hpp
  3. // ~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include <boost/asio/detail/pop_options.hpp>
  20. #include <boost/asio/basic_streambuf.hpp>
  21. #include <boost/asio/error.hpp>
  22. namespace boost {
  23. namespace asio {
  24. /**
  25. * @defgroup write boost::asio::write
  26. *
  27. * @brief Write a certain amount of data to a stream before returning.
  28. */
  29. /*@{*/
  30. /// Write all of the supplied data to a stream before returning.
  31. /**
  32. * This function is used to write a certain number of bytes of data to a stream.
  33. * The call will block until one of the following conditions is true:
  34. *
  35. * @li All of the data in the supplied buffers has been written. That is, the
  36. * bytes transferred is equal to the sum of the buffer sizes.
  37. *
  38. * @li An error occurred.
  39. *
  40. * This operation is implemented in terms of zero or more calls to the stream's
  41. * write_some function.
  42. *
  43. * @param s The stream to which the data is to be written. The type must support
  44. * the SyncWriteStream concept.
  45. *
  46. * @param buffers One or more buffers containing the data to be written. The sum
  47. * of the buffer sizes indicates the maximum number of bytes to write to the
  48. * stream.
  49. *
  50. * @returns The number of bytes transferred.
  51. *
  52. * @throws boost::system::system_error Thrown on failure.
  53. *
  54. * @par Example
  55. * To write a single data buffer use the @ref buffer function as follows:
  56. * @code boost::asio::write(s, boost::asio::buffer(data, size)); @endcode
  57. * See the @ref buffer documentation for information on writing multiple
  58. * buffers in one go, and how to use it with arrays, boost::array or
  59. * std::vector.
  60. *
  61. * @note This overload is equivalent to calling:
  62. * @code boost::asio::write(
  63. * s, buffers,
  64. * boost::asio::transfer_all()); @endcode
  65. */
  66. template <typename SyncWriteStream, typename ConstBufferSequence>
  67. std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers);
  68. /// Write a certain amount of data to a stream before returning.
  69. /**
  70. * This function is used to write a certain number of bytes of data to a stream.
  71. * The call will block until one of the following conditions is true:
  72. *
  73. * @li All of the data in the supplied buffers has been written. That is, the
  74. * bytes transferred is equal to the sum of the buffer sizes.
  75. *
  76. * @li The completion_condition function object returns 0.
  77. *
  78. * This operation is implemented in terms of zero or more calls to the stream's
  79. * write_some function.
  80. *
  81. * @param s The stream to which the data is to be written. The type must support
  82. * the SyncWriteStream concept.
  83. *
  84. * @param buffers One or more buffers containing the data to be written. The sum
  85. * of the buffer sizes indicates the maximum number of bytes to write to the
  86. * stream.
  87. *
  88. * @param completion_condition The function object to be called to determine
  89. * whether the write operation is complete. The signature of the function object
  90. * must be:
  91. * @code std::size_t completion_condition(
  92. * // Result of latest write_some operation.
  93. * const boost::system::error_code& error,
  94. *
  95. * // Number of bytes transferred so far.
  96. * std::size_t bytes_transferred
  97. * ); @endcode
  98. * A return value of 0 indicates that the write operation is complete. A
  99. * non-zero return value indicates the maximum number of bytes to be written on
  100. * the next call to the stream's write_some function.
  101. *
  102. * @returns The number of bytes transferred.
  103. *
  104. * @throws boost::system::system_error Thrown on failure.
  105. *
  106. * @par Example
  107. * To write a single data buffer use the @ref buffer function as follows:
  108. * @code boost::asio::write(s, boost::asio::buffer(data, size),
  109. * boost::asio::transfer_at_least(32)); @endcode
  110. * See the @ref buffer documentation for information on writing multiple
  111. * buffers in one go, and how to use it with arrays, boost::array or
  112. * std::vector.
  113. */
  114. template <typename SyncWriteStream, typename ConstBufferSequence,
  115. typename CompletionCondition>
  116. std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
  117. CompletionCondition completion_condition);
  118. /// Write a certain amount of data to a stream before returning.
  119. /**
  120. * This function is used to write a certain number of bytes of data to a stream.
  121. * The call will block until one of the following conditions is true:
  122. *
  123. * @li All of the data in the supplied buffers has been written. That is, the
  124. * bytes transferred is equal to the sum of the buffer sizes.
  125. *
  126. * @li The completion_condition function object returns 0.
  127. *
  128. * This operation is implemented in terms of zero or more calls to the stream's
  129. * write_some function.
  130. *
  131. * @param s The stream to which the data is to be written. The type must support
  132. * the SyncWriteStream concept.
  133. *
  134. * @param buffers One or more buffers containing the data to be written. The sum
  135. * of the buffer sizes indicates the maximum number of bytes to write to the
  136. * stream.
  137. *
  138. * @param completion_condition The function object to be called to determine
  139. * whether the write operation is complete. The signature of the function object
  140. * must be:
  141. * @code std::size_t completion_condition(
  142. * // Result of latest write_some operation.
  143. * const boost::system::error_code& error,
  144. *
  145. * // Number of bytes transferred so far.
  146. * std::size_t bytes_transferred
  147. * ); @endcode
  148. * A return value of 0 indicates that the write operation is complete. A
  149. * non-zero return value indicates the maximum number of bytes to be written on
  150. * the next call to the stream's write_some function.
  151. *
  152. * @param ec Set to indicate what error occurred, if any.
  153. *
  154. * @returns The number of bytes written. If an error occurs, returns the total
  155. * number of bytes successfully transferred prior to the error.
  156. */
  157. template <typename SyncWriteStream, typename ConstBufferSequence,
  158. typename CompletionCondition>
  159. std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
  160. CompletionCondition completion_condition, boost::system::error_code& ec);
  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 boost::system::system_error Thrown on failure.
  181. *
  182. * @note This overload is equivalent to calling:
  183. * @code boost::asio::write(
  184. * s, b,
  185. * boost::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 boost::system::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 boost::system::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 boost::system::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, boost::system::error_code& ec);
  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.
  291. *
  292. * @param s The stream to which the data is to be written. The type must support
  293. * the AsyncWriteStream concept.
  294. *
  295. * @param buffers One or more buffers containing the data to be written.
  296. * Although the buffers object may be copied as necessary, ownership of the
  297. * underlying memory blocks is retained by the caller, which must guarantee
  298. * that they remain valid until the handler is called.
  299. *
  300. * @param handler The handler to be called when the write operation completes.
  301. * Copies will be made of the handler as required. The function signature of
  302. * the handler must be:
  303. * @code void handler(
  304. * const boost::system::error_code& error, // Result of operation.
  305. *
  306. * std::size_t bytes_transferred // Number of bytes written from the
  307. * // buffers. If an error occurred,
  308. * // this will be less than the sum
  309. * // of the buffer sizes.
  310. * ); @endcode
  311. * Regardless of whether the asynchronous operation completes immediately or
  312. * not, the handler will not be invoked from within this function. Invocation of
  313. * the handler will be performed in a manner equivalent to using
  314. * boost::asio::io_service::post().
  315. *
  316. * @par Example
  317. * To write a single data buffer use the @ref buffer function as follows:
  318. * @code
  319. * boost::asio::async_write(s, boost::asio::buffer(data, size), handler);
  320. * @endcode
  321. * See the @ref buffer documentation for information on writing multiple
  322. * buffers in one go, and how to use it with arrays, boost::array or
  323. * std::vector.
  324. */
  325. template <typename AsyncWriteStream, typename ConstBufferSequence,
  326. typename WriteHandler>
  327. void async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
  328. WriteHandler handler);
  329. /// Start an asynchronous operation to write a certain amount of data to a
  330. /// stream.
  331. /**
  332. * This function is used to asynchronously write a certain number of bytes of
  333. * data to a stream. The function call always returns immediately. The
  334. * asynchronous operation will continue until one of the following conditions
  335. * is true:
  336. *
  337. * @li All of the data in the supplied buffers has been written. That is, the
  338. * bytes transferred is equal to the sum of the buffer sizes.
  339. *
  340. * @li The completion_condition function object returns 0.
  341. *
  342. * This operation is implemented in terms of zero or more calls to the stream's
  343. * async_write_some function.
  344. *
  345. * @param s The stream to which the data is to be written. The type must support
  346. * the AsyncWriteStream concept.
  347. *
  348. * @param buffers One or more buffers containing the data to be written.
  349. * Although the buffers object may be copied as necessary, ownership of the
  350. * underlying memory blocks is retained by the caller, which must guarantee
  351. * that they remain valid until the handler is called.
  352. *
  353. * @param completion_condition The function object to be called to determine
  354. * whether the write operation is complete. The signature of the function object
  355. * must be:
  356. * @code std::size_t completion_condition(
  357. * // Result of latest async_write_some operation.
  358. * const boost::system::error_code& error,
  359. *
  360. * // Number of bytes transferred so far.
  361. * std::size_t bytes_transferred
  362. * ); @endcode
  363. * A return value of 0 indicates that the write operation is complete. A
  364. * non-zero return value indicates the maximum number of bytes to be written on
  365. * the next call to the stream's async_write_some function.
  366. *
  367. * @param handler The handler to be called when the write operation completes.
  368. * Copies will be made of the handler as required. The function signature of the
  369. * handler must be:
  370. * @code void handler(
  371. * const boost::system::error_code& error, // Result of operation.
  372. *
  373. * std::size_t bytes_transferred // Number of bytes written from the
  374. * // buffers. If an error occurred,
  375. * // this will be less than the sum
  376. * // of the buffer sizes.
  377. * ); @endcode
  378. * Regardless of whether the asynchronous operation completes immediately or
  379. * not, the handler will not be invoked from within this function. Invocation of
  380. * the handler will be performed in a manner equivalent to using
  381. * boost::asio::io_service::post().
  382. *
  383. * @par Example
  384. * To write a single data buffer use the @ref buffer function as follows:
  385. * @code boost::asio::async_write(s,
  386. * boost::asio::buffer(data, size),
  387. * boost::asio::transfer_at_least(32),
  388. * handler); @endcode
  389. * See the @ref buffer documentation for information on writing multiple
  390. * buffers in one go, and how to use it with arrays, boost::array or
  391. * std::vector.
  392. */
  393. template <typename AsyncWriteStream, typename ConstBufferSequence,
  394. typename CompletionCondition, typename WriteHandler>
  395. void async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
  396. CompletionCondition completion_condition, WriteHandler handler);
  397. /// Start an asynchronous operation to write all of the supplied data to a
  398. /// stream.
  399. /**
  400. * This function is used to asynchronously write a certain number of bytes of
  401. * data to a stream. The function call always returns immediately. The
  402. * asynchronous operation will continue until one of the following conditions
  403. * is true:
  404. *
  405. * @li All of the data in the supplied basic_streambuf has been written.
  406. *
  407. * @li An error occurred.
  408. *
  409. * This operation is implemented in terms of zero or more calls to the stream's
  410. * async_write_some function.
  411. *
  412. * @param s The stream to which the data is to be written. The type must support
  413. * the AsyncWriteStream concept.
  414. *
  415. * @param b A basic_streambuf object from which data will be written. Ownership
  416. * of the streambuf is retained by the caller, which must guarantee that it
  417. * remains valid until the handler is called.
  418. *
  419. * @param handler The handler to be called when the write operation completes.
  420. * Copies will be made of the handler as required. The function signature of the
  421. * handler must be:
  422. * @code void handler(
  423. * const boost::system::error_code& error, // Result of operation.
  424. *
  425. * std::size_t bytes_transferred // Number of bytes written from the
  426. * // buffers. If an error occurred,
  427. * // this will be less than the sum
  428. * // of the buffer sizes.
  429. * ); @endcode
  430. * Regardless of whether the asynchronous operation completes immediately or
  431. * not, the handler will not be invoked from within this function. Invocation of
  432. * the handler will be performed in a manner equivalent to using
  433. * boost::asio::io_service::post().
  434. */
  435. template <typename AsyncWriteStream, typename Allocator, typename WriteHandler>
  436. void async_write(AsyncWriteStream& s, basic_streambuf<Allocator>& b,
  437. WriteHandler handler);
  438. /// Start an asynchronous operation to write a certain amount of data to a
  439. /// stream.
  440. /**
  441. * This function is used to asynchronously write a certain number of bytes of
  442. * data to a stream. The function call always returns immediately. The
  443. * asynchronous operation will continue until one of the following conditions
  444. * is true:
  445. *
  446. * @li All of the data in the supplied basic_streambuf has been written.
  447. *
  448. * @li The completion_condition function object returns 0.
  449. *
  450. * This operation is implemented in terms of zero or more calls to the stream's
  451. * async_write_some function.
  452. *
  453. * @param s The stream to which the data is to be written. The type must support
  454. * the AsyncWriteStream concept.
  455. *
  456. * @param b A basic_streambuf object from which data will be written. Ownership
  457. * of the streambuf is retained by the caller, which must guarantee that it
  458. * remains valid until the handler is called.
  459. *
  460. * @param completion_condition The function object to be called to determine
  461. * whether the write operation is complete. The signature of the function object
  462. * must be:
  463. * @code std::size_t completion_condition(
  464. * // Result of latest async_write_some operation.
  465. * const boost::system::error_code& error,
  466. *
  467. * // Number of bytes transferred so far.
  468. * std::size_t bytes_transferred
  469. * ); @endcode
  470. * A return value of 0 indicates that the write operation is complete. A
  471. * non-zero return value indicates the maximum number of bytes to be written on
  472. * the next call to the stream's async_write_some function.
  473. *
  474. * @param handler The handler to be called when the write operation completes.
  475. * Copies will be made of the handler as required. The function signature of the
  476. * handler must be:
  477. * @code void handler(
  478. * const boost::system::error_code& error, // Result of operation.
  479. *
  480. * std::size_t bytes_transferred // Number of bytes written from the
  481. * // buffers. If an error occurred,
  482. * // this will be less than the sum
  483. * // of the buffer sizes.
  484. * ); @endcode
  485. * Regardless of whether the asynchronous operation completes immediately or
  486. * not, the handler will not be invoked from within this function. Invocation of
  487. * the handler will be performed in a manner equivalent to using
  488. * boost::asio::io_service::post().
  489. */
  490. template <typename AsyncWriteStream, typename Allocator,
  491. typename CompletionCondition, typename WriteHandler>
  492. void async_write(AsyncWriteStream& s, basic_streambuf<Allocator>& b,
  493. CompletionCondition completion_condition, WriteHandler handler);
  494. /*@}*/
  495. } // namespace asio
  496. } // namespace boost
  497. #include <boost/asio/impl/write.ipp>
  498. #include <boost/asio/detail/pop_options.hpp>
  499. #endif // BOOST_ASIO_WRITE_HPP