Add executor rebind to test::stream

closes #2139
This commit is contained in:
Richard Hodges
2020-12-11 12:03:34 +01:00
parent a87330e53f
commit 8913a3cd21
13 changed files with 619 additions and 400 deletions

View File

@@ -35,3 +35,4 @@ echo '==================================> COMPILE'
cd ../boost-root cd ../boost-root
libs/beast/tools/retry.sh libs/beast/tools/build-and-test.sh libs/beast/tools/retry.sh libs/beast/tools/build-and-test.sh

View File

@@ -1,3 +1,4 @@
* Add executor rebind to test::stream.
* Remove floating point arithmetic requirement. * Remove floating point arithmetic requirement.
* Add `cxxstd` to json field. * Add `cxxstd` to json field.

View File

@@ -0,0 +1,135 @@
//
// 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/service_base.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

View File

@@ -0,0 +1,149 @@
//
// 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>
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

View File

@@ -27,53 +27,19 @@ namespace test {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
struct stream::service_impl template<class Executor>
{
std::mutex m_;
std::vector<state*> v_;
BOOST_BEAST_DECL
void
remove(state& impl);
};
class stream::service
: public beast::detail::service_base<service>
{
boost::shared_ptr<service_impl> sp_;
BOOST_BEAST_DECL
void
shutdown() override;
public:
BOOST_BEAST_DECL
explicit
service(net::execution_context& ctx);
BOOST_BEAST_DECL
static
auto
make_impl(
net::io_context& ctx,
test::fail_count* fc) ->
boost::shared_ptr<state>;
};
//------------------------------------------------------------------------------
template<class Handler, class Buffers> template<class Handler, class Buffers>
class stream::read_op : public stream::read_op_base class basic_stream<Executor>::read_op : public detail::stream_read_op_base
{ {
using ex1_type = using ex1_type =
net::io_context::executor_type; executor_type;
using ex2_type using ex2_type
= net::associated_executor_t<Handler, ex1_type>; = net::associated_executor_t<Handler, ex1_type>;
struct lambda struct lambda
{ {
Handler h_; Handler h_;
boost::weak_ptr<state> wp_; boost::weak_ptr<detail::stream_state> wp_;
Buffers b_; Buffers b_;
#if defined(BOOST_ASIO_NO_TS_EXECUTORS) #if defined(BOOST_ASIO_NO_TS_EXECUTORS)
net::any_io_executor wg2_; net::any_io_executor wg2_;
@@ -87,7 +53,7 @@ class stream::read_op : public stream::read_op_base
template<class Handler_> template<class Handler_>
lambda( lambda(
Handler_&& h, Handler_&& h,
boost::shared_ptr<state> const& s, boost::shared_ptr<detail::stream_state> const& s,
Buffers const& b) Buffers const& b)
: h_(std::forward<Handler_>(h)) : h_(std::forward<Handler_>(h))
, wp_(s) , wp_(s)
@@ -95,11 +61,11 @@ class stream::read_op : public stream::read_op_base
#if defined(BOOST_ASIO_NO_TS_EXECUTORS) #if defined(BOOST_ASIO_NO_TS_EXECUTORS)
, wg2_(net::prefer( , wg2_(net::prefer(
net::get_associated_executor( net::get_associated_executor(
h_, s->ioc.get_executor()), h_, s->exec),
net::execution::outstanding_work.tracked)) net::execution::outstanding_work.tracked))
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS) #else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
, wg2_(net::get_associated_executor( , wg2_(net::get_associated_executor(
h_, s->ioc.get_executor())) h_, s->exec))
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS) #endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
{ {
} }
@@ -151,43 +117,44 @@ class stream::read_op : public stream::read_op_base
}; };
lambda fn_; lambda fn_;
#if defined(BOOST_ASIO_NO_TS_EXECUTORS) #if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
net::executor_work_guard<net::any_io_executor> wg1_;
#else
net::any_io_executor wg1_; net::any_io_executor wg1_;
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS) #endif
net::executor_work_guard<ex1_type> wg1_;
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
public: public:
template<class Handler_> template<class Handler_>
read_op( read_op(
Handler_&& h, Handler_&& h,
boost::shared_ptr<state> const& s, boost::shared_ptr<detail::stream_state> const& s,
Buffers const& b) Buffers const& b)
: fn_(std::forward<Handler_>(h), s, b) : fn_(std::forward<Handler_>(h), s, b)
#if defined(BOOST_ASIO_NO_TS_EXECUTORS) #if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
, wg1_(net::prefer(s->ioc.get_executor(), , wg1_(s->exec)
#else
, wg1_(net::prefer(s->exec,
net::execution::outstanding_work.tracked)) net::execution::outstanding_work.tracked))
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS) #endif
, wg1_(s->ioc.get_executor())
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
{ {
} }
void void
operator()(error_code ec) override operator()(error_code ec) override
{ {
#if defined(BOOST_ASIO_NO_TS_EXECUTORS) #if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
net::post(wg1_, beast::bind_front_handler(std::move(fn_), ec));
wg1_ = net::any_io_executor(); // probably unnecessary
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
net::post(wg1_.get_executor(), net::post(wg1_.get_executor(),
beast::bind_front_handler(std::move(fn_), ec)); beast::bind_front_handler(std::move(fn_), ec));
wg1_.reset(); wg1_.reset();
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS) #else
net::post(wg1_, beast::bind_front_handler(std::move(fn_), ec));
wg1_ = net::any_io_executor(); // probably unnecessary
#endif
} }
}; };
struct stream::run_read_op template<class Executor>
struct basic_stream<Executor>::run_read_op
{ {
template< template<
class ReadHandler, class ReadHandler,
@@ -195,7 +162,7 @@ struct stream::run_read_op
void void
operator()( operator()(
ReadHandler&& h, ReadHandler&& h,
boost::shared_ptr<state> const& in, boost::shared_ptr<detail::stream_state> const& in,
MutableBufferSequence const& buffers) MutableBufferSequence const& buffers)
{ {
// If you get an error on the following line it means // If you get an error on the following line it means
@@ -209,7 +176,7 @@ struct stream::run_read_op
initiate_read( initiate_read(
in, in,
std::unique_ptr<read_op_base>{ std::unique_ptr<detail::stream_read_op_base>{
new read_op< new read_op<
typename std::decay<ReadHandler>::type, typename std::decay<ReadHandler>::type,
MutableBufferSequence>( MutableBufferSequence>(
@@ -220,7 +187,8 @@ struct stream::run_read_op
} }
}; };
struct stream::run_write_op template<class Executor>
struct basic_stream<Executor>::run_write_op
{ {
template< template<
class WriteHandler, class WriteHandler,
@@ -228,8 +196,8 @@ struct stream::run_write_op
void void
operator()( operator()(
WriteHandler&& h, WriteHandler&& h,
boost::shared_ptr<state> in_, boost::shared_ptr<detail::stream_state> in_,
boost::weak_ptr<state> out_, boost::weak_ptr<detail::stream_state> out_,
ConstBufferSequence const& buffers) ConstBufferSequence const& buffers)
{ {
// If you get an error on the following line it means // If you get an error on the following line it means
@@ -245,7 +213,7 @@ struct stream::run_write_op
auto const upcall = [&](error_code ec, std::size_t n) auto const upcall = [&](error_code ec, std::size_t n)
{ {
net::post( net::post(
in_->ioc.get_executor(), in_->exec,
beast::bind_front_handler(std::move(h), ec, n)); beast::bind_front_handler(std::move(h), ec, n));
}; };
@@ -281,9 +249,10 @@ struct stream::run_write_op
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
template<class Executor>
template<class MutableBufferSequence> template<class MutableBufferSequence>
std::size_t std::size_t
stream:: basic_stream<Executor>::
read_some(MutableBufferSequence const& buffers) read_some(MutableBufferSequence const& buffers)
{ {
static_assert(net::is_mutable_buffer_sequence< static_assert(net::is_mutable_buffer_sequence<
@@ -296,9 +265,10 @@ read_some(MutableBufferSequence const& buffers)
return n; return n;
} }
template<class Executor>
template<class MutableBufferSequence> template<class MutableBufferSequence>
std::size_t std::size_t
stream:: basic_stream<Executor>::
read_some(MutableBufferSequence const& buffers, read_some(MutableBufferSequence const& buffers,
error_code& ec) error_code& ec)
{ {
@@ -326,7 +296,7 @@ read_some(MutableBufferSequence const& buffers,
{ {
return return
in_->b.size() > 0 || in_->b.size() > 0 ||
in_->code != status::ok; in_->code != detail::stream_status::ok;
}); });
// deliver bytes before eof // deliver bytes before eof
@@ -340,14 +310,16 @@ read_some(MutableBufferSequence const& buffers,
} }
// deliver error // deliver error
BOOST_ASSERT(in_->code != status::ok); BOOST_ASSERT(in_->code != detail::stream_status::ok);
ec = net::error::eof; ec = net::error::eof;
return 0; return 0;
} }
template<class MutableBufferSequence, BOOST_BEAST_ASYNC_TPARAM2 ReadHandler> template<class Executor>
BOOST_BEAST_ASYNC_RESULT2(ReadHandler) template<class MutableBufferSequence,
stream:: BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code, std::size_t)) ReadHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, void(error_code, std::size_t))
basic_stream<Executor>::
async_read_some( async_read_some(
MutableBufferSequence const& buffers, MutableBufferSequence const& buffers,
ReadHandler&& handler) ReadHandler&& handler)
@@ -365,9 +337,10 @@ async_read_some(
buffers); buffers);
} }
template<class Executor>
template<class ConstBufferSequence> template<class ConstBufferSequence>
std::size_t std::size_t
stream:: basic_stream<Executor>::
write_some(ConstBufferSequence const& buffers) write_some(ConstBufferSequence const& buffers)
{ {
static_assert(net::is_const_buffer_sequence< static_assert(net::is_const_buffer_sequence<
@@ -381,9 +354,10 @@ write_some(ConstBufferSequence const& buffers)
return bytes_transferred; return bytes_transferred;
} }
template<class Executor>
template<class ConstBufferSequence> template<class ConstBufferSequence>
std::size_t std::size_t
stream:: basic_stream<Executor>::
write_some( write_some(
ConstBufferSequence const& buffers, error_code& ec) ConstBufferSequence const& buffers, error_code& ec)
{ {
@@ -425,9 +399,11 @@ write_some(
return n; return n;
} }
template<class ConstBufferSequence, BOOST_BEAST_ASYNC_TPARAM2 WriteHandler> template<class Executor>
BOOST_BEAST_ASYNC_RESULT2(WriteHandler) template<class ConstBufferSequence,
stream:: BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code, std::size_t)) WriteHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, void(error_code, std::size_t))
basic_stream<Executor>::
async_write_some( async_write_some(
ConstBufferSequence const& buffers, ConstBufferSequence const& buffers,
WriteHandler&& handler) WriteHandler&& handler)
@@ -448,11 +424,11 @@ async_write_some(
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
template<class TeardownHandler> template<class Executor, class TeardownHandler>
void void
async_teardown( async_teardown(
role_type, role_type,
stream& s, basic_stream<Executor>& s,
TeardownHandler&& handler) TeardownHandler&& handler)
{ {
error_code ec; error_code ec;
@@ -477,8 +453,8 @@ async_teardown(
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
template<class Arg1, class... ArgN> template<class Executor, class Arg1, class... ArgN>
stream basic_stream<Executor>
connect(stream& to, Arg1&& arg1, ArgN&&... argn) connect(stream& to, Arg1&& arg1, ArgN&&... argn)
{ {
stream from{ stream from{
@@ -488,6 +464,34 @@ connect(stream& to, Arg1&& arg1, ArgN&&... argn)
return from; return from;
} }
namespace detail
{
template<class To>
struct extract_executor_op
{
To operator()(net::any_io_executor& ex) const
{
assert(ex.template target<To>());
return *ex.template target<To>();
}
};
template<>
struct extract_executor_op<net::any_io_executor>
{
net::any_io_executor operator()(net::any_io_executor& ex) const
{
return ex;
}
};
}
template<class Executor>
auto basic_stream<Executor>::get_executor() noexcept -> executor_type
{
return detail::extract_executor_op<Executor>()(in_->exec);
}
} // test } // test
} // beast } // beast
} // boost } // boost

View File

@@ -23,62 +23,10 @@ namespace test {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
stream:: template<class Executor>
service:: void basic_stream<Executor>::initiate_read(
service(net::execution_context& ctx) boost::shared_ptr<detail::stream_state> const& in_,
: beast::detail::service_base<service>(ctx) std::unique_ptr<detail::stream_read_op_base>&& op,
, sp_(boost::make_shared<service_impl>())
{
}
void
stream::
service::
shutdown()
{
std::vector<std::unique_ptr<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 = status::eof;
}
}
auto
stream::
service::
make_impl(
net::io_context& ctx,
test::fail_count* fc) ->
boost::shared_ptr<state>
{
auto& svc = net::use_service<service>(ctx);
auto sp = boost::make_shared<state>(ctx, 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(state& impl)
{
std::lock_guard<std::mutex> g(m_);
*std::find(
v_.begin(), v_.end(),
&impl) = std::move(v_.back());
v_.pop_back();
}
//------------------------------------------------------------------------------
void stream::initiate_read(
boost::shared_ptr<state> const& in_,
std::unique_ptr<stream::read_op_base>&& op,
std::size_t buf_size) std::size_t buf_size)
{ {
std::unique_lock<std::mutex> lock(in_->m); std::unique_lock<std::mutex> lock(in_->m);
@@ -106,7 +54,7 @@ void stream::initiate_read(
} }
// deliver error // deliver error
if(in_->code != status::ok) if(in_->code != detail::stream_status::ok)
{ {
lock.unlock(); lock.unlock();
(*op)(net::error::eof); (*op)(net::error::eof);
@@ -117,98 +65,36 @@ void stream::initiate_read(
in_->op = std::move(op); in_->op = std::move(op);
} }
stream::
state::
state(
net::io_context& ioc_,
boost::weak_ptr<service_impl> wp_,
fail_count* fc_)
: ioc(ioc_)
, wp(std::move(wp_))
, fc(fc_)
{
}
stream::
state::
~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<read_op_base> p;
{
std::lock_guard<std::mutex> lock(m);
code = status::eof;
p = std::move(op);
}
if(p != nullptr)
(*p)(net::error::operation_aborted);
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
stream:: template<class Executor>
~stream() basic_stream<Executor>::
~basic_stream()
{ {
close(); close();
in_->remove(); in_->remove();
} }
stream:: template<class Executor>
stream(stream&& other) basic_stream<Executor>::
basic_stream(basic_stream&& other)
{ {
auto in = service::make_impl( auto in = detail::stream_service::make_impl(
other.in_->ioc, other.in_->fc); other.in_->exec, other.in_->fc);
in_ = std::move(other.in_); in_ = std::move(other.in_);
out_ = std::move(other.out_); out_ = std::move(other.out_);
other.in_ = in; other.in_ = in;
} }
stream&
stream:: template<class Executor>
operator=(stream&& other) basic_stream<Executor>&
basic_stream<Executor>::
operator=(basic_stream&& other)
{ {
close(); close();
auto in = service::make_impl( auto in = detail::stream_service::make_impl(
other.in_->ioc, other.in_->fc); other.in_->exec, other.in_->fc);
in_->remove(); in_->remove();
in_ = std::move(other.in_); in_ = std::move(other.in_);
out_ = std::move(other.out_); out_ = std::move(other.out_);
@@ -218,46 +104,51 @@ operator=(stream&& other)
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
stream:: template<class Executor>
stream(net::io_context& ioc) basic_stream<Executor>::
: in_(service::make_impl(ioc, nullptr)) basic_stream(executor_type exec)
: in_(detail::stream_service::make_impl(std::move(exec), nullptr))
{ {
} }
stream:: template<class Executor>
stream( basic_stream<Executor>::
basic_stream(
net::io_context& ioc, net::io_context& ioc,
fail_count& fc) fail_count& fc)
: in_(service::make_impl(ioc, &fc)) : in_(detail::stream_service::make_impl(ioc.get_executor(), &fc))
{ {
} }
stream:: template<class Executor>
stream( basic_stream<Executor>::
basic_stream(
net::io_context& ioc, net::io_context& ioc,
string_view s) string_view s)
: in_(service::make_impl(ioc, nullptr)) : in_(detail::stream_service::make_impl(ioc.get_executor(), nullptr))
{ {
in_->b.commit(net::buffer_copy( in_->b.commit(net::buffer_copy(
in_->b.prepare(s.size()), in_->b.prepare(s.size()),
net::buffer(s.data(), s.size()))); net::buffer(s.data(), s.size())));
} }
stream:: template<class Executor>
stream( basic_stream<Executor>::
basic_stream(
net::io_context& ioc, net::io_context& ioc,
fail_count& fc, fail_count& fc,
string_view s) string_view s)
: in_(service::make_impl(ioc, &fc)) : in_(detail::stream_service::make_impl(ioc.get_executor(), &fc))
{ {
in_->b.commit(net::buffer_copy( in_->b.commit(net::buffer_copy(
in_->b.prepare(s.size()), in_->b.prepare(s.size()),
net::buffer(s.data(), s.size()))); net::buffer(s.data(), s.size())));
} }
template<class Executor>
void void
stream:: basic_stream<Executor>::
connect(stream& remote) connect(basic_stream& remote)
{ {
BOOST_ASSERT(! out_.lock()); BOOST_ASSERT(! out_.lock());
BOOST_ASSERT(! remote.out_.lock()); BOOST_ASSERT(! remote.out_.lock());
@@ -266,12 +157,13 @@ connect(stream& remote)
std::lock_guard<std::mutex> guard2{remote.in_->m, std::adopt_lock}; std::lock_guard<std::mutex> guard2{remote.in_->m, std::adopt_lock};
out_ = remote.in_; out_ = remote.in_;
remote.out_ = in_; remote.out_ = in_;
in_->code = status::ok; in_->code = detail::stream_status::ok;
remote.in_->code = status::ok; remote.in_->code = detail::stream_status::ok;
} }
template<class Executor>
string_view string_view
stream:: basic_stream<Executor>::
str() const str() const
{ {
auto const bs = in_->b.data(); auto const bs = in_->b.data();
@@ -281,8 +173,9 @@ str() const
return {static_cast<char const*>(b.data()), b.size()}; return {static_cast<char const*>(b.data()), b.size()};
} }
template<class Executor>
void void
stream:: basic_stream<Executor>::
append(string_view s) append(string_view s)
{ {
std::lock_guard<std::mutex> lock{in_->m}; std::lock_guard<std::mutex> lock{in_->m};
@@ -291,16 +184,18 @@ append(string_view s)
net::buffer(s.data(), s.size()))); net::buffer(s.data(), s.size())));
} }
template<class Executor>
void void
stream:: basic_stream<Executor>::
clear() clear()
{ {
std::lock_guard<std::mutex> lock{in_->m}; std::lock_guard<std::mutex> lock{in_->m};
in_->b.consume(in_->b.size()); in_->b.consume(in_->b.size());
} }
template<class Executor>
void void
stream:: basic_stream<Executor>::
close() close()
{ {
in_->cancel_read(); in_->cancel_read();
@@ -314,31 +209,33 @@ close()
if(out) if(out)
{ {
std::lock_guard<std::mutex> lock(out->m); std::lock_guard<std::mutex> lock(out->m);
if(out->code == status::ok) if(out->code == detail::stream_status::ok)
{ {
out->code = status::eof; out->code = detail::stream_status::eof;
out->notify_read(); out->notify_read();
} }
} }
} }
} }
template<class Executor>
void void
stream:: basic_stream<Executor>::
close_remote() close_remote()
{ {
std::lock_guard<std::mutex> lock{in_->m}; std::lock_guard<std::mutex> lock{in_->m};
if(in_->code == status::ok) if(in_->code == detail::stream_status::ok)
{ {
in_->code = status::eof; in_->code = detail::stream_status::eof;
in_->notify_read(); in_->notify_read();
} }
} }
template<class Executor>
void void
teardown( teardown(
role_type, role_type,
stream& s, basic_stream<Executor>& s,
boost::system::error_code& ec) boost::system::error_code& ec)
{ {
if( s.in_->fc && if( s.in_->fc &&
@@ -356,20 +253,18 @@ teardown(
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
stream template<class Executor>
connect(stream& to) basic_stream<Executor>
connect(basic_stream<Executor>& to)
{ {
#if defined(BOOST_ASIO_NO_TS_EXECUTORS) basic_stream<Executor> from(to.get_executor());
stream from{net::query(to.get_executor(), net::execution::context)};
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
stream from{to.get_executor().context()};
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
from.connect(to); from.connect(to);
return from; return from;
} }
template<class Executor>
void void
connect(stream& s1, stream& s2) connect(basic_stream<Executor>& s1, basic_stream<Executor>& s2)
{ {
s1.connect(s2); s1.connect(s2);
} }

View File

@@ -16,10 +16,12 @@
#include <boost/beast/core/role.hpp> #include <boost/beast/core/role.hpp>
#include <boost/beast/core/string.hpp> #include <boost/beast/core/string.hpp>
#include <boost/beast/_experimental/test/fail_count.hpp> #include <boost/beast/_experimental/test/fail_count.hpp>
#include <boost/beast/_experimental/test/detail/stream_state.hpp>
#include <boost/asio/async_result.hpp> #include <boost/asio/async_result.hpp>
#include <boost/asio/buffer.hpp> #include <boost/asio/buffer.hpp>
#include <boost/asio/error.hpp> #include <boost/asio/error.hpp>
#include <boost/asio/executor_work_guard.hpp> #include <boost/asio/executor_work_guard.hpp>
#include <boost/asio/any_io_executor.hpp>
#include <boost/asio/io_context.hpp> #include <boost/asio/io_context.hpp>
#include <boost/asio/post.hpp> #include <boost/asio/post.hpp>
#include <boost/assert.hpp> #include <boost/assert.hpp>
@@ -106,71 +108,45 @@ namespace test {
@li <em>AsyncReadStream</em> @li <em>AsyncReadStream</em>
@li <em>AsyncWriteStream</em> @li <em>AsyncWriteStream</em>
*/ */
class stream template<class Executor = net::any_io_executor>
{ class basic_stream;
struct state;
boost::shared_ptr<state> in_; template<class Executor>
boost::weak_ptr<state> out_; void
teardown(
role_type,
basic_stream<Executor>& s,
boost::system::error_code& ec);
enum class status template<class Executor, class TeardownHandler>
void
async_teardown(
role_type role,
basic_stream<Executor>& s,
TeardownHandler&& handler);
template<class Executor>
class basic_stream
{ {
ok, public:
eof, /// The type of the executor associated with the object.
using executor_type =
Executor;
/// Rebinds the socket type to another executor.
template <typename Executor1>
struct rebind_executor
{
/// The socket type when rebound to the specified executor.
typedef basic_stream<Executor1> other;
}; };
class service; private:
struct service_impl; template<class Executor2>
friend class basic_stream;
struct read_op_base boost::shared_ptr<detail::stream_state> in_;
{ boost::weak_ptr<detail::stream_state> out_;
virtual ~read_op_base() = default;
virtual void operator()(error_code ec) = 0;
};
struct state
{
friend class stream;
net::io_context& ioc;
boost::weak_ptr<service_impl> wp;
std::mutex m;
flat_buffer b;
std::condition_variable cv;
std::unique_ptr<read_op_base> op;
status code = 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
state(
net::io_context& ioc_,
boost::weak_ptr<service_impl> wp_,
fail_count* fc_);
BOOST_BEAST_DECL
~state();
BOOST_BEAST_DECL
void
remove() noexcept;
BOOST_BEAST_DECL
void
notify_read();
BOOST_BEAST_DECL
void
cancel_read();
};
template<class Handler, class Buffers> template<class Handler, class Buffers>
class read_op; class read_op;
@@ -178,12 +154,11 @@ class stream
struct run_read_op; struct run_read_op;
struct run_write_op; struct run_write_op;
BOOST_BEAST_DECL
static static
void void
initiate_read( initiate_read(
boost::shared_ptr<state> const& in, boost::shared_ptr<detail::stream_state> const& in,
std::unique_ptr<read_op_base>&& op, std::unique_ptr<detail::stream_read_op_base>&& op,
std::size_t buf_size); std::size_t buf_size);
#if ! BOOST_BEAST_DOXYGEN #if ! BOOST_BEAST_DOXYGEN
@@ -192,7 +167,7 @@ class stream
template<class> template<class>
friend class boost::asio::ssl::stream; friend class boost::asio::ssl::stream;
// DEPRECATED // DEPRECATED
using lowest_layer_type = stream; using lowest_layer_type = basic_stream;
// DEPRECATED // DEPRECATED
lowest_layer_type& lowest_layer_type&
lowest_layer() noexcept lowest_layer() noexcept
@@ -220,25 +195,40 @@ public:
the peer will see the error `net::error::connection_reset` the peer will see the error `net::error::connection_reset`
when performing any reads or writes. when performing any reads or writes.
*/ */
BOOST_BEAST_DECL ~basic_stream();
~stream();
/** Move Constructor /** Move Constructor
Moving the stream while asynchronous operations are pending Moving the stream while asynchronous operations are pending
results in undefined behavior. results in undefined behavior.
*/ */
BOOST_BEAST_DECL basic_stream(basic_stream&& other);
stream(stream&& other);
/** Move Constructor
Moving the stream while asynchronous operations are pending
results in undefined behavior.
*/
template<class Executor2>
basic_stream(basic_stream<Executor2>&& other)
: in_(std::move(other.in_))
, out_(std::move(other.out_))
{
assert(in_->exec.target_type() == typeid(Executor2));
in_->exec = executor_type(*in_->exec.template target<Executor2>());
}
/** Move Assignment /** Move Assignment
Moving the stream while asynchronous operations are pending Moving the stream while asynchronous operations are pending
results in undefined behavior. results in undefined behavior.
*/ */
BOOST_BEAST_DECL basic_stream&
stream& operator=(basic_stream&& other);
operator=(stream&& other);
template<class Executor2>
basic_stream&
operator==(basic_stream<Executor2>&& other);
/** Construct a stream /** Construct a stream
@@ -247,9 +237,24 @@ public:
@param ioc The `io_context` object that the stream will use to @param ioc The `io_context` object that the stream will use to
dispatch handlers for any asynchronous operations. dispatch handlers for any asynchronous operations.
*/ */
BOOST_BEAST_DECL template <typename ExecutionContext>
explicit basic_stream(ExecutionContext& context,
typename std::enable_if<
std::is_convertible<ExecutionContext&, net::execution_context&>::value
>::type* = 0)
: basic_stream(context.get_executor())
{
}
/** Construct a stream
The stream will be created in a disconnected state.
@param exec The `executor` object that the stream will use to
dispatch handlers for any asynchronous operations.
*/
explicit explicit
stream(net::io_context& ioc); basic_stream(executor_type exec);
/** Construct a stream /** Construct a stream
@@ -263,8 +268,7 @@ public:
fail count. When the fail count reaches its internal limit, fail count. When the fail count reaches its internal limit,
a simulated failure error will be raised. a simulated failure error will be raised.
*/ */
BOOST_BEAST_DECL basic_stream(
stream(
net::io_context& ioc, net::io_context& ioc,
fail_count& fc); fail_count& fc);
@@ -278,8 +282,7 @@ public:
@param s A string which will be appended to the input area, not @param s A string which will be appended to the input area, not
including the null terminator. including the null terminator.
*/ */
BOOST_BEAST_DECL basic_stream(
stream(
net::io_context& ioc, net::io_context& ioc,
string_view s); string_view s);
@@ -298,27 +301,18 @@ public:
@param s A string which will be appended to the input area, not @param s A string which will be appended to the input area, not
including the null terminator. including the null terminator.
*/ */
BOOST_BEAST_DECL basic_stream(
stream(
net::io_context& ioc, net::io_context& ioc,
fail_count& fc, fail_count& fc,
string_view s); string_view s);
/// Establish a connection /// Establish a connection
BOOST_BEAST_DECL
void void
connect(stream& remote); connect(basic_stream& remote);
/// The type of the executor associated with the object.
using executor_type =
net::io_context::executor_type;
/// Return the executor associated with the object. /// Return the executor associated with the object.
executor_type executor_type
get_executor() noexcept get_executor() noexcept;
{
return in_->ioc.get_executor();
};
/// Set the maximum number of bytes returned by read_some /// Set the maximum number of bytes returned by read_some
void void
@@ -342,17 +336,14 @@ public:
} }
/// Returns a string view representing the pending input data /// Returns a string view representing the pending input data
BOOST_BEAST_DECL
string_view string_view
str() const; str() const;
/// Appends a string to the pending input data /// Appends a string to the pending input data
BOOST_BEAST_DECL
void void
append(string_view s); append(string_view s);
/// Clear the pending input area /// Clear the pending input area
BOOST_BEAST_DECL
void void
clear(); clear();
@@ -389,7 +380,6 @@ public:
The other end of the connection will see The other end of the connection will see
`error::eof` after reading all the remaining data. `error::eof` after reading all the remaining data.
*/ */
BOOST_BEAST_DECL
void void
close(); close();
@@ -398,7 +388,6 @@ public:
This end of the connection will see This end of the connection will see
`error::eof` after reading all the remaining data. `error::eof` after reading all the remaining data.
*/ */
BOOST_BEAST_DECL
void void
close_remote(); close_remote();
@@ -477,11 +466,12 @@ public:
*/ */
template< template<
class MutableBufferSequence, class MutableBufferSequence,
BOOST_BEAST_ASYNC_TPARAM2 ReadHandler> BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code, std::size_t)) ReadHandler
BOOST_BEAST_ASYNC_RESULT2(ReadHandler) BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, void(error_code, std::size_t))
async_read_some( async_read_some(
MutableBufferSequence const& buffers, MutableBufferSequence const& buffers,
ReadHandler&& handler); ReadHandler&& handler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type));
/** Write some data to the stream. /** Write some data to the stream.
@@ -555,36 +545,36 @@ public:
*/ */
template< template<
class ConstBufferSequence, class ConstBufferSequence,
BOOST_BEAST_ASYNC_TPARAM2 WriteHandler> BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code, std::size_t)) WriteHandler
BOOST_BEAST_ASYNC_RESULT2(WriteHandler) BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, void(error_code, std::size_t))
async_write_some( async_write_some(
ConstBufferSequence const& buffers, ConstBufferSequence const& buffers,
WriteHandler&& handler); WriteHandler&& handler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
);
#if ! BOOST_BEAST_DOXYGEN #if ! BOOST_BEAST_DOXYGEN
friend friend
BOOST_BEAST_DECL
void void
teardown( teardown<>(
role_type, role_type,
stream& s, basic_stream& s,
boost::system::error_code& ec); boost::system::error_code& ec);
template<class TeardownHandler> template<class Ex2, class TeardownHandler>
friend friend
BOOST_BEAST_DECL
void void
async_teardown( async_teardown(
role_type role, role_type role,
stream& s, basic_stream<Ex2>& s,
TeardownHandler&& handler); TeardownHandler&& handler);
#endif #endif
}; };
#if ! BOOST_BEAST_DOXYGEN #if ! BOOST_BEAST_DOXYGEN
inline template<class Executor>
void void
beast_close_socket(stream& s) beast_close_socket(basic_stream<Executor>& s)
{ {
s.close(); s.close();
} }
@@ -599,31 +589,34 @@ beast_close_socket(stream& s)
@return The new, connected stream. @return The new, connected stream.
*/ */
template<class Executor>
template<class... Args> template<class... Args>
stream bascic_stream
connect(stream& to, Args&&... args); connect(basic_stream& to, Args&&... args);
#else #else
BOOST_BEAST_DECL template<class Executor>
stream basic_stream<Executor>
connect(stream& to); connect(basic_stream<Executor>& to);
BOOST_BEAST_DECL template<class Executor>
void void
connect(stream& s1, stream& s2); connect(basic_stream<Executor>& s1, basic_stream<Executor>& s2);
template<class Arg1, class... ArgN> template<class Executor, class Arg1, class... ArgN>
stream basic_stream<Executor>
connect(stream& to, Arg1&& arg1, ArgN&&... argn); connect(basic_stream<Executor>& to, Arg1&& arg1, ArgN&&... argn);
#endif #endif
using stream = basic_stream<>;
} // test } // test
} // beast } // beast
} // boost } // boost
#include <boost/beast/_experimental/test/impl/stream.hpp> #include <boost/beast/_experimental/test/impl/stream.hpp>
#ifdef BOOST_BEAST_HEADER_ONLY //#ifdef BOOST_BEAST_HEADER_ONLY
#include <boost/beast/_experimental/test/impl/stream.ipp> #include <boost/beast/_experimental/test/impl/stream.ipp>
#endif //#endif
#endif #endif

View File

@@ -28,6 +28,7 @@ the program, with the macro BOOST_BEAST_SEPARATE_COMPILATION defined.
#include <boost/beast/_experimental/test/impl/error.ipp> #include <boost/beast/_experimental/test/impl/error.ipp>
#include <boost/beast/_experimental/test/impl/fail_count.ipp> #include <boost/beast/_experimental/test/impl/fail_count.ipp>
#include <boost/beast/_experimental/test/impl/stream.ipp> #include <boost/beast/_experimental/test/impl/stream.ipp>
#include <boost/beast/_experimental/test/detail/stream_state.ipp>
#include <boost/beast/core/detail/base64.ipp> #include <boost/beast/core/detail/base64.ipp>
#include <boost/beast/core/detail/sha1.ipp> #include <boost/beast/core/detail/sha1.ipp>

View File

@@ -14,7 +14,11 @@
#include <boost/beast/_experimental/test/handler.hpp> #include <boost/beast/_experimental/test/handler.hpp>
#include <boost/asio/ssl/stream.hpp> #include <boost/asio/ssl/stream.hpp>
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
#include <boost/asio/use_awaitable.hpp>
#endif
#define DEF boost::asio::use_future_t
namespace boost { namespace boost {
namespace beast { namespace beast {
@@ -153,6 +157,18 @@ public:
test::stream>::value); test::stream>::value);
} }
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
net::awaitable<void>
testRebind(net::mutable_buffer& b)
{
auto ex = co_await net::this_coro::executor;
auto s1 = test::stream(ex);
auto s2 = net::use_awaitable.as_default_on(std::move(s1));
auto bt = co_await s2.async_read_some(b);
bt = co_await s2.async_write_some(b);
}
#endif
void void
run() override run() override
{ {
@@ -160,6 +176,9 @@ public:
testSharedAbandon(); testSharedAbandon();
//testLifetimeViolation(); //testLifetimeViolation();
boost::ignore_unused(&stream_test::testAsioSSLCompat); boost::ignore_unused(&stream_test::testAsioSSLCompat);
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
boost::ignore_unused(&stream_test::testRebind);
#endif
} }
}; };

