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

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
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_TEARDOWN_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_TEARDOWN_HPP
2017-07-20 08:01:46 -07:00
#include <boost/beast/core/async_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>
#include <boost/asio/coroutine.hpp>
#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 Protocol, class Executor,
class Handler>
class teardown_tcp_op
: public beast::async_base<
2019-02-04 21:52:54 -08:00
Handler, beast::executor_type<
net::basic_stream_socket<
Protocol, Executor>>>
, public net::coroutine
2017-07-20 08:01:46 -07:00
{
using socket_type =
net::basic_stream_socket<Protocol, Executor>;
2017-07-20 08:01:46 -07:00
socket_type& s_;
role_type role_;
bool nb_;
2017-07-20 08:01:46 -07:00
public:
template<class Handler_>
2017-07-20 08:01:46 -07:00
teardown_tcp_op(
Handler_&& h,
socket_type& s,
role_type role)
: async_base<Handler,
2019-02-04 21:52:54 -08:00
beast::executor_type<
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)
2017-07-20 08:01:46 -07:00
{
BOOST_ASIO_CORO_REENTER(*this)
{
nb_ = s_.non_blocking();
s_.non_blocking(true, ec);
if(ec)
goto upcall;
2019-01-20 12:25:30 -08:00
if(role_ == role_type::server)
s_.shutdown(net::socket_base::shutdown_send, ec);
2019-01-20 12:25:30 -08:00
if(ec)
goto upcall;
for(;;)
{
{
char buf[2048];
2019-01-20 12:25:30 -08:00
s_.read_some(net::buffer(buf), ec);
}
if(ec == net::error::would_block)
{
BOOST_ASIO_CORO_YIELD
s_.async_wait(
net::socket_base::wait_read,
2019-01-20 12:25:30 -08:00
beast::detail::bind_continuation(std::move(*this)));
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;
}
}
if(role_ == role_type::client)
s_.shutdown(net::socket_base::shutdown_send, ec);
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));
}
{
error_code ignored;
s_.non_blocking(nb_, ignored);
}
this->complete_now(ec);
2017-07-20 08:01:46 -07:00
}
}
};
2017-07-20 08:01:46 -07:00
} // detail
//------------------------------------------------------------------------------
template<class Protocol, class Executor>
2017-07-20 08:01:46 -07:00
void
teardown(
role_type role,
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(
net::socket_base::shutdown_send, ec);
if(ec)
return;
for(;;)
2017-07-20 08:01:46 -07:00
{
char buf[2048];
auto const bytes_transferred =
2019-02-02 20:44:04 -08:00
socket.read_some(net::buffer(buf), ec);
if(ec)
{
if(ec != net::error::eof)
return;
ec = {};
2017-07-20 08:01:46 -07:00
break;
}
if(bytes_transferred == 0)
{
// happens sometimes
// https://github.com/boostorg/beast/issues/1373
break;
}
2017-07-20 08:01:46 -07:00
}
if(role == role_type::client)
socket.shutdown(
net::socket_base::shutdown_send, ec);
if(ec)
return;
2017-07-20 08:01:46 -07:00
socket.close(ec);
}
template<
class Protocol, class Executor,
class TeardownHandler>
2017-07-20 08:01:46 -07:00
void
async_teardown(
role_type role,
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");
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