2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3 // Copyright (c) 2020 Richard Hodges (hodges.r@gmail.com)
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 // Official repository: https://github.com/boostorg/beast
11 #ifndef BOOST_BEAST_TEST_DETAIL_STREAM_STATE_IPP
12 #define BOOST_BEAST_TEST_DETAIL_STREAM_STATE_IPP
14 #include <boost/beast/_experimental/test/error.hpp>
15 #include <boost/make_shared.hpp>
23 //------------------------------------------------------------------------------
26 stream_service(net::execution_context& ctx)
27 : beast::detail::service_base<stream_service>(ctx)
28 , sp_(boost::make_shared<stream_service_impl>())
36 std::vector<std::unique_ptr<detail::stream_read_op_base>> v;
37 std::lock_guard<std::mutex> g1(sp_->m_);
38 v.reserve(sp_->v_.size());
41 std::lock_guard<std::mutex> g2(p->m);
42 v.emplace_back(std::move(p->op));
43 p->code = detail::stream_status::eof;
50 net::any_io_executor exec,
51 test::fail_count* fc) ->
52 boost::shared_ptr<detail::stream_state>
54 #if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
55 auto& ctx = exec.context();
57 auto& ctx = net::query(
59 net::execution::context);
61 auto& svc = net::use_service<stream_service>(ctx);
62 auto sp = boost::make_shared<detail::stream_state>(exec, svc.sp_, fc);
63 std::lock_guard<std::mutex> g(svc.sp_->m_);
64 svc.sp_->v_.push_back(sp.get());
68 //------------------------------------------------------------------------------
72 remove(stream_state& impl)
74 std::lock_guard<std::mutex> g(m_);
77 &impl) = std::move(v_.back());
81 //------------------------------------------------------------------------------
85 net::any_io_executor exec_,
86 boost::weak_ptr<stream_service_impl> wp_,
88 : exec(std::move(exec_))
97 // cancel outstanding read
99 (*op)(net::error::operation_aborted);
108 // If this goes off, it means the lifetime of a test::stream object
109 // extended beyond the lifetime of the associated execution context.
121 auto op_ = std::move(op);
122 op_->operator()(error_code{});
134 std::unique_ptr<stream_read_op_base> p;
136 std::lock_guard<std::mutex> lock(m);
137 code = stream_status::eof;
141 (*p)(net::error::operation_aborted);
150 #endif // BOOST_BEAST_TEST_DETAIL_STREAM_STATE_IPP