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

283 lines
7.2 KiB
Plaintext
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
//
2017-07-20 13:40:34 -07:00
#ifndef BOOST_BEAST_WEBSOCKET_IMPL_PING_IPP
#define BOOST_BEAST_WEBSOCKET_IMPL_PING_IPP
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-08-13 06:48:38 -07:00
#include <boost/asio/coroutine.hpp>
#include <boost/asio/handler_alloc_hook.hpp>
#include <boost/asio/handler_continuation_hook.hpp>
#include <boost/asio/handler_invoke_hook.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>
template<class Handler>
class stream<NextLayer>::ping_op
2017-08-13 06:48:38 -07:00
: public boost::asio::coroutine
{
2017-08-13 06:48:38 -07:00
struct state
{
stream<NextLayer>& ws;
detail::frame_buffer fb;
token tok;
2017-08-13 06:48:38 -07:00
state(
Handler&,
stream<NextLayer>& ws_,
detail::opcode op,
ping_data const& payload)
: ws(ws_)
, tok(ws.t_.unique())
{
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);
}
};
2017-08-13 06:48:38 -07:00
handler_ptr<state, Handler> d_;
public:
ping_op(ping_op&&) = default;
ping_op(ping_op const&) = default;
2017-08-13 06:48:38 -07:00
template<class DeducedHandler>
ping_op(
DeducedHandler&& h,
stream<NextLayer>& ws,
detail::opcode op,
ping_data const& payload)
: d_(std::forward<DeducedHandler>(h),
2017-08-13 06:48:38 -07:00
ws, op, payload)
{
}
2017-08-13 06:48:38 -07:00
void operator()(
error_code ec = {},
2017-06-29 09:48:30 -07:00
std::size_t bytes_transferred = 0);
friend
void* asio_handler_allocate(
std::size_t size, ping_op* op)
{
using boost::asio::asio_handler_allocate;
return asio_handler_allocate(
size, std::addressof(op->d_.handler()));
}
friend
void asio_handler_deallocate(
void* p, std::size_t size, ping_op* op)
{
using boost::asio::asio_handler_deallocate;
asio_handler_deallocate(
p, size, std::addressof(op->d_.handler()));
}
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-08-26 08:01:44 -04:00
template<class Function>
friend
void asio_handler_invoke(Function&& f, ping_op* op)
{
using boost::asio::asio_handler_invoke;
asio_handler_invoke(
f, std::addressof(op->d_.handler()));
}
};
template<class NextLayer>
template<class Handler>
void
stream<NextLayer>::
ping_op<Handler>::
2017-06-29 09:48:30 -07:00
operator()(error_code ec, std::size_t)
{
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_)
{
2017-08-13 06:48:38 -07:00
// Acquire the write block
d.ws.wr_block_ = d.tok;
// Make sure the stream is open
if(d.ws.failed_)
{
BOOST_ASIO_CORO_YIELD
d.ws.get_io_service().post(
bind_handler(std::move(*this),
boost::asio::error::operation_aborted));
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
d.ws.ping_op_.emplace(std::move(*this));
// Acquire the write block
BOOST_ASSERT(! d.ws.wr_block_);
d.ws.wr_block_ = d.tok;
// Resume
BOOST_ASIO_CORO_YIELD
d.ws.get_io_service().post(std::move(*this));
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
// Make sure the stream is open
if(d.ws.failed_)
{
ec = boost::asio::error::operation_aborted;
goto upcall;
}
2017-06-29 09:48:30 -07: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-13 06:48:38 -07:00
if(ec)
d.ws.failed_ = true;
2017-06-29 09:48:30 -07:00
2017-08-13 06:48:38 -07:00
upcall:
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
2017-08-13 06:48:38 -07:00
d.ws.wr_block_.reset();
d.ws.close_op_.maybe_invoke() ||
d.ws.rd_op_.maybe_invoke() ||
d.ws.wr_op_.maybe_invoke();
d_.invoke(ec);
}
}
2017-08-13 06:48:38 -07:00
//------------------------------------------------------------------------------
template<class NextLayer>
void
stream<NextLayer>::
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>
void
stream<NextLayer>::
ping(ping_data const& payload, error_code& ec)
{
2017-08-13 06:48:38 -07:00
// Make sure the stream is open
if(failed_)
{
ec = boost::asio::error::operation_aborted;
return;
}
detail::frame_buffer fb;
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);
}
template<class NextLayer>
void
stream<NextLayer>::
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>
void
stream<NextLayer>::
pong(ping_data const& payload, error_code& ec)
{
2017-08-13 06:48:38 -07:00
// Make sure the stream is open
if(failed_)
{
ec = boost::asio::error::operation_aborted;
return;
}
detail::frame_buffer fb;
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-13 06:48:38 -07:00
template<class NextLayer>
template<class WriteHandler>
async_return_type<
WriteHandler, void(error_code)>
stream<NextLayer>::
async_ping(ping_data const& payload, WriteHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream requirements requirements not met");
async_completion<WriteHandler,
void(error_code)> init{handler};
ping_op<handler_type<
WriteHandler, void(error_code)>>{
init.completion_handler, *this,
detail::opcode::ping, payload}();
return init.result.get();
}
template<class NextLayer>
template<class WriteHandler>
async_return_type<
WriteHandler, void(error_code)>
stream<NextLayer>::
async_pong(ping_data const& payload, WriteHandler&& handler)
{
static_assert(is_async_stream<next_layer_type>::value,
"AsyncStream requirements requirements not met");
async_completion<WriteHandler,
void(error_code)> init{handler};
ping_op<handler_type<
WriteHandler, void(error_code)>>{
init.completion_handler, *this,
detail::opcode::pong, payload}();
return init.result.get();
}
} // websocket
} // beast
2017-07-20 13:40:34 -07:00
} // boost
#endif