buffered_read_stream.hpp 8.1 KB

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