Files
boost_beast/include/boost/beast/websocket/impl/teardown.ipp

195 lines
4.5 KiB
Plaintext
Raw Normal View History

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_TEARDOWN_IPP
#define BOOST_BEAST_WEBSOCKET_IMPL_TEARDOWN_IPP
2017-07-20 08:01:46 -07:00
#include <boost/beast/core/bind_handler.hpp>
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/type_traits.hpp>
#include <boost/asio/handler_alloc_hook.hpp>
#include <boost/asio/handler_continuation_hook.hpp>
#include <boost/asio/handler_invoke_hook.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 {
namespace detail {
template<class Handler>
class teardown_tcp_op
{
using socket_type =
boost::asio::ip::tcp::socket;
Handler h_;
socket_type& s_;
role_type role_;
int step_ = 0;
2017-07-20 08:01:46 -07:00
public:
teardown_tcp_op(teardown_tcp_op&& other) = default;
teardown_tcp_op(teardown_tcp_op const& other) = default;
2017-07-20 08:01:46 -07:00
template<class DeducedHandler>
teardown_tcp_op(
DeducedHandler&& h,
socket_type& s,
role_type role)
: h_(std::forward<DeducedHandler>(h))
, s_(s)
, role_(role)
2017-07-20 08:01:46 -07:00
{
}
void
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,
teardown_tcp_op* op)
{
using boost::asio::asio_handler_allocate;
return asio_handler_allocate(
size, std::addressof(op->h_));
2017-07-20 08:01:46 -07:00
}
friend
void asio_handler_deallocate(void* p,
std::size_t size, teardown_tcp_op* op)
{
using boost::asio::asio_handler_deallocate;
asio_handler_deallocate(
p, size, std::addressof(op->h_));
2017-07-20 08:01:46 -07:00
}
friend
bool asio_handler_is_continuation(teardown_tcp_op* op)
{
using boost::asio::asio_handler_is_continuation;
return op->step_ >= 3 ||
asio_handler_is_continuation(std::addressof(op->h_));
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,
teardown_tcp_op* op)
{
using boost::asio::asio_handler_invoke;
asio_handler_invoke(
f, std::addressof(op->h_));
2017-07-20 08:01:46 -07:00
}
};
template<class Handler>
void
teardown_tcp_op<Handler>::
operator()(error_code ec, std::size_t)
2017-07-20 08:01:46 -07:00
{
using boost::asio::buffer;
using tcp = boost::asio::ip::tcp;
switch(step_)
2017-07-20 08:01:46 -07:00
{
case 0:
s_.non_blocking(true, ec);
if(ec)
2017-07-20 08:01:46 -07:00
{
step_ = 1;
return s_.get_io_service().post(
bind_handler(std::move(*this), ec, 0));
}
step_ = 2;
if(role_ == role_type::server)
s_.shutdown(tcp::socket::shutdown_send, ec);
goto do_read;
case 1:
break;
2017-07-20 08:01:46 -07:00
case 2:
step_ = 3;
case 3:
if(ec != boost::asio::error::would_block)
break;
{
char buf[2048];
s_.read_some(
boost::asio::buffer(buf), ec);
if(ec)
break;
2017-07-20 08:01:46 -07:00
}
do_read:
return s_.async_read_some(
boost::asio::null_buffers{},
std::move(*this));
2017-07-20 08:01:46 -07:00
}
if(role_ == role_type::client)
s_.shutdown(tcp::socket::shutdown_send, ec);
s_.close(ec);
h_(ec);
2017-07-20 08:01:46 -07:00
}
} // detail
//------------------------------------------------------------------------------
inline
void
teardown(
role_type role,
2017-07-20 08:01:46 -07:00
boost::asio::ip::tcp::socket& socket,
error_code& ec)
2017-07-20 08:01:46 -07:00
{
using boost::asio::buffer;
if(role == role_type::server)
socket.shutdown(
boost::asio::ip::tcp::socket::shutdown_send, ec);
2017-07-20 08:01:46 -07:00
while(! ec)
{
char buf[8192];
auto const n = socket.read_some(
buffer(buf), ec);
if(! n)
break;
}
if(role == role_type::client)
socket.shutdown(
boost::asio::ip::tcp::socket::shutdown_send, ec);
2017-07-20 08:01:46 -07:00
socket.close(ec);
}
template<class TeardownHandler>
inline
void
async_teardown(
role_type role,
2017-07-20 08:01:46 -07:00
boost::asio::ip::tcp::socket& socket,
TeardownHandler&& handler)
2017-07-20 08:01:46 -07:00
{
static_assert(beast::is_completion_handler<
2017-07-20 08:01:46 -07:00
TeardownHandler, void(error_code)>::value,
"TeardownHandler requirements not met");
detail::teardown_tcp_op<typename std::decay<
TeardownHandler>::type>{std::forward<
TeardownHandler>(handler), socket,
role}();
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