View File

@@ -34,12 +34,18 @@ namespace beast {
namespace { namespace {
#if defined(BOOST_ASIO_NO_TS_EXECUTORS) #if defined(BOOST_ASIO_NO_TS_EXECUTORS)
static struct ex1_context : net::execution_context
{
} ex1ctx;
struct ex1_type struct ex1_type
{ {
net::execution_context & net::execution_context &
query(net::execution::context_t c) const noexcept query(net::execution::context_t c) const noexcept
{ return *reinterpret_cast<net::execution_context *>(0); } { return *reinterpret_cast<net::execution_context *>(&ex1ctx); }
net::execution::blocking_t net::execution::blocking_t
query(net::execution::blocking_t) const noexcept query(net::execution::blocking_t) const noexcept
@@ -716,9 +722,9 @@ public:
net::steady_timer timer; net::steady_timer timer;
temporary_data(std::string message_, net::io_context& ctx) temporary_data(std::string message_, net::any_io_executor ex)
: message(std::move(message_)) : message(std::move(message_))
, timer(ctx) , timer(std::move(ex))
{ {
} }
}; };
@@ -733,7 +739,7 @@ public:
, repeats_(repeats) , repeats_(repeats)
, data_(allocate_stable<temporary_data>(*this, , data_(allocate_stable<temporary_data>(*this,
std::move(message), std::move(message),
net::query(stream.get_executor(), net::execution::context))) stream.get_executor()))
{ {
(*this)(); // start the operation (*this)(); // start the operation
} }

View File

@@ -16,7 +16,9 @@
#include <boost/beast/core/bind_handler.hpp> #include <boost/beast/core/bind_handler.hpp>
#include <boost/beast/test/yield_to.hpp> #include <boost/beast/test/yield_to.hpp>
#include <boost/asio/buffer.hpp> #include <boost/asio/buffer.hpp>
#include <boost/asio/execution.hpp>
#include <boost/asio/io_context.hpp> #include <boost/asio/io_context.hpp>
#include <boost/asio/any_io_executor.hpp>
#include <boost/asio/read.hpp> #include <boost/asio/read.hpp>
#include <boost/asio/strand.hpp> #include <boost/asio/strand.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
@@ -41,10 +43,17 @@ public:
buffered_read_stream<test::stream, multi_buffer> srs(ioc); buffered_read_stream<test::stream, multi_buffer> srs(ioc);
buffered_read_stream<test::stream, multi_buffer> srs2(std::move(srs)); buffered_read_stream<test::stream, multi_buffer> srs2(std::move(srs));
srs = std::move(srs2); srs = std::move(srs2);
#if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
BEAST_EXPECT(&srs.get_executor().context() == &ioc);
BEAST_EXPECT(
&srs.get_executor().context() ==
&srs2.get_executor().context());
#else
BEAST_EXPECT(&net::query(srs.get_executor(), net::execution::context) == &ioc); BEAST_EXPECT(&net::query(srs.get_executor(), net::execution::context) == &ioc);
BEAST_EXPECT( BEAST_EXPECT(
&net::query(srs.get_executor(), net::execution::context) == &net::query(srs.get_executor(), net::execution::context) ==
&net::query(srs2.get_executor(), net::execution::context)); &net::query(srs2.get_executor(), net::execution::context));
#endif
} }
{ {
test::stream ts{ioc}; test::stream ts{ioc};

View File

@@ -55,7 +55,9 @@ public:
static static
void void
fail_loop( fail_loop(
std::function<void(stream<test::stream>&)> f, std::function<void(
stream<test::basic_stream<net::io_context::executor_type>>&)>
f,
std::chrono::steady_clock::duration amount = std::chrono::steady_clock::duration amount =
std::chrono::seconds(5)) std::chrono::seconds(5))
{ {
@@ -68,7 +70,8 @@ public:
test::fail_count fc(n); test::fail_count fc(n);
try try
{ {
stream<test::stream> ws(ioc, fc); stream<test::basic_stream<net::io_context::executor_type>>
ws(ioc, fc);
auto tr = connect(ws.next_layer()); auto tr = connect(ws.next_layer());
f(ws); f(ws);
break; break;
@@ -101,7 +104,7 @@ public:
net::io_context ioc; net::io_context ioc;
// request in stream // request in stream
fail_loop([&](stream<test::stream>& ws) fail_loop([&](stream<test::basic_stream<net::io_context::executor_type>>& ws)
{ {
ws.next_layer().append( ws.next_layer().append(
"GET / HTTP/1.1\r\n" "GET / HTTP/1.1\r\n"
@@ -116,7 +119,7 @@ public:
}); });
// request in stream, decorator // request in stream, decorator
fail_loop([&](stream<test::stream>& ws) fail_loop([&](stream<test::basic_stream<net::io_context::executor_type>>& ws)
{ {
ws.next_layer().append( ws.next_layer().append(
"GET / HTTP/1.1\r\n" "GET / HTTP/1.1\r\n"
@@ -135,7 +138,7 @@ public:
}); });
// request in buffers // request in buffers
fail_loop([&](stream<test::stream>& ws) fail_loop([&](stream<test::basic_stream<net::io_context::executor_type>>& ws)
{ {
api.accept(ws, sbuf( api.accept(ws, sbuf(
"GET / HTTP/1.1\r\n" "GET / HTTP/1.1\r\n"
@@ -149,7 +152,7 @@ public:
}); });
// request in buffers, decorator // request in buffers, decorator
fail_loop([&](stream<test::stream>& ws) fail_loop([&](stream<test::basic_stream<net::io_context::executor_type>>& ws)
{ {
bool called = false; bool called = false;
ws.set_option(stream_base::decorator( ws.set_option(stream_base::decorator(
@@ -166,7 +169,7 @@ public:
}); });
// request in buffers and stream // request in buffers and stream
fail_loop([&](stream<test::stream>& ws) fail_loop([&](stream<test::basic_stream<net::io_context::executor_type>>& ws)
{ {
ws.next_layer().append( ws.next_layer().append(
"Connection: upgrade\r\n" "Connection: upgrade\r\n"
@@ -183,7 +186,7 @@ public:
}); });
// request in buffers and stream, decorator // request in buffers and stream, decorator
fail_loop([&](stream<test::stream>& ws) fail_loop([&](stream<test::basic_stream<net::io_context::executor_type>>& ws)
{ {
ws.next_layer().append( ws.next_layer().append(
"Connection: upgrade\r\n" "Connection: upgrade\r\n"
@@ -213,7 +216,7 @@ public:
req.insert(http::field::sec_websocket_key, "dGhlIHNhbXBsZSBub25jZQ=="); req.insert(http::field::sec_websocket_key, "dGhlIHNhbXBsZSBub25jZQ==");
req.insert(http::field::sec_websocket_version, "13"); req.insert(http::field::sec_websocket_version, "13");
fail_loop([&](stream<test::stream>& ws) fail_loop([&](stream<test::basic_stream<net::io_context::executor_type>>& ws)
{ {
api.accept(ws, req); api.accept(ws, req);
}); });
@@ -231,7 +234,7 @@ public:
req.insert(http::field::sec_websocket_key, "dGhlIHNhbXBsZSBub25jZQ=="); req.insert(http::field::sec_websocket_key, "dGhlIHNhbXBsZSBub25jZQ==");
req.insert(http::field::sec_websocket_version, "13"); req.insert(http::field::sec_websocket_version, "13");
fail_loop([&](stream<test::stream>& ws) fail_loop([&](stream<test::basic_stream<net::io_context::executor_type>>& ws)
{ {
bool called = false; bool called = false;
ws.set_option(stream_base::decorator( ws.set_option(stream_base::decorator(
@@ -253,7 +256,7 @@ public:
req.insert(http::field::sec_websocket_key, "dGhlIHNhbXBsZSBub25jZQ=="); req.insert(http::field::sec_websocket_key, "dGhlIHNhbXBsZSBub25jZQ==");
req.insert(http::field::sec_websocket_version, "13"); req.insert(http::field::sec_websocket_version, "13");
fail_loop([&](stream<test::stream>& ws) fail_loop([&](stream<test::basic_stream<net::io_context::executor_type>>& ws)
{ {
ws.next_layer().append("\x88\x82\xff\xff\xff\xff\xfc\x17"); ws.next_layer().append("\x88\x82\xff\xff\xff\xff\xfc\x17");
api.accept(ws, req); api.accept(ws, req);
@@ -272,7 +275,7 @@ public:
} }
// failed handshake (missing Sec-WebSocket-Key) // failed handshake (missing Sec-WebSocket-Key)
fail_loop([&](stream<test::stream>& ws) fail_loop([&](stream<test::basic_stream<net::io_context::executor_type>>& ws)
{ {
ws.next_layer().append( ws.next_layer().append(
"GET / HTTP/1.1\r\n" "GET / HTTP/1.1\r\n"
@@ -311,7 +314,7 @@ public:
// request in stream // request in stream
{ {
stream<test::stream> ws{ioc, stream<test::basic_stream<net::io_context::executor_type>> ws{ioc,
"GET / HTTP/1.1\r\n" "GET / HTTP/1.1\r\n"
"Host: localhost\r\n" "Host: localhost\r\n"
"Upgrade: websocket\r\n" "Upgrade: websocket\r\n"
@@ -337,7 +340,7 @@ public:
// request in stream, decorator // request in stream, decorator
{ {
stream<test::stream> ws{ioc, stream<test::basic_stream<net::io_context::executor_type>> ws{ioc,
"GET / HTTP/1.1\r\n" "GET / HTTP/1.1\r\n"
"Host: localhost\r\n" "Host: localhost\r\n"
"Upgrade: websocket\r\n" "Upgrade: websocket\r\n"
@@ -366,7 +369,7 @@ public:
// request in buffers // request in buffers
{ {
stream<test::stream> ws{ioc}; stream<test::basic_stream<net::io_context::executor_type>> ws{ioc};
auto tr = connect(ws.next_layer()); auto tr = connect(ws.next_layer());
try try
{ {
@@ -392,7 +395,7 @@ public:
// request in buffers, decorator // request in buffers, decorator
{ {
stream<test::stream> ws{ioc}; stream<test::basic_stream<net::io_context::executor_type>> ws{ioc};
auto tr = connect(ws.next_layer()); auto tr = connect(ws.next_layer());
try try
{ {
@@ -420,7 +423,7 @@ public:
// request in buffers and stream // request in buffers and stream
{ {
stream<test::stream> ws{ioc, stream<test::basic_stream<net::io_context::executor_type>> ws{ioc,
"Connection: upgrade\r\n" "Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n" "Sec-WebSocket-Version: 13\r\n"
@@ -446,7 +449,7 @@ public:
// request in buffers and stream, decorator // request in buffers and stream, decorator
{ {
stream<test::stream> ws{ioc, stream<test::basic_stream<net::io_context::executor_type>> ws{ioc,
"Connection: upgrade\r\n" "Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n" "Sec-WebSocket-Version: 13\r\n"
@@ -497,7 +500,7 @@ public:
n = s.size() - 1; n = s.size() - 1;
break; break;
} }
stream<test::stream> ws(ioc); stream<test::basic_stream<net::io_context::executor_type>> ws(ioc);
auto tr = connect(ws.next_layer()); auto tr = connect(ws.next_layer());
ws.next_layer().append( ws.next_layer().append(
s.substr(n, s.size() - n)); s.substr(n, s.size() - n));
@@ -658,7 +661,7 @@ public:
{ {
net::io_context ioc; net::io_context ioc;
{ {
stream<test::stream> ws(ioc); stream<test::basic_stream<net::io_context::executor_type>> ws(ioc);
auto tr = connect(ws.next_layer()); auto tr = connect(ws.next_layer());
tr.close(); tr.close();
try try
@@ -675,7 +678,8 @@ public:
} }
} }
{ {
stream<test::stream> ws(ioc); stream<test::basic_stream<net::io_context::executor_type>>
ws(ioc.get_executor());
auto tr = connect(ws.next_layer()); auto tr = connect(ws.next_layer());
tr.close(); tr.close();
try try
@@ -713,8 +717,8 @@ public:
} }
{ {
stream<test::stream> ws1(ioc); stream<test::basic_stream<net::io_context::executor_type>> ws1(ioc);
stream<test::stream> ws2(ioc); stream<test::basic_stream<net::io_context::executor_type>> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer()); test::connect(ws1.next_layer(), ws2.next_layer());
ws1.async_handshake("test", "/", test::success_handler()); ws1.async_handshake("test", "/", test::success_handler());
@@ -739,8 +743,8 @@ public:
} }
{ {
stream<test::stream> ws1(ioc); stream<test::basic_stream<net::io_context::executor_type>> ws1(ioc);
stream<test::stream> ws2(ioc); stream<test::basic_stream<net::io_context::executor_type>> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer()); test::connect(ws1.next_layer(), ws2.next_layer());
ws1.set_option(stream_base::timeout{ ws1.set_option(stream_base::timeout{
@@ -768,8 +772,8 @@ public:
} }
{ {
stream<test::stream> ws1(ioc); stream<test::basic_stream<net::io_context::executor_type>> ws1(ioc);
stream<test::stream> ws2(ioc); stream<test::basic_stream<net::io_context::executor_type>> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer()); test::connect(ws1.next_layer(), ws2.next_layer());
ws1.set_option(stream_base::timeout{ ws1.set_option(stream_base::timeout{

View File

@@ -35,7 +35,8 @@ protected:
net::io_context ioc_; net::io_context ioc_;
private: private:
detail::select_work_guard_t<net::io_context::executor_type> beast::detail::select_work_guard_t<
net::io_context::executor_type>
work_; work_;
std::vector<std::thread> threads_; std::vector<std::thread> threads_;
std::mutex m_; std::mutex m_;
@@ -49,7 +50,8 @@ public:
explicit explicit
enable_yield_to(std::size_t concurrency = 1) enable_yield_to(std::size_t concurrency = 1)
: work_(detail::make_work_guard(ioc_.get_executor())) : work_(beast::detail::make_work_guard(
ioc_.get_executor()))
{ {
threads_.reserve(concurrency); threads_.reserve(concurrency);
while(concurrency--) while(concurrency--)