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

230 lines
6.5 KiB
C++
Raw Normal View History

//
2017-07-24 09:42:36 -07:00
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// 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
//
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_PING_HPP
#define BOOST_BEAST_WEBSOCKET_IMPL_PING_HPP
#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>
#include <boost/asio/post.hpp>
2017-05-22 15:30:12 -07:00
#include <boost/throw_exception.hpp>
#include <memory>
2017-07-20 13:40:34 -07:00
namespace boost {
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.
*/
template<class NextLayer, bool deflateSupported>
template<class Handler>
class stream<NextLayer, deflateSupported>::ping_op
: public beast::stable_async_op_base<
2019-02-04 21:52:54 -08:00
Handler, beast::executor_type<stream>>
, public net::coroutine
{
2017-08-13 06:48:38 -07:00
struct state
{
stream<NextLayer, deflateSupported>& ws;
detail::frame_buffer fb;
2017-08-13 06:48:38 -07:00
state(
stream<NextLayer, deflateSupported>& ws_,
2017-08-13 06:48:38 -07:00
detail::opcode op,
ping_data const& payload)
: ws(ws_)
{
2017-08-13 06:48:38 -07:00
// Serialize the control frame
ws.template write_ping<
2017-08-13 06:48:38 -07:00
flat_static_buffer_base>(
fb, op, payload);
}
};
state& d_;
public:
2018-02-18 18:46:12 -08:00
static constexpr int id = 3; // for soft_mutex
template<class Handler_>
2017-08-13 06:48:38 -07:00
ping_op(
Handler_&& h,
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())
, d_(beast::allocate_stable<state>(
*this, ws, op, payload))
{
}
void operator()(
error_code ec = {},
std::size_t bytes_transferred = 0)
{
boost::ignore_unused(bytes_transferred);
BOOST_ASIO_CORO_REENTER(*this)
{
// Maybe suspend
if(d_.ws.impl_->wr_block.try_lock(this))
2017-08-13 06:48:38 -07:00
{
// Make sure the stream is open
if(! d_.ws.impl_->check_open(ec))
{
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
d_.ws.impl_->paused_ping.emplace(std::move(*this));
// Acquire the write block
d_.ws.impl_->wr_block.lock(this);
// Resume
2017-08-13 06:48:38 -07:00
BOOST_ASIO_CORO_YIELD
net::post(
d_.ws.get_executor(), std::move(*this));
BOOST_ASSERT(d_.ws.impl_->wr_block.is_locked(this));
2017-08-13 06:48:38 -07:00
// Make sure the stream is open
if(! d_.ws.impl_->check_open(ec))
goto upcall;
}
2017-08-13 06:48:38 -07:00
// Send ping frame
2017-08-13 06:48:38 -07:00
BOOST_ASIO_CORO_YIELD
net::async_write(d_.ws.impl_->stream,
d_.fb.data(), std::move(*this));
if(! d_.ws.impl_->check_ok(ec))
2017-08-13 06:48:38 -07:00
goto upcall;
upcall:
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);
}
}
};
2017-08-13 06:48:38 -07:00
//------------------------------------------------------------------------------
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
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});
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
ping(ping_data const& payload, error_code& ec)
{
2017-08-13 06:48:38 -07:00
// Make sure the stream is open
if(! impl_->check_open(ec))
2017-08-13 06:48:38 -07:00
return;
detail::frame_buffer fb;
write_ping<flat_static_buffer_base>(
2017-08-13 06:48:38 -07:00
fb, detail::opcode::ping, payload);
net::write(impl_->stream, fb.data(), ec);
if(! impl_->check_ok(ec))
return;
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
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});
}
template<class NextLayer, bool deflateSupported>
void
stream<NextLayer, deflateSupported>::
pong(ping_data const& payload, error_code& ec)
{
2017-08-13 06:48:38 -07:00
// Make sure the stream is open
if(! impl_->check_open(ec))
2017-08-13 06:48:38 -07:00
return;
detail::frame_buffer fb;
write_ping<flat_static_buffer_base>(
2017-08-13 06:48:38 -07:00
fb, detail::opcode::pong, payload);
net::write(impl_->stream, fb.data(), ec);
if(! impl_->check_ok(ec))
return;
}
template<class NextLayer, bool deflateSupported>
2017-08-13 06:48:38 -07:00
template<class WriteHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(
WriteHandler, void(error_code))
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");
BOOST_BEAST_HANDLER_INIT(
WriteHandler, void(error_code));
ping_op<BOOST_ASIO_HANDLER_TYPE(
WriteHandler, void(error_code))>{
std::move(init.completion_handler), *this,
2017-08-13 06:48:38 -07:00
detail::opcode::ping, payload}();
return init.result.get();
}
template<class NextLayer, bool deflateSupported>
2017-08-13 06:48:38 -07:00
template<class WriteHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(
WriteHandler, void(error_code))
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");
BOOST_BEAST_HANDLER_INIT(
WriteHandler, void(error_code));
ping_op<BOOST_ASIO_HANDLER_TYPE(
WriteHandler, void(error_code))>{
std::move(init.completion_handler), *this,
2017-08-13 06:48:38 -07:00
detail::opcode::pong, payload}();
return init.result.get();
}
} // websocket
} // beast
2017-07-20 13:40:34 -07:00
} // boost
#endif