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

199 lines
5.1 KiB
C++
Raw Normal View History

2017-07-20 08:01:46 -07:00
//
2019-02-21 07:00:31 -08:00
// Copyright (c) 2016-2019 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
2019-01-19 07:24:00 -08:00
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_TEARDOWN_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_TEARDOWN_HPP
2017-07-20 08:01:46 -07:00
2019-01-07 13:35:19 -08:00
#include <boost/beast/core/async_op_base.hpp>
#include <boost/beast/core/bind_handler.hpp>
2019-02-04 21:52:54 -08:00
#include <boost/beast/core/stream_traits.hpp>
2019-01-20 12:25:30 -08:00
#include <boost/beast/core/detail/bind_continuation.hpp>
#include <boost/beast/core/detail/type_traits.hpp>
2018-02-20 03:33:02 -08:00
#include <boost/asio/coroutine.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 {
2019-02-18 12:42:24 -08:00
template<
class Protocol, class Executor,
class Handler>
2019-01-05 19:40:46 -08:00
class teardown_tcp_op
2019-01-07 13:35:19 -08:00
: public beast::async_op_base<
2019-02-04 21:52:54 -08:00
Handler, beast::executor_type<
2019-02-18 12:42:24 -08:00
net::basic_stream_socket<
Protocol, Executor>>>
2019-01-05 19:40:46 -08:00
, public net::coroutine
2017-07-20 08:01:46 -07:00
{
2019-02-18 12:42:24 -08:00
using socket_type =
net::basic_stream_socket<Protocol, Executor>;
2017-07-20 08:01:46 -07:00
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:
2019-01-05 19:40:46 -08:00
template<class Handler_>
2017-07-20 08:01:46 -07:00
teardown_tcp_op(
2019-01-05 19:40:46 -08:00
Handler_&& h,
socket_type& s,
role_type role)
2019-01-09 16:11:15 -08:00
: async_op_base<Handler,
2019-02-04 21:52:54 -08:00
beast::executor_type<
2019-02-18 12:42:24 -08:00
net::basic_stream_socket<
Protocol, Executor>>>(
std::forward<Handler_>(h),
s.get_executor())
, s_(s)
, role_(role)
2017-07-20 08:01:46 -07:00
{
2019-01-20 12:25:30 -08:00
(*this)({}, 0, false);
2017-07-20 08:01:46 -07:00
}
void
operator()(
error_code ec = {},
2019-01-20 12:25:30 -08:00
std::size_t bytes_transferred = 0,
bool cont = true)
{
2019-01-05 19:40:46 -08:00
BOOST_ASIO_CORO_REENTER(*this)
{
2019-01-05 19:40:46 -08:00
nb_ = s_.non_blocking();
s_.non_blocking(true, ec);
if(ec)
2019-01-05 19:40:46 -08:00
goto upcall;
2019-01-20 12:25:30 -08:00
if(role_ == role_type::server)
2019-02-18 12:42:24 -08:00
s_.shutdown(net::socket_base::shutdown_send, ec);
2019-01-20 12:25:30 -08:00
if(ec)
goto upcall;
2019-01-05 19:40:46 -08:00
for(;;)
2018-02-20 03:33:02 -08:00
{
2019-01-05 19:40:46 -08:00
{
char buf[2048];
2019-01-20 12:25:30 -08:00
s_.read_some(net::buffer(buf), ec);
2019-01-05 19:40:46 -08:00
}
if(ec == net::error::would_block)
{
BOOST_ASIO_CORO_YIELD
s_.async_wait(
2019-02-18 12:42:24 -08:00
net::socket_base::wait_read,
2019-01-20 12:25:30 -08:00
beast::detail::bind_continuation(std::move(*this)));
2019-01-05 19:40:46 -08:00
continue;
}
if(ec)
{
if(ec != net::error::eof)
goto upcall;
ec = {};
break;
}
if(bytes_transferred == 0)
{
// happens sometimes
// https://github.com/boostorg/beast/issues/1373
break;
}
2018-02-20 03:33:02 -08:00
}
2019-01-05 19:40:46 -08:00
if(role_ == role_type::client)
2019-02-18 12:42:24 -08:00
s_.shutdown(net::socket_base::shutdown_send, ec);
2019-01-05 19:40:46 -08:00
if(ec)
goto upcall;
s_.close(ec);
upcall:
2019-01-20 12:25:30 -08:00
if(! cont)
{
BOOST_ASIO_CORO_YIELD
net::post(bind_front_handler(
std::move(*this), ec));
}
2019-01-05 19:40:46 -08:00
{
error_code ignored;
s_.non_blocking(nb_, ignored);
}
2019-02-15 15:02:38 -08:00
this->invoke_now(ec);
}
2017-07-20 08:01:46 -07:00
}
2019-01-05 19:40:46 -08:00
};
2017-07-20 08:01:46 -07:00
} // detail
//------------------------------------------------------------------------------
2019-02-18 12:42:24 -08:00
template<class Protocol, class Executor>
2017-07-20 08:01:46 -07:00
void
teardown(
role_type role,
2019-02-18 12:42:24 -08:00
net::basic_stream_socket<
Protocol, Executor>& socket,
error_code& ec)
2017-07-20 08:01:46 -07:00
{
if(role == role_type::server)
socket.shutdown(
2019-02-18 12:42:24 -08:00
net::socket_base::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 =
2019-02-02 20:44:04 -08:00
socket.read_some(net::buffer(buf), ec);
2018-02-20 03:33:02 -08:00
if(ec)
{
2018-11-30 14:58:38 -08:00
if(ec != net::error::eof)
2018-02-20 03:33:02 -08:00
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
2019-01-05 19:40:46 -08:00
// https://github.com/boostorg/beast/issues/1373
2018-02-20 03:33:02 -08:00
break;
}
2017-07-20 08:01:46 -07:00
}
if(role == role_type::client)
socket.shutdown(
2019-02-18 12:42:24 -08:00
net::socket_base::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);
}
2019-02-18 12:42:24 -08:00
template<
class Protocol, class Executor,
class TeardownHandler>
2017-07-20 08:01:46 -07:00
void
async_teardown(
role_type role,
2019-02-18 12:42:24 -08:00
net::basic_stream_socket<
Protocol, Executor>& socket,
TeardownHandler&& handler)
2017-07-20 08:01:46 -07:00
{
static_assert(beast::detail::is_invocable<
2017-07-20 08:01:46 -07:00
TeardownHandler, void(error_code)>::value,
2019-02-20 19:19:59 -08:00
"TeardownHandler type requirements not met");
2019-02-18 12:42:24 -08:00
detail::teardown_tcp_op<
Protocol,
Executor,
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