buffered_read_stream.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // buffered_read_stream.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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_BUFFERED_READ_STREAM_HPP
  11. #define ASIO_BUFFERED_READ_STREAM_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <cstddef>
  17. #include "asio/async_result.hpp"
  18. #include "asio/buffered_read_stream_fwd.hpp"
  19. #include "asio/buffer.hpp"
  20. #include "asio/detail/bind_handler.hpp"
  21. #include "asio/detail/buffer_resize_guard.hpp"
  22. #include "asio/detail/buffered_stream_storage.hpp"
  23. #include "asio/detail/noncopyable.hpp"
  24. #include "asio/detail/type_traits.hpp"
  25. #include "asio/error.hpp"
  26. #include "asio/detail/push_options.hpp"
  27. namespace asio {
  28. /// Adds buffering to the read-related operations of a stream.
  29. /**
  30. * The buffered_read_stream class template can be used to add buffering to the
  31. * synchronous and asynchronous read operations of a stream.
  32. *
  33. * @par Thread Safety
  34. * @e Distinct @e objects: Safe.@n
  35. * @e Shared @e objects: Unsafe.
  36. *
  37. * @par Concepts:
  38. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  39. */
  40. template <typename Stream>
  41. class buffered_read_stream
  42. : private noncopyable
  43. {
  44. public:
  45. /// The type of the next layer.
  46. typedef typename remove_reference<Stream>::type next_layer_type;
  47. /// The type of the lowest layer.
  48. typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
  49. /// The type of the executor associated with the object.
  50. typedef typename lowest_layer_type::executor_type executor_type;
  51. #if defined(GENERATING_DOCUMENTATION)
  52. /// The default buffer size.
  53. static const std::size_t default_buffer_size = implementation_defined;
  54. #else
  55. ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
  56. #endif
  57. /// Construct, passing the specified argument to initialise the next layer.
  58. template <typename Arg>
  59. explicit buffered_read_stream(Arg& a)
  60. : next_layer_(a),
  61. storage_(default_buffer_size)
  62. {
  63. }
  64. /// Construct, passing the specified argument to initialise the next layer.
  65. template <typename Arg>
  66. buffered_read_stream(Arg& a, std::size_t buffer_size)
  67. : next_layer_(a),
  68. storage_(buffer_size)
  69. {
  70. }
  71. /// Get a reference to the next layer.
  72. next_layer_type& next_layer()
  73. {
  74. return next_layer_;
  75. }
  76. /// Get a reference to the lowest layer.
  77. lowest_layer_type& lowest_layer()
  78. {
  79. return next_layer_.lowest_layer();
  80. }
  81. /// Get a const reference to the lowest layer.
  82. const lowest_layer_type& lowest_layer() const
  83. {
  84. return next_layer_.lowest_layer();
  85. }
  86. /// Get the executor associated with the object.
  87. executor_type get_executor() ASIO_NOEXCEPT
  88. {
  89. return next_layer_.lowest_layer().get_executor();
  90. }
  91. /// Close the stream.
  92. void close()
  93. {
  94. next_layer_.close();
  95. }
  96. /// Close the stream.
  97. ASIO_SYNC_OP_VOID close(asio::error_code& ec)
  98. {
  99. next_layer_.close(ec);
  100. ASIO_SYNC_OP_VOID_RETURN(ec);
  101. }
  102. /// Write the given data to the stream. Returns the number of bytes written.
  103. /// Throws an exception on failure.
  104. template <typename ConstBufferSequence>
  105. std::size_t write_some(const ConstBufferSequence& buffers)
  106. {
  107. return next_layer_.write_some(buffers);
  108. }
  109. /// Write the given data to the stream. Returns the number of bytes written,
  110. /// or 0 if an error occurred.
  111. template <typename ConstBufferSequence>
  112. std::size_t write_some(const ConstBufferSequence& buffers,
  113. asio::error_code& ec)
  114. {
  115. return next_layer_.write_some(buffers, ec);
  116. }
  117. /// Start an asynchronous write. The data being written must be valid for the
  118. /// lifetime of the asynchronous operation.
  119. /**
  120. * @par Completion Signature
  121. * @code void(asio::error_code, std::size_t) @endcode
  122. */
  123. template <typename ConstBufferSequence,
  124. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  125. std::size_t)) WriteHandler
  126. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  127. ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  128. void (asio::error_code, std::size_t))
  129. async_write_some(const ConstBufferSequence& buffers,
  130. ASIO_MOVE_ARG(WriteHandler) handler
  131. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  132. {
  133. return next_layer_.async_write_some(buffers,
  134. ASIO_MOVE_CAST(WriteHandler)(handler));
  135. }
  136. /// Fill the buffer with some data. Returns the number of bytes placed in the
  137. /// buffer as a result of the operation. Throws an exception on failure.
  138. std::size_t fill();
  139. /// Fill the buffer with some data. Returns the number of bytes placed in the
  140. /// buffer as a result of the operation, or 0 if an error occurred.
  141. std::size_t fill(asio::error_code& ec);
  142. /// Start an asynchronous fill.
  143. /**
  144. * @par Completion Signature
  145. * @code void(asio::error_code, std::size_t) @endcode
  146. */
  147. template <
  148. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  149. std::size_t)) ReadHandler
  150. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  151. ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  152. void (asio::error_code, std::size_t))
  153. async_fill(
  154. ASIO_MOVE_ARG(ReadHandler) handler
  155. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type));
  156. /// Read some data from the stream. Returns the number of bytes read. Throws
  157. /// an exception on failure.
  158. template <typename MutableBufferSequence>
  159. std::size_t read_some(const MutableBufferSequence& buffers);
  160. /// Read some data from the stream. Returns the number of bytes read or 0 if
  161. /// an error occurred.
  162. template <typename MutableBufferSequence>
  163. std::size_t read_some(const MutableBufferSequence& buffers,
  164. asio::error_code& ec);
  165. /// Start an asynchronous read. The buffer into which the data will be read
  166. /// must be valid for the lifetime of the asynchronous operation.
  167. /**
  168. * @par Completion Signature
  169. * @code void(asio::error_code, std::size_t) @endcode
  170. */
  171. template <typename MutableBufferSequence,
  172. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  173. std::size_t)) ReadHandler
  174. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  175. ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  176. void (asio::error_code, std::size_t))
  177. async_read_some(const MutableBufferSequence& buffers,
  178. ASIO_MOVE_ARG(ReadHandler) handler
  179. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type));
  180. /// Peek at the incoming data on the stream. Returns the number of bytes read.
  181. /// Throws an exception on failure.
  182. template <typename MutableBufferSequence>
  183. std::size_t peek(const MutableBufferSequence& buffers);
  184. /// Peek at the incoming data on the stream. Returns the number of bytes read,
  185. /// or 0 if an error occurred.
  186. template <typename MutableBufferSequence>
  187. std::size_t peek(const MutableBufferSequence& buffers,
  188. asio::error_code& ec);
  189. /// Determine the amount of data that may be read without blocking.
  190. std::size_t in_avail()
  191. {
  192. return storage_.size();
  193. }
  194. /// Determine the amount of data that may be read without blocking.
  195. std::size_t in_avail(asio::error_code& ec)
  196. {
  197. ec = asio::error_code();
  198. return storage_.size();
  199. }
  200. private:
  201. /// Copy data out of the internal buffer to the specified target buffer.
  202. /// Returns the number of bytes copied.
  203. template <typename MutableBufferSequence>
  204. std::size_t copy(const MutableBufferSequence& buffers)
  205. {
  206. std::size_t bytes_copied = asio::buffer_copy(
  207. buffers, storage_.data(), storage_.size());
  208. storage_.consume(bytes_copied);
  209. return bytes_copied;
  210. }
  211. /// Copy data from the internal buffer to the specified target buffer, without
  212. /// removing the data from the internal buffer. Returns the number of bytes
  213. /// copied.
  214. template <typename MutableBufferSequence>
  215. std::size_t peek_copy(const MutableBufferSequence& buffers)
  216. {
  217. return asio::buffer_copy(buffers, storage_.data(), storage_.size());
  218. }
  219. /// The next layer.
  220. Stream next_layer_;
  221. // The data in the buffer.
  222. detail::buffered_stream_storage storage_;
  223. };
  224. } // namespace asio
  225. #include "asio/detail/pop_options.hpp"
  226. #include "asio/impl/buffered_read_stream.hpp"
  227. #endif // ASIO_BUFFERED_READ_STREAM_HPP