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
|
|
|
|
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>
|
2017-08-04 13:31:24 -07:00
|
|
|
#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>
|
2019-02-05 13:02:19 -08:00
|
|
|
#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 {
|
|
|
|
|
|
|
|
template<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-01-05 19:40:46 -08:00
|
|
|
net::ip::tcp::socket>>
|
|
|
|
, public net::coroutine
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
using socket_type = net::ip::tcp::socket;
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-08-04 13:31:24 -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,
|
2017-08-04 13:31:24 -07:00
|
|
|
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-01-09 16:11:15 -08:00
|
|
|
net::ip::tcp::socket>>(
|
|
|
|
std::forward<Handler_>(h), s.get_executor())
|
2017-08-04 13:31:24 -07:00
|
|
|
, 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
|
|
|
}
|
|
|
|
|
2017-09-07 07:39:52 -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
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
using tcp = net::ip::tcp;
|
|
|
|
BOOST_ASIO_CORO_REENTER(*this)
|
2017-08-04 13:31:24 -07:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
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(tcp::socket::shutdown_send, ec);
|
|
|
|
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(
|
|
|
|
net::ip::tcp::socket::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)
|
|
|
|
s_.shutdown(tcp::socket::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));
|
|
|
|
}
|
2018-02-20 03:33:02 -08:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
error_code ignored;
|
|
|
|
s_.non_blocking(nb_, ignored);
|
2018-02-20 03:33:02 -08:00
|
|
|
}
|
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
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
2017-08-04 13:31:24 -07:00
|
|
|
teardown(
|
|
|
|
role_type role,
|
2018-11-30 14:58:38 -08:00
|
|
|
net::ip::tcp::socket& socket,
|
2017-08-04 13:31:24 -07:00
|
|
|
error_code& ec)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-08-04 13:31:24 -07:00
|
|
|
if(role == role_type::server)
|
|
|
|
socket.shutdown(
|
2018-11-30 14:58:38 -08:00
|
|
|
net::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 =
|
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
|
|
|
}
|
2017-08-04 13:31:24 -07:00
|
|
|
if(role == role_type::client)
|
|
|
|
socket.shutdown(
|
2018-11-30 14:58:38 -08:00
|
|
|
net::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>
|
|
|
|
void
|
2017-08-04 13:31:24 -07:00
|
|
|
async_teardown(
|
|
|
|
role_type role,
|
2018-11-30 14:58:38 -08:00
|
|
|
net::ip::tcp::socket& socket,
|
2017-08-04 13:31:24 -07:00
|
|
|
TeardownHandler&& handler)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2019-02-05 13:02:19 -08:00
|
|
|
static_assert(beast::detail::is_invocable<
|
2017-07-20 08:01:46 -07:00
|
|
|
TeardownHandler, void(error_code)>::value,
|
|
|
|
"TeardownHandler requirements not met");
|
|
|
|
detail::teardown_tcp_op<typename std::decay<
|
2019-01-05 19:40:46 -08:00
|
|
|
TeardownHandler>::type>(std::forward<
|
2019-01-20 12:25:30 -08:00
|
|
|
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
|