2016-05-15 16:22:25 -04:00
|
|
|
//
|
2017-02-06 20:07:03 -05:00
|
|
|
// Copyright (c) 2013-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)
|
|
|
|
|
//
|
|
|
|
|
|
2016-10-24 08:12:09 -04:00
|
|
|
#ifndef BEAST_WEBSOCKET_IMPL_PING_IPP
|
|
|
|
|
#define BEAST_WEBSOCKET_IMPL_PING_IPP
|
2016-05-15 16:22:25 -04:00
|
|
|
|
|
|
|
|
#include <beast/core/bind_handler.hpp>
|
2017-01-05 09:07:18 -05:00
|
|
|
#include <beast/core/handler_helpers.hpp>
|
2017-01-02 13:29:48 -05:00
|
|
|
#include <beast/core/handler_ptr.hpp>
|
2016-10-24 08:12:09 -04:00
|
|
|
#include <beast/core/stream_concepts.hpp>
|
2016-05-15 16:22:25 -04:00
|
|
|
#include <beast/websocket/detail/frame.hpp>
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
namespace beast {
|
|
|
|
|
namespace websocket {
|
|
|
|
|
|
2016-10-24 08:12:09 -04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2016-05-15 16:22:25 -04:00
|
|
|
// write a ping frame
|
|
|
|
|
//
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
template<class Handler>
|
|
|
|
|
class stream<NextLayer>::ping_op
|
|
|
|
|
{
|
|
|
|
|
struct data : op
|
|
|
|
|
{
|
2017-01-02 13:29:48 -05:00
|
|
|
bool cont;
|
2016-05-15 16:22:25 -04:00
|
|
|
stream<NextLayer>& ws;
|
|
|
|
|
detail::frame_streambuf fb;
|
|
|
|
|
int state = 0;
|
|
|
|
|
|
2017-01-02 13:29:48 -05:00
|
|
|
data(Handler& handler, stream<NextLayer>& ws_,
|
2016-11-03 17:53:32 -04:00
|
|
|
opcode op_, ping_data const& payload)
|
2017-01-05 09:07:18 -05:00
|
|
|
: cont(beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
is_continuation(handler))
|
|
|
|
|
, ws(ws_)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
|
|
|
|
using boost::asio::buffer;
|
|
|
|
|
using boost::asio::buffer_copy;
|
2016-11-03 17:53:32 -04:00
|
|
|
ws.template write_ping<
|
|
|
|
|
static_streambuf>(fb, op_, payload);
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-01-02 13:29:48 -05:00
|
|
|
handler_ptr<data, Handler> d_;
|
2016-05-15 16:22:25 -04:00
|
|
|
|
|
|
|
|
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)
|
2017-01-29 19:46:17 -05:00
|
|
|
: d_(std::forward<DeducedHandler>(h),
|
|
|
|
|
ws, std::forward<Args>(args)...)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
|
|
|
|
(*this)(error_code{}, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void operator()()
|
|
|
|
|
{
|
|
|
|
|
(*this)(error_code{});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void operator()(error_code ec, std::size_t);
|
|
|
|
|
|
|
|
|
|
void operator()(error_code ec, bool again = true);
|
|
|
|
|
|
|
|
|
|
friend
|
|
|
|
|
void* asio_handler_allocate(
|
|
|
|
|
std::size_t size, ping_op* op)
|
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
allocate(size, op->d_.handler());
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend
|
|
|
|
|
void asio_handler_deallocate(
|
|
|
|
|
void* p, std::size_t size, ping_op* op)
|
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
deallocate(p, size, op->d_.handler());
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend
|
|
|
|
|
bool asio_handler_is_continuation(ping_op* op)
|
|
|
|
|
{
|
|
|
|
|
return op->d_->cont;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-26 08:01:44 -04:00
|
|
|
template<class Function>
|
2016-05-15 16:22:25 -04:00
|
|
|
friend
|
|
|
|
|
void asio_handler_invoke(Function&& f, ping_op* op)
|
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
invoke(f, op->d_.handler());
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
template<class Handler>
|
2017-02-06 20:07:03 -05:00
|
|
|
void
|
2016-05-15 16:22:25 -04:00
|
|
|
stream<NextLayer>::ping_op<Handler>::
|
|
|
|
|
operator()(error_code ec, std::size_t)
|
|
|
|
|
{
|
|
|
|
|
auto& d = *d_;
|
|
|
|
|
if(ec)
|
|
|
|
|
d.ws.failed_ = true;
|
|
|
|
|
(*this)(ec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
template<class Handler>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
ping_op<Handler>::
|
|
|
|
|
operator()(error_code ec, bool again)
|
|
|
|
|
{
|
|
|
|
|
auto& d = *d_;
|
|
|
|
|
d.cont = d.cont || again;
|
|
|
|
|
if(ec)
|
|
|
|
|
goto upcall;
|
|
|
|
|
for(;;)
|
|
|
|
|
{
|
|
|
|
|
switch(d.state)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
if(d.ws.wr_block_)
|
|
|
|
|
{
|
|
|
|
|
// suspend
|
|
|
|
|
d.state = 2;
|
|
|
|
|
d.ws.wr_op_.template emplace<
|
|
|
|
|
ping_op>(std::move(*this));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(d.ws.failed_ || d.ws.wr_close_)
|
|
|
|
|
{
|
|
|
|
|
// call handler
|
|
|
|
|
d.state = 99;
|
|
|
|
|
d.ws.get_io_service().post(
|
|
|
|
|
bind_handler(std::move(*this),
|
|
|
|
|
boost::asio::error::operation_aborted));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-02-24 14:53:20 -05:00
|
|
|
d.ws.wr_block_ = &d;
|
|
|
|
|
// [[fallthrough]]
|
2016-05-15 16:22:25 -04:00
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
// send ping frame
|
2017-02-24 14:53:20 -05:00
|
|
|
BOOST_ASSERT(d.ws.wr_block_ == &d);
|
2016-05-15 16:22:25 -04:00
|
|
|
d.state = 99;
|
|
|
|
|
boost::asio::async_write(d.ws.stream_,
|
|
|
|
|
d.fb.data(), std::move(*this));
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
case 2:
|
2017-02-24 14:53:20 -05:00
|
|
|
BOOST_ASSERT(! d.ws.wr_block_);
|
|
|
|
|
d.ws.wr_block_ = &d;
|
2016-05-15 16:22:25 -04:00
|
|
|
d.state = 3;
|
2017-02-24 14:53:20 -05:00
|
|
|
// 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.
|
2016-05-15 16:22:25 -04:00
|
|
|
d.ws.get_io_service().post(
|
|
|
|
|
bind_handler(std::move(*this), ec));
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
case 3:
|
2017-02-24 14:53:20 -05:00
|
|
|
BOOST_ASSERT(d.ws.wr_block_ == &d);
|
2016-05-15 16:22:25 -04:00
|
|
|
if(d.ws.failed_ || d.ws.wr_close_)
|
|
|
|
|
{
|
|
|
|
|
// call handler
|
|
|
|
|
ec = boost::asio::error::operation_aborted;
|
|
|
|
|
goto upcall;
|
|
|
|
|
}
|
|
|
|
|
d.state = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 99:
|
|
|
|
|
goto upcall;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
upcall:
|
|
|
|
|
if(d.ws.wr_block_ == &d)
|
|
|
|
|
d.ws.wr_block_ = nullptr;
|
|
|
|
|
d.ws.rd_op_.maybe_invoke();
|
2017-01-02 13:29:48 -05:00
|
|
|
d_.invoke(ec);
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
|
2016-10-24 08:12:09 -04:00
|
|
|
template<class NextLayer>
|
2016-11-03 17:53:32 -04:00
|
|
|
template<class WriteHandler>
|
2016-10-24 08:12:09 -04:00
|
|
|
typename async_completion<
|
2016-11-03 17:53:32 -04:00
|
|
|
WriteHandler, void(error_code)>::result_type
|
2016-10-24 08:12:09 -04:00
|
|
|
stream<NextLayer>::
|
2016-11-03 17:53:32 -04:00
|
|
|
async_ping(ping_data const& payload, WriteHandler&& handler)
|
2016-10-24 08:12:09 -04:00
|
|
|
{
|
|
|
|
|
static_assert(is_AsyncStream<next_layer_type>::value,
|
|
|
|
|
"AsyncStream requirements requirements not met");
|
|
|
|
|
beast::async_completion<
|
2016-11-03 17:53:32 -04:00
|
|
|
WriteHandler, void(error_code)
|
2017-01-09 11:25:34 -05:00
|
|
|
> completion{handler};
|
2016-10-24 08:12:09 -04:00
|
|
|
ping_op<decltype(completion.handler)>{
|
2016-11-03 17:53:32 -04:00
|
|
|
completion.handler, *this,
|
|
|
|
|
opcode::ping, payload};
|
|
|
|
|
return completion.result.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
template<class WriteHandler>
|
|
|
|
|
typename async_completion<
|
|
|
|
|
WriteHandler, void(error_code)>::result_type
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
async_pong(ping_data const& payload, WriteHandler&& handler)
|
|
|
|
|
{
|
|
|
|
|
static_assert(is_AsyncStream<next_layer_type>::value,
|
|
|
|
|
"AsyncStream requirements requirements not met");
|
|
|
|
|
beast::async_completion<
|
|
|
|
|
WriteHandler, void(error_code)
|
2017-01-09 11:25:34 -05:00
|
|
|
> completion{handler};
|
2016-11-03 17:53:32 -04:00
|
|
|
ping_op<decltype(completion.handler)>{
|
|
|
|
|
completion.handler, *this,
|
|
|
|
|
opcode::pong, payload};
|
2016-10-24 08:12:09 -04:00
|
|
|
return completion.result.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
ping(ping_data const& payload)
|
|
|
|
|
{
|
|
|
|
|
error_code ec;
|
|
|
|
|
ping(payload, ec);
|
|
|
|
|
if(ec)
|
|
|
|
|
throw system_error{ec};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
ping(ping_data const& payload, error_code& ec)
|
|
|
|
|
{
|
|
|
|
|
detail::frame_streambuf db;
|
|
|
|
|
write_ping<static_streambuf>(
|
|
|
|
|
db, opcode::ping, payload);
|
|
|
|
|
boost::asio::write(stream_, db.data(), ec);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-03 17:53:32 -04:00
|
|
|
template<class NextLayer>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
pong(ping_data const& payload)
|
|
|
|
|
{
|
|
|
|
|
error_code ec;
|
|
|
|
|
pong(payload, ec);
|
|
|
|
|
if(ec)
|
|
|
|
|
throw system_error{ec};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
pong(ping_data const& payload, error_code& ec)
|
|
|
|
|
{
|
|
|
|
|
detail::frame_streambuf db;
|
|
|
|
|
write_ping<static_streambuf>(
|
|
|
|
|
db, opcode::pong, payload);
|
|
|
|
|
boost::asio::write(stream_, db.data(), ec);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-24 08:12:09 -04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
2016-05-15 16:22:25 -04:00
|
|
|
} // websocket
|
|
|
|
|
} // beast
|
|
|
|
|
|
|
|
|
|
#endif
|