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

210 lines
5.0 KiB
C++
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>
2017-09-07 07:39:52 -07:00
#include <boost/asio/associated_allocator.hpp>
#include <boost/asio/associated_executor.hpp>
2018-02-20 03:33:02 -08:00
#include <boost/asio/coroutine.hpp>
#include <boost/asio/handler_continuation_hook.hpp>
2017-09-07 07:39:52 -07:00
#include <boost/asio/post.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>
2018-02-20 03:33:02 -08:00
class teardown_tcp_op : public boost::asio::coroutine
2017-07-20 08:01:46 -07:00
{
using socket_type =
boost::asio::ip::tcp::socket;
Handler h_;
socket_type& s_;
role_type role_;
2018-02-20 03:33:02 -08:00
bool nb_;
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
{
}
2017-09-07 07:39:52 -07:00
using allocator_type =
boost::asio::associated_allocator_t<Handler>;
allocator_type
get_allocator() const noexcept
{
2017-12-02 05:57:06 -08:00
return (boost::asio::get_associated_allocator)(h_);
2017-09-07 07:39:52 -07:00
}
using executor_type = boost::asio::associated_executor_t<
2017-10-24 06:40:22 -07:00
Handler, decltype(std::declval<socket_type&>().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-09-07 07:39:52 -07:00
{
2017-12-02 05:57:06 -08:00
return (boost::asio::get_associated_executor)(
2017-09-07 07:39:52 -07:00
h_, s_.get_executor());
}
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
};
template<class Handler>
void
teardown_tcp_op<Handler>::
2018-02-20 03:33:02 -08:00
operator()(error_code ec, std::size_t bytes_transferred)
2017-07-20 08:01:46 -07:00
{
using boost::asio::buffer;
using tcp = boost::asio::ip::tcp;
2018-02-20 03:33:02 -08:00
BOOST_ASIO_CORO_REENTER(*this)
2017-07-20 08:01:46 -07:00
{
2018-02-20 03:33:02 -08:00
nb_ = s_.non_blocking();
s_.non_blocking(true, ec);
2018-02-20 03:33:02 -08:00
if(! ec)
{
if(role_ == role_type::server)
s_.shutdown(tcp::socket::shutdown_send, ec);
}
if(ec)
2017-07-20 08:01:46 -07:00
{
2018-02-20 03:33:02 -08:00
BOOST_ASIO_CORO_YIELD
boost::asio::post(
2017-09-07 07:39:52 -07:00
s_.get_executor(),
bind_handler(std::move(*this), ec, 0));
2018-02-20 03:33:02 -08:00
goto upcall;
2017-07-20 08:01:46 -07:00
}
2018-02-20 03:33:02 -08:00
for(;;)
{
2018-02-20 03:33:02 -08:00
{
char buf[2048];
s_.read_some(
boost::asio::buffer(buf), ec);
}
if(ec == boost::asio::error::would_block)
{
BOOST_ASIO_CORO_YIELD
s_.async_wait(
boost::asio::ip::tcp::socket::wait_read,
std::move(*this));
continue;
}
if(ec)
2018-02-20 03:33:02 -08:00
{
if(ec != boost::asio::error::eof)
goto upcall;
ec = {};
break;
2018-02-20 03:33:02 -08:00
}
if(bytes_transferred == 0)
{
// happens sometimes
break;
}
}
2018-02-20 03:33:02 -08:00
if(role_ == role_type::client)
s_.shutdown(tcp::socket::shutdown_send, ec);
if(ec)
goto upcall;
s_.close(ec);
upcall:
{
error_code ignored;
s_.non_blocking(nb_, ignored);
}
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);
2018-02-20 03:33:02 -08:00
if(ec)
return;
for(;;)
2017-07-20 08:01:46 -07:00
{
2018-02-20 03:33:02 -08:00
char buf[2048];
auto const bytes_transferred =
socket.read_some(buffer(buf), ec);
if(ec)
{
if(ec != boost::asio::error::eof)
return;
ec = {};
2017-07-20 08:01:46 -07:00
break;
2018-02-20 03:33:02 -08:00
}
if(bytes_transferred == 0)
{
// happens sometimes
break;
}
2017-07-20 08:01:46 -07:00
}
if(role == role_type::client)
socket.shutdown(
boost::asio::ip::tcp::socket::shutdown_send, ec);
2018-02-20 03:33:02 -08:00
if(ec)
return;
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