read.hpp 21 KB

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