mirror of
https://github.com/boostorg/beast.git
synced 2025-07-31 13:27:33 +02:00
Refactor write_op
This commit is contained in:
@ -7,6 +7,7 @@ WebSocket:
|
|||||||
|
|
||||||
* websocket test improvements
|
* websocket test improvements
|
||||||
* Remove obsolete write_op
|
* Remove obsolete write_op
|
||||||
|
* Refactor write_op
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#include <boost/beast/core/detail/clamp.hpp>
|
#include <boost/beast/core/detail/clamp.hpp>
|
||||||
#include <boost/beast/core/detail/config.hpp>
|
#include <boost/beast/core/detail/config.hpp>
|
||||||
#include <boost/beast/websocket/detail/frame.hpp>
|
#include <boost/beast/websocket/detail/frame.hpp>
|
||||||
|
#include <boost/asio/coroutine.hpp>
|
||||||
#include <boost/asio/handler_alloc_hook.hpp>
|
#include <boost/asio/handler_alloc_hook.hpp>
|
||||||
#include <boost/asio/handler_continuation_hook.hpp>
|
#include <boost/asio/handler_continuation_hook.hpp>
|
||||||
#include <boost/asio/handler_invoke_hook.hpp>
|
#include <boost/asio/handler_invoke_hook.hpp>
|
||||||
@ -36,55 +37,54 @@ namespace websocket {
|
|||||||
template<class NextLayer>
|
template<class NextLayer>
|
||||||
template<class Buffers, class Handler>
|
template<class Buffers, class Handler>
|
||||||
class stream<NextLayer>::write_some_op
|
class stream<NextLayer>::write_some_op
|
||||||
|
: public boost::asio::coroutine
|
||||||
{
|
{
|
||||||
struct data : op
|
Handler h_;
|
||||||
{
|
stream<NextLayer>& ws_;
|
||||||
bool cont;
|
consuming_buffers<Buffers> cb_;
|
||||||
stream<NextLayer>& ws;
|
detail::frame_header fh_;
|
||||||
consuming_buffers<Buffers> cb;
|
detail::prepared_key key_;
|
||||||
bool fin;
|
std::size_t remain_;
|
||||||
detail::frame_header fh;
|
token tok_;
|
||||||
detail::fh_streambuf fh_buf;
|
int how_;
|
||||||
detail::prepared_key key;
|
bool fin_;
|
||||||
std::uint64_t remain;
|
bool more_;
|
||||||
int step = 0;
|
|
||||||
int entry_state;
|
|
||||||
token tok;
|
|
||||||
|
|
||||||
data(Handler& handler, stream<NextLayer>& ws_,
|
|
||||||
bool fin_, Buffers const& bs)
|
|
||||||
: ws(ws_)
|
|
||||||
, cb(bs)
|
|
||||||
, fin(fin_)
|
|
||||||
, tok(ws.t_.unique())
|
|
||||||
{
|
|
||||||
using boost::asio::asio_handler_is_continuation;
|
|
||||||
cont = asio_handler_is_continuation(std::addressof(handler));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
handler_ptr<data, Handler> d_;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
write_some_op(write_some_op&&) = default;
|
write_some_op(write_some_op&&) = default;
|
||||||
write_some_op(write_some_op const&) = default;
|
write_some_op(write_some_op const&) = default;
|
||||||
|
|
||||||
template<class DeducedHandler, class... Args>
|
template<class DeducedHandler>
|
||||||
write_some_op(DeducedHandler&& h,
|
write_some_op(
|
||||||
stream<NextLayer>& ws, Args&&... args)
|
DeducedHandler&& h,
|
||||||
: d_(std::forward<DeducedHandler>(h),
|
stream<NextLayer>& ws,
|
||||||
ws, std::forward<Args>(args)...)
|
bool fin,
|
||||||
|
Buffers const& bs)
|
||||||
|
: h_(std::forward<DeducedHandler>(h))
|
||||||
|
, ws_(ws)
|
||||||
|
, cb_(bs)
|
||||||
|
, tok_(ws_.t_.unique())
|
||||||
|
, fin_(fin)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator()()
|
Handler&
|
||||||
|
handler()
|
||||||
{
|
{
|
||||||
(*this)({}, 0, true);
|
return h_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator()(error_code ec,
|
void operator()(
|
||||||
|
error_code ec,
|
||||||
std::size_t bytes_transferred,
|
std::size_t bytes_transferred,
|
||||||
bool again = true);
|
bool)
|
||||||
|
{
|
||||||
|
(*this)(ec, bytes_transferred);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator()(
|
||||||
|
error_code ec = {},
|
||||||
|
std::size_t bytes_transferred = 0);
|
||||||
|
|
||||||
friend
|
friend
|
||||||
void* asio_handler_allocate(
|
void* asio_handler_allocate(
|
||||||
@ -92,7 +92,7 @@ public:
|
|||||||
{
|
{
|
||||||
using boost::asio::asio_handler_allocate;
|
using boost::asio::asio_handler_allocate;
|
||||||
return asio_handler_allocate(
|
return asio_handler_allocate(
|
||||||
size, std::addressof(op->d_.handler()));
|
size, std::addressof(op->h_));
|
||||||
}
|
}
|
||||||
|
|
||||||
friend
|
friend
|
||||||
@ -101,13 +101,15 @@ public:
|
|||||||
{
|
{
|
||||||
using boost::asio::asio_handler_deallocate;
|
using boost::asio::asio_handler_deallocate;
|
||||||
asio_handler_deallocate(
|
asio_handler_deallocate(
|
||||||
p, size, std::addressof(op->d_.handler()));
|
p, size, std::addressof(op->h_));
|
||||||
}
|
}
|
||||||
|
|
||||||
friend
|
friend
|
||||||
bool asio_handler_is_continuation(write_some_op* op)
|
bool asio_handler_is_continuation(write_some_op* op)
|
||||||
{
|
{
|
||||||
return op->d_->cont;
|
using boost::asio::asio_handler_is_continuation;
|
||||||
|
return asio_handler_is_continuation(
|
||||||
|
std::addressof(op->h_));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Function>
|
template<class Function>
|
||||||
@ -116,7 +118,7 @@ public:
|
|||||||
{
|
{
|
||||||
using boost::asio::asio_handler_invoke;
|
using boost::asio::asio_handler_invoke;
|
||||||
asio_handler_invoke(
|
asio_handler_invoke(
|
||||||
f, std::addressof(op->d_.handler()));
|
f, std::addressof(op->h_));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -126,386 +128,407 @@ void
|
|||||||
stream<NextLayer>::
|
stream<NextLayer>::
|
||||||
write_some_op<Buffers, Handler>::
|
write_some_op<Buffers, Handler>::
|
||||||
operator()(error_code ec,
|
operator()(error_code ec,
|
||||||
std::size_t bytes_transferred, bool again)
|
std::size_t bytes_transferred)
|
||||||
{
|
{
|
||||||
using beast::detail::clamp;
|
using beast::detail::clamp;
|
||||||
using boost::asio::buffer;
|
using boost::asio::buffer;
|
||||||
using boost::asio::buffer_copy;
|
using boost::asio::buffer_copy;
|
||||||
using boost::asio::buffer_size;
|
using boost::asio::buffer_size;
|
||||||
|
using boost::asio::mutable_buffers_1;
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
do_init = 0,
|
do_nomask_nofrag,
|
||||||
do_nomask_nofrag = 20,
|
do_nomask_frag,
|
||||||
do_nomask_frag = 30,
|
do_mask_nofrag,
|
||||||
do_mask_nofrag = 40,
|
do_mask_frag,
|
||||||
do_mask_frag = 50,
|
do_deflate
|
||||||
do_deflate = 60,
|
|
||||||
do_maybe_suspend = 80,
|
|
||||||
do_upcall = 99
|
|
||||||
};
|
};
|
||||||
auto& d = *d_;
|
std::size_t n;
|
||||||
d.cont = d.cont || again;
|
boost::asio::mutable_buffer b;
|
||||||
|
|
||||||
|
BOOST_ASIO_CORO_REENTER(*this)
|
||||||
|
{
|
||||||
|
// Set up the outgoing frame header
|
||||||
|
if(! ws_.wr_.cont)
|
||||||
|
{
|
||||||
|
ws_.wr_begin();
|
||||||
|
fh_.rsv1 = ws_.wr_.compress;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fh_.rsv1 = false;
|
||||||
|
}
|
||||||
|
fh_.rsv2 = false;
|
||||||
|
fh_.rsv3 = false;
|
||||||
|
fh_.op = ws_.wr_.cont ?
|
||||||
|
detail::opcode::cont : ws_.wr_opcode_;
|
||||||
|
fh_.mask =
|
||||||
|
ws_.role_ == role_type::client;
|
||||||
|
|
||||||
|
// Choose a write algorithm
|
||||||
|
if(ws_.wr_.compress)
|
||||||
|
{
|
||||||
|
how_ = do_deflate;
|
||||||
|
}
|
||||||
|
else if(! fh_.mask)
|
||||||
|
{
|
||||||
|
if(! ws_.wr_.autofrag)
|
||||||
|
{
|
||||||
|
how_ = do_nomask_nofrag;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BOOST_ASSERT(ws_.wr_.buf_size != 0);
|
||||||
|
remain_ = buffer_size(cb_);
|
||||||
|
if(remain_ > ws_.wr_.buf_size)
|
||||||
|
how_ = do_nomask_frag;
|
||||||
|
else
|
||||||
|
how_ = do_nomask_nofrag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(! ws_.wr_.autofrag)
|
||||||
|
{
|
||||||
|
how_ = do_mask_nofrag;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BOOST_ASSERT(ws_.wr_.buf_size != 0);
|
||||||
|
remain_ = buffer_size(cb_);
|
||||||
|
if(remain_ > ws_.wr_.buf_size)
|
||||||
|
how_ = do_mask_frag;
|
||||||
|
else
|
||||||
|
how_ = do_mask_nofrag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
do_maybe_suspend:
|
||||||
|
// Maybe suspend
|
||||||
|
if(! ws_.wr_block_)
|
||||||
|
{
|
||||||
|
// Acquire the write block
|
||||||
|
ws_.wr_block_ = tok_;
|
||||||
|
|
||||||
|
// Make sure the stream is open
|
||||||
|
if(ws_.failed_)
|
||||||
|
{
|
||||||
|
BOOST_ASIO_CORO_YIELD
|
||||||
|
ws_.get_io_service().post(
|
||||||
|
bind_handler(std::move(*this),
|
||||||
|
boost::asio::error::operation_aborted));
|
||||||
|
goto upcall;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Suspend
|
||||||
|
BOOST_ASSERT(ws_.wr_block_ != tok_);
|
||||||
|
BOOST_ASIO_CORO_YIELD
|
||||||
|
ws_.wr_op_.save(std::move(*this));
|
||||||
|
|
||||||
|
// Acquire the write block
|
||||||
|
BOOST_ASSERT(! ws_.wr_block_);
|
||||||
|
ws_.wr_block_ = tok_;
|
||||||
|
|
||||||
|
// Resume
|
||||||
|
BOOST_ASIO_CORO_YIELD
|
||||||
|
ws_.get_io_service().post(std::move(*this));
|
||||||
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
|
|
||||||
|
// Make sure the stream is open
|
||||||
|
if(ws_.failed_)
|
||||||
|
{
|
||||||
|
ec = boost::asio::error::operation_aborted;
|
||||||
|
goto upcall;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------
|
||||||
|
|
||||||
|
if(how_ == do_nomask_nofrag)
|
||||||
|
{
|
||||||
|
fh_.fin = fin_;
|
||||||
|
fh_.len = buffer_size(cb_);
|
||||||
|
ws_.wr_.fb.reset();
|
||||||
|
detail::write<flat_static_buffer_base>(
|
||||||
|
ws_.wr_.fb, fh_);
|
||||||
|
ws_.wr_.cont = ! fin_;
|
||||||
|
// Send frame
|
||||||
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
|
BOOST_ASIO_CORO_YIELD
|
||||||
|
boost::asio::async_write(ws_.stream_,
|
||||||
|
buffer_cat(ws_.wr_.fb.data(), cb_),
|
||||||
|
std::move(*this));
|
||||||
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
|
if(ec)
|
||||||
|
ws_.failed_ = true;
|
||||||
|
goto upcall;
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------
|
||||||
|
|
||||||
|
else if(how_ == do_nomask_frag)
|
||||||
|
{
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
fh_.len = clamp(remain_, ws_.wr_.buf_size);
|
||||||
|
remain_ -= clamp(fh_.len);
|
||||||
|
fh_.fin = fin_ ? remain_ == 0 : false;
|
||||||
|
ws_.wr_.fb.reset();
|
||||||
|
detail::write<flat_static_buffer_base>(
|
||||||
|
ws_.wr_.fb, fh_);
|
||||||
|
ws_.wr_.cont = ! fin_;
|
||||||
|
// Send frame
|
||||||
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
|
BOOST_ASIO_CORO_YIELD
|
||||||
|
boost::asio::async_write(
|
||||||
|
ws_.stream_, buffer_cat(
|
||||||
|
ws_.wr_.fb.data(), buffer_prefix(
|
||||||
|
clamp(fh_.len), cb_)),
|
||||||
|
std::move(*this));
|
||||||
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
if(ec)
|
if(ec)
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
ws_.failed_ = true;
|
||||||
d.ws.failed_ = true;
|
|
||||||
goto upcall;
|
goto upcall;
|
||||||
}
|
}
|
||||||
loop:
|
if(remain_ == 0)
|
||||||
switch(d.step)
|
goto upcall;
|
||||||
{
|
cb_.consume(
|
||||||
case do_init:
|
bytes_transferred - ws_.wr_.fb.size());
|
||||||
if(! d.ws.wr_.cont)
|
fh_.op = detail::opcode::cont;
|
||||||
{
|
|
||||||
d.ws.wr_begin();
|
|
||||||
d.fh.rsv1 = d.ws.wr_.compress;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
d.fh.rsv1 = false;
|
|
||||||
}
|
|
||||||
d.fh.rsv2 = false;
|
|
||||||
d.fh.rsv3 = false;
|
|
||||||
d.fh.op = d.ws.wr_.cont ?
|
|
||||||
detail::opcode::cont : d.ws.wr_opcode_;
|
|
||||||
d.fh.mask =
|
|
||||||
d.ws.role_ == role_type::client;
|
|
||||||
|
|
||||||
// entry_state determines which algorithm
|
|
||||||
// we will use to send. If we suspend, we
|
|
||||||
// will transition to entry_state + 1 on
|
|
||||||
// the resume.
|
|
||||||
if(d.ws.wr_.compress)
|
|
||||||
{
|
|
||||||
d.entry_state = do_deflate;
|
|
||||||
}
|
|
||||||
else if(! d.fh.mask)
|
|
||||||
{
|
|
||||||
if(! d.ws.wr_.autofrag)
|
|
||||||
{
|
|
||||||
d.entry_state = do_nomask_nofrag;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
BOOST_ASSERT(d.ws.wr_.buf_size != 0);
|
|
||||||
d.remain = buffer_size(d.cb);
|
|
||||||
if(d.remain > d.ws.wr_.buf_size)
|
|
||||||
d.entry_state = do_nomask_frag;
|
|
||||||
else
|
|
||||||
d.entry_state = do_nomask_nofrag;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(! d.ws.wr_.autofrag)
|
|
||||||
{
|
|
||||||
d.entry_state = do_mask_nofrag;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
BOOST_ASSERT(d.ws.wr_.buf_size != 0);
|
|
||||||
d.remain = buffer_size(d.cb);
|
|
||||||
if(d.remain > d.ws.wr_.buf_size)
|
|
||||||
d.entry_state = do_mask_frag;
|
|
||||||
else
|
|
||||||
d.entry_state = do_mask_nofrag;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
d.step = do_maybe_suspend;
|
|
||||||
goto loop;
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
|
||||||
|
|
||||||
case do_nomask_nofrag:
|
|
||||||
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
|
||||||
d.fh.fin = d.fin;
|
|
||||||
d.fh.len = buffer_size(d.cb);
|
|
||||||
detail::write<flat_static_buffer_base>(
|
|
||||||
d.fh_buf, d.fh);
|
|
||||||
d.ws.wr_.cont = ! d.fin;
|
|
||||||
// Send frame
|
|
||||||
d.step = do_upcall;
|
|
||||||
return boost::asio::async_write(d.ws.stream_,
|
|
||||||
buffer_cat(d.fh_buf.data(), d.cb),
|
|
||||||
std::move(*this));
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
|
||||||
|
|
||||||
go_nomask_frag:
|
|
||||||
case do_nomask_frag:
|
|
||||||
{
|
|
||||||
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
|
||||||
auto const n = clamp(
|
|
||||||
d.remain, d.ws.wr_.buf_size);
|
|
||||||
d.remain -= n;
|
|
||||||
d.fh.len = n;
|
|
||||||
d.fh.fin = d.fin ? d.remain == 0 : false;
|
|
||||||
detail::write<flat_static_buffer_base>(
|
|
||||||
d.fh_buf, d.fh);
|
|
||||||
d.ws.wr_.cont = ! d.fin;
|
|
||||||
// Send frame
|
|
||||||
d.step = d.remain == 0 ?
|
|
||||||
do_upcall : do_nomask_frag + 1;
|
|
||||||
return boost::asio::async_write(
|
|
||||||
d.ws.stream_, buffer_cat(
|
|
||||||
d.fh_buf.data(), buffer_prefix(
|
|
||||||
n, d.cb)), std::move(*this));
|
|
||||||
}
|
|
||||||
|
|
||||||
case do_nomask_frag + 1:
|
|
||||||
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
|
||||||
d.ws.wr_block_.reset();
|
|
||||||
d.cb.consume(
|
|
||||||
bytes_transferred - d.fh_buf.size());
|
|
||||||
d.fh_buf.consume(d.fh_buf.size());
|
|
||||||
d.fh.op = detail::opcode::cont;
|
|
||||||
// Allow outgoing control frames to
|
// Allow outgoing control frames to
|
||||||
// be sent in between message frames
|
// be sent in between message frames
|
||||||
if( d.ws.close_op_.maybe_invoke() ||
|
ws_.wr_block_.reset();
|
||||||
d.ws.rd_op_.maybe_invoke() ||
|
if( ws_.close_op_.maybe_invoke() ||
|
||||||
d.ws.ping_op_.maybe_invoke())
|
ws_.rd_op_.maybe_invoke() ||
|
||||||
|
ws_.ping_op_.maybe_invoke())
|
||||||
{
|
{
|
||||||
d.step = do_maybe_suspend;
|
BOOST_ASIO_CORO_YIELD
|
||||||
return d.ws.get_io_service().post(
|
ws_.get_io_service().post(
|
||||||
std::move(*this));
|
std::move(*this));
|
||||||
|
goto do_maybe_suspend;
|
||||||
|
}
|
||||||
|
ws_.wr_block_ = tok_;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
d.ws.wr_block_ = d.tok;
|
|
||||||
goto go_nomask_frag;
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
//------------------------------------------------------------------
|
||||||
|
|
||||||
case do_mask_nofrag:
|
else if(how_ == do_mask_nofrag)
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
remain_ = buffer_size(cb_);
|
||||||
d.remain = buffer_size(d.cb);
|
fh_.fin = fin_;
|
||||||
d.fh.fin = d.fin;
|
fh_.len = remain_;
|
||||||
d.fh.len = d.remain;
|
fh_.key = ws_.maskgen_();
|
||||||
d.fh.key = d.ws.maskgen_();
|
detail::prepare_key(key_, fh_.key);
|
||||||
detail::prepare_key(d.key, d.fh.key);
|
ws_.wr_.fb.reset();
|
||||||
detail::write<flat_static_buffer_base>(
|
detail::write<flat_static_buffer_base>(
|
||||||
d.fh_buf, d.fh);
|
ws_.wr_.fb, fh_);
|
||||||
auto const n =
|
n = clamp(remain_, ws_.wr_.buf_size);
|
||||||
clamp(d.remain, d.ws.wr_.buf_size);
|
buffer_copy(buffer(
|
||||||
auto const b =
|
ws_.wr_.buf.get(), n), cb_);
|
||||||
buffer(d.ws.wr_.buf.get(), n);
|
detail::mask_inplace(buffer(
|
||||||
buffer_copy(b, d.cb);
|
ws_.wr_.buf.get(), n), key_);
|
||||||
detail::mask_inplace(b, d.key);
|
remain_ -= n;
|
||||||
d.remain -= n;
|
ws_.wr_.cont = ! fin_;
|
||||||
d.ws.wr_.cont = ! d.fin;
|
|
||||||
// Send frame header and partial payload
|
// Send frame header and partial payload
|
||||||
d.step = d.remain == 0 ?
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
do_upcall : do_mask_nofrag + 1;
|
BOOST_ASIO_CORO_YIELD
|
||||||
return boost::asio::async_write(
|
boost::asio::async_write(
|
||||||
d.ws.stream_, buffer_cat(d.fh_buf.data(),
|
ws_.stream_, buffer_cat(ws_.wr_.fb.data(),
|
||||||
b), std::move(*this));
|
buffer(ws_.wr_.buf.get(), n)),
|
||||||
}
|
|
||||||
|
|
||||||
case do_mask_nofrag + 1:
|
|
||||||
{
|
|
||||||
d.cb.consume(d.ws.wr_.buf_size);
|
|
||||||
auto const n =
|
|
||||||
clamp(d.remain, d.ws.wr_.buf_size);
|
|
||||||
auto const b =
|
|
||||||
buffer(d.ws.wr_.buf.get(), n);
|
|
||||||
buffer_copy(b, d.cb);
|
|
||||||
detail::mask_inplace(b, d.key);
|
|
||||||
d.remain -= n;
|
|
||||||
// Send partial payload
|
|
||||||
if(d.remain == 0)
|
|
||||||
d.step = do_upcall;
|
|
||||||
return boost::asio::async_write(
|
|
||||||
d.ws.stream_, b, std::move(*this));
|
|
||||||
}
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
|
||||||
|
|
||||||
go_mask_frag:
|
|
||||||
case do_mask_frag:
|
|
||||||
{
|
|
||||||
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
|
||||||
auto const n = clamp(
|
|
||||||
d.remain, d.ws.wr_.buf_size);
|
|
||||||
d.remain -= n;
|
|
||||||
d.fh.len = n;
|
|
||||||
d.fh.key = d.ws.maskgen_();
|
|
||||||
d.fh.fin = d.fin ? d.remain == 0 : false;
|
|
||||||
detail::prepare_key(d.key, d.fh.key);
|
|
||||||
auto const b = buffer(
|
|
||||||
d.ws.wr_.buf.get(), n);
|
|
||||||
buffer_copy(b, d.cb);
|
|
||||||
detail::mask_inplace(b, d.key);
|
|
||||||
detail::write<flat_static_buffer_base>(
|
|
||||||
d.fh_buf, d.fh);
|
|
||||||
d.ws.wr_.cont = ! d.fin;
|
|
||||||
// Send frame
|
|
||||||
d.step = d.remain == 0 ?
|
|
||||||
do_upcall : do_mask_frag + 1;
|
|
||||||
return boost::asio::async_write(
|
|
||||||
d.ws.stream_, buffer_cat(
|
|
||||||
d.fh_buf.data(), b),
|
|
||||||
std::move(*this));
|
std::move(*this));
|
||||||
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
|
if(ec)
|
||||||
|
{
|
||||||
|
ws_.failed_ = true;
|
||||||
|
goto upcall;
|
||||||
|
}
|
||||||
|
while(remain_ > 0)
|
||||||
|
{
|
||||||
|
cb_.consume(ws_.wr_.buf_size);
|
||||||
|
n = clamp(remain_, ws_.wr_.buf_size);
|
||||||
|
buffer_copy(buffer(
|
||||||
|
ws_.wr_.buf.get(), n), cb_);
|
||||||
|
detail::mask_inplace(buffer(
|
||||||
|
ws_.wr_.buf.get(), n), key_);
|
||||||
|
remain_ -= n;
|
||||||
|
// Send partial payload
|
||||||
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
|
BOOST_ASIO_CORO_YIELD
|
||||||
|
boost::asio::async_write(ws_.stream_,
|
||||||
|
buffer(ws_.wr_.buf.get(), n),
|
||||||
|
std::move(*this));
|
||||||
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
|
if(ec)
|
||||||
|
{
|
||||||
|
ws_.failed_ = true;
|
||||||
|
goto upcall;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
goto upcall;
|
||||||
}
|
}
|
||||||
|
|
||||||
case do_mask_frag + 1:
|
//------------------------------------------------------------------
|
||||||
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
|
||||||
d.ws.wr_block_.reset();
|
else if(how_ == do_mask_frag)
|
||||||
d.cb.consume(
|
{
|
||||||
bytes_transferred - d.fh_buf.size());
|
for(;;)
|
||||||
d.fh_buf.consume(d.fh_buf.size());
|
{
|
||||||
d.fh.op = detail::opcode::cont;
|
n = clamp(remain_, ws_.wr_.buf_size);
|
||||||
|
remain_ -= n;
|
||||||
|
fh_.len = n;
|
||||||
|
fh_.key = ws_.maskgen_();
|
||||||
|
fh_.fin = fin_ ? remain_ == 0 : false;
|
||||||
|
detail::prepare_key(key_, fh_.key);
|
||||||
|
buffer_copy(buffer(
|
||||||
|
ws_.wr_.buf.get(), n), cb_);
|
||||||
|
detail::mask_inplace(buffer(
|
||||||
|
ws_.wr_.buf.get(), n), key_);
|
||||||
|
ws_.wr_.fb.reset();
|
||||||
|
detail::write<flat_static_buffer_base>(
|
||||||
|
ws_.wr_.fb, fh_);
|
||||||
|
ws_.wr_.cont = ! fin_;
|
||||||
|
// Send frame
|
||||||
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
|
BOOST_ASIO_CORO_YIELD
|
||||||
|
boost::asio::async_write(ws_.stream_,
|
||||||
|
buffer_cat(ws_.wr_.fb.data(),
|
||||||
|
buffer(ws_.wr_.buf.get(), n)),
|
||||||
|
std::move(*this));
|
||||||
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
|
if(ec)
|
||||||
|
{
|
||||||
|
ws_.failed_ = true;
|
||||||
|
goto upcall;
|
||||||
|
}
|
||||||
|
if(remain_ == 0)
|
||||||
|
goto upcall;
|
||||||
|
cb_.consume(
|
||||||
|
bytes_transferred - ws_.wr_.fb.size());
|
||||||
|
fh_.op = detail::opcode::cont;
|
||||||
// Allow outgoing control frames to
|
// Allow outgoing control frames to
|
||||||
// be sent in between message frames:
|
// be sent in between message frames:
|
||||||
if( d.ws.close_op_.maybe_invoke() ||
|
ws_.wr_block_.reset();
|
||||||
d.ws.rd_op_.maybe_invoke() ||
|
if( ws_.close_op_.maybe_invoke() ||
|
||||||
d.ws.ping_op_.maybe_invoke())
|
ws_.rd_op_.maybe_invoke() ||
|
||||||
|
ws_.ping_op_.maybe_invoke())
|
||||||
{
|
{
|
||||||
d.step = do_maybe_suspend;
|
BOOST_ASIO_CORO_YIELD
|
||||||
d.ws.get_io_service().post(
|
ws_.get_io_service().post(
|
||||||
std::move(*this));
|
std::move(*this));
|
||||||
return;
|
goto do_maybe_suspend;
|
||||||
|
}
|
||||||
|
ws_.wr_block_ = tok_;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
d.ws.wr_block_ = d.tok;
|
|
||||||
goto go_mask_frag;
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
//------------------------------------------------------------------
|
||||||
|
|
||||||
go_deflate:
|
else if(how_ == do_deflate)
|
||||||
case do_deflate:
|
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
for(;;)
|
||||||
auto b = buffer(d.ws.wr_.buf.get(),
|
{
|
||||||
d.ws.wr_.buf_size);
|
b = buffer(ws_.wr_.buf.get(),
|
||||||
auto const more = detail::deflate(
|
ws_.wr_.buf_size);
|
||||||
d.ws.pmd_->zo, b, d.cb, d.fin, ec);
|
more_ = detail::deflate(
|
||||||
d.ws.failed_ = !!ec;
|
ws_.pmd_->zo, b, cb_, fin_, ec);
|
||||||
if(d.ws.failed_)
|
ws_.failed_ = !!ec;
|
||||||
|
if(ws_.failed_)
|
||||||
|
{
|
||||||
|
// Always dispatching is easiest
|
||||||
|
BOOST_ASIO_CORO_YIELD
|
||||||
|
ws_.get_io_service().post(
|
||||||
|
bind_handler(std::move(*this), ec));
|
||||||
goto upcall;
|
goto upcall;
|
||||||
auto const n = buffer_size(b);
|
}
|
||||||
|
n = buffer_size(b);
|
||||||
if(n == 0)
|
if(n == 0)
|
||||||
{
|
{
|
||||||
// The input was consumed, but there
|
// The input was consumed, but there
|
||||||
// is no output due to compression
|
// is no output due to compression
|
||||||
// latency.
|
// latency.
|
||||||
BOOST_ASSERT(! d.fin);
|
BOOST_ASSERT(! fin_);
|
||||||
BOOST_ASSERT(buffer_size(d.cb) == 0);
|
BOOST_ASSERT(buffer_size(cb_) == 0);
|
||||||
|
|
||||||
// We can skip the dispatch if the
|
// We can skip the dispatch if the
|
||||||
// asynchronous initiation function is
|
// asynchronous initiation function is
|
||||||
// not on call stack but its hard to
|
// not on call stack but its hard to
|
||||||
// figure out so be safe and dispatch.
|
// figure out so be safe and dispatch.
|
||||||
d.step = do_upcall;
|
BOOST_ASIO_CORO_YIELD
|
||||||
d.ws.get_io_service().post(std::move(*this));
|
ws_.get_io_service().post(
|
||||||
return;
|
std::move(*this));
|
||||||
|
goto upcall;
|
||||||
}
|
}
|
||||||
if(d.fh.mask)
|
if(fh_.mask)
|
||||||
{
|
{
|
||||||
d.fh.key = d.ws.maskgen_();
|
fh_.key = ws_.maskgen_();
|
||||||
detail::prepared_key key;
|
detail::prepared_key key;
|
||||||
detail::prepare_key(key, d.fh.key);
|
detail::prepare_key(key, fh_.key);
|
||||||
detail::mask_inplace(b, key);
|
detail::mask_inplace(b, key);
|
||||||
}
|
}
|
||||||
d.fh.fin = ! more;
|
fh_.fin = ! more_;
|
||||||
d.fh.len = n;
|
fh_.len = n;
|
||||||
|
ws_.wr_.fb.reset();
|
||||||
detail::write<
|
detail::write<
|
||||||
flat_static_buffer_base>(d.fh_buf, d.fh);
|
flat_static_buffer_base>(ws_.wr_.fb, fh_);
|
||||||
d.ws.wr_.cont = ! d.fin;
|
ws_.wr_.cont = ! fin_;
|
||||||
// Send frame
|
// Send frame
|
||||||
d.step = more ?
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
do_deflate + 1 : do_deflate + 2;
|
BOOST_ASIO_CORO_YIELD
|
||||||
boost::asio::async_write(d.ws.stream_,
|
boost::asio::async_write(ws_.stream_,
|
||||||
buffer_cat(d.fh_buf.data(), b),
|
buffer_cat(ws_.wr_.fb.data(),
|
||||||
std::move(*this));
|
mutable_buffers_1{b}), std::move(*this));
|
||||||
return;
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
|
if(ec)
|
||||||
|
{
|
||||||
|
ws_.failed_ = true;
|
||||||
|
goto upcall;
|
||||||
}
|
}
|
||||||
|
if(more_)
|
||||||
case do_deflate + 1:
|
{
|
||||||
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
fh_.op = detail::opcode::cont;
|
||||||
d.fh_buf.consume(d.fh_buf.size());
|
fh_.rsv1 = false;
|
||||||
d.ws.wr_block_.reset();
|
|
||||||
d.fh.op = detail::opcode::cont;
|
|
||||||
d.fh.rsv1 = false;
|
|
||||||
// Allow outgoing control frames to
|
// Allow outgoing control frames to
|
||||||
// be sent in between message frames:
|
// be sent in between message frames:
|
||||||
if( d.ws.close_op_.maybe_invoke() ||
|
ws_.wr_block_.reset();
|
||||||
d.ws.rd_op_.maybe_invoke() ||
|
if( ws_.close_op_.maybe_invoke() ||
|
||||||
d.ws.ping_op_.maybe_invoke())
|
ws_.rd_op_.maybe_invoke() ||
|
||||||
|
ws_.ping_op_.maybe_invoke())
|
||||||
{
|
{
|
||||||
d.step = do_maybe_suspend;
|
BOOST_ASIO_CORO_YIELD
|
||||||
d.ws.get_io_service().post(
|
ws_.get_io_service().post(
|
||||||
std::move(*this));
|
std::move(*this));
|
||||||
return;
|
goto do_maybe_suspend;
|
||||||
}
|
}
|
||||||
d.ws.wr_block_ = d.tok;
|
ws_.wr_block_ = tok_;
|
||||||
goto go_deflate;
|
|
||||||
|
|
||||||
case do_deflate + 2:
|
|
||||||
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
|
||||||
if(d.fh.fin && (
|
|
||||||
(d.ws.role_ == role_type::client &&
|
|
||||||
d.ws.pmd_config_.client_no_context_takeover) ||
|
|
||||||
(d.ws.role_ == role_type::server &&
|
|
||||||
d.ws.pmd_config_.server_no_context_takeover)))
|
|
||||||
d.ws.pmd_->zo.reset();
|
|
||||||
goto upcall;
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
|
||||||
|
|
||||||
case do_maybe_suspend:
|
|
||||||
if(d.ws.wr_block_)
|
|
||||||
{
|
|
||||||
// suspend
|
|
||||||
BOOST_ASSERT(d.ws.wr_block_ != d.tok);
|
|
||||||
d.step = do_maybe_suspend + 1;
|
|
||||||
d.ws.wr_op_.emplace(std::move(*this));
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
d.ws.wr_block_ = d.tok;
|
else
|
||||||
if(d.ws.failed_ || d.ws.wr_close_)
|
|
||||||
{
|
{
|
||||||
// call handler
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
||||||
return d.ws.get_io_service().post(
|
if(fh_.fin && (
|
||||||
bind_handler(std::move(*this),
|
(ws_.role_ == role_type::client &&
|
||||||
boost::asio::error::operation_aborted, 0));
|
ws_.pmd_config_.client_no_context_takeover) ||
|
||||||
}
|
(ws_.role_ == role_type::server &&
|
||||||
d.step = d.entry_state;
|
ws_.pmd_config_.server_no_context_takeover)))
|
||||||
goto loop;
|
ws_.pmd_->zo.reset();
|
||||||
|
|
||||||
case do_maybe_suspend + 1:
|
|
||||||
BOOST_ASSERT(! d.ws.wr_block_);
|
|
||||||
d.ws.wr_block_ = d.tok;
|
|
||||||
d.step = do_maybe_suspend + 2;
|
|
||||||
// The current context is safe but might not be
|
|
||||||
// the same as the one for this operation (since
|
|
||||||
// we are being called from a write operation).
|
|
||||||
// Call post to make sure we are invoked the same
|
|
||||||
// way as the final handler for this operation.
|
|
||||||
d.ws.get_io_service().post(bind_handler(
|
|
||||||
std::move(*this), ec, 0));
|
|
||||||
return;
|
|
||||||
|
|
||||||
case do_maybe_suspend + 2:
|
|
||||||
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
|
||||||
if(d.ws.failed_ || d.ws.wr_close_)
|
|
||||||
{
|
|
||||||
// call handler
|
|
||||||
ec = boost::asio::error::operation_aborted;
|
|
||||||
goto upcall;
|
goto upcall;
|
||||||
}
|
}
|
||||||
d.step = d.entry_state;
|
|
||||||
goto loop;
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
|
||||||
|
|
||||||
case do_upcall:
|
|
||||||
goto upcall;
|
|
||||||
}
|
}
|
||||||
upcall:
|
}
|
||||||
if(d.ws.wr_block_ == d.tok)
|
|
||||||
d.ws.wr_block_.reset();
|
//--------------------------------------------------------------------------
|
||||||
d.ws.close_op_.maybe_invoke() ||
|
|
||||||
d.ws.rd_op_.maybe_invoke() ||
|
upcall:
|
||||||
d.ws.ping_op_.maybe_invoke();
|
if(ws_.wr_block_ == tok_)
|
||||||
d_.invoke(ec);
|
ws_.wr_block_.reset();
|
||||||
|
ws_.close_op_.maybe_invoke() ||
|
||||||
|
ws_.rd_op_.maybe_invoke() ||
|
||||||
|
ws_.ping_op_.maybe_invoke();
|
||||||
|
h_(ec);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
@ -543,6 +566,12 @@ write_some(bool fin,
|
|||||||
using boost::asio::buffer;
|
using boost::asio::buffer;
|
||||||
using boost::asio::buffer_copy;
|
using boost::asio::buffer_copy;
|
||||||
using boost::asio::buffer_size;
|
using boost::asio::buffer_size;
|
||||||
|
// Make sure the stream is open
|
||||||
|
if(failed_)
|
||||||
|
{
|
||||||
|
ec = boost::asio::error::operation_aborted;
|
||||||
|
return;
|
||||||
|
}
|
||||||
detail::frame_header fh;
|
detail::frame_header fh;
|
||||||
if(! wr_.cont)
|
if(! wr_.cont)
|
||||||
{
|
{
|
||||||
@ -750,7 +779,7 @@ async_write_some(bool fin,
|
|||||||
void(error_code)> init{handler};
|
void(error_code)> init{handler};
|
||||||
write_some_op<ConstBufferSequence, handler_type<
|
write_some_op<ConstBufferSequence, handler_type<
|
||||||
WriteHandler, void(error_code)>>{init.completion_handler,
|
WriteHandler, void(error_code)>>{init.completion_handler,
|
||||||
*this, fin, bs}({}, 0, false);
|
*this, fin, bs}();
|
||||||
return init.result.get();
|
return init.result.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -804,7 +833,7 @@ async_write(
|
|||||||
void(error_code)> init{handler};
|
void(error_code)> init{handler};
|
||||||
write_some_op<ConstBufferSequence, handler_type<
|
write_some_op<ConstBufferSequence, handler_type<
|
||||||
WriteHandler, void(error_code)>>{init.completion_handler,
|
WriteHandler, void(error_code)>>{init.completion_handler,
|
||||||
*this, true, bs}({}, 0, false);
|
*this, true, bs}();
|
||||||
return init.result.get();
|
return init.result.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,6 +200,8 @@ class stream
|
|||||||
// The buffer is allocated or reallocated at the beginning of
|
// The buffer is allocated or reallocated at the beginning of
|
||||||
// sending a message.
|
// sending a message.
|
||||||
std::unique_ptr<std::uint8_t[]> buf;
|
std::unique_ptr<std::uint8_t[]> buf;
|
||||||
|
|
||||||
|
detail::fh_streambuf fb;
|
||||||
};
|
};
|
||||||
|
|
||||||
// State information for the permessage-deflate extension
|
// State information for the permessage-deflate extension
|
||||||
|
Reference in New Issue
Block a user