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.

buffered_stream.hpp 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // buffered_stream.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. template <typename WriteHandler>
  110. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  111. void (asio::error_code, std::size_t))
  112. async_flush(ASIO_MOVE_ARG(WriteHandler) handler)
  113. {
  114. return stream_impl_.next_layer().async_flush(
  115. ASIO_MOVE_CAST(WriteHandler)(handler));
  116. }
  117. /// Write the given data to the stream. Returns the number of bytes written.
  118. /// Throws an exception on failure.
  119. template <typename ConstBufferSequence>
  120. std::size_t write_some(const ConstBufferSequence& buffers)
  121. {
  122. return stream_impl_.write_some(buffers);
  123. }
  124. /// Write the given data to the stream. Returns the number of bytes written,
  125. /// or 0 if an error occurred.
  126. template <typename ConstBufferSequence>
  127. std::size_t write_some(const ConstBufferSequence& buffers,
  128. asio::error_code& ec)
  129. {
  130. return stream_impl_.write_some(buffers, ec);
  131. }
  132. /// Start an asynchronous write. The data being written must be valid for the
  133. /// lifetime of the asynchronous operation.
  134. template <typename ConstBufferSequence, typename WriteHandler>
  135. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  136. void (asio::error_code, std::size_t))
  137. async_write_some(const ConstBufferSequence& buffers,
  138. ASIO_MOVE_ARG(WriteHandler) handler)
  139. {
  140. return stream_impl_.async_write_some(buffers,
  141. ASIO_MOVE_CAST(WriteHandler)(handler));
  142. }
  143. /// Fill the buffer with some data. Returns the number of bytes placed in the
  144. /// buffer as a result of the operation. Throws an exception on failure.
  145. std::size_t fill()
  146. {
  147. return stream_impl_.fill();
  148. }
  149. /// Fill the buffer with some data. Returns the number of bytes placed in the
  150. /// buffer as a result of the operation, or 0 if an error occurred.
  151. std::size_t fill(asio::error_code& ec)
  152. {
  153. return stream_impl_.fill(ec);
  154. }
  155. /// Start an asynchronous fill.
  156. template <typename ReadHandler>
  157. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  158. void (asio::error_code, std::size_t))
  159. async_fill(ASIO_MOVE_ARG(ReadHandler) handler)
  160. {
  161. return stream_impl_.async_fill(ASIO_MOVE_CAST(ReadHandler)(handler));
  162. }
  163. /// Read some data from the stream. Returns the number of bytes read. Throws
  164. /// an exception on failure.
  165. template <typename MutableBufferSequence>
  166. std::size_t read_some(const MutableBufferSequence& buffers)
  167. {
  168. return stream_impl_.read_some(buffers);
  169. }
  170. /// Read some data from the stream. Returns the number of bytes read or 0 if
  171. /// an error occurred.
  172. template <typename MutableBufferSequence>
  173. std::size_t read_some(const MutableBufferSequence& buffers,
  174. asio::error_code& ec)
  175. {
  176. return stream_impl_.read_some(buffers, ec);
  177. }
  178. /// Start an asynchronous read. The buffer into which the data will be read
  179. /// must be valid for the lifetime of the asynchronous operation.
  180. template <typename MutableBufferSequence, typename ReadHandler>
  181. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  182. void (asio::error_code, std::size_t))
  183. async_read_some(const MutableBufferSequence& buffers,
  184. ASIO_MOVE_ARG(ReadHandler) handler)
  185. {
  186. return stream_impl_.async_read_some(buffers,
  187. ASIO_MOVE_CAST(ReadHandler)(handler));
  188. }
  189. /// Peek at the incoming data on the stream. Returns the number of bytes read.
  190. /// Throws an exception on failure.
  191. template <typename MutableBufferSequence>
  192. std::size_t peek(const MutableBufferSequence& buffers)
  193. {
  194. return stream_impl_.peek(buffers);
  195. }
  196. /// Peek at the incoming data on the stream. Returns the number of bytes read,
  197. /// or 0 if an error occurred.
  198. template <typename MutableBufferSequence>
  199. std::size_t peek(const MutableBufferSequence& buffers,
  200. asio::error_code& ec)
  201. {
  202. return stream_impl_.peek(buffers, ec);
  203. }
  204. /// Determine the amount of data that may be read without blocking.
  205. std::size_t in_avail()
  206. {
  207. return stream_impl_.in_avail();
  208. }
  209. /// Determine the amount of data that may be read without blocking.
  210. std::size_t in_avail(asio::error_code& ec)
  211. {
  212. return stream_impl_.in_avail(ec);
  213. }
  214. private:
  215. // The buffered write stream.
  216. typedef buffered_write_stream<Stream> write_stream_type;
  217. write_stream_type inner_stream_impl_;
  218. // The buffered read stream.
  219. typedef buffered_read_stream<write_stream_type&> read_stream_type;
  220. read_stream_type stream_impl_;
  221. };
  222. } // namespace asio
  223. #include "asio/detail/pop_options.hpp"
  224. #endif // ASIO_BUFFERED_STREAM_HPP