Files
boost_beast/include/beast/websocket/impl/close.ipp
T

241 lines
6.0 KiB
C++
Raw Normal View History

2017-07-20 08:01:46 -07:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-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)
//
2016-10-24 08:12:09 -04:00
#ifndef BEAST_WEBSOCKET_IMPL_CLOSE_IPP
#define BEAST_WEBSOCKET_IMPL_CLOSE_IPP
2017-07-20 08:01:46 -07:00
#include <beast/core/handler_ptr.hpp>
#include <beast/core/static_buffer.hpp>
#include <beast/core/type_traits.hpp>
2017-06-26 17:20:42 -07:00
#include <beast/core/detail/config.hpp>
#include <boost/asio/handler_alloc_hook.hpp>
#include <boost/asio/handler_continuation_hook.hpp>
#include <boost/asio/handler_invoke_hook.hpp>
2017-05-22 15:30:12 -07:00
#include <boost/throw_exception.hpp>
2017-07-20 08:01:46 -07:00
#include <memory>
namespace beast {
namespace websocket {
2016-10-24 08:12:09 -04:00
//------------------------------------------------------------------------------
2017-07-20 08:01:46 -07:00
// send the close message and wait for the response
//
template<class NextLayer>
template<class Handler>
class stream<NextLayer>::close_op
{
struct data : op
{
stream<NextLayer>& ws;
close_reason cr;
2017-06-29 09:48:30 -07:00
detail::frame_streambuf fb;
2017-07-20 08:01:46 -07:00
int state = 0;
2017-06-29 09:48:30 -07:00
data(Handler&, stream<NextLayer>& ws_,
2017-07-20 08:01:46 -07:00
close_reason const& cr_)
: ws(ws_)
2017-07-20 08:01:46 -07:00
, cr(cr_)
{
ws.template write_close<
static_buffer>(fb, cr);
2017-07-20 08:01:46 -07:00
}
};
handler_ptr<data, Handler> d_;
2017-07-20 08:01:46 -07:00
public:
close_op(close_op&&) = default;
close_op(close_op const&) = default;
template<class DeducedHandler, class... Args>
close_op(DeducedHandler&& h,
stream<NextLayer>& ws, Args&&... args)
2017-01-29 19:46:17 -05:00
: d_(std::forward<DeducedHandler>(h),
ws, std::forward<Args>(args)...)
2017-07-20 08:01:46 -07:00
{
}
void operator()()
{
2017-06-29 09:48:30 -07:00
(*this)({});
2017-07-20 08:01:46 -07:00
}
void
2017-06-29 09:48:30 -07:00
operator()(error_code ec,
std::size_t bytes_transferred = 0);
2017-07-20 08:01:46 -07:00
friend
void* asio_handler_allocate(
std::size_t size, close_op* op)
{
using boost::asio::asio_handler_allocate;
return asio_handler_allocate(
size, std::addressof(op->d_.handler()));
2017-07-20 08:01:46 -07:00
}
friend
void asio_handler_deallocate(
void* p, std::size_t size, close_op* op)
{
using boost::asio::asio_handler_deallocate;
asio_handler_deallocate(
p, size, std::addressof(op->d_.handler()));
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;
return asio_handler_is_continuation(
std::addressof(op->d_.handler()));
2017-07-20 08:01:46 -07:00
}
2016-08-26 08:01:44 -04:00
template<class Function>
2017-07-20 08:01:46 -07:00
friend
void asio_handler_invoke(Function&& f, close_op* op)
{
using boost::asio::asio_handler_invoke;
asio_handler_invoke(
f, std::addressof(op->d_.handler()));
2017-07-20 08:01:46 -07:00
}
};
template<class NextLayer>
template<class Handler>
2017-02-06 20:07:03 -05:00
void
stream<NextLayer>::close_op<Handler>::
2016-05-15 16:22:25 -04:00
operator()(error_code ec, std::size_t)
{
auto& d = *d_;
if(ec)
2017-07-20 08:01:46 -07:00
{
2017-06-29 09:48:30 -07:00
BOOST_ASSERT(d.ws.wr_block_ == &d);
d.ws.failed_ = true;
goto upcall;
}
switch(d.state)
{
case 0:
if(d.ws.wr_block_)
2017-07-20 08:01:46 -07:00
{
2017-06-29 09:48:30 -07:00
// suspend
2016-05-15 16:22:25 -04:00
d.state = 1;
d.ws.close_op_.emplace(std::move(*this));
2017-06-29 09:48:30 -07:00
return;
}
d.ws.wr_block_ = &d;
if(d.ws.failed_ || d.ws.wr_close_)
{
// call handler
d.ws.get_io_service().post(
bind_handler(std::move(*this),
boost::asio::error::operation_aborted));
return;
}
2016-05-15 16:22:25 -04:00
2017-06-29 09:48:30 -07:00
do_write:
// send close frame
BOOST_ASSERT(d.ws.wr_block_ == &d);
d.state = 3;
d.ws.wr_close_ = true;
boost::asio::async_write(d.ws.stream_,
d.fb.data(), std::move(*this));
return;
case 1:
BOOST_ASSERT(! d.ws.wr_block_);
d.ws.wr_block_ = &d;
d.state = 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));
return;
case 2:
BOOST_ASSERT(d.ws.wr_block_ == &d);
if(d.ws.failed_ || d.ws.wr_close_)
{
// call handler
ec = boost::asio::error::operation_aborted;
2016-05-15 16:22:25 -04:00
goto upcall;
2017-07-20 08:01:46 -07:00
}
2017-06-29 09:48:30 -07:00
goto do_write;
case 3:
break;
2017-07-20 08:01:46 -07:00
}
2016-05-15 16:22:25 -04:00
upcall:
2017-06-29 09:48:30 -07:00
BOOST_ASSERT(d.ws.wr_block_ == &d);
d.ws.wr_block_ = nullptr;
d.ws.rd_op_.maybe_invoke() ||
2017-06-29 09:48:30 -07:00
d.ws.ping_op_.maybe_invoke() ||
d.ws.wr_op_.maybe_invoke();
d_.invoke(ec);
2017-07-20 08:01:46 -07:00
}
2016-10-24 08:12:09 -04:00
template<class NextLayer>
template<class CloseHandler>
async_return_type<
CloseHandler, void(error_code)>
2016-10-24 08:12:09 -04:00
stream<NextLayer>::
async_close(close_reason const& cr, CloseHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
2016-10-24 08:12:09 -04:00
"AsyncStream requirements not met");
async_completion<CloseHandler,
void(error_code)> init{handler};
close_op<handler_type<
CloseHandler, void(error_code)>>{
2017-06-29 09:48:30 -07:00
init.completion_handler, *this, cr}({});
return init.result.get();
2016-10-24 08:12:09 -04:00
}
template<class NextLayer>
void
stream<NextLayer>::
close(close_reason const& cr)
{
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
}
template<class NextLayer>
void
stream<NextLayer>::
close(close_reason const& cr, error_code& ec)
{
static_assert(is_sync_stream<next_layer_type>::value,
2016-10-24 08:12:09 -04:00
"SyncStream requirements not met");
BOOST_ASSERT(! wr_close_);
2017-06-29 09:48:30 -07:00
if(wr_close_)
{
ec = boost::asio::error::operation_aborted;
return;
}
2016-10-24 08:12:09 -04:00
wr_close_ = true;
detail::frame_streambuf fb;
write_close<static_buffer>(fb, cr);
2016-10-24 08:12:09 -04:00
boost::asio::write(stream_, fb.data(), ec);
2017-06-19 16:58:23 -07:00
failed_ = !!ec;
2016-10-24 08:12:09 -04:00
}
//------------------------------------------------------------------------------
2017-07-20 08:01:46 -07:00
} // websocket
} // beast
#endif