2017-07-15 17:05:24 -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-15 17:05:24 -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-15 17:05:24 -07:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_FAIL_IPP
|
|
|
|
|
#define BOOST_BEAST_WEBSOCKET_IMPL_FAIL_IPP
|
2017-07-15 17:05:24 -07:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/websocket/teardown.hpp>
|
|
|
|
|
#include <boost/beast/core/bind_handler.hpp>
|
|
|
|
|
#include <boost/beast/core/handler_ptr.hpp>
|
|
|
|
|
#include <boost/beast/core/flat_static_buffer.hpp>
|
|
|
|
|
#include <boost/beast/core/detail/config.hpp>
|
2017-07-15 17:05:24 -07:00
|
|
|
#include <boost/asio/handler_alloc_hook.hpp>
|
|
|
|
|
#include <boost/asio/handler_continuation_hook.hpp>
|
|
|
|
|
#include <boost/asio/handler_invoke_hook.hpp>
|
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
|
#include <boost/config.hpp>
|
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
namespace boost {
|
2017-07-15 17:05:24 -07:00
|
|
|
namespace beast {
|
|
|
|
|
namespace websocket {
|
|
|
|
|
|
|
|
|
|
// _Fail the WebSocket Connection_
|
|
|
|
|
//
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
template<class Handler>
|
|
|
|
|
class stream<NextLayer>::fail_op
|
|
|
|
|
{
|
|
|
|
|
Handler h_;
|
|
|
|
|
stream<NextLayer>& ws_;
|
|
|
|
|
int step_ = 0;
|
|
|
|
|
bool dispatched_ = false;
|
2017-08-01 20:15:07 -07:00
|
|
|
std::uint16_t code_;
|
2017-08-02 13:25:53 -07:00
|
|
|
error_code ev_;
|
2017-07-15 17:05:24 -07:00
|
|
|
token tok_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
fail_op(fail_op&&) = default;
|
|
|
|
|
fail_op(fail_op const&) = default;
|
|
|
|
|
|
|
|
|
|
template<class DeducedHandler>
|
|
|
|
|
fail_op(
|
|
|
|
|
DeducedHandler&& h,
|
|
|
|
|
stream<NextLayer>& ws,
|
2017-08-01 20:15:07 -07:00
|
|
|
std::uint16_t code,
|
2017-08-02 13:25:53 -07:00
|
|
|
error_code ev)
|
2017-07-15 17:05:24 -07:00
|
|
|
: h_(std::forward<DeducedHandler>(h))
|
|
|
|
|
, ws_(ws)
|
2017-08-02 13:25:53 -07:00
|
|
|
, code_(code)
|
|
|
|
|
, ev_(ev)
|
2017-07-15 17:05:24 -07:00
|
|
|
, tok_(ws_.t_.unique())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Handler&
|
|
|
|
|
handler()
|
|
|
|
|
{
|
|
|
|
|
return h_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void operator()(error_code ec = {},
|
|
|
|
|
std::size_t bytes_transferred = 0);
|
|
|
|
|
|
|
|
|
|
friend
|
|
|
|
|
void* asio_handler_allocate(
|
|
|
|
|
std::size_t size, fail_op* op)
|
|
|
|
|
{
|
|
|
|
|
using boost::asio::asio_handler_allocate;
|
|
|
|
|
return asio_handler_allocate(
|
|
|
|
|
size, std::addressof(op->h_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend
|
|
|
|
|
void asio_handler_deallocate(
|
|
|
|
|
void* p, std::size_t size, fail_op* op)
|
|
|
|
|
{
|
|
|
|
|
using boost::asio::asio_handler_deallocate;
|
|
|
|
|
asio_handler_deallocate(
|
|
|
|
|
p, size, std::addressof(op->h_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend
|
|
|
|
|
bool asio_handler_is_continuation(fail_op* op)
|
|
|
|
|
{
|
|
|
|
|
using boost::asio::asio_handler_is_continuation;
|
|
|
|
|
return op->dispatched_ ||
|
|
|
|
|
asio_handler_is_continuation(
|
|
|
|
|
std::addressof(op->h_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class Function>
|
|
|
|
|
friend
|
|
|
|
|
void asio_handler_invoke(Function&& f, fail_op* op)
|
|
|
|
|
{
|
|
|
|
|
using boost::asio::asio_handler_invoke;
|
|
|
|
|
asio_handler_invoke(f,
|
|
|
|
|
std::addressof(op->h_));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
template<class Handler>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
fail_op<Handler>::
|
|
|
|
|
operator()(error_code ec, std::size_t)
|
|
|
|
|
{
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
do_start = 0,
|
|
|
|
|
do_resume = 20,
|
|
|
|
|
do_teardown = 40
|
|
|
|
|
};
|
|
|
|
|
switch(step_)
|
|
|
|
|
{
|
|
|
|
|
case do_start:
|
|
|
|
|
// Acquire write block
|
|
|
|
|
if(ws_.wr_block_)
|
|
|
|
|
{
|
|
|
|
|
// suspend
|
|
|
|
|
BOOST_ASSERT(ws_.wr_block_ != tok_);
|
|
|
|
|
step_ = do_resume;
|
|
|
|
|
ws_.rd_op_.save(std::move(*this));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ws_.wr_block_ = tok_;
|
|
|
|
|
goto go_write;
|
|
|
|
|
|
|
|
|
|
case do_resume:
|
|
|
|
|
BOOST_ASSERT(! ws_.wr_block_);
|
|
|
|
|
ws_.wr_block_ = tok_;
|
|
|
|
|
step_ = do_resume + 1;
|
|
|
|
|
// We were invoked from a foreign context, so post
|
|
|
|
|
return ws_.get_io_service().post(std::move(*this));
|
|
|
|
|
|
|
|
|
|
case do_resume + 1:
|
|
|
|
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
|
|
|
|
dispatched_ = true;
|
|
|
|
|
go_write:
|
|
|
|
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
|
|
|
|
if(ws_.failed_)
|
|
|
|
|
{
|
|
|
|
|
ws_.wr_block_.reset();
|
|
|
|
|
ec = boost::asio::error::operation_aborted;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-08-02 13:25:53 -07:00
|
|
|
if(code_ == close_code::none)
|
2017-07-15 17:05:24 -07:00
|
|
|
goto go_teardown;
|
|
|
|
|
if(ws_.wr_close_)
|
|
|
|
|
goto go_teardown;
|
|
|
|
|
// send close frame
|
|
|
|
|
step_ = do_teardown;
|
2017-08-02 13:25:53 -07:00
|
|
|
ws_.rd_.fb.consume(ws_.rd_.fb.size());
|
|
|
|
|
ws_.template write_close<
|
|
|
|
|
flat_static_buffer_base>(
|
|
|
|
|
ws_.rd_.fb, code_);
|
2017-07-15 17:05:24 -07:00
|
|
|
ws_.wr_close_ = true;
|
|
|
|
|
return boost::asio::async_write(
|
|
|
|
|
ws_.stream_, ws_.rd_.fb.data(),
|
|
|
|
|
std::move(*this));
|
|
|
|
|
|
|
|
|
|
case do_teardown:
|
|
|
|
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
|
|
|
|
dispatched_ = true;
|
|
|
|
|
ws_.failed_ = !!ec;
|
|
|
|
|
if(ws_.failed_)
|
|
|
|
|
{
|
|
|
|
|
ws_.wr_block_.reset();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
go_teardown:
|
|
|
|
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
|
|
|
|
step_ = do_teardown + 1;
|
2017-08-04 13:31:24 -07:00
|
|
|
using beast::websocket::async_teardown;
|
|
|
|
|
async_teardown(ws_.role_,
|
2017-08-02 13:25:53 -07:00
|
|
|
ws_.stream_, std::move(*this));
|
2017-07-15 17:05:24 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
case do_teardown + 1:
|
|
|
|
|
BOOST_ASSERT(ws_.wr_block_ == tok_);
|
|
|
|
|
dispatched_ = true;
|
|
|
|
|
ws_.wr_block_.reset();
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
if(! ec)
|
2017-08-02 13:25:53 -07:00
|
|
|
ec = ev_;
|
|
|
|
|
ws_.failed_ = true;
|
2017-07-15 17:05:24 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// upcall
|
|
|
|
|
BOOST_ASSERT(ws_.wr_block_ != tok_);
|
|
|
|
|
ws_.close_op_.maybe_invoke() ||
|
|
|
|
|
ws_.ping_op_.maybe_invoke() ||
|
|
|
|
|
ws_.wr_op_.maybe_invoke();
|
|
|
|
|
if(! dispatched_)
|
|
|
|
|
ws_.stream_.get_io_service().post(
|
2017-08-03 04:21:24 -07:00
|
|
|
bind_handler(std::move(h_), ec, 0));
|
2017-07-15 17:05:24 -07:00
|
|
|
else
|
2017-08-03 04:21:24 -07:00
|
|
|
h_(ec, 0);
|
2017-07-15 17:05:24 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-02 13:25:53 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/* _Fail the WebSocket Connection_
|
|
|
|
|
*/
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
do_fail(
|
2017-08-01 20:15:07 -07:00
|
|
|
std::uint16_t code, // if set, send a close frame first
|
2017-08-02 13:25:53 -07:00
|
|
|
error_code ev, // error code to use upon success
|
|
|
|
|
error_code& ec) // set to the error, else set to ev
|
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(ev);
|
|
|
|
|
if(code != close_code::none && ! wr_close_)
|
|
|
|
|
{
|
|
|
|
|
wr_close_ = true;
|
|
|
|
|
detail::frame_streambuf fb;
|
|
|
|
|
write_close<flat_static_buffer_base>(fb, code);
|
|
|
|
|
boost::asio::write(stream_, fb.data(), ec);
|
|
|
|
|
failed_ = !!ec;
|
|
|
|
|
if(failed_)
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-08-04 13:31:24 -07:00
|
|
|
using beast::websocket::teardown;
|
|
|
|
|
teardown(role_, stream_, ec);
|
2017-08-02 13:25:53 -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());
|
|
|
|
|
}
|
|
|
|
|
failed_ = !!ec;
|
|
|
|
|
if(failed_)
|
|
|
|
|
return;
|
|
|
|
|
ec = ev;
|
|
|
|
|
failed_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* _Fail the WebSocket Connection_
|
|
|
|
|
*/
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
template<class Handler>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
do_async_fail(
|
2017-08-01 20:15:07 -07:00
|
|
|
std::uint16_t code, // if set, send a close frame first
|
2017-08-02 13:25:53 -07:00
|
|
|
error_code ev, // error code to use upon success
|
|
|
|
|
Handler&& handler)
|
|
|
|
|
{
|
|
|
|
|
fail_op<typename std::decay<Handler>::type>{
|
|
|
|
|
std::forward<Handler>(handler),
|
|
|
|
|
*this,
|
|
|
|
|
code,
|
|
|
|
|
ev}();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 17:05:24 -07:00
|
|
|
} // websocket
|
|
|
|
|
} // beast
|
2017-07-20 13:40:34 -07:00
|
|
|
} // boost
|
2017-07-15 17:05:24 -07:00
|
|
|
|
|
|
|
|
#endif
|