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>
|
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
|
|
|
{
|
2017-08-13 06:48:38 -07:00
|
|
|
struct state
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>& ws;
|
2017-08-15 16:59:17 -07:00
|
|
|
detail::frame_buffer fb;
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2017-08-13 06:48:38 -07:00
|
|
|
state(
|
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)
|
2017-05-14 09:25:43 -07:00
|
|
|
: ws(ws_)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2017-08-13 06:48:38 -07:00
|
|
|
// Serialize the control frame
|
2016-11-03 17:53:32 -04:00
|
|
|
ws.template write_ping<
|
2017-08-13 06:48:38 -07:00
|
|
|
flat_static_buffer_base>(
|
|
|
|
|
fb, op, payload);
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
state& d_;
|
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-07 13:35:19 -08:00
|
|
|
, d_(beast::allocate_stable<state>(
|
2019-01-05 19:40:46 -08:00
|
|
|
*this, ws, op, payload))
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 07:39:52 -07:00
|
|
|
void operator()(
|
|
|
|
|
error_code ec = {},
|
2019-01-05 19:40:46 -08:00
|
|
|
std::size_t bytes_transferred = 0)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
boost::ignore_unused(bytes_transferred);
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
BOOST_ASIO_CORO_REENTER(*this)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
// Maybe suspend
|
2019-01-19 07:24:00 -08:00
|
|
|
if(d_.ws.impl_->wr_block.try_lock(this))
|
2017-08-13 06:48:38 -07:00
|
|
|
{
|
2019-01-05 19:40:46 -08:00
|
|
|
// Make sure the stream is open
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! d_.ws.impl_->check_open(ec))
|
2019-01-05 19:40:46 -08:00
|
|
|
{
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
|
|
|
|
net::post(
|
|
|
|
|
d_.ws.get_executor(),
|
|
|
|
|
beast::bind_front_handler(std::move(*this), ec));
|
|
|
|
|
goto upcall;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Suspend
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->paused_ping.emplace(std::move(*this));
|
2019-01-05 19:40:46 -08:00
|
|
|
|
|
|
|
|
// Acquire the write block
|
2019-01-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->wr_block.lock(this);
|
2019-01-05 19:40:46 -08:00
|
|
|
|
|
|
|
|
// Resume
|
2017-08-13 06:48:38 -07:00
|
|
|
BOOST_ASIO_CORO_YIELD
|
2018-11-30 14:58:38 -08:00
|
|
|
net::post(
|
2019-01-05 19:40:46 -08:00
|
|
|
d_.ws.get_executor(), std::move(*this));
|
2019-01-19 07:24:00 -08:00
|
|
|
BOOST_ASSERT(d_.ws.impl_->wr_block.is_locked(this));
|
2017-08-13 06:48:38 -07:00
|
|
|
|
2019-01-05 19:40:46 -08:00
|
|
|
// Make sure the stream is open
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! d_.ws.impl_->check_open(ec))
|
2019-01-05 19:40:46 -08:00
|
|
|
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-19 07:24:00 -08:00
|
|
|
net::async_write(d_.ws.impl_->stream,
|
2019-01-05 19:40:46 -08:00
|
|
|
d_.fb.data(), std::move(*this));
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! d_.ws.impl_->check_ok(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-19 07:24:00 -08:00
|
|
|
d_.ws.impl_->wr_block.unlock(this);
|
|
|
|
|
d_.ws.impl_->paused_close.maybe_invoke() ||
|
|
|
|
|
d_.ws.impl_->paused_rd.maybe_invoke() ||
|
|
|
|
|
d_.ws.impl_->paused_wr.maybe_invoke();
|
2019-02-15 15:02:38 -08:00
|
|
|
this->invoke_now(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)
|
|
|
|
|
{
|
2017-08-13 06:48:38 -07:00
|
|
|
// Make sure the stream is open
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! impl_->check_open(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);
|
|
|
|
|
if(! impl_->check_ok(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)
|
|
|
|
|
{
|
2017-08-13 06:48:38 -07:00
|
|
|
// Make sure the stream is open
|
2019-01-19 07:24:00 -08:00
|
|
|
if(! impl_->check_open(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);
|
|
|
|
|
if(! impl_->check_ok(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(
|
|
|
|
|
WriteHandler, void(error_code))>{
|
2017-12-02 12:59:30 +01:00
|
|
|
std::move(init.completion_handler), *this,
|
2017-08-13 06:48:38 -07:00
|
|
|
detail::opcode::ping, payload}();
|
|
|
|
|
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(
|
|
|
|
|
WriteHandler, void(error_code))>{
|
2017-12-02 12:59:30 +01:00
|
|
|
std::move(init.completion_handler), *this,
|
2017-08-13 06:48:38 -07:00
|
|
|
detail::opcode::pong, payload}();
|
|
|
|
|
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
|