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.

449 lines
13KB

  1. //
  2. // impl/buffered_read_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_IMPL_BUFFERED_READ_STREAM_HPP
  11. #define ASIO_IMPL_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/associated_allocator.hpp"
  16. #include "asio/associated_executor.hpp"
  17. #include "asio/detail/handler_alloc_helpers.hpp"
  18. #include "asio/detail/handler_cont_helpers.hpp"
  19. #include "asio/detail/handler_invoke_helpers.hpp"
  20. #include "asio/detail/handler_type_requirements.hpp"
  21. #include "asio/detail/non_const_lvalue.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. template <typename Stream>
  25. std::size_t buffered_read_stream<Stream>::fill()
  26. {
  27. detail::buffer_resize_guard<detail::buffered_stream_storage>
  28. resize_guard(storage_);
  29. std::size_t previous_size = storage_.size();
  30. storage_.resize(storage_.capacity());
  31. storage_.resize(previous_size + next_layer_.read_some(buffer(
  32. storage_.data() + previous_size,
  33. storage_.size() - previous_size)));
  34. resize_guard.commit();
  35. return storage_.size() - previous_size;
  36. }
  37. template <typename Stream>
  38. std::size_t buffered_read_stream<Stream>::fill(asio::error_code& ec)
  39. {
  40. detail::buffer_resize_guard<detail::buffered_stream_storage>
  41. resize_guard(storage_);
  42. std::size_t previous_size = storage_.size();
  43. storage_.resize(storage_.capacity());
  44. storage_.resize(previous_size + next_layer_.read_some(buffer(
  45. storage_.data() + previous_size,
  46. storage_.size() - previous_size),
  47. ec));
  48. resize_guard.commit();
  49. return storage_.size() - previous_size;
  50. }
  51. namespace detail
  52. {
  53. template <typename ReadHandler>
  54. class buffered_fill_handler
  55. {
  56. public:
  57. buffered_fill_handler(detail::buffered_stream_storage& storage,
  58. std::size_t previous_size, ReadHandler& handler)
  59. : storage_(storage),
  60. previous_size_(previous_size),
  61. handler_(ASIO_MOVE_CAST(ReadHandler)(handler))
  62. {
  63. }
  64. #if defined(ASIO_HAS_MOVE)
  65. buffered_fill_handler(const buffered_fill_handler& other)
  66. : storage_(other.storage_),
  67. previous_size_(other.previous_size_),
  68. handler_(other.handler_)
  69. {
  70. }
  71. buffered_fill_handler(buffered_fill_handler&& other)
  72. : storage_(other.storage_),
  73. previous_size_(other.previous_size_),
  74. handler_(ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  75. {
  76. }
  77. #endif // defined(ASIO_HAS_MOVE)
  78. void operator()(const asio::error_code& ec,
  79. const std::size_t bytes_transferred)
  80. {
  81. storage_.resize(previous_size_ + bytes_transferred);
  82. handler_(ec, bytes_transferred);
  83. }
  84. //private:
  85. detail::buffered_stream_storage& storage_;
  86. std::size_t previous_size_;
  87. ReadHandler handler_;
  88. };
  89. template <typename ReadHandler>
  90. inline void* asio_handler_allocate(std::size_t size,
  91. buffered_fill_handler<ReadHandler>* this_handler)
  92. {
  93. return asio_handler_alloc_helpers::allocate(
  94. size, this_handler->handler_);
  95. }
  96. template <typename ReadHandler>
  97. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  98. buffered_fill_handler<ReadHandler>* this_handler)
  99. {
  100. asio_handler_alloc_helpers::deallocate(
  101. pointer, size, this_handler->handler_);
  102. }
  103. template <typename ReadHandler>
  104. inline bool asio_handler_is_continuation(
  105. buffered_fill_handler<ReadHandler>* this_handler)
  106. {
  107. return asio_handler_cont_helpers::is_continuation(
  108. this_handler->handler_);
  109. }
  110. template <typename Function, typename ReadHandler>
  111. inline void asio_handler_invoke(Function& function,
  112. buffered_fill_handler<ReadHandler>* this_handler)
  113. {
  114. asio_handler_invoke_helpers::invoke(
  115. function, this_handler->handler_);
  116. }
  117. template <typename Function, typename ReadHandler>
  118. inline void asio_handler_invoke(const Function& function,
  119. buffered_fill_handler<ReadHandler>* this_handler)
  120. {
  121. asio_handler_invoke_helpers::invoke(
  122. function, this_handler->handler_);
  123. }
  124. struct initiate_async_buffered_fill
  125. {
  126. template <typename ReadHandler, typename Stream>
  127. void operator()(ASIO_MOVE_ARG(ReadHandler) handler,
  128. buffered_stream_storage* storage, Stream* next_layer) const
  129. {
  130. // If you get an error on the following line it means that your handler
  131. // does not meet the documented type requirements for a ReadHandler.
  132. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  133. non_const_lvalue<ReadHandler> handler2(handler);
  134. std::size_t previous_size = storage->size();
  135. storage->resize(storage->capacity());
  136. next_layer->async_read_some(
  137. buffer(
  138. storage->data() + previous_size,
  139. storage->size() - previous_size),
  140. buffered_fill_handler<typename decay<ReadHandler>::type>(
  141. *storage, previous_size, handler2.value));
  142. }
  143. };
  144. } // namespace detail
  145. #if !defined(GENERATING_DOCUMENTATION)
  146. template <typename ReadHandler, typename Allocator>
  147. struct associated_allocator<
  148. detail::buffered_fill_handler<ReadHandler>, Allocator>
  149. {
  150. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  151. static type get(const detail::buffered_fill_handler<ReadHandler>& h,
  152. const Allocator& a = Allocator()) ASIO_NOEXCEPT
  153. {
  154. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  155. }
  156. };
  157. template <typename ReadHandler, typename Executor>
  158. struct associated_executor<
  159. detail::buffered_fill_handler<ReadHandler>, Executor>
  160. {
  161. typedef typename associated_executor<ReadHandler, Executor>::type type;
  162. static type get(const detail::buffered_fill_handler<ReadHandler>& h,
  163. const Executor& ex = Executor()) ASIO_NOEXCEPT
  164. {
  165. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  166. }
  167. };
  168. #endif // !defined(GENERATING_DOCUMENTATION)
  169. template <typename Stream>
  170. template <typename ReadHandler>
  171. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  172. void (asio::error_code, std::size_t))
  173. buffered_read_stream<Stream>::async_fill(
  174. ASIO_MOVE_ARG(ReadHandler) handler)
  175. {
  176. return async_initiate<ReadHandler,
  177. void (asio::error_code, std::size_t)>(
  178. detail::initiate_async_buffered_fill(), handler, &storage_, &next_layer_);
  179. }
  180. template <typename Stream>
  181. template <typename MutableBufferSequence>
  182. std::size_t buffered_read_stream<Stream>::read_some(
  183. const MutableBufferSequence& buffers)
  184. {
  185. using asio::buffer_size;
  186. if (buffer_size(buffers) == 0)
  187. return 0;
  188. if (storage_.empty())
  189. this->fill();
  190. return this->copy(buffers);
  191. }
  192. template <typename Stream>
  193. template <typename MutableBufferSequence>
  194. std::size_t buffered_read_stream<Stream>::read_some(
  195. const MutableBufferSequence& buffers, asio::error_code& ec)
  196. {
  197. ec = asio::error_code();
  198. using asio::buffer_size;
  199. if (buffer_size(buffers) == 0)
  200. return 0;
  201. if (storage_.empty() && !this->fill(ec))
  202. return 0;
  203. return this->copy(buffers);
  204. }
  205. namespace detail
  206. {
  207. template <typename MutableBufferSequence, typename ReadHandler>
  208. class buffered_read_some_handler
  209. {
  210. public:
  211. buffered_read_some_handler(detail::buffered_stream_storage& storage,
  212. const MutableBufferSequence& buffers, ReadHandler& handler)
  213. : storage_(storage),
  214. buffers_(buffers),
  215. handler_(ASIO_MOVE_CAST(ReadHandler)(handler))
  216. {
  217. }
  218. #if defined(ASIO_HAS_MOVE)
  219. buffered_read_some_handler(const buffered_read_some_handler& other)
  220. : storage_(other.storage_),
  221. buffers_(other.buffers_),
  222. handler_(other.handler_)
  223. {
  224. }
  225. buffered_read_some_handler(buffered_read_some_handler&& other)
  226. : storage_(other.storage_),
  227. buffers_(other.buffers_),
  228. handler_(ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  229. {
  230. }
  231. #endif // defined(ASIO_HAS_MOVE)
  232. void operator()(const asio::error_code& ec, std::size_t)
  233. {
  234. if (ec || storage_.empty())
  235. {
  236. const std::size_t length = 0;
  237. handler_(ec, length);
  238. }
  239. else
  240. {
  241. const std::size_t bytes_copied = asio::buffer_copy(
  242. buffers_, storage_.data(), storage_.size());
  243. storage_.consume(bytes_copied);
  244. handler_(ec, bytes_copied);
  245. }
  246. }
  247. //private:
  248. detail::buffered_stream_storage& storage_;
  249. MutableBufferSequence buffers_;
  250. ReadHandler handler_;
  251. };
  252. template <typename MutableBufferSequence, typename ReadHandler>
  253. inline void* asio_handler_allocate(std::size_t size,
  254. buffered_read_some_handler<
  255. MutableBufferSequence, ReadHandler>* this_handler)
  256. {
  257. return asio_handler_alloc_helpers::allocate(
  258. size, this_handler->handler_);
  259. }
  260. template <typename MutableBufferSequence, typename ReadHandler>
  261. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  262. buffered_read_some_handler<
  263. MutableBufferSequence, ReadHandler>* this_handler)
  264. {
  265. asio_handler_alloc_helpers::deallocate(
  266. pointer, size, this_handler->handler_);
  267. }
  268. template <typename MutableBufferSequence, typename ReadHandler>
  269. inline bool asio_handler_is_continuation(
  270. buffered_read_some_handler<
  271. MutableBufferSequence, ReadHandler>* this_handler)
  272. {
  273. return asio_handler_cont_helpers::is_continuation(
  274. this_handler->handler_);
  275. }
  276. template <typename Function, typename MutableBufferSequence,
  277. typename ReadHandler>
  278. inline void asio_handler_invoke(Function& function,
  279. buffered_read_some_handler<
  280. MutableBufferSequence, ReadHandler>* this_handler)
  281. {
  282. asio_handler_invoke_helpers::invoke(
  283. function, this_handler->handler_);
  284. }
  285. template <typename Function, typename MutableBufferSequence,
  286. typename ReadHandler>
  287. inline void asio_handler_invoke(const Function& function,
  288. buffered_read_some_handler<
  289. MutableBufferSequence, ReadHandler>* this_handler)
  290. {
  291. asio_handler_invoke_helpers::invoke(
  292. function, this_handler->handler_);
  293. }
  294. struct initiate_async_buffered_read_some
  295. {
  296. template <typename ReadHandler, typename Stream,
  297. typename MutableBufferSequence>
  298. void operator()(ASIO_MOVE_ARG(ReadHandler) handler,
  299. buffered_stream_storage* storage, Stream* next_layer,
  300. const MutableBufferSequence& buffers) const
  301. {
  302. // If you get an error on the following line it means that your handler
  303. // does not meet the documented type requirements for a ReadHandler.
  304. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  305. using asio::buffer_size;
  306. non_const_lvalue<ReadHandler> handler2(handler);
  307. if (buffer_size(buffers) == 0 || !storage->empty())
  308. {
  309. next_layer->async_read_some(ASIO_MUTABLE_BUFFER(0, 0),
  310. buffered_read_some_handler<MutableBufferSequence,
  311. typename decay<ReadHandler>::type>(
  312. *storage, buffers, handler2.value));
  313. }
  314. else
  315. {
  316. initiate_async_buffered_fill()(
  317. buffered_read_some_handler<MutableBufferSequence,
  318. typename decay<ReadHandler>::type>(
  319. *storage, buffers, handler2.value),
  320. storage, next_layer);
  321. }
  322. }
  323. };
  324. } // namespace detail
  325. #if !defined(GENERATING_DOCUMENTATION)
  326. template <typename MutableBufferSequence,
  327. typename ReadHandler, typename Allocator>
  328. struct associated_allocator<
  329. detail::buffered_read_some_handler<MutableBufferSequence, ReadHandler>,
  330. Allocator>
  331. {
  332. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  333. static type get(
  334. const detail::buffered_read_some_handler<
  335. MutableBufferSequence, ReadHandler>& h,
  336. const Allocator& a = Allocator()) ASIO_NOEXCEPT
  337. {
  338. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  339. }
  340. };
  341. template <typename MutableBufferSequence,
  342. typename ReadHandler, typename Executor>
  343. struct associated_executor<
  344. detail::buffered_read_some_handler<MutableBufferSequence, ReadHandler>,
  345. Executor>
  346. {
  347. typedef typename associated_executor<ReadHandler, Executor>::type type;
  348. static type get(
  349. const detail::buffered_read_some_handler<
  350. MutableBufferSequence, ReadHandler>& h,
  351. const Executor& ex = Executor()) ASIO_NOEXCEPT
  352. {
  353. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  354. }
  355. };
  356. #endif // !defined(GENERATING_DOCUMENTATION)
  357. template <typename Stream>
  358. template <typename MutableBufferSequence, typename ReadHandler>
  359. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  360. void (asio::error_code, std::size_t))
  361. buffered_read_stream<Stream>::async_read_some(
  362. const MutableBufferSequence& buffers,
  363. ASIO_MOVE_ARG(ReadHandler) handler)
  364. {
  365. return async_initiate<ReadHandler,
  366. void (asio::error_code, std::size_t)>(
  367. detail::initiate_async_buffered_read_some(),
  368. handler, &storage_, &next_layer_, buffers);
  369. }
  370. template <typename Stream>
  371. template <typename MutableBufferSequence>
  372. std::size_t buffered_read_stream<Stream>::peek(
  373. const MutableBufferSequence& buffers)
  374. {
  375. if (storage_.empty())
  376. this->fill();
  377. return this->peek_copy(buffers);
  378. }
  379. template <typename Stream>
  380. template <typename MutableBufferSequence>
  381. std::size_t buffered_read_stream<Stream>::peek(
  382. const MutableBufferSequence& buffers, asio::error_code& ec)
  383. {
  384. ec = asio::error_code();
  385. if (storage_.empty() && !this->fill(ec))
  386. return 0;
  387. return this->peek_copy(buffers);
  388. }
  389. } // namespace asio
  390. #include "asio/detail/pop_options.hpp"
  391. #endif // ASIO_IMPL_BUFFERED_READ_STREAM_HPP