buffered_stream.hpp 8.9 KB

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