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.

263 lines
7.9KB

  1. //
  2. // buffered_read_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_READ_STREAM_HPP
  11. #define ASIO_BUFFERED_READ_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_fwd.hpp"
  19. #include "asio/buffer.hpp"
  20. #include "asio/detail/bind_handler.hpp"
  21. #include "asio/detail/buffer_resize_guard.hpp"
  22. #include "asio/detail/buffered_stream_storage.hpp"
  23. #include "asio/detail/noncopyable.hpp"
  24. #include "asio/detail/type_traits.hpp"
  25. #include "asio/error.hpp"
  26. #include "asio/io_context.hpp"
  27. #include "asio/detail/push_options.hpp"
  28. namespace asio {
  29. /// Adds buffering to the read-related operations of a stream.
  30. /**
  31. * The buffered_read_stream class template can be used to add buffering to the
  32. * synchronous and asynchronous read operations of a stream.
  33. *
  34. * @par Thread Safety
  35. * @e Distinct @e objects: Safe.@n
  36. * @e Shared @e objects: Unsafe.
  37. *
  38. * @par Concepts:
  39. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  40. */
  41. template <typename Stream>
  42. class buffered_read_stream
  43. : private noncopyable
  44. {
  45. public:
  46. /// The type of the next layer.
  47. typedef typename remove_reference<Stream>::type next_layer_type;
  48. /// The type of the lowest layer.
  49. typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
  50. /// The type of the executor associated with the object.
  51. typedef typename lowest_layer_type::executor_type executor_type;
  52. #if defined(GENERATING_DOCUMENTATION)
  53. /// The default buffer size.
  54. static const std::size_t default_buffer_size = implementation_defined;
  55. #else
  56. ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
  57. #endif
  58. /// Construct, passing the specified argument to initialise the next layer.
  59. template <typename Arg>
  60. explicit buffered_read_stream(Arg& a)
  61. : next_layer_(a),
  62. storage_(default_buffer_size)
  63. {
  64. }
  65. /// Construct, passing the specified argument to initialise the next layer.
  66. template <typename Arg>
  67. buffered_read_stream(Arg& a, std::size_t buffer_size)
  68. : next_layer_(a),
  69. storage_(buffer_size)
  70. {
  71. }
  72. /// Get a reference to the next layer.
  73. next_layer_type& next_layer()
  74. {
  75. return next_layer_;
  76. }
  77. /// Get a reference to the lowest layer.
  78. lowest_layer_type& lowest_layer()
  79. {
  80. return next_layer_.lowest_layer();
  81. }
  82. /// Get a const reference to the lowest layer.
  83. const lowest_layer_type& lowest_layer() const
  84. {
  85. return next_layer_.lowest_layer();
  86. }
  87. /// Get the executor associated with the object.
  88. executor_type get_executor() ASIO_NOEXCEPT
  89. {
  90. return next_layer_.lowest_layer().get_executor();
  91. }
  92. #if !defined(ASIO_NO_DEPRECATED)
  93. /// (Deprecated: Use get_executor().) Get the io_context associated with the
  94. /// object.
  95. asio::io_context& get_io_context()
  96. {
  97. return next_layer_.get_io_context();
  98. }
  99. /// (Deprecated: Use get_executor().) Get the io_context associated with the
  100. /// object.
  101. asio::io_context& get_io_service()
  102. {
  103. return next_layer_.get_io_service();
  104. }
  105. #endif // !defined(ASIO_NO_DEPRECATED)
  106. /// Close the stream.
  107. void close()
  108. {
  109. next_layer_.close();
  110. }
  111. /// Close the stream.
  112. asio::error_code close(asio::error_code& ec)
  113. {
  114. return next_layer_.close(ec);
  115. }
  116. /// Write the given data to the stream. Returns the number of bytes written.
  117. /// Throws an exception on failure.
  118. template <typename ConstBufferSequence>
  119. std::size_t write_some(const ConstBufferSequence& buffers)
  120. {
  121. return next_layer_.write_some(buffers);
  122. }
  123. /// Write the given data to the stream. Returns the number of bytes written,
  124. /// or 0 if an error occurred.
  125. template <typename ConstBufferSequence>
  126. std::size_t write_some(const ConstBufferSequence& buffers,
  127. asio::error_code& ec)
  128. {
  129. return next_layer_.write_some(buffers, ec);
  130. }
  131. /// Start an asynchronous write. The data being written must be valid for the
  132. /// lifetime of the asynchronous operation.
  133. template <typename ConstBufferSequence, typename WriteHandler>
  134. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  135. void (asio::error_code, std::size_t))
  136. async_write_some(const ConstBufferSequence& buffers,
  137. ASIO_MOVE_ARG(WriteHandler) handler)
  138. {
  139. async_completion<WriteHandler,
  140. void (asio::error_code, std::size_t)> init(handler);
  141. next_layer_.async_write_some(buffers,
  142. ASIO_MOVE_CAST(ASIO_HANDLER_TYPE(WriteHandler,
  143. void (asio::error_code, std::size_t)))(init.handler));
  144. return init.result.get();
  145. }
  146. /// Fill the buffer with some data. Returns the number of bytes placed in the
  147. /// buffer as a result of the operation. Throws an exception on failure.
  148. std::size_t fill();
  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. /// Start an asynchronous fill.
  153. template <typename ReadHandler>
  154. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  155. void (asio::error_code, std::size_t))
  156. async_fill(ASIO_MOVE_ARG(ReadHandler) handler);
  157. /// Read some data from the stream. Returns the number of bytes read. Throws
  158. /// an exception on failure.
  159. template <typename MutableBufferSequence>
  160. std::size_t read_some(const MutableBufferSequence& buffers);
  161. /// Read some data from the stream. Returns the number of bytes read or 0 if
  162. /// an error occurred.
  163. template <typename MutableBufferSequence>
  164. std::size_t read_some(const MutableBufferSequence& buffers,
  165. asio::error_code& ec);
  166. /// Start an asynchronous read. The buffer into which the data will be read
  167. /// must be valid for the lifetime of the asynchronous operation.
  168. template <typename MutableBufferSequence, typename ReadHandler>
  169. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  170. void (asio::error_code, std::size_t))
  171. async_read_some(const MutableBufferSequence& buffers,
  172. ASIO_MOVE_ARG(ReadHandler) handler);
  173. /// Peek at the incoming data on the stream. Returns the number of bytes read.
  174. /// Throws an exception on failure.
  175. template <typename MutableBufferSequence>
  176. std::size_t peek(const MutableBufferSequence& buffers);
  177. /// Peek at the incoming data on the stream. Returns the number of bytes read,
  178. /// or 0 if an error occurred.
  179. template <typename MutableBufferSequence>
  180. std::size_t peek(const MutableBufferSequence& buffers,
  181. asio::error_code& ec);
  182. /// Determine the amount of data that may be read without blocking.
  183. std::size_t in_avail()
  184. {
  185. return storage_.size();
  186. }
  187. /// Determine the amount of data that may be read without blocking.
  188. std::size_t in_avail(asio::error_code& ec)
  189. {
  190. ec = asio::error_code();
  191. return storage_.size();
  192. }
  193. private:
  194. /// Copy data out of the internal buffer to the specified target buffer.
  195. /// Returns the number of bytes copied.
  196. template <typename MutableBufferSequence>
  197. std::size_t copy(const MutableBufferSequence& buffers)
  198. {
  199. std::size_t bytes_copied = asio::buffer_copy(
  200. buffers, storage_.data(), storage_.size());
  201. storage_.consume(bytes_copied);
  202. return bytes_copied;
  203. }
  204. /// Copy data from the internal buffer to the specified target buffer, without
  205. /// removing the data from the internal buffer. Returns the number of bytes
  206. /// copied.
  207. template <typename MutableBufferSequence>
  208. std::size_t peek_copy(const MutableBufferSequence& buffers)
  209. {
  210. return asio::buffer_copy(buffers, storage_.data(), storage_.size());
  211. }
  212. /// The next layer.
  213. Stream next_layer_;
  214. // The data in the buffer.
  215. detail::buffered_stream_storage storage_;
  216. };
  217. } // namespace asio
  218. #include "asio/detail/pop_options.hpp"
  219. #include "asio/impl/buffered_read_stream.hpp"
  220. #endif // ASIO_BUFFERED_READ_STREAM_HPP