2017-07-20 08:01:46 -07:00
|
|
|
//
|
2017-07-24 09:42:36 -07:00
|
|
|
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2017-07-20 08:01:46 -07: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
|
|
|
|
|
//
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2019-01-19 07:24:00 -08:00
|
|
|
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_CLOSE_HPP
|
|
|
|
|
#define BOOST_BEAST_WEBSOCKET_IMPL_CLOSE_HPP
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-08-13 11:55:18 -07:00
|
|
|
#include <boost/beast/websocket/teardown.hpp>
|
2019-01-19 07:24:00 -08:00
|
|
|
#include <boost/beast/websocket/detail/mask.hpp>
|
2019-01-07 13:35:19 -08:00
|
|
|
#include <boost/beast/core/async_op_base.hpp>
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/core/flat_static_buffer.hpp>
|
2019-02-04 21:52:54 -08:00
|
|
|
#include <boost/beast/core/stream_traits.hpp>
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/core/detail/config.hpp>
|
2017-08-13 09:45:49 -07:00
|
|
|
#include <boost/asio/coroutine.hpp>
|
2017-09-07 07:39:52 -07:00
|
|
|
#include <boost/asio/post.hpp>
|
2017-05-22 15:30:12 -07:00
|
|
|
#include <boost/throw_exception.hpp>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <memory>
|
|
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
namespace boost {
|
2017-07-20 08:01:46 -07:00
|
|
|
namespace beast {
|
|
|
|
|
namespace websocket {
|
|
|
|
|
|
2017-08-13 09:45:49 -07:00
|
|
|
/* Close the WebSocket Connection
|
|
|
|
|
|
|
|
|
|
This composed operation sends the close frame if it hasn't already
|
|
|
|
|
been sent, then reads and discards frames until receiving a close
|
|
|
|
|
frame. Finally it invokes the teardown operation to shut down the
|
|
|
|
|
underlying connection.
|
|
|
|
|
*/
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-07-20 08:01:46 -07:00
|
|
|
template<class Handler>
|
2017-11-18 16:52:18 -08:00
|
|
|
class stream<NextLayer, deflateSupported>::close_op
|
2019-01-07 13:35:19 -08:00
|
|
|
: public beast::stable_async_op_base<
|
2019-02-04 21:52:54 -08:00
|
|
|
Handler, beast::executor_type<stream>>
|
2019-01-05 19:40:46 -08:00
|
|
|
, public net::coroutine
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-08-13 11:55:18 -07:00
|
|
|
struct state
|
|
|
|
|
{
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>& ws;
|
2017-08-15 16:59:17 -07:00
|
|
|
detail::frame_buffer fb;
|
2017-08-17 22:38:50 -07:00
|
|
|
error_code ev;
|
2017-08-31 17:52:09 -07:00
|
|
|
bool cont = false;
|
2017-08-13 11:55:18 -07:00
|
|
|
|
|
|
|
|
state(
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>& ws_,
|
2017-08-13 11:55:18 -07:00
|
|
|
close_reason const& cr)
|
|
|
|
|
: ws(ws_)
|
|
|
|
|
{
|
|
|
|
|
// Serialize the close frame
|
|
|
|
|
ws.template write_close<
|
|
|
|
|
flat_static_buffer_base>(fb, cr);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
state& d_;
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
|
public:
|
2018-02-18 18:46:12 -08:00
|
|
|
static constexpr int id = 4; // for soft_mutex
|
|
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
template<class Handler_>
|
2017-08-13 09:45:49 -07:00
|
|
|
close_op(
|
2019-01-05 19:40:46 -08:00
|
|
|
Handler_&& h,
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>& ws,
|
2017-08-13 09:45:49 -07:00
|
|
|
close_reason const& cr)
|
2019-01-09 16:11:15 -08:00
|
|
|
: stable_async_op_base<
|
2019-02-04 21:52:54 -08:00
|
|
|
Handler, beast::executor_type<stream>>(
|
2019-01-09 16:11:15 -08:00
|
|
|
std::forward<Handler_>(h), ws.get_executor())
|
2019-01-07 13:35:19 -08:00
|
|
|
, d_(beast::allocate_stable<state>(
|
2019-01-05 19:40:46 -08:00
|
|
|
*this, ws, cr))
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 07:39:52 -07:00
|
|
|
void
|
|
|
|
|
operator()(
|
|
|
|
|
error_code ec = {},
|
|
|
|
|
std::size_t bytes_transferred = 0,
|
2019-01-05 19:40:46 -08:00
|
|
|
bool cont = true)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
using beast::detail::clamp;
|
|
|
|
|
d_.cont = cont;
|
|
|
|
|
BOOST_ASIO_CORO_REENTER(*this)
|
2017-06-29 09:48:30 -07:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
// Attempt to acquire write block
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! d_.ws.impl_->wr_block.try_lock(this))
|
2019-01-05 19:40:46 -08:00
|
|
|
{
|
|
|
|
|
// Suspend
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->paused_close.emplace(std::move(*this));
|
2017-08-26 20:10:04 -07:00
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
// Acquire the write block
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->wr_block.lock(this);
|
2017-08-31 17:52:09 -07:00
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
// Resume
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
|
|
|
|
net::post(
|
|
|
|
|
d_.ws.get_executor(), std::move(*this));
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(d_.ws.impl_->wr_block.is_locked(this));
|
2019-01-05 19:40:46 -08:00
|
|
|
}
|
2017-08-13 11:55:18 -07:00
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
// Make sure the stream is open
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! d_.ws.impl_->check_open(ec))
|
2019-01-05 19:40:46 -08:00
|
|
|
goto upcall;
|
2018-11-25 18:51:38 +01:00
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
// Can't call close twice
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(! d_.ws.impl_->wr_close);
|
2017-08-13 11:55:18 -07:00
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
// Change status to closing
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(d_.ws.impl_->status_ == status::open);
|
|
|
|
|
d_.ws.impl_->status_ = status::closing;
|
2017-08-13 11:55:18 -07:00
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
// Send close frame
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->wr_close = true;
|
2017-08-13 11:55:18 -07:00
|
|
|
BOOST_ASIO_CORO_YIELD
|
2019-01-19 07:24:00 -08:00
|
|
|
net::async_write(d_.ws.impl_->stream,
|
2019-01-05 19:40:46 -08:00
|
|
|
d_.fb.data(), std::move(*this));
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! d_.ws.impl_->check_ok(ec))
|
2017-08-13 11:55:18 -07:00
|
|
|
goto upcall;
|
2017-08-26 20:10:04 -07:00
|
|
|
|
2019-01-19 07:24:00 -08:00
|
|
|
if(d_.ws.impl_->rd_close)
|
2019-01-05 19:40:46 -08:00
|
|
|
{
|
|
|
|
|
// This happens when the read_op gets a close frame
|
|
|
|
|
// at the same time close_op is sending the close frame.
|
|
|
|
|
// The read_op will be suspended on the write block.
|
|
|
|
|
goto teardown;
|
|
|
|
|
}
|
2017-08-13 11:55:18 -07:00
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
// Maybe suspend
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! d_.ws.impl_->rd_block.try_lock(this))
|
2017-08-13 11:55:18 -07:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
// Suspend
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->paused_r_close.emplace(std::move(*this));
|
2019-01-05 19:40:46 -08:00
|
|
|
|
|
|
|
|
// Acquire the read block
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->rd_block.lock(this);
|
2019-01-05 19:40:46 -08:00
|
|
|
|
|
|
|
|
// Resume
|
2017-08-13 11:55:18 -07:00
|
|
|
BOOST_ASIO_CORO_YIELD
|
2019-01-05 19:40:46 -08:00
|
|
|
net::post(
|
|
|
|
|
d_.ws.get_executor(), std::move(*this));
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(d_.ws.impl_->rd_block.is_locked(this));
|
2019-01-05 19:40:46 -08:00
|
|
|
|
|
|
|
|
// Make sure the stream is open
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(d_.ws.impl_->status_ != status::open);
|
|
|
|
|
BOOST_ASSERT(d_.ws.impl_->status_ != status::closed);
|
|
|
|
|
if( d_.ws.impl_->status_ == status::failed)
|
2017-08-13 11:55:18 -07:00
|
|
|
goto upcall;
|
2019-01-05 19:40:46 -08:00
|
|
|
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(! d_.ws.impl_->rd_close);
|
2017-08-13 11:55:18 -07:00
|
|
|
}
|
2019-01-05 19:40:46 -08:00
|
|
|
|
|
|
|
|
// Drain
|
2019-01-19 07:24:00 -08:00
|
|
|
if(d_.ws.impl_->rd_remain > 0)
|
2019-01-05 19:40:46 -08:00
|
|
|
goto read_payload;
|
|
|
|
|
for(;;)
|
2017-08-13 11:55:18 -07:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
// Read frame header
|
|
|
|
|
while(! d_.ws.parse_fh(
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->rd_fh, d_.ws.impl_->rd_buf, d_.ev))
|
2017-08-13 11:55:18 -07:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
if(d_.ev)
|
2017-08-17 22:38:50 -07:00
|
|
|
goto teardown;
|
2017-08-13 11:55:18 -07:00
|
|
|
BOOST_ASIO_CORO_YIELD
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->stream.async_read_some(
|
|
|
|
|
d_.ws.impl_->rd_buf.prepare(read_size(d_.ws.impl_->rd_buf,
|
|
|
|
|
d_.ws.impl_->rd_buf.max_size())),
|
2017-08-13 11:55:18 -07:00
|
|
|
std::move(*this));
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! d_.ws.impl_->check_ok(ec))
|
2017-08-13 11:55:18 -07:00
|
|
|
goto upcall;
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->rd_buf.commit(bytes_transferred);
|
2019-01-05 19:40:46 -08:00
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
if(detail::is_control(d_.ws.impl_->rd_fh.op))
|
2019-01-05 19:40:46 -08:00
|
|
|
{
|
|
|
|
|
// Process control frame
|
2019-01-19 07:24:00 -08:00
|
|
|
if(d_.ws.impl_->rd_fh.op == detail::opcode::close)
|
2019-01-05 19:40:46 -08:00
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(! d_.ws.impl_->rd_close);
|
|
|
|
|
d_.ws.impl_->rd_close = true;
|
2019-01-05 19:40:46 -08:00
|
|
|
auto const mb = buffers_prefix(
|
2019-01-19 07:24:00 -08:00
|
|
|
clamp(d_.ws.impl_->rd_fh.len),
|
|
|
|
|
d_.ws.impl_->rd_buf.data());
|
|
|
|
|
if(d_.ws.impl_->rd_fh.len > 0 && d_.ws.impl_->rd_fh.mask)
|
|
|
|
|
detail::mask_inplace(mb, d_.ws.impl_->rd_key);
|
|
|
|
|
detail::read_close(d_.ws.impl_->cr, mb, d_.ev);
|
2019-01-05 19:40:46 -08:00
|
|
|
if(d_.ev)
|
|
|
|
|
goto teardown;
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->rd_buf.consume(clamp(d_.ws.impl_->rd_fh.len));
|
2019-01-05 19:40:46 -08:00
|
|
|
goto teardown;
|
|
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->rd_buf.consume(clamp(d_.ws.impl_->rd_fh.len));
|
2019-01-05 19:40:46 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
read_payload:
|
2019-01-19 07:24:00 -08:00
|
|
|
while(d_.ws.impl_->rd_buf.size() < d_.ws.impl_->rd_remain)
|
2019-01-05 19:40:46 -08:00
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->rd_remain -= d_.ws.impl_->rd_buf.size();
|
|
|
|
|
d_.ws.impl_->rd_buf.consume(d_.ws.impl_->rd_buf.size());
|
2019-01-05 19:40:46 -08:00
|
|
|
BOOST_ASIO_CORO_YIELD
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->stream.async_read_some(
|
|
|
|
|
d_.ws.impl_->rd_buf.prepare(read_size(d_.ws.impl_->rd_buf,
|
|
|
|
|
d_.ws.impl_->rd_buf.max_size())),
|
2019-01-05 19:40:46 -08:00
|
|
|
std::move(*this));
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! d_.ws.impl_->check_ok(ec))
|
2019-01-05 19:40:46 -08:00
|
|
|
goto upcall;
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->rd_buf.commit(bytes_transferred);
|
2019-01-05 19:40:46 -08:00
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(d_.ws.impl_->rd_buf.size() >= d_.ws.impl_->rd_remain);
|
|
|
|
|
d_.ws.impl_->rd_buf.consume(clamp(d_.ws.impl_->rd_remain));
|
|
|
|
|
d_.ws.impl_->rd_remain = 0;
|
2017-08-13 11:55:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
teardown:
|
|
|
|
|
// Teardown
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(d_.ws.impl_->wr_block.is_locked(this));
|
2019-01-05 19:40:46 -08:00
|
|
|
using beast::websocket::async_teardown;
|
2018-04-09 16:22:15 -07:00
|
|
|
BOOST_ASIO_CORO_YIELD
|
2019-01-19 07:24:00 -08:00
|
|
|
async_teardown(d_.ws.impl_->role,
|
|
|
|
|
d_.ws.impl_->stream, std::move(*this));
|
|
|
|
|
BOOST_ASSERT(d_.ws.impl_->wr_block.is_locked(this));
|
2019-01-05 19:40:46 -08:00
|
|
|
if(ec == net::error::eof)
|
|
|
|
|
{
|
|
|
|
|
// Rationale:
|
|
|
|
|
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
|
|
|
|
|
ec = {};
|
|
|
|
|
}
|
|
|
|
|
if(! ec)
|
|
|
|
|
ec = d_.ev;
|
|
|
|
|
if(ec)
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->status_ = status::failed;
|
2019-01-05 19:40:46 -08:00
|
|
|
else
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->status_ = status::closed;
|
|
|
|
|
d_.ws.impl_->close();
|
2019-01-05 19:40:46 -08:00
|
|
|
|
|
|
|
|
upcall:
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(d_.ws.impl_->wr_block.is_locked(this));
|
|
|
|
|
d_.ws.impl_->wr_block.unlock(this);
|
|
|
|
|
if(d_.ws.impl_->rd_block.try_unlock(this))
|
|
|
|
|
d_.ws.impl_->paused_r_rd.maybe_invoke();
|
|
|
|
|
d_.ws.impl_->paused_rd.maybe_invoke() ||
|
|
|
|
|
d_.ws.impl_->paused_ping.maybe_invoke() ||
|
|
|
|
|
d_.ws.impl_->paused_wr.maybe_invoke();
|
2019-02-15 15:47:36 -08:00
|
|
|
this->invoke(d_.cont, ec);
|
2017-08-26 20:10:04 -07:00
|
|
|
}
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
2019-01-05 19:40:46 -08:00
|
|
|
};
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-07-15 17:05:24 -07:00
|
|
|
//------------------------------------------------------------------------------
|
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
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2016-10-24 08:12:09 -04:00
|
|
|
close(close_reason const& cr)
|
|
|
|
|
{
|
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");
|
|
|
|
|
error_code ec;
|
|
|
|
|
close(cr, ec);
|
|
|
|
|
if(ec)
|
2017-05-22 15:30:12 -07:00
|
|
|
BOOST_THROW_EXCEPTION(system_error{ec});
|
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
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2016-10-24 08:12:09 -04:00
|
|
|
close(close_reason const& cr, 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-08-01 20:15:07 -07:00
|
|
|
using beast::detail::clamp;
|
2019-01-19 07:24:00 -08:00
|
|
|
ec = {};
|
2017-08-13 09:45:49 -07:00
|
|
|
// Make sure the stream is open
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! impl_->check_open(ec))
|
2017-08-13 09:45:49 -07:00
|
|
|
return;
|
2017-07-15 17:05:24 -07:00
|
|
|
// If rd_close_ is set then we already sent a close
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(! impl_->rd_close);
|
|
|
|
|
BOOST_ASSERT(! impl_->wr_close);
|
|
|
|
|
impl_->wr_close = true;
|
2017-08-01 20:15:07 -07:00
|
|
|
{
|
2017-08-15 16:59:17 -07:00
|
|
|
detail::frame_buffer fb;
|
2017-08-01 20:15:07 -07:00
|
|
|
write_close<flat_static_buffer_base>(fb, cr);
|
2019-01-19 07:24:00 -08:00
|
|
|
net::write(impl_->stream, fb.data(), ec);
|
2017-08-01 20:15:07 -07:00
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! impl_->check_ok(ec))
|
2017-07-15 17:05:24 -07:00
|
|
|
return;
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->status_ = status::closing;
|
2018-01-01 17:49:19 -08:00
|
|
|
error_code result;
|
2017-08-01 20:15:07 -07:00
|
|
|
// Drain the connection
|
2019-01-19 07:24:00 -08:00
|
|
|
if(impl_->rd_remain > 0)
|
2017-08-01 20:15:07 -07:00
|
|
|
goto read_payload;
|
|
|
|
|
for(;;)
|
|
|
|
|
{
|
|
|
|
|
// Read frame header
|
2019-01-19 07:24:00 -08:00
|
|
|
while(! parse_fh(
|
|
|
|
|
impl_->rd_fh, impl_->rd_buf, result))
|
2017-08-01 20:15:07 -07:00
|
|
|
{
|
2018-01-01 17:49:19 -08:00
|
|
|
if(result)
|
|
|
|
|
return do_fail(
|
|
|
|
|
close_code::none, result, ec);
|
2017-08-01 20:15:07 -07:00
|
|
|
auto const bytes_transferred =
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->stream.read_some(
|
|
|
|
|
impl_->rd_buf.prepare(read_size(impl_->rd_buf,
|
|
|
|
|
impl_->rd_buf.max_size())), ec);
|
|
|
|
|
if(! impl_->check_ok(ec))
|
2017-08-01 20:15:07 -07:00
|
|
|
return;
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->rd_buf.commit(bytes_transferred);
|
2017-08-01 20:15:07 -07:00
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
if(detail::is_control(impl_->rd_fh.op))
|
2017-08-01 20:15:07 -07:00
|
|
|
{
|
|
|
|
|
// Process control frame
|
2019-01-19 07:24:00 -08:00
|
|
|
if(impl_->rd_fh.op == detail::opcode::close)
|
2017-08-01 20:15:07 -07:00
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(! impl_->rd_close);
|
|
|
|
|
impl_->rd_close = true;
|
2017-09-15 09:55:03 -07:00
|
|
|
auto const mb = buffers_prefix(
|
2019-01-19 07:24:00 -08:00
|
|
|
clamp(impl_->rd_fh.len),
|
|
|
|
|
impl_->rd_buf.data());
|
|
|
|
|
if(impl_->rd_fh.len > 0 && impl_->rd_fh.mask)
|
|
|
|
|
detail::mask_inplace(mb, impl_->rd_key);
|
|
|
|
|
detail::read_close(impl_->cr, mb, result);
|
2018-01-01 17:49:19 -08:00
|
|
|
if(result)
|
2017-08-26 20:10:04 -07:00
|
|
|
{
|
2018-01-01 17:49:19 -08:00
|
|
|
// Protocol violation
|
|
|
|
|
return do_fail(
|
|
|
|
|
close_code::none, result, ec);
|
2017-08-26 20:10:04 -07:00
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->rd_buf.consume(clamp(impl_->rd_fh.len));
|
2017-08-01 20:15:07 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->rd_buf.consume(clamp(impl_->rd_fh.len));
|
2017-08-01 20:15:07 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
read_payload:
|
2019-01-19 07:24:00 -08:00
|
|
|
while(impl_->rd_buf.size() < impl_->rd_remain)
|
2017-08-01 20:15:07 -07:00
|
|
|
{
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->rd_remain -= impl_->rd_buf.size();
|
|
|
|
|
impl_->rd_buf.consume(impl_->rd_buf.size());
|
2017-08-01 20:15:07 -07:00
|
|
|
auto const bytes_transferred =
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->stream.read_some(
|
|
|
|
|
impl_->rd_buf.prepare(read_size(impl_->rd_buf,
|
|
|
|
|
impl_->rd_buf.max_size())), ec);
|
|
|
|
|
if(! impl_->check_ok(ec))
|
2017-08-01 20:15:07 -07:00
|
|
|
return;
|
2019-01-19 07:24:00 -08:00
|
|
|
impl_->rd_buf.commit(bytes_transferred);
|
2017-08-01 20:15:07 -07:00
|
|
|
}
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(
|
|
|
|
|
impl_->rd_buf.size() >= impl_->rd_remain);
|
|
|
|
|
impl_->rd_buf.consume(clamp(impl_->rd_remain));
|
|
|
|
|
impl_->rd_remain = 0;
|
2017-08-01 20:15:07 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// _Close the WebSocket Connection_
|
|
|
|
|
do_fail(close_code::none, error::closed, ec);
|
|
|
|
|
if(ec == error::closed)
|
2019-01-19 07:24:00 -08:00
|
|
|
ec = {};
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-07-15 17:05:24 -07:00
|
|
|
template<class CloseHandler>
|
2017-09-07 07:39:52 -07:00
|
|
|
BOOST_ASIO_INITFN_RESULT_TYPE(
|
|
|
|
|
CloseHandler, void(error_code))
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-07-15 17:05:24 -07:00
|
|
|
async_close(close_reason const& cr, CloseHandler&& handler)
|
|
|
|
|
{
|
|
|
|
|
static_assert(is_async_stream<next_layer_type>::value,
|
|
|
|
|
"AsyncStream requirements not met");
|
2018-01-26 08:58:19 -08:00
|
|
|
BOOST_BEAST_HANDLER_INIT(
|
|
|
|
|
CloseHandler, void(error_code));
|
2017-09-07 07:39:52 -07:00
|
|
|
close_op<BOOST_ASIO_HANDLER_TYPE(
|
|
|
|
|
CloseHandler, void(error_code))>{
|
2017-12-02 12:59:30 +01:00
|
|
|
std::move(init.completion_handler), *this, cr}(
|
2017-08-26 20:10:04 -07:00
|
|
|
{}, 0, false);
|
2017-07-15 17:05:24 -07:00
|
|
|
return init.result.get();
|
|
|
|
|
}
|
2016-10-24 08:12:09 -04:00
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
} // websocket
|
|
|
|
|
} // beast
|
2017-07-20 13:40:34 -07:00
|
|
|
} // boost
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
|
#endif
|