stream.ipp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. stream::
  22. service::
  23. service(net::execution_context& ctx)
  24. : beast::detail::service_base<service>(ctx)
  25. , sp_(boost::make_shared<service_impl>())
  26. {
  27. }
  28. void
  29. stream::
  30. service::
  31. shutdown()
  32. {
  33. std::vector<std::unique_ptr<read_op_base>> v;
  34. std::lock_guard<std::mutex> g1(sp_->m_);
  35. v.reserve(sp_->v_.size());
  36. for(auto p : sp_->v_)
  37. {
  38. std::lock_guard<std::mutex> g2(p->m);
  39. v.emplace_back(std::move(p->op));
  40. p->code = status::eof;
  41. }
  42. }
  43. auto
  44. stream::
  45. service::
  46. make_impl(
  47. net::io_context& ctx,
  48. test::fail_count* fc) ->
  49. boost::shared_ptr<state>
  50. {
  51. auto& svc = net::use_service<service>(ctx);
  52. auto sp = boost::make_shared<state>(ctx, svc.sp_, fc);
  53. std::lock_guard<std::mutex> g(svc.sp_->m_);
  54. svc.sp_->v_.push_back(sp.get());
  55. return sp;
  56. }
  57. void
  58. stream::
  59. service_impl::
  60. remove(state& impl)
  61. {
  62. std::lock_guard<std::mutex> g(m_);
  63. *std::find(
  64. v_.begin(), v_.end(),
  65. &impl) = std::move(v_.back());
  66. v_.pop_back();
  67. }
  68. //------------------------------------------------------------------------------
  69. void stream::initiate_read(
  70. boost::shared_ptr<state> const& in_,
  71. std::unique_ptr<stream::read_op_base>&& op,
  72. std::size_t buf_size)
  73. {
  74. std::unique_lock<std::mutex> lock(in_->m);
  75. ++in_->nread;
  76. if(in_->op != nullptr)
  77. BOOST_THROW_EXCEPTION(
  78. std::logic_error{"in_->op != nullptr"});
  79. // test failure
  80. error_code ec;
  81. if(in_->fc && in_->fc->fail(ec))
  82. {
  83. lock.unlock();
  84. (*op)(ec);
  85. return;
  86. }
  87. // A request to read 0 bytes from a stream is a no-op.
  88. if(buf_size == 0 || buffer_bytes(in_->b.data()) > 0)
  89. {
  90. lock.unlock();
  91. (*op)(ec);
  92. return;
  93. }
  94. // deliver error
  95. if(in_->code != status::ok)
  96. {
  97. lock.unlock();
  98. (*op)(net::error::eof);
  99. return;
  100. }
  101. // complete when bytes available or closed
  102. in_->op = std::move(op);
  103. }
  104. stream::
  105. state::
  106. state(
  107. net::io_context& ioc_,
  108. boost::weak_ptr<service_impl> wp_,
  109. fail_count* fc_)
  110. : ioc(ioc_)
  111. , wp(std::move(wp_))
  112. , fc(fc_)
  113. {
  114. }
  115. stream::
  116. state::
  117. ~state()
  118. {
  119. // cancel outstanding read
  120. if(op != nullptr)
  121. (*op)(net::error::operation_aborted);
  122. }
  123. void
  124. stream::
  125. state::
  126. remove() noexcept
  127. {
  128. auto sp = wp.lock();
  129. // If this goes off, it means the lifetime of a test::stream object
  130. // extended beyond the lifetime of the associated execution context.
  131. BOOST_ASSERT(sp);
  132. sp->remove(*this);
  133. }
  134. void
  135. stream::
  136. state::
  137. notify_read()
  138. {
  139. if(op)
  140. {
  141. auto op_ = std::move(op);
  142. op_->operator()(error_code{});
  143. }
  144. else
  145. {
  146. cv.notify_all();
  147. }
  148. }
  149. void
  150. stream::
  151. state::
  152. cancel_read()
  153. {
  154. std::unique_ptr<read_op_base> p;
  155. {
  156. std::lock_guard<std::mutex> lock(m);
  157. code = status::eof;
  158. p = std::move(op);
  159. }
  160. if(p != nullptr)
  161. (*p)(net::error::operation_aborted);
  162. }
  163. //------------------------------------------------------------------------------
  164. stream::
  165. ~stream()
  166. {
  167. close();
  168. in_->remove();
  169. }
  170. stream::
  171. stream(stream&& other)
  172. {
  173. auto in = service::make_impl(
  174. other.in_->ioc, other.in_->fc);
  175. in_ = std::move(other.in_);
  176. out_ = std::move(other.out_);
  177. other.in_ = in;
  178. }
  179. stream&
  180. stream::
  181. operator=(stream&& other)
  182. {
  183. close();
  184. auto in = service::make_impl(
  185. other.in_->ioc, other.in_->fc);
  186. in_->remove();
  187. in_ = std::move(other.in_);
  188. out_ = std::move(other.out_);
  189. other.in_ = in;
  190. return *this;
  191. }
  192. //------------------------------------------------------------------------------
  193. stream::
  194. stream(net::io_context& ioc)
  195. : in_(service::make_impl(ioc, nullptr))
  196. {
  197. }
  198. stream::
  199. stream(
  200. net::io_context& ioc,
  201. fail_count& fc)
  202. : in_(service::make_impl(ioc, &fc))
  203. {
  204. }
  205. stream::
  206. stream(
  207. net::io_context& ioc,
  208. string_view s)
  209. : in_(service::make_impl(ioc, nullptr))
  210. {
  211. in_->b.commit(net::buffer_copy(
  212. in_->b.prepare(s.size()),
  213. net::buffer(s.data(), s.size())));
  214. }
  215. stream::
  216. stream(
  217. net::io_context& ioc,
  218. fail_count& fc,
  219. string_view s)
  220. : in_(service::make_impl(ioc, &fc))
  221. {
  222. in_->b.commit(net::buffer_copy(
  223. in_->b.prepare(s.size()),
  224. net::buffer(s.data(), s.size())));
  225. }
  226. void
  227. stream::
  228. connect(stream& remote)
  229. {
  230. BOOST_ASSERT(! out_.lock());
  231. BOOST_ASSERT(! remote.out_.lock());
  232. std::lock(in_->m, remote.in_->m);
  233. std::lock_guard<std::mutex> guard1{in_->m, std::adopt_lock};
  234. std::lock_guard<std::mutex> guard2{remote.in_->m, std::adopt_lock};
  235. out_ = remote.in_;
  236. remote.out_ = in_;
  237. in_->code = status::ok;
  238. remote.in_->code = status::ok;
  239. }
  240. string_view
  241. stream::
  242. str() const
  243. {
  244. auto const bs = in_->b.data();
  245. if(buffer_bytes(bs) == 0)
  246. return {};
  247. net::const_buffer const b = *net::buffer_sequence_begin(bs);
  248. return {static_cast<char const*>(b.data()), b.size()};
  249. }
  250. void
  251. stream::
  252. append(string_view s)
  253. {
  254. std::lock_guard<std::mutex> lock{in_->m};
  255. in_->b.commit(net::buffer_copy(
  256. in_->b.prepare(s.size()),
  257. net::buffer(s.data(), s.size())));
  258. }
  259. void
  260. stream::
  261. clear()
  262. {
  263. std::lock_guard<std::mutex> lock{in_->m};
  264. in_->b.consume(in_->b.size());
  265. }
  266. void
  267. stream::
  268. close()
  269. {
  270. in_->cancel_read();
  271. // disconnect
  272. {
  273. auto out = out_.lock();
  274. out_.reset();
  275. // notify peer
  276. if(out)
  277. {
  278. std::lock_guard<std::mutex> lock(out->m);
  279. if(out->code == status::ok)
  280. {
  281. out->code = status::eof;
  282. out->notify_read();
  283. }
  284. }
  285. }
  286. }
  287. void
  288. stream::
  289. close_remote()
  290. {
  291. std::lock_guard<std::mutex> lock{in_->m};
  292. if(in_->code == status::ok)
  293. {
  294. in_->code = status::eof;
  295. in_->notify_read();
  296. }
  297. }
  298. void
  299. teardown(
  300. role_type,
  301. stream& s,
  302. boost::system::error_code& ec)
  303. {
  304. if( s.in_->fc &&
  305. s.in_->fc->fail(ec))
  306. return;
  307. s.close();
  308. if( s.in_->fc &&
  309. s.in_->fc->fail(ec))
  310. ec = net::error::eof;
  311. else
  312. ec = {};
  313. }
  314. //------------------------------------------------------------------------------
  315. stream
  316. connect(stream& to)
  317. {
  318. stream from{to.get_executor().context()};
  319. from.connect(to);
  320. return from;
  321. }
  322. void
  323. connect(stream& s1, stream& s2)
  324. {
  325. s1.connect(s2);
  326. }
  327. } // test
  328. } // beast
  329. } // boost
  330. #endif