Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
8.1KB

  1. //
  2. // buffered_stream.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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/io_context.hpp"
  24. #include "asio/detail/push_options.hpp"
  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(Arg& a)
  52. : inner_stream_impl_(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(Arg& a, std::size_t read_buffer_size,
  59. std::size_t write_buffer_size)
  60. : inner_stream_impl_(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() ASIO_NOEXCEPT
  81. {
  82. return stream_impl_.lowest_layer().get_executor();
  83. }
  84. #if !defined(ASIO_NO_DEPRECATED)
  85. /// (Deprecated: Use get_executor().) Get the io_context associated with the
  86. /// object.
  87. asio::io_context& get_io_context()
  88. {
  89. return stream_impl_.get_io_context();
  90. }
  91. /// (Deprecated: Use get_executor().) Get the io_context associated with the
  92. /// object.
  93. asio::io_context& get_io_service()
  94. {
  95. return stream_impl_.get_io_service();
  96. }
  97. #endif // !defined(ASIO_NO_DEPRECATED)
  98. /// Close the stream.
  99. void close()
  100. {
  101. stream_impl_.close();
  102. }
  103. /// Close the stream.
  104. asio::error_code close(asio::error_code& ec)
  105. {
  106. return stream_impl_.close(ec);
  107. }
  108. /// Flush all data from the buffer to the next layer. Returns the number of
  109. /// bytes written to the next layer on the last write operation. Throws an
  110. /// exception on failure.
  111. std::size_t flush()
  112. {
  113. return stream_impl_.next_layer().flush();
  114. }
  115. /// Flush all data from the buffer to the next layer. Returns the number of
  116. /// bytes written to the next layer on the last write operation, or 0 if an
  117. /// error occurred.
  118. std::size_t flush(asio::error_code& ec)
  119. {
  120. return stream_impl_.next_layer().flush(ec);
  121. }
  122. /// Start an asynchronous flush.
  123. template <typename WriteHandler>
  124. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  125. void (asio::error_code, std::size_t))
  126. async_flush(ASIO_MOVE_ARG(WriteHandler) handler)
  127. {
  128. return stream_impl_.next_layer().async_flush(
  129. ASIO_MOVE_CAST(WriteHandler)(handler));
  130. }
  131. /// Write the given data to the stream. Returns the number of bytes written.
  132. /// Throws an exception on failure.
  133. template <typename ConstBufferSequence>
  134. std::size_t write_some(const ConstBufferSequence& buffers)
  135. {
  136. return stream_impl_.write_some(buffers);
  137. }
  138. /// Write the given data to the stream. Returns the number of bytes written,
  139. /// or 0 if an error occurred.
  140. template <typename ConstBufferSequence>
  141. std::size_t write_some(const ConstBufferSequence& buffers,
  142. asio::error_code& ec)
  143. {
  144. return stream_impl_.write_some(buffers, ec);
  145. }
  146. /// Start an asynchronous write. The data being written must be valid for the
  147. /// lifetime of the asynchronous operation.
  148. template <typename ConstBufferSequence, typename WriteHandler>
  149. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  150. void (asio::error_code, std::size_t))
  151. async_write_some(const ConstBufferSequence& buffers,
  152. ASIO_MOVE_ARG(WriteHandler) handler)
  153. {
  154. return stream_impl_.async_write_some(buffers,
  155. ASIO_MOVE_CAST(WriteHandler)(handler));
  156. }
  157. /// Fill the buffer with some data. Returns the number of bytes placed in the
  158. /// buffer as a result of the operation. Throws an exception on failure.
  159. std::size_t fill()
  160. {
  161. return stream_impl_.fill();
  162. }
  163. /// Fill the buffer with some data. Returns the number of bytes placed in the
  164. /// buffer as a result of the operation, or 0 if an error occurred.
  165. std::size_t fill(asio::error_code& ec)
  166. {
  167. return stream_impl_.fill(ec);
  168. }
  169. /// Start an asynchronous fill.
  170. template <typename ReadHandler>
  171. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  172. void (asio::error_code, std::size_t))
  173. async_fill(ASIO_MOVE_ARG(ReadHandler) handler)
  174. {
  175. return stream_impl_.async_fill(ASIO_MOVE_CAST(ReadHandler)(handler));
  176. }
  177. /// Read some data from the stream. Returns the number of bytes read. Throws
  178. /// an exception on failure.
  179. template <typename MutableBufferSequence>
  180. std::size_t read_some(const MutableBufferSequence& buffers)
  181. {
  182. return stream_impl_.read_some(buffers);
  183. }
  184. /// Read some data from the stream. Returns the number of bytes read or 0 if
  185. /// an error occurred.
  186. template <typename MutableBufferSequence>
  187. std::size_t read_some(const MutableBufferSequence& buffers,
  188. asio::error_code& ec)
  189. {
  190. return stream_impl_.read_some(buffers, ec);
  191. }
  192. /// Start an asynchronous read. The buffer into which the data will be read
  193. /// must be valid for the lifetime of the asynchronous operation.
  194. template <typename MutableBufferSequence, typename ReadHandler>
  195. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  196. void (asio::error_code, std::size_t))
  197. async_read_some(const MutableBufferSequence& buffers,
  198. ASIO_MOVE_ARG(ReadHandler) handler)
  199. {
  200. return stream_impl_.async_read_some(buffers,
  201. ASIO_MOVE_CAST(ReadHandler)(handler));
  202. }
  203. /// Peek at the incoming data on the stream. Returns the number of bytes read.
  204. /// Throws an exception on failure.
  205. template <typename MutableBufferSequence>
  206. std::size_t peek(const MutableBufferSequence& buffers)
  207. {
  208. return stream_impl_.peek(buffers);
  209. }
  210. /// Peek at the incoming data on the stream. Returns the number of bytes read,
  211. /// or 0 if an error occurred.
  212. template <typename MutableBufferSequence>
  213. std::size_t peek(const MutableBufferSequence& buffers,
  214. asio::error_code& ec)
  215. {
  216. return stream_impl_.peek(buffers, ec);
  217. }
  218. /// Determine the amount of data that may be read without blocking.
  219. std::size_t in_avail()
  220. {
  221. return stream_impl_.in_avail();
  222. }
  223. /// Determine the amount of data that may be read without blocking.
  224. std::size_t in_avail(asio::error_code& ec)
  225. {
  226. return stream_impl_.in_avail(ec);
  227. }
  228. private:
  229. // The buffered write stream.
  230. typedef buffered_write_stream<Stream> write_stream_type;
  231. write_stream_type inner_stream_impl_;
  232. // The buffered read stream.
  233. typedef buffered_read_stream<write_stream_type&> read_stream_type;
  234. read_stream_type stream_impl_;
  235. };
  236. } // namespace asio
  237. #include "asio/detail/pop_options.hpp"
  238. #endif // ASIO_BUFFERED_STREAM_HPP