buffered_stream.hpp 10 KB

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