forked from boostorg/beast
Refactor websocket::stream operations
This commit is contained in:
1 parent
6af6a8da99
commit
dfd08bf6ae
25 files changed
+1103
-914
No files matched your search
@@ -13,6 +13,7 @@
|
||||
#include <boost/beast/core/async_op_base.hpp>
|
||||
#include <boost/beast/core/bind_handler.hpp>
|
||||
#include <boost/beast/core/stream_traits.hpp>
|
||||
#include <boost/beast/core/detail/bind_continuation.hpp>
|
||||
#include <boost/beast/websocket/detail/frame.hpp>
|
||||
#include <boost/asio/coroutine.hpp>
|
||||
#include <boost/asio/post.hpp>
|
||||
@@ -35,25 +36,8 @@ class stream<NextLayer, deflateSupported>::ping_op
|
||||
Handler, beast::executor_type<stream>>
|
||||
, public net::coroutine
|
||||
{
|
||||
struct state
|
||||
{
|
||||
stream<NextLayer, deflateSupported>& ws;
|
||||
detail::frame_buffer fb;
|
||||
|
||||
state(
|
||||
stream<NextLayer, deflateSupported>& ws_,
|
||||
detail::opcode op,
|
||||
ping_data const& payload)
|
||||
: ws(ws_)
|
||||
{
|
||||
// Serialize the control frame
|
||||
ws.template write_ping<
|
||||
flat_static_buffer_base>(
|
||||
fb, op, payload);
|
||||
}
|
||||
};
|
||||
|
||||
state& d_;
|
||||
stream<NextLayer, deflateSupported>& ws_;
|
||||
detail::frame_buffer& fb_;
|
||||
|
||||
public:
|
||||
static constexpr int id = 3; // for soft_mutex
|
||||
@@ -67,65 +51,51 @@ public:
|
||||
: stable_async_op_base<
|
||||
Handler, beast::executor_type<stream>>(
|
||||
std::forward<Handler_>(h), ws.get_executor())
|
||||
, d_(beast::allocate_stable<state>(
|
||||
*this, ws, op, payload))
|
||||
, ws_(ws)
|
||||
, fb_(beast::allocate_stable<
|
||||
detail::frame_buffer>(*this))
|
||||
{
|
||||
// Serialize the ping or pong frame
|
||||
ws.template write_ping<
|
||||
flat_static_buffer_base>(fb_, op, payload);
|
||||
(*this)({}, 0, false);
|
||||
}
|
||||
|
||||
void operator()(
|
||||
error_code ec = {},
|
||||
std::size_t bytes_transferred = 0)
|
||||
std::size_t bytes_transferred = 0,
|
||||
bool cont = true)
|
||||
{
|
||||
boost::ignore_unused(bytes_transferred);
|
||||
|
||||
auto& impl = *ws_.impl_;
|
||||
BOOST_ASIO_CORO_REENTER(*this)
|
||||
{
|
||||
// Maybe suspend
|
||||
if(d_.ws.impl_->wr_block.try_lock(this))
|
||||
// Acquire the write lock
|
||||
if(! impl.wr_block.try_lock(this))
|
||||
{
|
||||
// Make sure the stream is open
|
||||
if(! d_.ws.impl_->check_open(ec))
|
||||
{
|
||||
BOOST_ASIO_CORO_YIELD
|
||||
net::post(
|
||||
d_.ws.get_executor(),
|
||||
beast::bind_front_handler(std::move(*this), ec));
|
||||
goto upcall;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Suspend
|
||||
BOOST_ASIO_CORO_YIELD
|
||||
d_.ws.impl_->paused_ping.emplace(std::move(*this));
|
||||
|
||||
// Acquire the write block
|
||||
d_.ws.impl_->wr_block.lock(this);
|
||||
|
||||
// Resume
|
||||
impl.paused_ping.emplace(std::move(*this));
|
||||
impl.wr_block.lock(this);
|
||||
BOOST_ASIO_CORO_YIELD
|
||||
net::post(
|
||||
d_.ws.get_executor(), std::move(*this));
|
||||
BOOST_ASSERT(d_.ws.impl_->wr_block.is_locked(this));
|
||||
|
||||
// Make sure the stream is open
|
||||
if(! d_.ws.impl_->check_open(ec))
|
||||
goto upcall;
|
||||
net::post(std::move(*this));
|
||||
BOOST_ASSERT(impl.wr_block.is_locked(this));
|
||||
}
|
||||
if(impl.check_stop_now(ec))
|
||||
goto upcall;
|
||||
|
||||
// Send ping frame
|
||||
BOOST_ASIO_CORO_YIELD
|
||||
net::async_write(d_.ws.impl_->stream,
|
||||
d_.fb.data(), std::move(*this));
|
||||
if(! d_.ws.impl_->check_ok(ec))
|
||||
net::async_write(impl.stream, fb_.data(),
|
||||
beast::detail::bind_continuation(std::move(*this)));
|
||||
if(impl.check_stop_now(ec))
|
||||
goto upcall;
|
||||
|
||||
upcall:
|
||||
d_.ws.impl_->wr_block.unlock(this);
|
||||
d_.ws.impl_->paused_close.maybe_invoke() ||
|
||||
d_.ws.impl_->paused_rd.maybe_invoke() ||
|
||||
d_.ws.impl_->paused_wr.maybe_invoke();
|
||||
this->invoke_now(ec);
|
||||
impl.wr_block.unlock(this);
|
||||
impl.paused_close.maybe_invoke()
|
||||
|| impl.paused_rd.maybe_invoke()
|
||||
|| impl.paused_wr.maybe_invoke();
|
||||
this->invoke(cont, ec);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -148,14 +118,13 @@ void
|
||||
stream<NextLayer, deflateSupported>::
|
||||
ping(ping_data const& payload, error_code& ec)
|
||||
{
|
||||
// Make sure the stream is open
|
||||
if(! impl_->check_open(ec))
|
||||
if(impl_->check_stop_now(ec))
|
||||
return;
|
||||
detail::frame_buffer fb;
|
||||
write_ping<flat_static_buffer_base>(
|
||||
fb, detail::opcode::ping, payload);
|
||||
net::write(impl_->stream, fb.data(), ec);
|
||||
if(! impl_->check_ok(ec))
|
||||
if(impl_->check_stop_now(ec))
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -175,14 +144,13 @@ void
|
||||
stream<NextLayer, deflateSupported>::
|
||||
pong(ping_data const& payload, error_code& ec)
|
||||
{
|
||||
// Make sure the stream is open
|
||||
if(! impl_->check_open(ec))
|
||||
if(impl_->check_stop_now(ec))
|
||||
return;
|
||||
detail::frame_buffer fb;
|
||||
write_ping<flat_static_buffer_base>(
|
||||
fb, detail::opcode::pong, payload);
|
||||
net::write(impl_->stream, fb.data(), ec);
|
||||
if(! impl_->check_ok(ec))
|
||||
if(impl_->check_stop_now(ec))
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -198,9 +166,9 @@ async_ping(ping_data const& payload, WriteHandler&& handler)
|
||||
BOOST_BEAST_HANDLER_INIT(
|
||||
WriteHandler, void(error_code));
|
||||
ping_op<BOOST_ASIO_HANDLER_TYPE(
|
||||
WriteHandler, void(error_code))>{
|
||||
WriteHandler, void(error_code))>(
|
||||
std::move(init.completion_handler), *this,
|
||||
detail::opcode::ping, payload}();
|
||||
detail::opcode::ping, payload);
|
||||
return init.result.get();
|
||||
}
|
||||
|
||||
@@ -216,9 +184,9 @@ async_pong(ping_data const& payload, WriteHandler&& handler)
|
||||
BOOST_BEAST_HANDLER_INIT(
|
||||
WriteHandler, void(error_code));
|
||||
ping_op<BOOST_ASIO_HANDLER_TYPE(
|
||||
WriteHandler, void(error_code))>{
|
||||
WriteHandler, void(error_code))>(
|
||||
std::move(init.completion_handler), *this,
|
||||
detail::opcode::pong, payload}();
|
||||
detail::opcode::pong, payload);
|
||||
return init.result.get();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user