2016-05-15 16:22:25 -04:00
|
|
|
//
|
2017-07-24 09:42:36 -07:00
|
|
|
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2016-05-15 16:22:25 -04: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
|
|
|
|
|
//
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2019-01-19 07:24:00 -08:00
|
|
|
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_PING_HPP
|
|
|
|
|
#define BOOST_BEAST_WEBSOCKET_IMPL_PING_HPP
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2019-01-07 13:35:19 -08:00
|
|
|
#include <boost/beast/core/async_op_base.hpp>
|
2017-07-20 13:40:34 -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>
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/websocket/detail/frame.hpp>
|
2017-08-13 06:48:38 -07:00
|
|
|
#include <boost/asio/coroutine.hpp>
|
2017-09-07 07:39:52 -07:00
|
|
|
#include <boost/asio/post.hpp>
|
2017-05-22 15:30:12 -07:00
|
|
|
#include <boost/throw_exception.hpp>
|
2016-05-15 16:22:25 -04:00
|
|
|
#include <memory>
|
|
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
namespace boost {
|
2016-05-15 16:22:25 -04:00
|
|
|
namespace beast {
|
|
|
|
|
namespace websocket {
|
|
|
|
|
|
2017-08-13 06:48:38 -07:00
|
|
|
/*
|
|
|
|
|
This composed operation handles sending ping and pong frames.
|
|
|
|
|
It only sends the frames it does not make attempts to read
|
|
|
|
|
any frame data.
|
|
|
|
|
*/
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-05-15 16:22:25 -04:00
|
|
|
template<class Handler>
|
2017-11-18 16:52:18 -08:00
|
|
|
class stream<NextLayer, deflateSupported>::ping_op
|
2019-01-07 13:35:19 -08:00
|
|
|
: public beast::stable_async_op_base<
|
2019-02-04 21:52:54 -08:00
|
|
|
Handler, beast::executor_type<stream>>
|
2019-01-05 19:40:46 -08:00
|
|
|
, public net::coroutine
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2019-01-20 12:25:30 -08:00
|
|
|
stream<NextLayer, deflateSupported>& ws_;
|
|
|
|
|
detail::frame_buffer& fb_;
|
2016-05-15 16:22:25 -04:00
|
|
|
|
|
|
|
|
public:
|
2018-02-18 18:46:12 -08:00
|
|
|
static constexpr int id = 3; // for soft_mutex
|
|
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
template<class Handler_>
|
2017-08-13 06:48:38 -07:00
|
|
|
ping_op(
|
2019-01-05 19:40:46 -08:00
|
|
|
Handler_&& h,
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>& ws,
|
2017-08-13 06:48:38 -07:00
|
|
|
detail::opcode op,
|
|
|
|
|
ping_data const& payload)
|
2019-01-09 16:11:15 -08:00
|
|
|
: stable_async_op_base<
|
2019-02-04 21:52:54 -08:00
|
|
|
Handler, beast::executor_type<stream>>(
|
2019-01-09 16:11:15 -08:00
|
|
|
std::forward<Handler_>(h), ws.get_executor())
|
2019-01-20 12:25:30 -08:00
|
|
|
, ws_(ws)
|
|
|
|
|
, fb_(beast::allocate_stable<
|
|
|
|
|
detail::frame_buffer>(*this))
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2019-01-20 12:25:30 -08:00
|
|
|
// Serialize the ping or pong frame
|
|
|
|
|
ws.template write_ping<
|
|
|
|
|
flat_static_buffer_base>(fb_, op, payload);
|
|
|
|
|
(*this)({}, 0, false);
|
2016-05-15 16:22:25 -04: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)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
boost::ignore_unused(bytes_transferred);
|
2019-01-20 12:25:30 -08:00
|
|
|
auto& impl = *ws_.impl_;
|
2019-01-05 19:40:46 -08:00
|
|
|
BOOST_ASIO_CORO_REENTER(*this)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2019-01-20 12:25:30 -08:00
|
|
|
// Acquire the write lock
|
|
|
|
|
if(! impl.wr_block.try_lock(this))
|
2017-08-13 06:48:38 -07:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
BOOST_ASIO_CORO_YIELD
|
2019-01-20 12:25:30 -08:00
|
|
|
impl.paused_ping.emplace(std::move(*this));
|
|
|
|
|
impl.wr_block.lock(this);
|
2017-08-13 06:48:38 -07:00
|
|
|
BOOST_ASIO_CORO_YIELD
|
2019-01-20 12:25:30 -08:00
|
|
|
net::post(std::move(*this));
|
|
|
|
|
BOOST_ASSERT(impl.wr_block.is_locked(this));
|
2019-01-05 19:40:46 -08:00
|
|
|
}
|
2019-01-20 12:25:30 -08:00
|
|
|
if(impl.check_stop_now(ec))
|
|
|
|
|
goto upcall;
|
2017-08-13 06:48:38 -07:00
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
// Send ping frame
|
2017-08-13 06:48:38 -07:00
|
|
|
BOOST_ASIO_CORO_YIELD
|
2019-01-20 12:25:30 -08:00
|
|
|
net::async_write(impl.stream, fb_.data(),
|
|
|
|
|
beast::detail::bind_continuation(std::move(*this)));
|
|
|
|
|
if(impl.check_stop_now(ec))
|
2017-08-13 06:48:38 -07:00
|
|
|
goto upcall;
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
upcall:
|
2019-01-20 12:25:30 -08:00
|
|
|
impl.wr_block.unlock(this);
|
|
|
|
|
impl.paused_close.maybe_invoke()
|
|
|
|
|
|| impl.paused_rd.maybe_invoke()
|
|
|
|
|
|| impl.paused_wr.maybe_invoke();
|
|
|
|
|
this->invoke(cont, ec);
|
2018-04-09 16:22:15 -07:00
|
|
|
}
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
2019-01-05 19:40:46 -08:00
|
|
|
};
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2017-08-13 06:48:38 -07:00
|
|
|
//------------------------------------------------------------------------------
|
2016-10-24 08:12:09 -04:00
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-10-24 08:12:09 -04:00
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2016-10-24 08:12:09 -04:00
|
|
|
ping(ping_data const& payload)
|
|
|
|
|
{
|
|
|
|
|
error_code ec;
|
|
|
|
|
ping(payload, ec);
|
|
|
|
|
if(ec)
|
2017-05-22 15:30:12 -07:00
|
|
|
BOOST_THROW_EXCEPTION(system_error{ec});
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-10-24 08:12:09 -04:00
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2016-10-24 08:12:09 -04:00
|
|
|
ping(ping_data const& payload, error_code& ec)
|
|
|
|
|
{
|
2019-01-20 12:25:30 -08:00
|
|
|
if(impl_->check_stop_now(ec))
|
2017-08-13 06:48:38 -07:00
|
|
|
return;
|
2017-08-15 16:59:17 -07:00
|
|
|
detail::frame_buffer fb;
|
2017-07-14 20:25:39 -07:00
|
|
|
write_ping<flat_static_buffer_base>(
|
2017-08-13 06:48:38 -07:00
|
|
|
fb, detail::opcode::ping, payload);
|
2019-01-19 07:24:00 -08:00
|
|
|
net::write(impl_->stream, fb.data(), ec);
|
2019-01-20 12:25:30 -08:00
|
|
|
if(impl_->check_stop_now(ec))
|
2017-08-26 20:10:04 -07:00
|
|
|
return;
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-11-03 17:53:32 -04:00
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2016-11-03 17:53:32 -04:00
|
|
|
pong(ping_data const& payload)
|
|
|
|
|
{
|
|
|
|
|
error_code ec;
|
|
|
|
|
pong(payload, ec);
|
|
|
|
|
if(ec)
|
2017-05-22 15:30:12 -07:00
|
|
|
BOOST_THROW_EXCEPTION(system_error{ec});
|
2016-11-03 17:53:32 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-11-03 17:53:32 -04:00
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2016-11-03 17:53:32 -04:00
|
|
|
pong(ping_data const& payload, error_code& ec)
|
|
|
|
|
{
|
2019-01-20 12:25:30 -08:00
|
|
|
if(impl_->check_stop_now(ec))
|
2017-08-13 06:48:38 -07:00
|
|
|
return;
|
2017-08-15 16:59:17 -07:00
|
|
|
detail::frame_buffer fb;
|
2017-07-14 20:25:39 -07:00
|
|
|
write_ping<flat_static_buffer_base>(
|
2017-08-13 06:48:38 -07:00
|
|
|
fb, detail::opcode::pong, payload);
|
2019-01-19 07:24:00 -08:00
|
|
|
net::write(impl_->stream, fb.data(), ec);
|
2019-01-20 12:25:30 -08:00
|
|
|
if(impl_->check_stop_now(ec))
|
2017-08-26 20:10:04 -07:00
|
|
|
return;
|
2016-11-03 17:53:32 -04:00
|
|
|
}
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-08-13 06:48:38 -07:00
|
|
|
template<class WriteHandler>
|
2017-09-07 07:39:52 -07:00
|
|
|
BOOST_ASIO_INITFN_RESULT_TYPE(
|
|
|
|
|
WriteHandler, void(error_code))
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-08-13 06:48:38 -07:00
|
|
|
async_ping(ping_data const& payload, WriteHandler&& handler)
|
|
|
|
|
{
|
|
|
|
|
static_assert(is_async_stream<next_layer_type>::value,
|
2017-11-21 11:50:15 +09:00
|
|
|
"AsyncStream requirements not met");
|
2018-01-26 08:58:19 -08:00
|
|
|
BOOST_BEAST_HANDLER_INIT(
|
|
|
|
|
WriteHandler, void(error_code));
|
2017-09-07 07:39:52 -07:00
|
|
|
ping_op<BOOST_ASIO_HANDLER_TYPE(
|
2019-01-20 12:25:30 -08:00
|
|
|
WriteHandler, void(error_code))>(
|
2017-12-02 12:59:30 +01:00
|
|
|
std::move(init.completion_handler), *this,
|
2019-01-20 12:25:30 -08:00
|
|
|
detail::opcode::ping, payload);
|
2017-08-13 06:48:38 -07:00
|
|
|
return init.result.get();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2017-08-13 06:48:38 -07:00
|
|
|
template<class WriteHandler>
|
2017-09-07 07:39:52 -07:00
|
|
|
BOOST_ASIO_INITFN_RESULT_TYPE(
|
|
|
|
|
WriteHandler, void(error_code))
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2017-08-13 06:48:38 -07:00
|
|
|
async_pong(ping_data const& payload, WriteHandler&& handler)
|
|
|
|
|
{
|
|
|
|
|
static_assert(is_async_stream<next_layer_type>::value,
|
2017-11-21 11:50:15 +09:00
|
|
|
"AsyncStream requirements not met");
|
2018-01-26 08:58:19 -08:00
|
|
|
BOOST_BEAST_HANDLER_INIT(
|
|
|
|
|
WriteHandler, void(error_code));
|
2017-09-07 07:39:52 -07:00
|
|
|
ping_op<BOOST_ASIO_HANDLER_TYPE(
|
2019-01-20 12:25:30 -08:00
|
|
|
WriteHandler, void(error_code))>(
|
2017-12-02 12:59:30 +01:00
|
|
|
std::move(init.completion_handler), *this,
|
2019-01-20 12:25:30 -08:00
|
|
|
detail::opcode::pong, payload);
|
2017-08-13 06:48:38 -07:00
|
|
|
return init.result.get();
|
|
|
|
|
}
|
2016-10-24 08:12:09 -04:00
|
|
|
|
2016-05-15 16:22:25 -04:00
|
|
|
} // websocket
|
|
|
|
|
} // beast
|
2017-07-20 13:40:34 -07:00
|
|
|
} // boost
|
2016-05-15 16:22:25 -04:00
|
|
|
|
|
|
|
|
#endif
|