stream.ipp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_TEST_IMPL_STREAM_IPP
  10. #define BOOST_BEAST_TEST_IMPL_STREAM_IPP
  11. #include <boost/beast/_experimental/test/stream.hpp>
  12. #include <boost/beast/core/bind_handler.hpp>
  13. #include <boost/beast/core/buffer_traits.hpp>
  14. #include <boost/make_shared.hpp>
  15. #include <stdexcept>
  16. #include <vector>
  17. namespace boost {
  18. namespace beast {
  19. namespace test {
  20. //------------------------------------------------------------------------------
  21. template<class Executor>
  22. void basic_stream<Executor>::initiate_read(
  23. boost::shared_ptr<detail::stream_state> const& in_,
  24. std::unique_ptr<detail::stream_read_op_base>&& op,
  25. std::size_t buf_size)
  26. {
  27. std::unique_lock<std::mutex> lock(in_->m);
  28. ++in_->nread;
  29. if(in_->op != nullptr)
  30. BOOST_THROW_EXCEPTION(
  31. std::logic_error{"in_->op != nullptr"});
  32. // test failure
  33. error_code ec;
  34. if(in_->fc && in_->fc->fail(ec))
  35. {
  36. lock.unlock();
  37. (*op)(ec);
  38. return;
  39. }
  40. // A request to read 0 bytes from a stream is a no-op.
  41. if(buf_size == 0 || buffer_bytes(in_->b.data()) > 0)
  42. {
  43. lock.unlock();
  44. (*op)(ec);
  45. return;
  46. }
  47. // deliver error
  48. if(in_->code != detail::stream_status::ok)
  49. {
  50. lock.unlock();
  51. (*op)(net::error::eof);
  52. return;
  53. }
  54. // complete when bytes available or closed
  55. in_->op = std::move(op);
  56. }
  57. //------------------------------------------------------------------------------
  58. template<class Executor>
  59. basic_stream<Executor>::
  60. ~basic_stream()
  61. {
  62. close();
  63. in_->remove();
  64. }
  65. template<class Executor>
  66. basic_stream<Executor>::
  67. basic_stream(basic_stream&& other)
  68. {
  69. auto in = detail::stream_service::make_impl(
  70. other.in_->exec, other.in_->fc);
  71. in_ = std::move(other.in_);
  72. out_ = std::move(other.out_);
  73. other.in_ = in;
  74. }
  75. template<class Executor>
  76. basic_stream<Executor>&
  77. basic_stream<Executor>::
  78. operator=(basic_stream&& other)
  79. {
  80. close();
  81. auto in = detail::stream_service::make_impl(
  82. other.in_->exec, other.in_->fc);
  83. in_->remove();
  84. in_ = std::move(other.in_);
  85. out_ = std::move(other.out_);
  86. other.in_ = in;
  87. return *this;
  88. }
  89. //------------------------------------------------------------------------------
  90. template<class Executor>
  91. basic_stream<Executor>::
  92. basic_stream(executor_type exec)
  93. : in_(detail::stream_service::make_impl(std::move(exec), nullptr))
  94. {
  95. }
  96. template<class Executor>
  97. basic_stream<Executor>::
  98. basic_stream(
  99. net::io_context& ioc,
  100. fail_count& fc)
  101. : in_(detail::stream_service::make_impl(ioc.get_executor(), &fc))
  102. {
  103. }
  104. template<class Executor>
  105. basic_stream<Executor>::
  106. basic_stream(
  107. net::io_context& ioc,
  108. string_view s)
  109. : in_(detail::stream_service::make_impl(ioc.get_executor(), nullptr))
  110. {
  111. in_->b.commit(net::buffer_copy(
  112. in_->b.prepare(s.size()),
  113. net::buffer(s.data(), s.size())));
  114. }
  115. template<class Executor>
  116. basic_stream<Executor>::
  117. basic_stream(
  118. net::io_context& ioc,
  119. fail_count& fc,
  120. string_view s)
  121. : in_(detail::stream_service::make_impl(ioc.get_executor(), &fc))
  122. {
  123. in_->b.commit(net::buffer_copy(
  124. in_->b.prepare(s.size()),
  125. net::buffer(s.data(), s.size())));
  126. }
  127. template<class Executor>
  128. void
  129. basic_stream<Executor>::
  130. connect(basic_stream& remote)
  131. {
  132. BOOST_ASSERT(! out_.lock());
  133. BOOST_ASSERT(! remote.out_.lock());
  134. std::lock(in_->m, remote.in_->m);
  135. std::lock_guard<std::mutex> guard1{in_->m, std::adopt_lock};
  136. std::lock_guard<std::mutex> guard2{remote.in_->m, std::adopt_lock};
  137. out_ = remote.in_;
  138. remote.out_ = in_;
  139. in_->code = detail::stream_status::ok;
  140. remote.in_->code = detail::stream_status::ok;
  141. }
  142. template<class Executor>
  143. string_view
  144. basic_stream<Executor>::
  145. str() const
  146. {
  147. auto const bs = in_->b.data();
  148. if(buffer_bytes(bs) == 0)
  149. return {};
  150. net::const_buffer const b = *net::buffer_sequence_begin(bs);
  151. return {static_cast<char const*>(b.data()), b.size()};
  152. }
  153. template<class Executor>
  154. void
  155. basic_stream<Executor>::
  156. append(string_view s)
  157. {
  158. std::lock_guard<std::mutex> lock{in_->m};
  159. in_->b.commit(net::buffer_copy(
  160. in_->b.prepare(s.size()),
  161. net::buffer(s.data(), s.size())));
  162. }
  163. template<class Executor>
  164. void
  165. basic_stream<Executor>::
  166. clear()
  167. {
  168. std::lock_guard<std::mutex> lock{in_->m};
  169. in_->b.consume(in_->b.size());
  170. }
  171. template<class Executor>
  172. void
  173. basic_stream<Executor>::
  174. close()
  175. {
  176. in_->cancel_read();
  177. // disconnect
  178. {
  179. auto out = out_.lock();
  180. out_.reset();
  181. // notify peer
  182. if(out)
  183. {
  184. std::lock_guard<std::mutex> lock(out->m);
  185. if(out->code == detail::stream_status::ok)
  186. {
  187. out->code = detail::stream_status::eof;
  188. out->notify_read();
  189. }
  190. }
  191. }
  192. }
  193. template<class Executor>
  194. void
  195. basic_stream<Executor>::
  196. close_remote()
  197. {
  198. std::lock_guard<std::mutex> lock{in_->m};
  199. if(in_->code == detail::stream_status::ok)
  200. {
  201. in_->code = detail::stream_status::eof;
  202. in_->notify_read();
  203. }
  204. }
  205. template<class Executor>
  206. void
  207. teardown(
  208. role_type,
  209. basic_stream<Executor>& s,
  210. boost::system::error_code& ec)
  211. {
  212. if( s.in_->fc &&
  213. s.in_->fc->fail(ec))
  214. return;
  215. s.close();
  216. if( s.in_->fc &&
  217. s.in_->fc->fail(ec))
  218. {
  219. BOOST_BEAST_ASSIGN_EC(ec, net::error::eof);
  220. }
  221. else
  222. ec = {};
  223. }
  224. //------------------------------------------------------------------------------
  225. template<class Executor>
  226. basic_stream<Executor>
  227. connect(basic_stream<Executor>& to)
  228. {
  229. basic_stream<Executor> from(to.get_executor());
  230. from.connect(to);
  231. return from;
  232. }
  233. template<class Executor>
  234. void
  235. connect(basic_stream<Executor>& s1, basic_stream<Executor>& s2)
  236. {
  237. s1.connect(s2);
  238. }
  239. } // test
  240. } // beast
  241. } // boost
  242. #endif