read.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_DETAIL_READ_HPP
  10. #define BOOST_BEAST_DETAIL_READ_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/error.hpp>
  13. #include <boost/beast/core/stream_traits.hpp>
  14. #include <boost/beast/core/detail/is_invocable.hpp>
  15. #include <boost/asio/async_result.hpp>
  16. #include <cstdlib>
  17. namespace boost {
  18. namespace beast {
  19. namespace detail {
  20. //------------------------------------------------------------------------------
  21. /** Read data into a dynamic buffer from a stream until a condition is met.
  22. This function is used to read from a stream into a dynamic buffer until
  23. a condition is met. The call will block until one of the following is true:
  24. @li The specified dynamic buffer sequence is full (that is, it has
  25. reached its currently configured maximum size).
  26. @li The `completion_condition` function object returns 0.
  27. This operation is implemented in terms of zero or more calls to the
  28. stream's `read_some` function.
  29. @param stream The stream from which the data is to be read. The type
  30. must support the <em>SyncReadStream</em> requirements.
  31. @param buffer The dynamic buffer sequence into which the data will be read.
  32. @param completion_condition The function object to be called to determine
  33. whether the read operation is complete. The function object must be invocable
  34. with this signature:
  35. @code
  36. std::size_t
  37. completion_condition(
  38. // Modifiable result of latest read_some operation.
  39. error_code& ec,
  40. // Number of bytes transferred so far.
  41. std::size_t bytes_transferred
  42. // The dynamic buffer used to store the bytes read
  43. DynamicBuffer& buffer
  44. );
  45. @endcode
  46. A non-zero return value indicates the maximum number of bytes to be read on
  47. the next call to the stream's `read_some` function. A return value of 0
  48. from the completion condition indicates that the read operation is complete;
  49. in this case the optionally modifiable error passed to the completion
  50. condition will be delivered to the caller as an exception.
  51. @returns The number of bytes transferred from the stream.
  52. @throws net::system_error Thrown on failure.
  53. */
  54. template<
  55. class SyncReadStream,
  56. class DynamicBuffer,
  57. class CompletionCondition
  58. #if ! BOOST_BEAST_DOXYGEN
  59. , class = typename std::enable_if<
  60. is_sync_read_stream<SyncReadStream>::value &&
  61. net::is_dynamic_buffer<DynamicBuffer>::value &&
  62. detail::is_invocable<CompletionCondition,
  63. void(error_code&, std::size_t, DynamicBuffer&)>::value
  64. >::type
  65. #endif
  66. >
  67. std::size_t
  68. read(
  69. SyncReadStream& stream,
  70. DynamicBuffer& buffer,
  71. CompletionCondition completion_condition);
  72. /** Read data into a dynamic buffer from a stream until a condition is met.
  73. This function is used to read from a stream into a dynamic buffer until
  74. a condition is met. The call will block until one of the following is true:
  75. @li The specified dynamic buffer sequence is full (that is, it has
  76. reached its currently configured maximum size).
  77. @li The `completion_condition` function object returns 0.
  78. This operation is implemented in terms of zero or more calls to the
  79. stream's `read_some` function.
  80. @param stream The stream from which the data is to be read. The type
  81. must support the <em>SyncReadStream</em> requirements.
  82. @param buffer The dynamic buffer sequence into which the data will be read.
  83. @param completion_condition The function object to be called to determine
  84. whether the read operation is complete. The function object must be invocable
  85. with this signature:
  86. @code
  87. std::size_t
  88. completion_condition(
  89. // Modifiable result of latest read_some operation.
  90. error_code& ec,
  91. // Number of bytes transferred so far.
  92. std::size_t bytes_transferred
  93. // The dynamic buffer used to store the bytes read
  94. DynamicBuffer& buffer
  95. );
  96. @endcode
  97. A non-zero return value indicates the maximum number of bytes to be read on
  98. the next call to the stream's `read_some` function. A return value of 0
  99. from the completion condition indicates that the read operation is complete;
  100. in this case the optionally modifiable error passed to the completion
  101. condition will be delivered to the caller.
  102. @returns The number of bytes transferred from the stream.
  103. */
  104. template<
  105. class SyncReadStream,
  106. class DynamicBuffer,
  107. class CompletionCondition
  108. #if ! BOOST_BEAST_DOXYGEN
  109. , class = typename std::enable_if<
  110. is_sync_read_stream<SyncReadStream>::value &&
  111. net::is_dynamic_buffer<DynamicBuffer>::value &&
  112. detail::is_invocable<CompletionCondition,
  113. void(error_code&, std::size_t, DynamicBuffer&)>::value
  114. >::type
  115. #endif
  116. >
  117. std::size_t
  118. read(
  119. SyncReadStream& stream,
  120. DynamicBuffer& buffer,
  121. CompletionCondition completion_condition,
  122. error_code& ec);
  123. /** Asynchronously read data into a dynamic buffer from a stream until a condition is met.
  124. This function is used to asynchronously read from a stream into a dynamic
  125. buffer until a condition is met. The function call always returns immediately.
  126. The asynchronous operation will continue until one of the following is true:
  127. @li The specified dynamic buffer sequence is full (that is, it has
  128. reached its currently configured maximum size).
  129. @li The `completion_condition` function object returns 0.
  130. This operation is implemented in terms of zero or more calls to the stream's
  131. `async_read_some` function, and is known as a <em>composed operation</em>. The
  132. program must ensure that the stream performs no other read operations (such
  133. as `async_read`, the stream's `async_read_some` function, or any other composed
  134. operations that perform reads) until this operation completes.
  135. @param stream The stream from which the data is to be read. The type must
  136. support the <em>AsyncReadStream</em> requirements.
  137. @param buffer The dynamic buffer sequence into which the data will be read.
  138. Ownership of the object is retained by the caller, which must guarantee
  139. that it remains valid until the handler is called.
  140. @param completion_condition The function object to be called to determine
  141. whether the read operation is complete. The function object must be invocable
  142. with this signature:
  143. @code
  144. std::size_t
  145. completion_condition(
  146. // Modifiable result of latest async_read_some operation.
  147. error_code& ec,
  148. // Number of bytes transferred so far.
  149. std::size_t bytes_transferred,
  150. // The dynamic buffer used to store the bytes read
  151. DynamicBuffer& buffer
  152. );
  153. @endcode
  154. A non-zero return value indicates the maximum number of bytes to be read on
  155. the next call to the stream's `async_read_some` function. A return value of 0
  156. from the completion condition indicates that the read operation is complete;
  157. in this case the optionally modifiable error passed to the completion
  158. condition will be delivered to the completion handler.
  159. @param handler The completion handler to invoke when the operation
  160. completes. The implementation takes ownership of the handler by
  161. performing a decay-copy. The equivalent function signature of
  162. the handler must be:
  163. @code
  164. void
  165. handler(
  166. error_code const& ec, // Result of operation.
  167. std::size_t bytes_transferred // Number of bytes copied into
  168. // the dynamic buffer. If an error
  169. // occurred, this will be the number
  170. // of bytes successfully transferred
  171. // prior to the error.
  172. );
  173. @endcode
  174. Regardless of whether the asynchronous operation completes
  175. immediately or not, the handler will not be invoked from within
  176. this function. Invocation of the handler will be performed in a
  177. manner equivalent to using `net::post`.
  178. */
  179. template<
  180. class AsyncReadStream,
  181. class DynamicBuffer,
  182. class CompletionCondition,
  183. class ReadHandler
  184. #if ! BOOST_BEAST_DOXYGEN
  185. , class = typename std::enable_if<
  186. is_async_read_stream<AsyncReadStream>::value &&
  187. net::is_dynamic_buffer<DynamicBuffer>::value &&
  188. detail::is_invocable<CompletionCondition,
  189. void(error_code&, std::size_t, DynamicBuffer&)>::value
  190. >::type
  191. #endif
  192. >
  193. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  194. async_read(
  195. AsyncReadStream& stream,
  196. DynamicBuffer& buffer,
  197. CompletionCondition&& completion_condition,
  198. ReadHandler&& handler);
  199. } // detail
  200. } // beast
  201. } // boost
  202. #include <boost/beast/core/detail/impl/read.hpp>
  203. #endif