2016-10-24 08:12:09 -04:00
|
|
|
//
|
2017-07-24 09:42:36 -07:00
|
|
|
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2016-10-24 08:12:09 -04:00
|
|
|
//
|
|
|
|
|
// 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)
|
|
|
|
|
//
|
2017-07-20 13:40:34 -07:00
|
|
|
// Official repository: https://github.com/boostorg/beast
|
|
|
|
|
//
|
2016-10-24 08:12:09 -04:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_WRITE_IPP
|
|
|
|
|
#define BOOST_BEAST_WEBSOCKET_IMPL_WRITE_IPP
|
|
|
|
|
|
|
|
|
|
#include <boost/beast/core/bind_handler.hpp>
|
2017-09-15 10:03:59 -07:00
|
|
|
#include <boost/beast/core/buffers_cat.hpp>
|
2017-09-15 09:55:03 -07:00
|
|
|
#include <boost/beast/core/buffers_prefix.hpp>
|
2018-11-11 20:59:57 -08:00
|
|
|
#include <boost/beast/core/buffers_range.hpp>
|
2017-09-15 12:52:45 -07:00
|
|
|
#include <boost/beast/core/buffers_suffix.hpp>
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/core/flat_static_buffer.hpp>
|
|
|
|
|
#include <boost/beast/core/type_traits.hpp>
|
|
|
|
|
#include <boost/beast/core/detail/clamp.hpp>
|
|
|
|
|
#include <boost/beast/core/detail/config.hpp>
|
|
|
|
|
#include <boost/beast/websocket/detail/frame.hpp>
|
2017-09-07 07:39:52 -07:00
|
|
|
#include <boost/asio/associated_allocator.hpp>
|
|
|
|
|
#include <boost/asio/associated_executor.hpp>
|
2017-08-12 20:50:23 -07:00
|
|
|
#include <boost/asio/coroutine.hpp>
|
2018-04-09 16:22:15 -07:00
|
|
|
#include <boost/asio/executor_work_guard.hpp>
|
2017-05-14 09:25:43 -07:00
|
|
|
#include <boost/asio/handler_continuation_hook.hpp>
|
2018-03-01 06:36:18 -08:00
|
|
|
#include <boost/asio/handler_invoke_hook.hpp>
|
2016-10-24 08:12:09 -04:00
|
|
|
#include <boost/assert.hpp>
|
2017-06-08 05:54:47 -07:00
|
|
|
#include <boost/config.hpp>
|
2017-05-22 15:30:12 -07:00
|
|
|
#include <boost/throw_exception.hpp>
|
2016-10-24 08:12:09 -04:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
namespace boost {
|
2016-10-24 08:12:09 -04:00
|
|
|
namespace beast {
|
|
|
|
|
namespace websocket {
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
|
|
// Compress a buffer sequence
|
|
|
|
|
// Returns: `true` if more calls are needed
|
|
|
|
|
//
|
|
|
|
|
template<>
|
|
|
|
|
template<class ConstBufferSequence>
|
|
|
|
|
bool
|
|
|
|
|
stream_base<true>::
|
|
|
|
|
deflate(
|
|
|
|
|
boost::asio::mutable_buffer& out,
|
|
|
|
|
buffers_suffix<ConstBufferSequence>& cb,
|
|
|
|
|
bool fin,
|
|
|
|
|
std::size_t& total_in,
|
|
|
|
|
error_code& ec)
|
|
|
|
|
{
|
|
|
|
|
using boost::asio::buffer;
|
|
|
|
|
BOOST_ASSERT(out.size() >= 6);
|
|
|
|
|
auto& zo = this->pmd_->zo;
|
|
|
|
|
zlib::z_params zs;
|
|
|
|
|
zs.avail_in = 0;
|
|
|
|
|
zs.next_in = nullptr;
|
|
|
|
|
zs.avail_out = out.size();
|
|
|
|
|
zs.next_out = out.data();
|
2018-11-11 20:59:57 -08:00
|
|
|
for(auto in : beast::buffers_range(std::ref(cb)))
|
2017-11-18 16:52:18 -08:00
|
|
|
{
|
|
|
|
|
zs.avail_in = in.size();
|
|
|
|
|
if(zs.avail_in == 0)
|
|
|
|
|
continue;
|
|
|
|
|
zs.next_in = in.data();
|
|
|
|
|
zo.write(zs, zlib::Flush::none, ec);
|
|
|
|
|
if(ec)
|
|
|
|
|
{
|
|
|
|
|
if(ec != zlib::error::need_buffers)
|
|
|
|
|
return false;
|
|
|
|
|
BOOST_ASSERT(zs.avail_out == 0);
|
|
|
|
|
BOOST_ASSERT(zs.total_out == out.size());
|
|
|
|
|
ec.assign(0, ec.category());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(zs.avail_out == 0)
|
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(zs.total_out == out.size());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
BOOST_ASSERT(zs.avail_in == 0);
|
|
|
|
|
}
|
|
|
|
|
total_in = zs.total_in;
|
|
|
|
|
cb.consume(zs.total_in);
|
|
|
|
|
if(zs.avail_out > 0 && fin)
|
|
|
|
|
{
|
|
|
|
|
auto const remain = boost::asio::buffer_size(cb);
|
|
|
|
|
if(remain == 0)
|
|
|
|
|
{
|
|
|
|
|
// Inspired by Mark Adler
|
|
|
|
|
// https://github.com/madler/zlib/issues/149
|
|
|
|
|
//
|
|
|
|
|
// VFALCO We could do this flush twice depending
|
|
|
|
|
// on how much space is in the output.
|
|
|
|
|
zo.write(zs, zlib::Flush::block, ec);
|
|
|
|
|
BOOST_ASSERT(! ec || ec == zlib::error::need_buffers);
|
|
|
|
|
if(ec == zlib::error::need_buffers)
|
|
|
|
|
ec.assign(0, ec.category());
|
|
|
|
|
if(ec)
|
|
|
|
|
return false;
|
|
|
|
|
if(zs.avail_out >= 6)
|
|
|
|
|
{
|
|
|
|
|
zo.write(zs, zlib::Flush::full, ec);
|
|
|
|
|
BOOST_ASSERT(! ec);
|
|
|
|
|
// remove flush marker
|
|
|
|
|
zs.total_out -= 4;
|
|
|
|
|
out = buffer(out.data(), zs.total_out);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ec.assign(0, ec.category());
|
|
|
|
|
out = buffer(out.data(), zs.total_out);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
inline
|
|
|
|
|
void
|
|
|
|
|
stream_base<true>::
|
|
|
|
|
do_context_takeover_write(role_type role)
|
|
|
|
|
{
|
|
|
|
|
if((role == role_type::client &&
|
|
|
|
|
this->pmd_config_.client_no_context_takeover) ||
|
|
|
|
|
(role == role_type::server &&
|
|
|
|
|
this->pmd_config_.server_no_context_takeover))
|
|
|
|
|
{
|
|
|
|
|
this->pmd_->zo.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // detail
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-10-24 08:12:09 -04:00
|
|
|
template<class Buffers, class Handler>
|
2017-11-18 16:52:18 -08:00
|
|
|
class stream<NextLayer, deflateSupported>::write_some_op
|
2017-08-12 20:50:23 -07:00
|
|
|
: public boost::asio::coroutine
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
Handler h_;
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>& ws_;
|
2018-04-09 16:22:15 -07:00
|
|
|
boost::asio::executor_work_guard<decltype(std::declval<
|
|
|
|
|
stream<NextLayer, deflateSupported>&>().get_executor())> wg_;
|
2017-09-15 12:52:45 -07:00
|
|
|
buffers_suffix<Buffers> cb_;
|
2017-08-12 20:50:23 -07:00
|
|
|
detail::frame_header fh_;
|
|
|
|
|
detail::prepared_key key_;
|
2017-09-03 06:18:07 -07:00
|
|
|
std::size_t bytes_transferred_ = 0;
|
2017-08-12 20:50:23 -07:00
|
|
|
std::size_t remain_;
|
2017-09-03 06:18:07 -07:00
|
|
|
std::size_t in_;
|
2017-08-12 20:50:23 -07:00
|
|
|
int how_;
|
|
|
|
|
bool fin_;
|
2018-08-02 07:20:12 -07:00
|
|
|
bool more_ = false; // for ubsan
|
2017-08-31 17:52:09 -07:00
|
|
|
bool cont_ = false;
|
2016-10-24 08:12:09 -04:00
|
|
|
|
|
|
|
|
public:
|
2018-02-18 18:46:12 -08:00
|
|
|
static constexpr int id = 2; // for soft_mutex
|
|
|
|
|
|
2017-07-15 17:05:24 -07:00
|
|
|
write_some_op(write_some_op&&) = default;
|
2017-12-02 12:59:30 +01:00
|
|
|
write_some_op(write_some_op const&) = delete;
|
2016-10-24 08:12:09 -04:00
|
|
|
|
2017-08-12 20:50:23 -07:00
|
|
|
template<class DeducedHandler>
|
|
|
|
|
write_some_op(
|
|
|
|
|
DeducedHandler&& h,
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>& ws,
|
2017-08-12 20:50:23 -07:00
|
|
|
bool fin,
|
|
|
|
|
Buffers const& bs)
|
|
|
|
|
: h_(std::forward<DeducedHandler>(h))
|
|
|
|
|
, ws_(ws)
|
2018-04-09 16:22:15 -07:00
|
|
|
, wg_(ws_.get_executor())
|
2017-08-12 20:50:23 -07:00
|
|
|
, cb_(bs)
|
|
|
|
|
, fin_(fin)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 07:39:52 -07:00
|
|
|
using allocator_type =
|
|
|
|
|
boost::asio::associated_allocator_t<Handler>;
|
|
|
|
|
|
|
|
|
|
allocator_type
|
|
|
|
|
get_allocator() const noexcept
|
|
|
|
|
{
|
2017-12-02 05:57:06 -08:00
|
|
|
return (boost::asio::get_associated_allocator)(h_);
|
2017-09-07 07:39:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using executor_type = boost::asio::associated_executor_t<
|
2017-11-18 16:52:18 -08:00
|
|
|
Handler, decltype(std::declval<stream<NextLayer, deflateSupported>&>().get_executor())>;
|
2017-09-07 07:39:52 -07:00
|
|
|
|
2017-10-24 06:40:22 -07:00
|
|
|
executor_type
|
|
|
|
|
get_executor() const noexcept
|
2017-09-07 07:39:52 -07:00
|
|
|
{
|
2017-12-02 05:57:06 -08:00
|
|
|
return (boost::asio::get_associated_executor)(
|
2017-09-07 07:39:52 -07:00
|
|
|
h_, ws_.get_executor());
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-12 20:50:23 -07:00
|
|
|
Handler&
|
|
|
|
|
handler()
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
return h_;
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
2017-08-12 20:50:23 -07:00
|
|
|
void operator()(
|
|
|
|
|
error_code ec = {},
|
2017-08-26 20:10:04 -07:00
|
|
|
std::size_t bytes_transferred = 0,
|
|
|
|
|
bool cont = true);
|
2016-10-24 08:12:09 -04:00
|
|
|
|
|
|
|
|
friend
|
2017-07-15 17:05:24 -07:00
|
|
|
bool asio_handler_is_continuation(write_some_op* op)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
using boost::asio::asio_handler_is_continuation;
|
2017-08-26 20:10:04 -07:00
|
|
|
return op->cont_ || asio_handler_is_continuation(
|
2017-08-12 20:50:23 -07:00
|
|
|
std::addressof(op->h_));
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
2018-03-01 06:36:18 -08:00
|
|
|
|
|
|
|
|
template<class Function>
|
|
|
|
|
friend
|
|
|
|
|
void asio_handler_invoke(Function&& f, write_some_op* op)
|
|
|
|
|
{
|
|
|
|
|
using boost::asio::asio_handler_invoke;
|
|
|
|
|
asio_handler_invoke(
|
|
|
|
|
f, std::addressof(op->h_));
|
|
|
|
|
}
|
2016-10-24 08:12:09 -04:00
|
|
|
};
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-10-24 08:12:09 -04:00
|
|
|
template<class Buffers, class Handler>
|
|
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-07-15 17:05:24 -07:00
|
|
|
write_some_op<Buffers, Handler>::
|
2017-08-26 20:10:04 -07:00
|
|
|
operator()(
|
|
|
|
|
error_code ec,
|
|
|
|
|
std::size_t bytes_transferred,
|
|
|
|
|
bool cont)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2016-10-24 20:02:38 -04:00
|
|
|
using beast::detail::clamp;
|
2016-10-24 18:41:25 -04:00
|
|
|
using boost::asio::buffer;
|
2016-10-24 08:12:09 -04:00
|
|
|
using boost::asio::buffer_copy;
|
2016-10-24 18:41:25 -04:00
|
|
|
using boost::asio::buffer_size;
|
2017-09-07 07:39:52 -07:00
|
|
|
using boost::asio::mutable_buffer;
|
2016-10-24 18:41:25 -04:00
|
|
|
enum
|
|
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
do_nomask_nofrag,
|
|
|
|
|
do_nomask_frag,
|
|
|
|
|
do_mask_nofrag,
|
|
|
|
|
do_mask_frag,
|
|
|
|
|
do_deflate
|
2016-10-24 18:41:25 -04:00
|
|
|
};
|
2017-08-12 20:50:23 -07:00
|
|
|
std::size_t n;
|
|
|
|
|
boost::asio::mutable_buffer b;
|
2017-08-26 20:10:04 -07:00
|
|
|
cont_ = cont;
|
2017-08-12 20:50:23 -07:00
|
|
|
BOOST_ASIO_CORO_REENTER(*this)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
// Set up the outgoing frame header
|
2017-08-26 15:18:02 -07:00
|
|
|
if(! ws_.wr_cont_)
|
2017-06-29 11:36:14 -07:00
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.begin_msg();
|
|
|
|
|
fh_.rsv1 = ws_.wr_compress_;
|
2017-06-29 11:36:14 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
fh_.rsv1 = false;
|
2017-06-29 11:36:14 -07:00
|
|
|
}
|
2017-08-12 20:50:23 -07:00
|
|
|
fh_.rsv2 = false;
|
|
|
|
|
fh_.rsv3 = false;
|
2017-08-26 15:18:02 -07:00
|
|
|
fh_.op = ws_.wr_cont_ ?
|
2017-08-12 20:50:23 -07:00
|
|
|
detail::opcode::cont : ws_.wr_opcode_;
|
|
|
|
|
fh_.mask =
|
|
|
|
|
ws_.role_ == role_type::client;
|
|
|
|
|
|
|
|
|
|
// Choose a write algorithm
|
2017-08-26 15:18:02 -07:00
|
|
|
if(ws_.wr_compress_)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
how_ = do_deflate;
|
2017-06-29 11:36:14 -07:00
|
|
|
}
|
2017-08-12 20:50:23 -07:00
|
|
|
else if(! fh_.mask)
|
2017-06-29 11:36:14 -07:00
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
if(! ws_.wr_frag_)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
how_ = do_nomask_nofrag;
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
2016-10-24 18:41:25 -04:00
|
|
|
else
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
BOOST_ASSERT(ws_.wr_buf_size_ != 0);
|
2017-08-12 20:50:23 -07:00
|
|
|
remain_ = buffer_size(cb_);
|
2017-08-26 15:18:02 -07:00
|
|
|
if(remain_ > ws_.wr_buf_size_)
|
2017-08-12 20:50:23 -07:00
|
|
|
how_ = do_nomask_frag;
|
2017-06-29 11:36:14 -07:00
|
|
|
else
|
2017-08-12 20:50:23 -07:00
|
|
|
how_ = do_nomask_nofrag;
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
2017-06-29 11:36:14 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
if(! ws_.wr_frag_)
|
2016-10-24 18:41:25 -04:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
how_ = do_mask_nofrag;
|
2016-10-24 18:41:25 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
BOOST_ASSERT(ws_.wr_buf_size_ != 0);
|
2017-08-12 20:50:23 -07:00
|
|
|
remain_ = buffer_size(cb_);
|
2017-08-26 15:18:02 -07:00
|
|
|
if(remain_ > ws_.wr_buf_size_)
|
2017-08-12 20:50:23 -07:00
|
|
|
how_ = do_mask_frag;
|
2016-10-24 18:41:25 -04:00
|
|
|
else
|
2017-08-12 20:50:23 -07:00
|
|
|
how_ = do_mask_nofrag;
|
2016-10-24 18:41:25 -04:00
|
|
|
}
|
2017-06-29 11:36:14 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-12 20:50:23 -07:00
|
|
|
// Maybe suspend
|
2018-02-15 11:30:15 -08:00
|
|
|
if(ws_.wr_block_.try_lock(this))
|
2017-06-29 11:36:14 -07:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
// Make sure the stream is open
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! ws_.check_open(ec))
|
2017-08-12 20:50:23 -07:00
|
|
|
goto upcall;
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-10-24 18:41:25 -04:00
|
|
|
{
|
2017-08-26 20:10:04 -07:00
|
|
|
do_suspend:
|
2017-08-12 20:50:23 -07:00
|
|
|
// Suspend
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2017-12-02 11:22:51 -08:00
|
|
|
ws_.paused_wr_.emplace(std::move(*this));
|
2017-08-12 20:50:23 -07:00
|
|
|
|
|
|
|
|
// Acquire the write block
|
2018-02-15 11:30:15 -08:00
|
|
|
ws_.wr_block_.lock(this);
|
2017-08-12 20:50:23 -07:00
|
|
|
|
|
|
|
|
// Resume
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2018-03-22 20:13:39 -07:00
|
|
|
boost::asio::post(
|
|
|
|
|
ws_.get_executor(), std::move(*this));
|
2018-02-15 11:30:15 -08:00
|
|
|
BOOST_ASSERT(ws_.wr_block_.is_locked(this));
|
2017-08-12 20:50:23 -07:00
|
|
|
|
|
|
|
|
// Make sure the stream is open
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! ws_.check_open(ec))
|
2017-08-12 20:50:23 -07:00
|
|
|
goto upcall;
|
2016-10-24 18:41:25 -04:00
|
|
|
}
|
|
|
|
|
|
2017-08-12 20:50:23 -07:00
|
|
|
//------------------------------------------------------------------
|
2016-10-24 18:41:25 -04:00
|
|
|
|
2017-08-12 20:50:23 -07:00
|
|
|
if(how_ == do_nomask_nofrag)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
fh_.fin = fin_;
|
|
|
|
|
fh_.len = buffer_size(cb_);
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_fb_.reset();
|
2017-08-12 20:50:23 -07:00
|
|
|
detail::write<flat_static_buffer_base>(
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_fb_, fh_);
|
|
|
|
|
ws_.wr_cont_ = ! fin_;
|
2017-08-12 20:50:23 -07:00
|
|
|
// Send frame
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
|
|
|
|
boost::asio::async_write(ws_.stream_,
|
2017-09-15 10:03:59 -07:00
|
|
|
buffers_cat(ws_.wr_fb_.data(), cb_),
|
2017-08-12 20:50:23 -07:00
|
|
|
std::move(*this));
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! ws_.check_ok(ec))
|
2017-08-26 20:10:04 -07:00
|
|
|
goto upcall;
|
2017-09-03 06:18:07 -07:00
|
|
|
bytes_transferred_ += clamp(fh_.len);
|
2017-08-12 20:50:23 -07:00
|
|
|
goto upcall;
|
2016-10-24 18:41:25 -04:00
|
|
|
}
|
|
|
|
|
|
2017-08-12 20:50:23 -07:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
else if(how_ == do_nomask_frag)
|
2016-10-24 18:41:25 -04:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
for(;;)
|
|
|
|
|
{
|
2017-09-03 06:18:07 -07:00
|
|
|
n = clamp(remain_, ws_.wr_buf_size_);
|
|
|
|
|
fh_.len = n;
|
|
|
|
|
remain_ -= n;
|
2017-08-12 20:50:23 -07:00
|
|
|
fh_.fin = fin_ ? remain_ == 0 : false;
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_fb_.reset();
|
2017-08-12 20:50:23 -07:00
|
|
|
detail::write<flat_static_buffer_base>(
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_fb_, fh_);
|
|
|
|
|
ws_.wr_cont_ = ! fin_;
|
2017-08-12 20:50:23 -07:00
|
|
|
// Send frame
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
|
|
|
|
boost::asio::async_write(
|
2017-09-15 10:03:59 -07:00
|
|
|
ws_.stream_, buffers_cat(
|
2017-09-15 09:55:03 -07:00
|
|
|
ws_.wr_fb_.data(), buffers_prefix(
|
2017-08-12 20:50:23 -07:00
|
|
|
clamp(fh_.len), cb_)),
|
|
|
|
|
std::move(*this));
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! ws_.check_ok(ec))
|
2017-08-12 20:50:23 -07:00
|
|
|
goto upcall;
|
2017-09-03 06:18:07 -07:00
|
|
|
n = clamp(fh_.len); // because yield
|
|
|
|
|
bytes_transferred_ += n;
|
2017-08-12 20:50:23 -07:00
|
|
|
if(remain_ == 0)
|
2017-08-26 20:10:04 -07:00
|
|
|
break;
|
2017-09-03 06:18:07 -07:00
|
|
|
cb_.consume(n);
|
2017-08-12 20:50:23 -07:00
|
|
|
fh_.op = detail::opcode::cont;
|
|
|
|
|
// Allow outgoing control frames to
|
|
|
|
|
// be sent in between message frames
|
2018-02-15 11:30:15 -08:00
|
|
|
ws_.wr_block_.unlock(this);
|
2017-08-26 15:18:02 -07:00
|
|
|
if( ws_.paused_close_.maybe_invoke() ||
|
|
|
|
|
ws_.paused_rd_.maybe_invoke() ||
|
|
|
|
|
ws_.paused_ping_.maybe_invoke())
|
2017-08-12 20:50:23 -07:00
|
|
|
{
|
2018-02-15 11:30:15 -08:00
|
|
|
BOOST_ASSERT(ws_.wr_block_.is_locked());
|
2017-08-26 20:10:04 -07:00
|
|
|
goto do_suspend;
|
2017-08-12 20:50:23 -07:00
|
|
|
}
|
2018-02-15 11:30:15 -08:00
|
|
|
ws_.wr_block_.lock(this);
|
2017-08-12 20:50:23 -07:00
|
|
|
}
|
2017-08-26 20:10:04 -07:00
|
|
|
goto upcall;
|
2016-10-24 18:41:25 -04:00
|
|
|
}
|
2017-08-12 20:50:23 -07:00
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
else if(how_ == do_mask_nofrag)
|
2016-10-24 18:41:25 -04:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
remain_ = buffer_size(cb_);
|
|
|
|
|
fh_.fin = fin_;
|
|
|
|
|
fh_.len = remain_;
|
2018-07-05 16:47:07 -07:00
|
|
|
fh_.key = ws_.create_mask();
|
2017-08-12 20:50:23 -07:00
|
|
|
detail::prepare_key(key_, fh_.key);
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_fb_.reset();
|
2017-08-12 20:50:23 -07:00
|
|
|
detail::write<flat_static_buffer_base>(
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_fb_, fh_);
|
|
|
|
|
n = clamp(remain_, ws_.wr_buf_size_);
|
2017-08-12 20:50:23 -07:00
|
|
|
buffer_copy(buffer(
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_buf_.get(), n), cb_);
|
2017-08-12 20:50:23 -07:00
|
|
|
detail::mask_inplace(buffer(
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_buf_.get(), n), key_);
|
2017-08-12 20:50:23 -07:00
|
|
|
remain_ -= n;
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_cont_ = ! fin_;
|
2017-08-12 20:50:23 -07:00
|
|
|
// Send frame header and partial payload
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
|
|
|
|
boost::asio::async_write(
|
2017-09-15 10:03:59 -07:00
|
|
|
ws_.stream_, buffers_cat(ws_.wr_fb_.data(),
|
2017-08-26 15:18:02 -07:00
|
|
|
buffer(ws_.wr_buf_.get(), n)),
|
2017-08-12 20:50:23 -07:00
|
|
|
std::move(*this));
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! ws_.check_ok(ec))
|
2017-08-12 20:50:23 -07:00
|
|
|
goto upcall;
|
2017-09-03 06:18:07 -07:00
|
|
|
bytes_transferred_ +=
|
|
|
|
|
bytes_transferred - ws_.wr_fb_.size();
|
2017-08-12 20:50:23 -07:00
|
|
|
while(remain_ > 0)
|
|
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
cb_.consume(ws_.wr_buf_size_);
|
|
|
|
|
n = clamp(remain_, ws_.wr_buf_size_);
|
2017-08-12 20:50:23 -07:00
|
|
|
buffer_copy(buffer(
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_buf_.get(), n), cb_);
|
2017-08-12 20:50:23 -07:00
|
|
|
detail::mask_inplace(buffer(
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_buf_.get(), n), key_);
|
2017-08-12 20:50:23 -07:00
|
|
|
remain_ -= n;
|
|
|
|
|
// Send partial payload
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
|
|
|
|
boost::asio::async_write(ws_.stream_,
|
2017-08-26 15:18:02 -07:00
|
|
|
buffer(ws_.wr_buf_.get(), n),
|
2017-08-12 20:50:23 -07:00
|
|
|
std::move(*this));
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! ws_.check_ok(ec))
|
2017-08-12 20:50:23 -07:00
|
|
|
goto upcall;
|
2017-09-03 06:18:07 -07:00
|
|
|
bytes_transferred_ += bytes_transferred;
|
2017-08-12 20:50:23 -07:00
|
|
|
}
|
|
|
|
|
goto upcall;
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
2017-08-12 20:50:23 -07:00
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
else if(how_ == do_mask_frag)
|
2016-10-24 18:41:25 -04:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
for(;;)
|
|
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
n = clamp(remain_, ws_.wr_buf_size_);
|
2017-08-12 20:50:23 -07:00
|
|
|
remain_ -= n;
|
|
|
|
|
fh_.len = n;
|
2018-07-05 16:47:07 -07:00
|
|
|
fh_.key = ws_.create_mask();
|
2017-08-12 20:50:23 -07:00
|
|
|
fh_.fin = fin_ ? remain_ == 0 : false;
|
|
|
|
|
detail::prepare_key(key_, fh_.key);
|
|
|
|
|
buffer_copy(buffer(
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_buf_.get(), n), cb_);
|
2017-08-12 20:50:23 -07:00
|
|
|
detail::mask_inplace(buffer(
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_buf_.get(), n), key_);
|
|
|
|
|
ws_.wr_fb_.reset();
|
2017-08-12 20:50:23 -07:00
|
|
|
detail::write<flat_static_buffer_base>(
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_fb_, fh_);
|
|
|
|
|
ws_.wr_cont_ = ! fin_;
|
2017-08-12 20:50:23 -07:00
|
|
|
// Send frame
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
|
|
|
|
boost::asio::async_write(ws_.stream_,
|
2017-09-15 10:03:59 -07:00
|
|
|
buffers_cat(ws_.wr_fb_.data(),
|
2017-08-26 15:18:02 -07:00
|
|
|
buffer(ws_.wr_buf_.get(), n)),
|
2017-08-12 20:50:23 -07:00
|
|
|
std::move(*this));
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! ws_.check_ok(ec))
|
2017-08-12 20:50:23 -07:00
|
|
|
goto upcall;
|
2017-09-03 06:18:07 -07:00
|
|
|
n = bytes_transferred - ws_.wr_fb_.size();
|
|
|
|
|
bytes_transferred_ += n;
|
2017-08-12 20:50:23 -07:00
|
|
|
if(remain_ == 0)
|
2017-08-26 20:10:04 -07:00
|
|
|
break;
|
2017-09-03 06:18:07 -07:00
|
|
|
cb_.consume(n);
|
2017-08-12 20:50:23 -07:00
|
|
|
fh_.op = detail::opcode::cont;
|
|
|
|
|
// Allow outgoing control frames to
|
|
|
|
|
// be sent in between message frames:
|
2018-02-15 11:30:15 -08:00
|
|
|
ws_.wr_block_.unlock(this);
|
2017-08-26 15:18:02 -07:00
|
|
|
if( ws_.paused_close_.maybe_invoke() ||
|
|
|
|
|
ws_.paused_rd_.maybe_invoke() ||
|
|
|
|
|
ws_.paused_ping_.maybe_invoke())
|
2017-08-12 20:50:23 -07:00
|
|
|
{
|
2018-02-15 11:30:15 -08:00
|
|
|
BOOST_ASSERT(ws_.wr_block_.is_locked());
|
2017-08-26 20:10:04 -07:00
|
|
|
goto do_suspend;
|
2017-08-12 20:50:23 -07:00
|
|
|
}
|
2018-02-15 11:30:15 -08:00
|
|
|
ws_.wr_block_.lock(this);
|
2017-08-12 20:50:23 -07:00
|
|
|
}
|
2017-08-26 20:10:04 -07:00
|
|
|
goto upcall;
|
2016-10-24 18:41:25 -04:00
|
|
|
}
|
|
|
|
|
|
2017-08-12 20:50:23 -07:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
else if(how_ == do_deflate)
|
2017-06-29 11:36:14 -07:00
|
|
|
{
|
2017-08-12 20:50:23 -07:00
|
|
|
for(;;)
|
|
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
b = buffer(ws_.wr_buf_.get(),
|
|
|
|
|
ws_.wr_buf_size_);
|
2017-11-18 16:52:18 -08:00
|
|
|
more_ = ws_.deflate(b, cb_, fin_, in_, ec);
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! ws_.check_ok(ec))
|
2017-08-12 20:50:23 -07:00
|
|
|
goto upcall;
|
|
|
|
|
n = buffer_size(b);
|
|
|
|
|
if(n == 0)
|
|
|
|
|
{
|
|
|
|
|
// The input was consumed, but there
|
|
|
|
|
// is no output due to compression
|
|
|
|
|
// latency.
|
|
|
|
|
BOOST_ASSERT(! fin_);
|
|
|
|
|
BOOST_ASSERT(buffer_size(cb_) == 0);
|
|
|
|
|
goto upcall;
|
|
|
|
|
}
|
|
|
|
|
if(fh_.mask)
|
|
|
|
|
{
|
2018-07-05 16:47:07 -07:00
|
|
|
fh_.key = ws_.create_mask();
|
2017-08-12 20:50:23 -07:00
|
|
|
detail::prepared_key key;
|
|
|
|
|
detail::prepare_key(key, fh_.key);
|
|
|
|
|
detail::mask_inplace(b, key);
|
|
|
|
|
}
|
|
|
|
|
fh_.fin = ! more_;
|
|
|
|
|
fh_.len = n;
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.wr_fb_.reset();
|
2017-08-12 20:50:23 -07:00
|
|
|
detail::write<
|
2017-08-26 15:18:02 -07:00
|
|
|
flat_static_buffer_base>(ws_.wr_fb_, fh_);
|
|
|
|
|
ws_.wr_cont_ = ! fin_;
|
2017-08-12 20:50:23 -07:00
|
|
|
// Send frame
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
|
|
|
|
boost::asio::async_write(ws_.stream_,
|
2017-09-07 07:39:52 -07:00
|
|
|
buffers_cat(ws_.wr_fb_.data(), b),
|
|
|
|
|
std::move(*this));
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! ws_.check_ok(ec))
|
2017-08-12 20:50:23 -07:00
|
|
|
goto upcall;
|
2017-09-03 06:18:07 -07:00
|
|
|
bytes_transferred_ += in_;
|
2017-08-12 20:50:23 -07:00
|
|
|
if(more_)
|
|
|
|
|
{
|
|
|
|
|
fh_.op = detail::opcode::cont;
|
|
|
|
|
fh_.rsv1 = false;
|
|
|
|
|
// Allow outgoing control frames to
|
|
|
|
|
// be sent in between message frames:
|
2018-02-15 11:30:15 -08:00
|
|
|
ws_.wr_block_.unlock(this);
|
2017-08-26 15:18:02 -07:00
|
|
|
if( ws_.paused_close_.maybe_invoke() ||
|
|
|
|
|
ws_.paused_rd_.maybe_invoke() ||
|
|
|
|
|
ws_.paused_ping_.maybe_invoke())
|
2017-08-12 20:50:23 -07:00
|
|
|
{
|
2018-02-15 11:30:15 -08:00
|
|
|
BOOST_ASSERT(ws_.wr_block_.is_locked());
|
2017-08-26 20:10:04 -07:00
|
|
|
goto do_suspend;
|
2017-08-12 20:50:23 -07:00
|
|
|
}
|
2018-02-15 11:30:15 -08:00
|
|
|
ws_.wr_block_.lock(this);
|
2017-08-12 20:50:23 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-11-18 16:52:18 -08:00
|
|
|
if(fh_.fin)
|
|
|
|
|
ws_.do_context_takeover_write(ws_.role_);
|
2017-08-12 20:50:23 -07:00
|
|
|
goto upcall;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
2017-06-29 11:36:14 -07:00
|
|
|
|
2017-08-12 20:50:23 -07:00
|
|
|
//--------------------------------------------------------------------------
|
2017-06-29 11:36:14 -07:00
|
|
|
|
2017-08-12 20:50:23 -07:00
|
|
|
upcall:
|
2018-02-15 11:30:15 -08:00
|
|
|
ws_.wr_block_.unlock(this);
|
2017-08-26 15:18:02 -07:00
|
|
|
ws_.paused_close_.maybe_invoke() ||
|
|
|
|
|
ws_.paused_rd_.maybe_invoke() ||
|
|
|
|
|
ws_.paused_ping_.maybe_invoke();
|
2017-08-26 20:10:04 -07:00
|
|
|
if(! cont_)
|
2018-04-09 16:22:15 -07:00
|
|
|
{
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
|
|
|
|
boost::asio::post(
|
|
|
|
|
ws_.get_executor(),
|
|
|
|
|
bind_handler(std::move(*this), ec, bytes_transferred_));
|
|
|
|
|
}
|
2017-09-03 06:18:07 -07:00
|
|
|
h_(ec, bytes_transferred_);
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 21:28:17 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-10-24 08:12:09 -04:00
|
|
|
template<class ConstBufferSequence>
|
2017-09-03 06:18:07 -07:00
|
|
|
std::size_t
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-07-15 17:05:24 -07:00
|
|
|
write_some(bool fin, ConstBufferSequence const& buffers)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-05-10 12:03:00 -07:00
|
|
|
static_assert(is_sync_stream<next_layer_type>::value,
|
2016-10-24 08:12:09 -04:00
|
|
|
"SyncStream requirements not met");
|
2017-09-07 07:39:52 -07:00
|
|
|
static_assert(boost::asio::is_const_buffer_sequence<
|
2016-10-24 08:12:09 -04:00
|
|
|
ConstBufferSequence>::value,
|
|
|
|
|
"ConstBufferSequence requirements not met");
|
|
|
|
|
error_code ec;
|
2017-09-03 06:18:07 -07:00
|
|
|
auto const bytes_transferred =
|
|
|
|
|
write_some(fin, buffers, ec);
|
2016-10-24 08:12:09 -04:00
|
|
|
if(ec)
|
2017-05-22 15:30:12 -07:00
|
|
|
BOOST_THROW_EXCEPTION(system_error{ec});
|
2017-09-03 06:18:07 -07:00
|
|
|
return bytes_transferred;
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-10-24 08:12:09 -04:00
|
|
|
template<class ConstBufferSequence>
|
2017-09-03 06:18:07 -07:00
|
|
|
std::size_t
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-07-15 17:05:24 -07:00
|
|
|
write_some(bool fin,
|
2016-10-24 08:12:09 -04:00
|
|
|
ConstBufferSequence const& buffers, error_code& ec)
|
|
|
|
|
{
|
2017-05-10 12:03:00 -07:00
|
|
|
static_assert(is_sync_stream<next_layer_type>::value,
|
2016-10-24 08:12:09 -04:00
|
|
|
"SyncStream requirements not met");
|
2017-09-07 07:39:52 -07:00
|
|
|
static_assert(boost::asio::is_const_buffer_sequence<
|
2016-10-24 08:12:09 -04:00
|
|
|
ConstBufferSequence>::value,
|
|
|
|
|
"ConstBufferSequence requirements not met");
|
2016-10-24 20:02:38 -04:00
|
|
|
using beast::detail::clamp;
|
2016-10-24 08:12:09 -04:00
|
|
|
using boost::asio::buffer;
|
|
|
|
|
using boost::asio::buffer_copy;
|
|
|
|
|
using boost::asio::buffer_size;
|
2017-09-03 06:18:07 -07:00
|
|
|
std::size_t bytes_transferred = 0;
|
2017-08-26 20:10:04 -07:00
|
|
|
ec.assign(0, ec.category());
|
2017-08-12 20:50:23 -07:00
|
|
|
// Make sure the stream is open
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! check_open(ec))
|
2017-09-03 06:18:07 -07:00
|
|
|
return bytes_transferred;
|
2016-10-24 08:12:09 -04:00
|
|
|
detail::frame_header fh;
|
2017-08-26 15:18:02 -07:00
|
|
|
if(! wr_cont_)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
begin_msg();
|
|
|
|
|
fh.rsv1 = wr_compress_;
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
2016-10-24 18:41:25 -04:00
|
|
|
else
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2016-10-24 18:41:25 -04:00
|
|
|
fh.rsv1 = false;
|
2016-10-24 11:18:31 -04:00
|
|
|
}
|
2016-10-24 18:41:25 -04:00
|
|
|
fh.rsv2 = false;
|
|
|
|
|
fh.rsv3 = false;
|
2017-08-26 15:18:02 -07:00
|
|
|
fh.op = wr_cont_ ?
|
2017-06-08 21:01:35 -07:00
|
|
|
detail::opcode::cont : wr_opcode_;
|
2017-06-24 10:13:17 -07:00
|
|
|
fh.mask = role_ == role_type::client;
|
2016-10-24 18:41:25 -04:00
|
|
|
auto remain = buffer_size(buffers);
|
2017-08-26 15:18:02 -07:00
|
|
|
if(wr_compress_)
|
2016-10-24 11:18:31 -04:00
|
|
|
{
|
2017-09-15 12:52:45 -07:00
|
|
|
buffers_suffix<
|
2016-10-24 18:41:25 -04:00
|
|
|
ConstBufferSequence> cb{buffers};
|
2016-10-24 11:18:31 -04:00
|
|
|
for(;;)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2016-10-24 18:41:25 -04:00
|
|
|
auto b = buffer(
|
2017-08-26 15:18:02 -07:00
|
|
|
wr_buf_.get(), wr_buf_size_);
|
2017-11-18 16:52:18 -08:00
|
|
|
auto const more = this->deflate(
|
|
|
|
|
b, cb, fin, bytes_transferred, ec);
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! check_ok(ec))
|
2017-09-03 06:18:07 -07:00
|
|
|
return bytes_transferred;
|
2016-10-24 18:41:25 -04:00
|
|
|
auto const n = buffer_size(b);
|
|
|
|
|
if(n == 0)
|
|
|
|
|
{
|
|
|
|
|
// The input was consumed, but there
|
|
|
|
|
// is no output due to compression
|
|
|
|
|
// latency.
|
|
|
|
|
BOOST_ASSERT(! fin);
|
|
|
|
|
BOOST_ASSERT(buffer_size(cb) == 0);
|
|
|
|
|
fh.fin = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(fh.mask)
|
|
|
|
|
{
|
2018-07-05 16:47:07 -07:00
|
|
|
fh.key = this->create_mask();
|
2016-10-24 18:41:25 -04:00
|
|
|
detail::prepared_key key;
|
|
|
|
|
detail::prepare_key(key, fh.key);
|
|
|
|
|
detail::mask_inplace(b, key);
|
|
|
|
|
}
|
|
|
|
|
fh.fin = ! more;
|
2016-10-24 11:18:31 -04:00
|
|
|
fh.len = n;
|
2017-08-15 16:59:17 -07:00
|
|
|
detail::fh_buffer fh_buf;
|
|
|
|
|
detail::write<
|
|
|
|
|
flat_static_buffer_base>(fh_buf, fh);
|
2017-08-26 15:18:02 -07:00
|
|
|
wr_cont_ = ! fin;
|
2016-10-24 08:12:09 -04:00
|
|
|
boost::asio::write(stream_,
|
2017-09-15 10:03:59 -07:00
|
|
|
buffers_cat(fh_buf.data(), b), ec);
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! check_ok(ec))
|
2017-09-03 06:18:07 -07:00
|
|
|
return bytes_transferred;
|
2016-10-24 18:41:25 -04:00
|
|
|
if(! more)
|
2016-10-24 11:18:31 -04:00
|
|
|
break;
|
2017-06-08 21:01:35 -07:00
|
|
|
fh.op = detail::opcode::cont;
|
2016-10-24 18:41:25 -04:00
|
|
|
fh.rsv1 = false;
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
2017-11-18 16:52:18 -08:00
|
|
|
if(fh.fin)
|
|
|
|
|
this->do_context_takeover_write(role_);
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
2017-08-26 20:10:04 -07:00
|
|
|
else if(! fh.mask)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
if(! wr_frag_)
|
2016-10-24 18:41:25 -04:00
|
|
|
{
|
|
|
|
|
// no mask, no autofrag
|
|
|
|
|
fh.fin = fin;
|
|
|
|
|
fh.len = remain;
|
2017-08-15 16:59:17 -07:00
|
|
|
detail::fh_buffer fh_buf;
|
|
|
|
|
detail::write<
|
|
|
|
|
flat_static_buffer_base>(fh_buf, fh);
|
2017-08-26 15:18:02 -07:00
|
|
|
wr_cont_ = ! fin;
|
2016-10-24 18:41:25 -04:00
|
|
|
boost::asio::write(stream_,
|
2017-09-15 10:03:59 -07:00
|
|
|
buffers_cat(fh_buf.data(), buffers), ec);
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! check_ok(ec))
|
2017-09-03 06:18:07 -07:00
|
|
|
return bytes_transferred;
|
|
|
|
|
bytes_transferred += remain;
|
2016-10-24 18:41:25 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// no mask, autofrag
|
2017-08-26 15:18:02 -07:00
|
|
|
BOOST_ASSERT(wr_buf_size_ != 0);
|
2017-09-15 12:52:45 -07:00
|
|
|
buffers_suffix<
|
2016-10-24 18:41:25 -04:00
|
|
|
ConstBufferSequence> cb{buffers};
|
|
|
|
|
for(;;)
|
|
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
auto const n = clamp(remain, wr_buf_size_);
|
2016-10-24 18:41:25 -04:00
|
|
|
remain -= n;
|
|
|
|
|
fh.len = n;
|
|
|
|
|
fh.fin = fin ? remain == 0 : false;
|
2017-08-15 16:59:17 -07:00
|
|
|
detail::fh_buffer fh_buf;
|
|
|
|
|
detail::write<
|
|
|
|
|
flat_static_buffer_base>(fh_buf, fh);
|
2017-08-26 15:18:02 -07:00
|
|
|
wr_cont_ = ! fin;
|
2016-10-24 18:41:25 -04:00
|
|
|
boost::asio::write(stream_,
|
2017-09-15 10:03:59 -07:00
|
|
|
buffers_cat(fh_buf.data(),
|
2017-09-15 09:55:03 -07:00
|
|
|
buffers_prefix(n, cb)), ec);
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! check_ok(ec))
|
2017-09-03 06:18:07 -07:00
|
|
|
return bytes_transferred;
|
|
|
|
|
bytes_transferred += n;
|
2016-10-24 18:41:25 -04:00
|
|
|
if(remain == 0)
|
|
|
|
|
break;
|
2017-06-08 21:01:35 -07:00
|
|
|
fh.op = detail::opcode::cont;
|
2016-10-24 18:41:25 -04:00
|
|
|
cb.consume(n);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-26 20:10:04 -07:00
|
|
|
else if(! wr_frag_)
|
2016-10-24 18:41:25 -04:00
|
|
|
{
|
|
|
|
|
// mask, no autofrag
|
2016-10-24 08:12:09 -04:00
|
|
|
fh.fin = fin;
|
|
|
|
|
fh.len = remain;
|
2018-07-05 16:47:07 -07:00
|
|
|
fh.key = this->create_mask();
|
2016-10-24 18:41:25 -04:00
|
|
|
detail::prepared_key key;
|
|
|
|
|
detail::prepare_key(key, fh.key);
|
2017-08-15 16:59:17 -07:00
|
|
|
detail::fh_buffer fh_buf;
|
|
|
|
|
detail::write<
|
|
|
|
|
flat_static_buffer_base>(fh_buf, fh);
|
2017-09-15 12:52:45 -07:00
|
|
|
buffers_suffix<
|
2016-10-24 18:41:25 -04:00
|
|
|
ConstBufferSequence> cb{buffers};
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
auto const n = clamp(remain, wr_buf_size_);
|
|
|
|
|
auto const b = buffer(wr_buf_.get(), n);
|
2016-10-24 18:41:25 -04:00
|
|
|
buffer_copy(b, cb);
|
2016-10-24 08:12:09 -04:00
|
|
|
cb.consume(n);
|
|
|
|
|
remain -= n;
|
2016-10-24 18:41:25 -04:00
|
|
|
detail::mask_inplace(b, key);
|
2017-08-26 15:18:02 -07:00
|
|
|
wr_cont_ = ! fin;
|
2016-10-24 08:12:09 -04:00
|
|
|
boost::asio::write(stream_,
|
2017-09-15 10:03:59 -07:00
|
|
|
buffers_cat(fh_buf.data(), b), ec);
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! check_ok(ec))
|
2017-09-03 06:18:07 -07:00
|
|
|
return bytes_transferred;
|
|
|
|
|
bytes_transferred += n;
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
while(remain > 0)
|
|
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
auto const n = clamp(remain, wr_buf_size_);
|
|
|
|
|
auto const b = buffer(wr_buf_.get(), n);
|
2016-10-24 18:41:25 -04:00
|
|
|
buffer_copy(b, cb);
|
2016-10-24 08:12:09 -04:00
|
|
|
cb.consume(n);
|
|
|
|
|
remain -= n;
|
2016-10-24 18:41:25 -04:00
|
|
|
detail::mask_inplace(b, key);
|
|
|
|
|
boost::asio::write(stream_, b, ec);
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! check_ok(ec))
|
2017-09-03 06:18:07 -07:00
|
|
|
return bytes_transferred;
|
|
|
|
|
bytes_transferred += n;
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
2017-08-26 20:10:04 -07:00
|
|
|
else
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2016-10-24 18:41:25 -04:00
|
|
|
// mask, autofrag
|
2017-08-26 15:18:02 -07:00
|
|
|
BOOST_ASSERT(wr_buf_size_ != 0);
|
2017-09-15 12:52:45 -07:00
|
|
|
buffers_suffix<
|
2016-10-24 18:41:25 -04:00
|
|
|
ConstBufferSequence> cb{buffers};
|
2016-10-24 11:18:31 -04:00
|
|
|
for(;;)
|
|
|
|
|
{
|
2018-07-05 16:47:07 -07:00
|
|
|
fh.key = this->create_mask();
|
2016-10-24 18:41:25 -04:00
|
|
|
detail::prepared_key key;
|
2016-10-24 11:18:31 -04:00
|
|
|
detail::prepare_key(key, fh.key);
|
2017-08-26 15:18:02 -07:00
|
|
|
auto const n = clamp(remain, wr_buf_size_);
|
|
|
|
|
auto const b = buffer(wr_buf_.get(), n);
|
2016-10-24 18:41:25 -04:00
|
|
|
buffer_copy(b, cb);
|
|
|
|
|
detail::mask_inplace(b, key);
|
2016-10-24 11:18:31 -04:00
|
|
|
fh.len = n;
|
|
|
|
|
remain -= n;
|
|
|
|
|
fh.fin = fin ? remain == 0 : false;
|
2017-08-26 15:18:02 -07:00
|
|
|
wr_cont_ = ! fh.fin;
|
2017-08-15 16:59:17 -07:00
|
|
|
detail::fh_buffer fh_buf;
|
|
|
|
|
detail::write<
|
|
|
|
|
flat_static_buffer_base>(fh_buf, fh);
|
2016-10-24 11:18:31 -04:00
|
|
|
boost::asio::write(stream_,
|
2017-09-15 10:03:59 -07:00
|
|
|
buffers_cat(fh_buf.data(), b), ec);
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! check_ok(ec))
|
2017-09-03 06:18:07 -07:00
|
|
|
return bytes_transferred;
|
|
|
|
|
bytes_transferred += n;
|
2016-10-24 11:18:31 -04:00
|
|
|
if(remain == 0)
|
|
|
|
|
break;
|
2017-06-08 21:01:35 -07:00
|
|
|
fh.op = detail::opcode::cont;
|
2016-10-24 11:18:31 -04:00
|
|
|
cb.consume(n);
|
|
|
|
|
}
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
2017-09-03 06:18:07 -07:00
|
|
|
return bytes_transferred;
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-10-24 08:12:09 -04:00
|
|
|
template<class ConstBufferSequence, class WriteHandler>
|
2017-09-07 07:39:52 -07:00
|
|
|
BOOST_ASIO_INITFN_RESULT_TYPE(
|
|
|
|
|
WriteHandler, void(error_code, std::size_t))
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-07-15 17:05:24 -07:00
|
|
|
async_write_some(bool fin,
|
2017-06-20 21:28:17 -07:00
|
|
|
ConstBufferSequence const& bs, WriteHandler&& handler)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
2017-05-10 12:03:00 -07:00
|
|
|
static_assert(is_async_stream<next_layer_type>::value,
|
2016-10-24 08:12:09 -04:00
|
|
|
"AsyncStream requirements not met");
|
2017-09-07 07:39:52 -07:00
|
|
|
static_assert(boost::asio::is_const_buffer_sequence<
|
2016-10-24 08:12:09 -04:00
|
|
|
ConstBufferSequence>::value,
|
|
|
|
|
"ConstBufferSequence requirements not met");
|
2018-01-26 08:58:19 -08:00
|
|
|
BOOST_BEAST_HANDLER_INIT(
|
|
|
|
|
WriteHandler, void(error_code, std::size_t));
|
2017-09-07 07:39:52 -07:00
|
|
|
write_some_op<ConstBufferSequence, BOOST_ASIO_HANDLER_TYPE(
|
|
|
|
|
WriteHandler, void(error_code, std::size_t))>{
|
2017-12-02 12:59:30 +01:00
|
|
|
std::move(init.completion_handler), *this, fin, bs}(
|
2017-09-03 06:18:07 -07:00
|
|
|
{}, 0, false);
|
2017-05-06 12:36:40 -07:00
|
|
|
return init.result.get();
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-20 21:28:17 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-10-24 08:12:09 -04:00
|
|
|
template<class ConstBufferSequence>
|
2017-09-03 06:18:07 -07:00
|
|
|
std::size_t
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2016-10-24 08:12:09 -04:00
|
|
|
write(ConstBufferSequence const& buffers)
|
|
|
|
|
{
|
2017-05-10 12:03:00 -07:00
|
|
|
static_assert(is_sync_stream<next_layer_type>::value,
|
2016-10-24 08:12:09 -04:00
|
|
|
"SyncStream requirements not met");
|
2017-09-07 07:39:52 -07:00
|
|
|
static_assert(boost::asio::is_const_buffer_sequence<
|
2016-10-24 08:12:09 -04:00
|
|
|
ConstBufferSequence>::value,
|
|
|
|
|
"ConstBufferSequence requirements not met");
|
|
|
|
|
error_code ec;
|
2017-09-03 06:18:07 -07:00
|
|
|
auto const bytes_transferred = write(buffers, ec);
|
2016-10-24 08:12:09 -04:00
|
|
|
if(ec)
|
2017-05-22 15:30:12 -07:00
|
|
|
BOOST_THROW_EXCEPTION(system_error{ec});
|
2017-09-03 06:18:07 -07:00
|
|
|
return bytes_transferred;
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-10-24 08:12:09 -04:00
|
|
|
template<class ConstBufferSequence>
|
2017-09-03 06:18:07 -07:00
|
|
|
std::size_t
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2016-10-24 08:12:09 -04:00
|
|
|
write(ConstBufferSequence const& buffers, error_code& ec)
|
|
|
|
|
{
|
2017-05-10 12:03:00 -07:00
|
|
|
static_assert(is_sync_stream<next_layer_type>::value,
|
2016-10-24 08:12:09 -04:00
|
|
|
"SyncStream requirements not met");
|
2017-09-07 07:39:52 -07:00
|
|
|
static_assert(boost::asio::is_const_buffer_sequence<
|
2016-10-24 08:12:09 -04:00
|
|
|
ConstBufferSequence>::value,
|
|
|
|
|
"ConstBufferSequence requirements not met");
|
2017-09-03 06:18:07 -07:00
|
|
|
return write_some(true, buffers, ec);
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-06-20 21:28:17 -07:00
|
|
|
template<class ConstBufferSequence, class WriteHandler>
|
2017-09-07 07:39:52 -07:00
|
|
|
BOOST_ASIO_INITFN_RESULT_TYPE(
|
|
|
|
|
WriteHandler, void(error_code, std::size_t))
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-06-20 21:28:17 -07:00
|
|
|
async_write(
|
|
|
|
|
ConstBufferSequence const& bs, WriteHandler&& handler)
|
|
|
|
|
{
|
|
|
|
|
static_assert(is_async_stream<next_layer_type>::value,
|
|
|
|
|
"AsyncStream requirements not met");
|
2017-09-07 07:39:52 -07:00
|
|
|
static_assert(boost::asio::is_const_buffer_sequence<
|
2017-06-20 21:28:17 -07:00
|
|
|
ConstBufferSequence>::value,
|
|
|
|
|
"ConstBufferSequence requirements not met");
|
2018-01-26 08:58:19 -08:00
|
|
|
BOOST_BEAST_HANDLER_INIT(
|
|
|
|
|
WriteHandler, void(error_code, std::size_t));
|
2017-09-07 07:39:52 -07:00
|
|
|
write_some_op<ConstBufferSequence, BOOST_ASIO_HANDLER_TYPE(
|
|
|
|
|
WriteHandler, void(error_code, std::size_t))>{
|
2017-12-02 12:59:30 +01:00
|
|
|
std::move(init.completion_handler), *this, true, bs}(
|
2017-09-03 06:18:07 -07:00
|
|
|
{}, 0, false);
|
2017-06-20 21:28:17 -07:00
|
|
|
return init.result.get();
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-24 08:12:09 -04:00
|
|
|
} // websocket
|
|
|
|
|
} // beast
|
2017-07-20 13:40:34 -07:00
|
|
|
} // boost
|
2016-10-24 08:12:09 -04:00
|
|
|
|
|
|
|
|
#endif
|