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
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_PING_IPP
|
|
|
|
|
#define BOOST_BEAST_WEBSOCKET_IMPL_PING_IPP
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/core/bind_handler.hpp>
|
|
|
|
|
#include <boost/beast/core/handler_ptr.hpp>
|
|
|
|
|
#include <boost/beast/core/type_traits.hpp>
|
|
|
|
|
#include <boost/beast/core/detail/config.hpp>
|
|
|
|
|
#include <boost/beast/websocket/detail/frame.hpp>
|
2017-09-07 07:39:52 -07:00
|
|
|
#include <boost/asio/associated_allocator.hpp>
|
|
|
|
|
#include <boost/asio/associated_executor.hpp>
|
2017-08-13 06:48:38 -07:00
|
|
|
#include <boost/asio/coroutine.hpp>
|
2017-05-14 09:25:43 -07:00
|
|
|
#include <boost/asio/handler_continuation_hook.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
|
2017-08-13 06:48:38 -07:00
|
|
|
: public boost::asio::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;
|
2017-07-15 17:05:24 -07:00
|
|
|
token tok;
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2017-08-13 06:48:38 -07:00
|
|
|
state(
|
2017-12-04 13:02:31 -08:00
|
|
|
Handler const&,
|
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_)
|
2017-08-26 15:18:02 -07:00
|
|
|
, tok(ws.tok_.unique())
|
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
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-13 06:48:38 -07:00
|
|
|
handler_ptr<state, Handler> d_;
|
2016-05-15 16:22:25 -04:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ping_op(ping_op&&) = default;
|
2017-12-02 12:59:30 +01:00
|
|
|
ping_op(ping_op const&) = delete;
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2017-08-13 06:48:38 -07:00
|
|
|
template<class DeducedHandler>
|
|
|
|
|
ping_op(
|
|
|
|
|
DeducedHandler&& 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)
|
2017-01-29 19:46:17 -05:00
|
|
|
: d_(std::forward<DeducedHandler>(h),
|
2017-08-13 06:48:38 -07:00
|
|
|
ws, op, payload)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 07:39:52 -07:00
|
|
|
using allocator_type =
|
|
|
|
|
boost::asio::associated_allocator_t<Handler>;
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2017-09-07 07:39:52 -07:00
|
|
|
allocator_type
|
|
|
|
|
get_allocator() const noexcept
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2017-12-02 05:57:06 -08:00
|
|
|
return (boost::asio::get_associated_allocator)(d_.handler());
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
|
2017-09-07 07:39:52 -07:00
|
|
|
using executor_type = boost::asio::associated_executor_t<
|
2017-11-18 16:52:18 -08:00
|
|
|
Handler, decltype(std::declval<stream<NextLayer, deflateSupported>&>().get_executor())>;
|
2017-09-07 07:39:52 -07:00
|
|
|
|
2017-10-24 06:40:22 -07:00
|
|
|
executor_type
|
|
|
|
|
get_executor() const noexcept
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2017-12-02 05:57:06 -08:00
|
|
|
return (boost::asio::get_associated_executor)(
|
2017-09-07 07:39:52 -07:00
|
|
|
d_.handler(), d_->ws.get_executor());
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
|
2017-09-07 07:39:52 -07:00
|
|
|
void operator()(
|
|
|
|
|
error_code ec = {},
|
|
|
|
|
std::size_t bytes_transferred = 0);
|
|
|
|
|
|
2016-05-15 16:22:25 -04:00
|
|
|
friend
|
|
|
|
|
bool asio_handler_is_continuation(ping_op* op)
|
|
|
|
|
{
|
2017-06-29 09:48:30 -07:00
|
|
|
using boost::asio::asio_handler_is_continuation;
|
|
|
|
|
return asio_handler_is_continuation(
|
|
|
|
|
std::addressof(op->d_.handler()));
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-11-18 16:52:18 -08:00
|
|
|
template<class NextLayer, bool deflateSupported>
|
2016-05-15 16:22:25 -04:00
|
|
|
template<class Handler>
|
|
|
|
|
void
|
2017-11-18 16:52:18 -08:00
|
|
|
stream<NextLayer, deflateSupported>::
|
2016-05-15 16:22:25 -04:00
|
|
|
ping_op<Handler>::
|
2017-06-29 09:48:30 -07:00
|
|
|
operator()(error_code ec, std::size_t)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
|
|
|
|
auto& d = *d_;
|
2017-08-13 06:48:38 -07:00
|
|
|
BOOST_ASIO_CORO_REENTER(*this)
|
2017-06-29 09:48:30 -07:00
|
|
|
{
|
2017-08-13 06:48:38 -07:00
|
|
|
// Maybe suspend
|
|
|
|
|
if(! d.ws.wr_block_)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2017-08-13 06:48:38 -07:00
|
|
|
// Acquire the write block
|
|
|
|
|
d.ws.wr_block_ = d.tok;
|
|
|
|
|
|
|
|
|
|
// Make sure the stream is open
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! d.ws.check_open(ec))
|
2017-08-13 06:48:38 -07:00
|
|
|
{
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2017-09-07 07:39:52 -07:00
|
|
|
boost::asio::post(
|
|
|
|
|
d.ws.get_executor(),
|
2017-08-26 20:10:04 -07:00
|
|
|
bind_handler(std::move(*this), ec));
|
2017-08-13 06:48:38 -07:00
|
|
|
goto upcall;
|
|
|
|
|
}
|
2017-06-29 09:48:30 -07:00
|
|
|
}
|
2017-08-13 06:48:38 -07:00
|
|
|
else
|
2017-06-29 09:48:30 -07:00
|
|
|
{
|
2017-08-13 06:48:38 -07:00
|
|
|
// Suspend
|
|
|
|
|
BOOST_ASSERT(d.ws.wr_block_ != d.tok);
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2017-08-26 15:18:02 -07:00
|
|
|
d.ws.paused_ping_.emplace(std::move(*this));
|
2017-08-13 06:48:38 -07:00
|
|
|
|
|
|
|
|
// Acquire the write block
|
|
|
|
|
BOOST_ASSERT(! d.ws.wr_block_);
|
|
|
|
|
d.ws.wr_block_ = d.tok;
|
|
|
|
|
|
|
|
|
|
// Resume
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2017-09-07 07:39:52 -07:00
|
|
|
boost::asio::post(
|
|
|
|
|
d.ws.get_executor(), std::move(*this));
|
2017-08-13 06:48:38 -07:00
|
|
|
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
|
|
|
|
|
|
|
|
|
// Make sure the stream is open
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! d.ws.check_open(ec))
|
2017-08-13 06:48:38 -07:00
|
|
|
goto upcall;
|
2017-06-29 09:48:30 -07:00
|
|
|
}
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2017-08-13 06:48:38 -07:00
|
|
|
// Send ping frame
|
|
|
|
|
BOOST_ASIO_CORO_YIELD
|
2017-06-29 09:48:30 -07:00
|
|
|
boost::asio::async_write(d.ws.stream_,
|
|
|
|
|
d.fb.data(), std::move(*this));
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! d.ws.check_ok(ec))
|
2017-08-26 20:10:04 -07:00
|
|
|
goto upcall;
|
2017-06-29 09:48:30 -07:00
|
|
|
|
2017-08-13 06:48:38 -07:00
|
|
|
upcall:
|
2017-07-15 17:05:24 -07:00
|
|
|
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
2017-08-13 06:48:38 -07:00
|
|
|
d.ws.wr_block_.reset();
|
2017-08-26 15:18:02 -07:00
|
|
|
d.ws.paused_close_.maybe_invoke() ||
|
|
|
|
|
d.ws.paused_rd_.maybe_invoke() ||
|
|
|
|
|
d.ws.paused_wr_.maybe_invoke();
|
2017-08-13 06:48:38 -07:00
|
|
|
d_.invoke(ec);
|
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
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! 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);
|
|
|
|
|
boost::asio::write(stream_, fb.data(), ec);
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! 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
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! 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);
|
|
|
|
|
boost::asio::write(stream_, fb.data(), ec);
|
2017-08-31 17:52:09 -07:00
|
|
|
if(! 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
|