buffered_stream.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // buffered_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_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 typename remove_reference<Stream>::type 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_(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, std::size_t read_buffer_size,
  58. std::size_t write_buffer_size)
  59. : inner_stream_impl_(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() ASIO_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
  116. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  117. ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  118. void (asio::error_code, std::size_t))
  119. async_flush(
  120. ASIO_MOVE_ARG(WriteHandler) handler
  121. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  122. {
  123. return stream_impl_.next_layer().async_flush(
  124. ASIO_MOVE_CAST(WriteHandler)(handler));
  125. }
  126. /// Write the given data to the stream. Returns the number of bytes written.
  127. /// Throws an exception on failure.
  128. template <typename ConstBufferSequence>
  129. std::size_t write_some(const ConstBufferSequence& buffers)
  130. {
  131. return stream_impl_.write_some(buffers);
  132. }
  133. /// Write the given data to the stream. Returns the number of bytes written,
  134. /// or 0 if an error occurred.
  135. template <typename ConstBufferSequence>
  136. std::size_t write_some(const ConstBufferSequence& buffers,
  137. asio::error_code& ec)
  138. {
  139. return stream_impl_.write_some(buffers, ec);
  140. }
  141. /// Start an asynchronous write. The data being written must be valid for the
  142. /// lifetime of the asynchronous operation.
  143. /**
  144. * @par Completion Signature
  145. * @code void(asio::error_code, std::size_t) @endcode
  146. */
  147. template <typename ConstBufferSequence,
  148. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  149. std::size_t)) WriteHandler
  150. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  151. ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  152. void (asio::error_code, std::size_t))
  153. async_write_some(const ConstBufferSequence& buffers,
  154. ASIO_MOVE_ARG(WriteHandler) handler
  155. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  156. {
  157. return stream_impl_.async_write_some(buffers,
  158. ASIO_MOVE_CAST(WriteHandler)(handler));
  159. }
  160. /// Fill the buffer with some data. Returns the number of bytes placed in the
  161. /// buffer as a result of the operation. Throws an exception on failure.
  162. std::size_t fill()
  163. {
  164. return stream_impl_.fill();
  165. }
  166. /// Fill the buffer with some data. Returns the number of bytes placed in the
  167. /// buffer as a result of the operation, or 0 if an error occurred.
  168. std::size_t fill(asio::error_code& ec)
  169. {
  170. return stream_impl_.fill(ec);
  171. }
  172. /// Start an asynchronous fill.
  173. /**
  174. * @par Completion Signature
  175. * @code void(asio::error_code, std::size_t) @endcode
  176. */
  177. template <
  178. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  179. std::size_t)) ReadHandler
  180. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  181. ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  182. void (asio::error_code, std::size_t))
  183. async_fill(
  184. ASIO_MOVE_ARG(ReadHandler) handler
  185. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  186. {
  187. return stream_impl_.async_fill(ASIO_MOVE_CAST(ReadHandler)(handler));
  188. }
  189. /// Read some data from the stream. Returns the number of bytes read. Throws
  190. /// an exception on failure.
  191. template <typename MutableBufferSequence>
  192. std::size_t read_some(const MutableBufferSequence& buffers)
  193. {
  194. return stream_impl_.read_some(buffers);
  195. }
  196. /// Read some data from the stream. Returns the number of bytes read or 0 if
  197. /// an error occurred.
  198. template <typename MutableBufferSequence>
  199. std::size_t read_some(const MutableBufferSequence& buffers,
  200. asio::error_code& ec)
  201. {
  202. return stream_impl_.read_some(buffers, ec);
  203. }
  204. /// Start an asynchronous read. The buffer into which the data will be read
  205. /// must be valid for the lifetime of the asynchronous operation.
  206. /**
  207. * @par Completion Signature
  208. * @code void(asio::error_code, std::size_t) @endcode
  209. */
  210. template <typename MutableBufferSequence,
  211. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  212. std::size_t)) ReadHandler
  213. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  214. ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  215. void (asio::error_code, std::size_t))
  216. async_read_some(const MutableBufferSequence& buffers,
  217. ASIO_MOVE_ARG(ReadHandler) handler
  218. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  219. {
  220. return stream_impl_.async_read_some(buffers,
  221. ASIO_MOVE_CAST(ReadHandler)(handler));
  222. }
  223. /// Peek at the incoming data on the stream. Returns the number of bytes read.
  224. /// Throws an exception on failure.
  225. template <typename MutableBufferSequence>
  226. std::size_t peek(const MutableBufferSequence& buffers)
  227. {
  228. return stream_impl_.peek(buffers);
  229. }
  230. /// Peek at the incoming data on the stream. Returns the number of bytes read,
  231. /// or 0 if an error occurred.
  232. template <typename MutableBufferSequence>
  233. std::size_t peek(const MutableBufferSequence& buffers,
  234. asio::error_code& ec)
  235. {
  236. return stream_impl_.peek(buffers, ec);
  237. }
  238. /// Determine the amount of data that may be read without blocking.
  239. std::size_t in_avail()
  240. {
  241. return stream_impl_.in_avail();
  242. }
  243. /// Determine the amount of data that may be read without blocking.
  244. std::size_t in_avail(asio::error_code& ec)
  245. {
  246. return stream_impl_.in_avail(ec);
  247. }
  248. private:
  249. // The buffered write stream.
  250. typedef buffered_write_stream<Stream> write_stream_type;
  251. write_stream_type inner_stream_impl_;
  252. // The buffered read stream.
  253. typedef buffered_read_stream<write_stream_type&> read_stream_type;
  254. read_stream_type stream_impl_;
  255. };
  256. } // namespace asio
  257. #include "asio/detail/pop_options.hpp"
  258. #endif // ASIO_BUFFERED_STREAM_HPP