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