read.hpp 21 KB

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