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
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_CLOSE_IPP
|
|
|
|
|
#define BOOST_BEAST_WEBSOCKET_IMPL_CLOSE_IPP
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-08-13 11:55:18 -07:00
|
|
|
#include <boost/beast/websocket/teardown.hpp>
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/core/handler_ptr.hpp>
|
|
|
|
|
#include <boost/beast/core/flat_static_buffer.hpp>
|
|
|
|
|
#include <boost/beast/core/type_traits.hpp>
|
|
|
|
|
#include <boost/beast/core/detail/config.hpp>
|
2017-09-07 07:39:52 -07:00
|
|
|
#include <boost/asio/associated_allocator.hpp>
|
|
|
|
|
#include <boost/asio/associated_executor.hpp>
|
2017-08-13 09:45:49 -07:00
|
|
|
#include <boost/asio/coroutine.hpp>
|
2017-05-14 09:25:43 -07:00
|
|
|
#include <boost/asio/handler_continuation_hook.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
|
2017-08-13 09:45:49 -07:00
|
|
|
: public boost::asio::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-12-04 13:02:31 -08:00
|
|
|
Handler const&,
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handler_ptr<state, Handler> d_;
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
close_op(close_op&&) = default;
|
2017-12-02 12:59:30 +01:00
|
|
|
close_op(close_op const&) = delete;
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-08-13 09:45:49 -07:00
|
|
|
template<class DeducedHandler>
|
|
|
|
|
close_op(
|
|
|
|
|
DeducedHandler&& 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)
|
2017-08-13 11:55:18 -07:00
|
|
|
: d_(std::forward<DeducedHandler>(h), ws, cr)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 07:39:52 -07:00
|
|
|
using allocator_type =
|
|
|
|
|
boost::asio::associated_allocator_t<Handler>;
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-09-07 07:39:52 -07:00
|
|
|
allocator_type
|
|
|
|
|
get_allocator() const noexcept
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-12-02 05:57:06 -08:00
|
|
|
return (boost::asio::get_associated_allocator)(d_.handler());
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
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-07-20 08:01:46 -07:00
|
|
|
{
|
2017-12-02 05:57:06 -08:00
|
|
|
return (boost::asio::get_associated_executor)(
|
2017-09-07 07:39:52 -07:00
|
|
|
d_.handler(), d_->ws.get_executor());
|
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,
|
|
|
|
|
bool cont = true);
|
|
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
friend
|
|
|
|
|
bool asio_handler_is_continuation(close_op* op)
|
|
|
|
|
{
|
2017-06-29 09:48:30 -07:00
|
|
|
using boost::asio::asio_handler_is_continuation;
|
2017-08-26 20:10:04 -07:00
|
|
|
return op->d_->cont || asio_handler_is_continuation(
|
2017-08-13 11:55:18 -07:00
|
|
|
std::addressof(op->d_.handler()));
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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-02-06 20:07:03 -05:00
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-08-31 17:52:09 -07:00
|
|
|
close_op<Handler>::
|
2017-08-26 20:10:04 -07:00
|
|
|
operator()(
|
|
|
|
|
error_code ec,
|
|
|
|
|
std::size_t bytes_transferred,
|
|
|
|
|
bool cont)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2017-08-13 11:55:18 -07:00
|
|
|
using beast::detail::clamp;
|
|
|
|
|
auto& d = *d_;
|
2017-08-26 20:10:04 -07:00
|
|
|
d.cont = cont;
|
2017-08-13 09:45:49 -07:00
|
|
|
BOOST_ASIO_CORO_REENTER(*this)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-08-13 09:45:49 -07:00
|
|
|
// Maybe suspend
|
2018-02-15 11:30:15 -08:00
|
|
|
if(d.ws.wr_block_.try_lock(this))
|
2017-06-29 09:48:30 -07:00
|
|
|
{
|
2017-08-13 09:45:49 -07:00
|
|
|
// Make sure the stream is open
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! d.ws.check_open(ec))
|
2017-08-13 09:45:49 -07:00
|
|
|
goto upcall;
|
2017-06-29 09:48:30 -07:00
|
|
|
}
|
2017-08-13 09:45:49 -07:00
|
|
|
else
|
2017-06-29 09:48:30 -07:00
|
|
|
{
|
2017-08-13 09:45:49 -07:00
|
|
|
// Suspend
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2017-08-26 15:18:02 -07:00
|
|
|
d.ws.paused_close_.emplace(std::move(*this));
|
2017-08-13 09:45:49 -07:00
|
|
|
|
|
|
|
|
// Acquire the write block
|
2018-02-15 11:30:15 -08:00
|
|
|
d.ws.wr_block_.lock(this);
|
2017-08-13 09:45:49 -07:00
|
|
|
|
|
|
|
|
// Resume
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2017-09-07 07:39:52 -07:00
|
|
|
boost::asio::post(
|
|
|
|
|
d.ws.get_executor(), std::move(*this));
|
2018-02-15 11:30:15 -08:00
|
|
|
BOOST_ASSERT(d.ws.wr_block_.is_locked(this));
|
2017-08-13 09:45:49 -07:00
|
|
|
|
|
|
|
|
// Make sure the stream is open
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! d.ws.check_open(ec))
|
2017-08-13 09:45:49 -07:00
|
|
|
goto upcall;
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
2017-06-29 09:48:30 -07:00
|
|
|
|
2017-08-26 20:10:04 -07:00
|
|
|
// Can't call close twice
|
2017-08-13 11:55:18 -07:00
|
|
|
BOOST_ASSERT(! d.ws.wr_close_);
|
2017-08-26 20:10:04 -07:00
|
|
|
|
2017-08-31 17:52:09 -07:00
|
|
|
// Change status to closing
|
|
|
|
|
BOOST_ASSERT(d.ws.status_ == status::open);
|
|
|
|
|
d.ws.status_ = status::closing;
|
|
|
|
|
|
2017-08-26 20:10:04 -07:00
|
|
|
// Send close frame
|
2017-08-13 11:55:18 -07:00
|
|
|
d.ws.wr_close_ = true;
|
2017-08-13 09:45:49 -07:00
|
|
|
BOOST_ASIO_CORO_YIELD
|
2017-08-13 11:55:18 -07:00
|
|
|
boost::asio::async_write(d.ws.stream_,
|
|
|
|
|
d.fb.data(), std::move(*this));
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! d.ws.check_ok(ec))
|
2017-08-13 11:55:18 -07:00
|
|
|
goto upcall;
|
|
|
|
|
|
|
|
|
|
if(d.ws.rd_close_)
|
|
|
|
|
{
|
|
|
|
|
// This happens when the read_op gets a close frame
|
2017-08-31 17:52:09 -07:00
|
|
|
// at the same time close_op is sending the close frame.
|
|
|
|
|
// The read_op will be suspended on the write block.
|
2017-08-13 11:55:18 -07:00
|
|
|
goto teardown;
|
|
|
|
|
}
|
2017-08-31 17:52:09 -07:00
|
|
|
|
2017-08-26 20:10:04 -07:00
|
|
|
// Maybe suspend
|
2018-02-15 11:30:15 -08:00
|
|
|
if(! d.ws.rd_block_.try_lock(this))
|
2017-08-13 11:55:18 -07:00
|
|
|
{
|
|
|
|
|
// Suspend
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2017-08-26 15:18:02 -07:00
|
|
|
d.ws.paused_r_close_.emplace(std::move(*this));
|
2017-08-13 11:55:18 -07:00
|
|
|
|
|
|
|
|
// Acquire the read block
|
2018-02-15 11:30:15 -08:00
|
|
|
d.ws.rd_block_.lock(this);
|
2017-08-13 11:55:18 -07:00
|
|
|
|
|
|
|
|
// Resume
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2017-09-07 07:39:52 -07:00
|
|
|
boost::asio::post(
|
|
|
|
|
d.ws.get_executor(), std::move(*this));
|
2018-02-15 11:30:15 -08:00
|
|
|
BOOST_ASSERT(d.ws.rd_block_.is_locked(this));
|
2017-08-13 11:55:18 -07:00
|
|
|
|
2017-08-26 20:10:04 -07:00
|
|
|
// Make sure the stream is open
|
2017-08-31 17:52:09 -07:00
|
|
|
BOOST_ASSERT(d.ws.status_ != status::open);
|
|
|
|
|
BOOST_ASSERT(d.ws.status_ != status::closed);
|
|
|
|
|
if( d.ws.status_ == status::failed)
|
2017-08-13 11:55:18 -07:00
|
|
|
goto upcall;
|
2017-08-26 20:10:04 -07:00
|
|
|
|
|
|
|
|
BOOST_ASSERT(! d.ws.rd_close_);
|
2017-08-13 11:55:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Drain
|
2017-08-26 15:18:02 -07:00
|
|
|
if(d.ws.rd_remain_ > 0)
|
2017-08-13 11:55:18 -07:00
|
|
|
goto read_payload;
|
|
|
|
|
for(;;)
|
|
|
|
|
{
|
|
|
|
|
// Read frame header
|
|
|
|
|
while(! d.ws.parse_fh(
|
2018-01-01 17:49:19 -08:00
|
|
|
d.ws.rd_fh_, d.ws.rd_buf_, d.ev))
|
2017-08-13 11:55:18 -07:00
|
|
|
{
|
2018-01-01 17:49:19 -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
|
|
|
|
|
d.ws.stream_.async_read_some(
|
2017-08-26 15:18:02 -07:00
|
|
|
d.ws.rd_buf_.prepare(read_size(d.ws.rd_buf_,
|
|
|
|
|
d.ws.rd_buf_.max_size())),
|
2017-08-13 11:55:18 -07:00
|
|
|
std::move(*this));
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! d.ws.check_ok(ec))
|
2017-08-13 11:55:18 -07:00
|
|
|
goto upcall;
|
2017-08-26 15:18:02 -07:00
|
|
|
d.ws.rd_buf_.commit(bytes_transferred);
|
2017-08-13 11:55:18 -07:00
|
|
|
}
|
2017-08-26 15:18:02 -07:00
|
|
|
if(detail::is_control(d.ws.rd_fh_.op))
|
2017-08-13 11:55:18 -07:00
|
|
|
{
|
|
|
|
|
// Process control frame
|
2017-08-26 15:18:02 -07:00
|
|
|
if(d.ws.rd_fh_.op == detail::opcode::close)
|
2017-08-13 11:55:18 -07:00
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(! d.ws.rd_close_);
|
|
|
|
|
d.ws.rd_close_ = true;
|
2017-09-15 09:55:03 -07:00
|
|
|
auto const mb = buffers_prefix(
|
2017-08-26 15:18:02 -07:00
|
|
|
clamp(d.ws.rd_fh_.len),
|
|
|
|
|
d.ws.rd_buf_.data());
|
|
|
|
|
if(d.ws.rd_fh_.len > 0 && d.ws.rd_fh_.mask)
|
|
|
|
|
detail::mask_inplace(mb, d.ws.rd_key_);
|
2018-01-01 17:49:19 -08:00
|
|
|
detail::read_close(d.ws.cr_, mb, d.ev);
|
|
|
|
|
if(d.ev)
|
2017-08-17 22:38:50 -07:00
|
|
|
goto teardown;
|
2017-08-26 15:18:02 -07:00
|
|
|
d.ws.rd_buf_.consume(clamp(d.ws.rd_fh_.len));
|
2017-08-17 22:38:50 -07:00
|
|
|
goto teardown;
|
2017-08-13 11:55:18 -07:00
|
|
|
}
|
2017-08-26 15:18:02 -07:00
|
|
|
d.ws.rd_buf_.consume(clamp(d.ws.rd_fh_.len));
|
2017-08-13 11:55:18 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
read_payload:
|
2017-08-26 15:18:02 -07:00
|
|
|
while(d.ws.rd_buf_.size() < d.ws.rd_remain_)
|
2017-08-13 11:55:18 -07:00
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
d.ws.rd_remain_ -= d.ws.rd_buf_.size();
|
|
|
|
|
d.ws.rd_buf_.consume(d.ws.rd_buf_.size());
|
2017-08-13 11:55:18 -07:00
|
|
|
BOOST_ASIO_CORO_YIELD
|
|
|
|
|
d.ws.stream_.async_read_some(
|
2017-08-26 15:18:02 -07:00
|
|
|
d.ws.rd_buf_.prepare(read_size(d.ws.rd_buf_,
|
|
|
|
|
d.ws.rd_buf_.max_size())),
|
2017-08-13 11:55:18 -07:00
|
|
|
std::move(*this));
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! d.ws.check_ok(ec))
|
2017-08-13 11:55:18 -07:00
|
|
|
goto upcall;
|
2017-08-26 15:18:02 -07:00
|
|
|
d.ws.rd_buf_.commit(bytes_transferred);
|
2017-08-13 11:55:18 -07:00
|
|
|
}
|
2017-08-26 15:18:02 -07:00
|
|
|
BOOST_ASSERT(d.ws.rd_buf_.size() >= d.ws.rd_remain_);
|
|
|
|
|
d.ws.rd_buf_.consume(clamp(d.ws.rd_remain_));
|
|
|
|
|
d.ws.rd_remain_ = 0;
|
2017-08-13 11:55:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
|
// Teardown
|
2018-02-15 11:30:15 -08:00
|
|
|
BOOST_ASSERT(d.ws.wr_block_.is_locked(this));
|
2017-08-13 11:55:18 -07:00
|
|
|
using beast::websocket::async_teardown;
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
|
|
|
|
async_teardown(d.ws.role_,
|
|
|
|
|
d.ws.stream_, std::move(*this));
|
2018-02-15 11:30:15 -08:00
|
|
|
BOOST_ASSERT(d.ws.wr_block_.is_locked(this));
|
2017-08-13 11:55:18 -07:00
|
|
|
if(ec == boost::asio::error::eof)
|
|
|
|
|
{
|
|
|
|
|
// Rationale:
|
|
|
|
|
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
|
|
|
|
|
ec.assign(0, ec.category());
|
|
|
|
|
}
|
2017-08-17 22:38:50 -07:00
|
|
|
if(! ec)
|
|
|
|
|
ec = d.ev;
|
2017-08-31 17:52:09 -07:00
|
|
|
if(ec)
|
|
|
|
|
d.ws.status_ = status::failed;
|
|
|
|
|
else
|
|
|
|
|
d.ws.status_ = status::closed;
|
|
|
|
|
d.ws.close();
|
2017-08-13 09:45:49 -07:00
|
|
|
|
|
|
|
|
upcall:
|
2018-02-15 11:30:15 -08:00
|
|
|
BOOST_ASSERT(d.ws.wr_block_.is_locked(this));
|
|
|
|
|
d.ws.wr_block_.unlock(this);
|
|
|
|
|
if(d.ws.rd_block_.try_unlock(this))
|
2017-08-26 15:18:02 -07:00
|
|
|
d.ws.paused_r_rd_.maybe_invoke();
|
|
|
|
|
d.ws.paused_rd_.maybe_invoke() ||
|
|
|
|
|
d.ws.paused_ping_.maybe_invoke() ||
|
|
|
|
|
d.ws.paused_wr_.maybe_invoke();
|
2017-08-26 20:10:04 -07:00
|
|
|
if(! d.cont)
|
|
|
|
|
{
|
|
|
|
|
auto& ws = d.ws;
|
2017-09-07 07:39:52 -07:00
|
|
|
return boost::asio::post(
|
|
|
|
|
ws.stream_.get_executor(),
|
2017-08-26 20:10:04 -07:00
|
|
|
bind_handler(d_.release_handler(), ec));
|
|
|
|
|
}
|
2017-08-13 11:55:18 -07:00
|
|
|
d_.invoke(ec);
|
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;
|
2017-08-26 20:10:04 -07:00
|
|
|
ec.assign(0, ec.category());
|
2017-08-13 09:45:49 -07:00
|
|
|
// Make sure the stream is open
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! 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
|
|
|
|
|
BOOST_ASSERT(! rd_close_);
|
2017-08-13 11:55:18 -07:00
|
|
|
BOOST_ASSERT(! wr_close_);
|
2016-10-24 08:12:09 -04:00
|
|
|
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);
|
|
|
|
|
boost::asio::write(stream_, fb.data(), ec);
|
|
|
|
|
}
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! check_ok(ec))
|
2017-07-15 17:05:24 -07:00
|
|
|
return;
|
2017-08-31 17:52:09 -07:00
|
|
|
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
|
2017-08-26 15:18:02 -07:00
|
|
|
if(rd_remain_ > 0)
|
2017-08-01 20:15:07 -07:00
|
|
|
goto read_payload;
|
|
|
|
|
for(;;)
|
|
|
|
|
{
|
|
|
|
|
// Read frame header
|
2018-01-01 17:49:19 -08:00
|
|
|
while(! parse_fh(rd_fh_, 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 =
|
|
|
|
|
stream_.read_some(
|
2017-08-26 15:18:02 -07:00
|
|
|
rd_buf_.prepare(read_size(rd_buf_,
|
|
|
|
|
rd_buf_.max_size())), ec);
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! check_ok(ec))
|
2017-08-01 20:15:07 -07:00
|
|
|
return;
|
2017-08-26 15:18:02 -07:00
|
|
|
rd_buf_.commit(bytes_transferred);
|
2017-08-01 20:15:07 -07:00
|
|
|
}
|
2017-08-26 15:18:02 -07:00
|
|
|
if(detail::is_control(rd_fh_.op))
|
2017-08-01 20:15:07 -07:00
|
|
|
{
|
|
|
|
|
// Process control frame
|
2017-08-26 15:18:02 -07:00
|
|
|
if(rd_fh_.op == detail::opcode::close)
|
2017-08-01 20:15:07 -07:00
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(! rd_close_);
|
|
|
|
|
rd_close_ = true;
|
2017-09-15 09:55:03 -07:00
|
|
|
auto const mb = buffers_prefix(
|
2017-08-26 15:18:02 -07:00
|
|
|
clamp(rd_fh_.len),
|
|
|
|
|
rd_buf_.data());
|
|
|
|
|
if(rd_fh_.len > 0 && rd_fh_.mask)
|
|
|
|
|
detail::mask_inplace(mb, rd_key_);
|
2018-01-01 17:49:19 -08:00
|
|
|
detail::read_close(cr_, mb, result);
|
|
|
|
|
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
|
|
|
}
|
2017-08-26 15:18:02 -07:00
|
|
|
rd_buf_.consume(clamp(rd_fh_.len));
|
2017-08-01 20:15:07 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2017-08-26 15:18:02 -07:00
|
|
|
rd_buf_.consume(clamp(rd_fh_.len));
|
2017-08-01 20:15:07 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
read_payload:
|
2017-08-26 15:18:02 -07:00
|
|
|
while(rd_buf_.size() < rd_remain_)
|
2017-08-01 20:15:07 -07:00
|
|
|
{
|
2017-08-26 15:18:02 -07:00
|
|
|
rd_remain_ -= rd_buf_.size();
|
|
|
|
|
rd_buf_.consume(rd_buf_.size());
|
2017-08-01 20:15:07 -07:00
|
|
|
auto const bytes_transferred =
|
|
|
|
|
stream_.read_some(
|
2017-08-26 15:18:02 -07:00
|
|
|
rd_buf_.prepare(read_size(rd_buf_,
|
|
|
|
|
rd_buf_.max_size())), ec);
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! check_ok(ec))
|
2017-08-01 20:15:07 -07:00
|
|
|
return;
|
2017-08-26 15:18:02 -07:00
|
|
|
rd_buf_.commit(bytes_transferred);
|
2017-08-01 20:15:07 -07:00
|
|
|
}
|
2017-08-26 15:18:02 -07:00
|
|
|
BOOST_ASSERT(rd_buf_.size() >= rd_remain_);
|
|
|
|
|
rd_buf_.consume(clamp(rd_remain_));
|
|
|
|
|
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)
|
|
|
|
|
ec.assign(0, ec.category());
|
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
|