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

276 lines
7.0 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>
#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 {
//------------------------------------------------------------------------------
// write a ping frame
//
template<class NextLayer>
template<class Handler>
class stream<NextLayer>::ping_op
{
struct data : op
{
stream<NextLayer>& ws;
detail::frame_streambuf fb;
int state = 0;
token tok;
2017-06-29 09:48:30 -07:00
data(Handler&, stream<NextLayer>& ws_,
detail::opcode op_, ping_data const& payload)
: ws(ws_)
, tok(ws.t_.unique())
{
using boost::asio::buffer;
using boost::asio::buffer_copy;
ws.template write_ping<
flat_static_buffer_base>(fb, op_, payload);
}
};
handler_ptr<data, Handler> d_;
public:
ping_op(ping_op&&) = default;
ping_op(ping_op const&) = default;
template<class DeducedHandler, class... Args>
ping_op(DeducedHandler&& h,
stream<NextLayer>& ws, Args&&... args)
: d_(std::forward<DeducedHandler>(h),
ws, std::forward<Args>(args)...)
{
}
void operator()()
{
2017-06-29 09:48:30 -07:00
(*this)({});
}
2017-06-29 09:48:30 -07:00
void operator()(error_code ec,
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_;
if(ec)
2017-06-29 09:48:30 -07:00
{
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
2017-06-29 09:48:30 -07:00
d.ws.failed_ = true;
goto upcall;
2017-06-29 09:48:30 -07:00
}
switch(d.state)
{
2017-06-29 09:48:30 -07:00
case 0:
if(d.ws.wr_block_)
{
2017-06-29 09:48:30 -07:00
// suspend
d.state = 1;
d.ws.ping_op_.emplace(std::move(*this));
return;
2017-06-29 09:48:30 -07:00
}
d.ws.wr_block_ = d.tok;
2017-06-29 09:48:30 -07:00
if(d.ws.failed_ || d.ws.wr_close_)
{
// call handler
return d.ws.get_io_service().post(
2017-06-29 09:48:30 -07:00
bind_handler(std::move(*this),
boost::asio::error::operation_aborted));
}
2017-06-29 09:48:30 -07:00
do_write:
// send ping frame
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
2017-06-29 09:48:30 -07:00
d.state = 3;
boost::asio::async_write(d.ws.stream_,
d.fb.data(), std::move(*this));
return;
case 1:
BOOST_ASSERT(! d.ws.wr_block_);
d.ws.wr_block_ = d.tok;
2017-06-29 09:48:30 -07:00
d.state = 2;
// The current context is safe but might not be
// the same as the one for this operation (since
// we are being called from a write operation).
// Call post to make sure we are invoked the same
// way as the final handler for this operation.
d.ws.get_io_service().post(
bind_handler(std::move(*this), ec));
return;
case 2:
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
2017-06-29 09:48:30 -07:00
if(d.ws.failed_ || d.ws.wr_close_)
{
// call handler
ec = boost::asio::error::operation_aborted;
goto upcall;
}
2017-06-29 09:48:30 -07:00
goto do_write;
case 3:
break;
}
upcall:
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
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);
}
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,
2017-06-29 09:48:30 -07:00
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,
2017-06-29 09:48:30 -07:00
detail::opcode::pong, payload}({});
return init.result.get();
}
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)
{
detail::frame_streambuf db;
write_ping<flat_static_buffer_base>(
db, detail::opcode::ping, payload);
boost::asio::write(stream_, db.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)
{
detail::frame_streambuf db;
write_ping<flat_static_buffer_base>(
db, detail::opcode::pong, payload);
boost::asio::write(stream_, db.data(), ec);
}
//------------------------------------------------------------------------------
} // websocket
} // beast
2017-07-20 13:40:34 -07:00
} // boost
#endif