update boost
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
//
|
||||
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
// Copyright (c) 2020 Richard Hodges (hodges.r@gmail.com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#ifndef BOOST_BEAST_TEST_DETAIL_STREAM_STATE_HPP
|
||||
#define BOOST_BEAST_TEST_DETAIL_STREAM_STATE_HPP
|
||||
|
||||
#include <boost/asio/any_io_executor.hpp>
|
||||
#include <boost/beast/core/detail/config.hpp>
|
||||
#include <boost/beast/_experimental/test/fail_count.hpp>
|
||||
#include <boost/beast/core/detail/service_base.hpp>
|
||||
#include <boost/beast/core/flat_buffer.hpp>
|
||||
#include <boost/smart_ptr/weak_ptr.hpp>
|
||||
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
namespace test {
|
||||
namespace detail {
|
||||
|
||||
struct stream_state;
|
||||
|
||||
struct stream_service_impl
|
||||
{
|
||||
std::mutex m_;
|
||||
std::vector<stream_state*> v_;
|
||||
|
||||
BOOST_BEAST_DECL
|
||||
void
|
||||
remove(stream_state& impl);
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class stream_service
|
||||
: public beast::detail::service_base<stream_service>
|
||||
{
|
||||
boost::shared_ptr<detail::stream_service_impl> sp_;
|
||||
|
||||
BOOST_BEAST_DECL
|
||||
void
|
||||
shutdown() override;
|
||||
|
||||
public:
|
||||
BOOST_BEAST_DECL
|
||||
explicit
|
||||
stream_service(net::execution_context& ctx);
|
||||
|
||||
BOOST_BEAST_DECL
|
||||
static
|
||||
auto
|
||||
make_impl(
|
||||
net::any_io_executor exec,
|
||||
test::fail_count* fc) ->
|
||||
boost::shared_ptr<detail::stream_state>;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
struct stream_read_op_base
|
||||
{
|
||||
virtual ~stream_read_op_base() = default;
|
||||
virtual void operator()(error_code ec) = 0;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
enum class stream_status
|
||||
{
|
||||
ok,
|
||||
eof,
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
struct stream_state
|
||||
{
|
||||
net::any_io_executor exec;
|
||||
boost::weak_ptr<stream_service_impl> wp;
|
||||
std::mutex m;
|
||||
flat_buffer b;
|
||||
std::condition_variable cv;
|
||||
std::unique_ptr<stream_read_op_base> op;
|
||||
stream_status code = stream_status::ok;
|
||||
fail_count* fc = nullptr;
|
||||
std::size_t nread = 0;
|
||||
std::size_t nread_bytes = 0;
|
||||
std::size_t nwrite = 0;
|
||||
std::size_t nwrite_bytes = 0;
|
||||
std::size_t read_max =
|
||||
(std::numeric_limits<std::size_t>::max)();
|
||||
std::size_t write_max =
|
||||
(std::numeric_limits<std::size_t>::max)();
|
||||
|
||||
BOOST_BEAST_DECL
|
||||
stream_state(
|
||||
net::any_io_executor exec_,
|
||||
boost::weak_ptr<stream_service_impl> wp_,
|
||||
fail_count* fc_);
|
||||
|
||||
BOOST_BEAST_DECL
|
||||
~stream_state();
|
||||
|
||||
BOOST_BEAST_DECL
|
||||
void
|
||||
remove() noexcept;
|
||||
|
||||
BOOST_BEAST_DECL
|
||||
void
|
||||
notify_read();
|
||||
|
||||
BOOST_BEAST_DECL
|
||||
void
|
||||
cancel_read();
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // detail
|
||||
} // test
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#ifdef BOOST_BEAST_HEADER_ONLY
|
||||
#include <boost/beast/_experimental/test/detail/stream_state.ipp>
|
||||
#endif
|
||||
|
||||
#endif // BOOST_BEAST_TEST_DETAIL_STREAM_STATE_HPP
|
||||
@@ -0,0 +1,150 @@
|
||||
//
|
||||
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||
// Copyright (c) 2020 Richard Hodges (hodges.r@gmail.com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// Official repository: https://github.com/boostorg/beast
|
||||
//
|
||||
|
||||
#ifndef BOOST_BEAST_TEST_DETAIL_STREAM_STATE_IPP
|
||||
#define BOOST_BEAST_TEST_DETAIL_STREAM_STATE_IPP
|
||||
|
||||
#include <boost/beast/_experimental/test/error.hpp>
|
||||
#include <boost/make_shared.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace beast {
|
||||
namespace test {
|
||||
|
||||
namespace detail {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
stream_service::
|
||||
stream_service(net::execution_context& ctx)
|
||||
: beast::detail::service_base<stream_service>(ctx)
|
||||
, sp_(boost::make_shared<stream_service_impl>())
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
stream_service::
|
||||
shutdown()
|
||||
{
|
||||
std::vector<std::unique_ptr<detail::stream_read_op_base>> v;
|
||||
std::lock_guard<std::mutex> g1(sp_->m_);
|
||||
v.reserve(sp_->v_.size());
|
||||
for(auto p : sp_->v_)
|
||||
{
|
||||
std::lock_guard<std::mutex> g2(p->m);
|
||||
v.emplace_back(std::move(p->op));
|
||||
p->code = detail::stream_status::eof;
|
||||
}
|
||||
}
|
||||
|
||||
auto
|
||||
stream_service::
|
||||
make_impl(
|
||||
net::any_io_executor exec,
|
||||
test::fail_count* fc) ->
|
||||
boost::shared_ptr<detail::stream_state>
|
||||
{
|
||||
#if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
|
||||
auto& ctx = exec.context();
|
||||
#else
|
||||
auto& ctx = net::query(
|
||||
exec,
|
||||
net::execution::context);
|
||||
#endif
|
||||
auto& svc = net::use_service<stream_service>(ctx);
|
||||
auto sp = boost::make_shared<detail::stream_state>(exec, svc.sp_, fc);
|
||||
std::lock_guard<std::mutex> g(svc.sp_->m_);
|
||||
svc.sp_->v_.push_back(sp.get());
|
||||
return sp;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
void
|
||||
stream_service_impl::
|
||||
remove(stream_state& impl)
|
||||
{
|
||||
std::lock_guard<std::mutex> g(m_);
|
||||
*std::find(
|
||||
v_.begin(), v_.end(),
|
||||
&impl) = std::move(v_.back());
|
||||
v_.pop_back();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
stream_state::
|
||||
stream_state(
|
||||
net::any_io_executor exec_,
|
||||
boost::weak_ptr<stream_service_impl> wp_,
|
||||
fail_count* fc_)
|
||||
: exec(std::move(exec_))
|
||||
, wp(std::move(wp_))
|
||||
, fc(fc_)
|
||||
{
|
||||
}
|
||||
|
||||
stream_state::
|
||||
~stream_state()
|
||||
{
|
||||
// cancel outstanding read
|
||||
if(op != nullptr)
|
||||
(*op)(net::error::operation_aborted);
|
||||
}
|
||||
|
||||
void
|
||||
stream_state::
|
||||
remove() noexcept
|
||||
{
|
||||
auto sp = wp.lock();
|
||||
|
||||
// If this goes off, it means the lifetime of a test::stream object
|
||||
// extended beyond the lifetime of the associated execution context.
|
||||
BOOST_ASSERT(sp);
|
||||
|
||||
sp->remove(*this);
|
||||
}
|
||||
|
||||
void
|
||||
stream_state::
|
||||
notify_read()
|
||||
{
|
||||
if(op)
|
||||
{
|
||||
auto op_ = std::move(op);
|
||||
op_->operator()(error_code{});
|
||||
}
|
||||
else
|
||||
{
|
||||
cv.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
stream_state::
|
||||
cancel_read()
|
||||
{
|
||||
std::unique_ptr<stream_read_op_base> p;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
code = stream_status::eof;
|
||||
p = std::move(op);
|
||||
}
|
||||
if(p != nullptr)
|
||||
(*p)(net::error::operation_aborted);
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
} // test
|
||||
} // beast
|
||||
} // boost
|
||||
|
||||
#endif // BOOST_BEAST_TEST_DETAIL_STREAM_STATE_IPP
|
||||
Reference in New Issue
Block a user