buffered_stream.hpp 8.5 KB

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