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-05-14 09:25:43 -07:00
|
|
|
#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>
|
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 {
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
stream<NextLayer>& ws;
|
|
|
|
|
detail::frame_streambuf fb;
|
|
|
|
|
int state = 0;
|
2017-07-15 17:05:24 -07:00
|
|
|
token tok;
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2017-06-29 09:48:30 -07:00
|
|
|
data(Handler&, stream<NextLayer>& ws_,
|
2017-06-08 21:01:35 -07:00
|
|
|
detail::opcode op_, ping_data const& payload)
|
2017-05-14 09:25:43 -07:00
|
|
|
: ws(ws_)
|
2017-07-15 17:05:24 -07:00
|
|
|
, tok(ws.t_.unique())
|
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<
|
2017-07-14 20:25:39 -07:00
|
|
|
flat_static_buffer_base>(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
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void operator()()
|
|
|
|
|
{
|
2017-06-29 09:48:30 -07:00
|
|
|
(*this)({});
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-29 09:48:30 -07:00
|
|
|
void operator()(error_code ec,
|
|
|
|
|
std::size_t bytes_transferred = 0);
|
2016-05-15 16:22:25 -04:00
|
|
|
|
|
|
|
|
friend
|
|
|
|
|
void* asio_handler_allocate(
|
|
|
|
|
std::size_t size, ping_op* op)
|
|
|
|
|
{
|
2017-05-14 09:25:43 -07:00
|
|
|
using boost::asio::asio_handler_allocate;
|
|
|
|
|
return asio_handler_allocate(
|
|
|
|
|
size, std::addressof(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-05-14 09:25:43 -07:00
|
|
|
using boost::asio::asio_handler_deallocate;
|
|
|
|
|
asio_handler_deallocate(
|
|
|
|
|
p, size, std::addressof(op->d_.handler()));
|
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
|
|
|
}
|
|
|
|
|
|
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-05-14 09:25:43 -07:00
|
|
|
using boost::asio::asio_handler_invoke;
|
|
|
|
|
asio_handler_invoke(
|
|
|
|
|
f, std::addressof(op->d_.handler()));
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
|
|
|
|
auto& d = *d_;
|
|
|
|
|
if(ec)
|
2017-06-29 09:48:30 -07:00
|
|
|
{
|
2017-07-15 17:05:24 -07:00
|
|
|
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
2017-06-29 09:48:30 -07:00
|
|
|
d.ws.failed_ = true;
|
2016-05-15 16:22:25 -04:00
|
|
|
goto upcall;
|
2017-06-29 09:48:30 -07:00
|
|
|
}
|
|
|
|
|
switch(d.state)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2017-06-29 09:48:30 -07:00
|
|
|
case 0:
|
|
|
|
|
if(d.ws.wr_block_)
|
2016-05-15 16:22:25 -04:00
|
|
|
{
|
2017-06-29 09:48:30 -07:00
|
|
|
// suspend
|
|
|
|
|
d.state = 1;
|
|
|
|
|
d.ws.ping_op_.emplace(std::move(*this));
|
2016-05-15 16:22:25 -04:00
|
|
|
return;
|
2017-06-29 09:48:30 -07:00
|
|
|
}
|
2017-07-15 17:05:24 -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
|
2017-06-29 11:36:14 -07:00
|
|
|
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));
|
|
|
|
|
}
|
2016-05-15 16:22:25 -04:00
|
|
|
|
2017-06-29 09:48:30 -07:00
|
|
|
do_write:
|
|
|
|
|
// send ping frame
|
2017-07-15 17:05:24 -07:00
|
|
|
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_);
|
2017-07-15 17:05:24 -07:00
|
|
|
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:
|
2017-07-15 17:05:24 -07:00
|
|
|
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;
|
2016-05-15 16:22:25 -04:00
|
|
|
goto upcall;
|
|
|
|
|
}
|
2017-06-29 09:48:30 -07:00
|
|
|
goto do_write;
|
|
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
|
break;
|
2016-05-15 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
upcall:
|
2017-07-15 17:05:24 -07:00
|
|
|
BOOST_ASSERT(d.ws.wr_block_ == d.tok);
|
|
|
|
|
d.ws.wr_block_.reset();
|
2017-06-29 11:36:14 -07:00
|
|
|
d.ws.close_op_.maybe_invoke() ||
|
|
|
|
|
d.ws.rd_op_.maybe_invoke() ||
|
2017-02-24 10:03:33 -05:00
|
|
|
d.ws.wr_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>
|
2017-05-12 17:13:03 -07:00
|
|
|
async_return_type<
|
|
|
|
|
WriteHandler, void(error_code)>
|
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
|
|
|
{
|
2017-05-10 12:03:00 -07:00
|
|
|
static_assert(is_async_stream<next_layer_type>::value,
|
2016-10-24 08:12:09 -04:00
|
|
|
"AsyncStream requirements requirements not met");
|
2017-05-06 12:36:40 -07:00
|
|
|
async_completion<WriteHandler,
|
|
|
|
|
void(error_code)> init{handler};
|
2017-05-12 17:13:03 -07:00
|
|
|
ping_op<handler_type<
|
|
|
|
|
WriteHandler, void(error_code)>>{
|
2017-06-08 21:01:35 -07:00
|
|
|
init.completion_handler, *this,
|
2017-06-29 09:48:30 -07:00
|
|
|
detail::opcode::ping, payload}({});
|
2017-05-06 12:36:40 -07:00
|
|
|
return init.result.get();
|
2016-11-03 17:53:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
template<class WriteHandler>
|
2017-05-12 17:13:03 -07:00
|
|
|
async_return_type<
|
|
|
|
|
WriteHandler, void(error_code)>
|
2016-11-03 17:53:32 -04:00
|
|
|
stream<NextLayer>::
|
|
|
|
|
async_pong(ping_data const& payload, WriteHandler&& handler)
|
|
|
|
|
{
|
2017-05-10 12:03:00 -07:00
|
|
|
static_assert(is_async_stream<next_layer_type>::value,
|
2016-11-03 17:53:32 -04:00
|
|
|
"AsyncStream requirements requirements not met");
|
2017-05-06 12:36:40 -07:00
|
|
|
async_completion<WriteHandler,
|
|
|
|
|
void(error_code)> init{handler};
|
2017-05-12 17:13:03 -07:00
|
|
|
ping_op<handler_type<
|
|
|
|
|
WriteHandler, void(error_code)>>{
|
2017-06-08 21:01:35 -07:00
|
|
|
init.completion_handler, *this,
|
2017-06-29 09:48:30 -07:00
|
|
|
detail::opcode::pong, payload}({});
|
2017-05-06 12:36:40 -07:00
|
|
|
return init.result.get();
|
2016-10-24 08:12:09 -04: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});
|
2016-10-24 08:12:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
ping(ping_data const& payload, error_code& ec)
|
|
|
|
|
{
|
|
|
|
|
detail::frame_streambuf db;
|
2017-07-14 20:25:39 -07:00
|
|
|
write_ping<flat_static_buffer_base>(
|
2017-06-08 21:01:35 -07:00
|
|
|
db, detail::opcode::ping, payload);
|
2016-10-24 08:12:09 -04:00
|
|
|
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)
|
2017-05-22 15:30:12 -07:00
|
|
|
BOOST_THROW_EXCEPTION(system_error{ec});
|
2016-11-03 17:53:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class NextLayer>
|
|
|
|
|
void
|
|
|
|
|
stream<NextLayer>::
|
|
|
|
|
pong(ping_data const& payload, error_code& ec)
|
|
|
|
|
{
|
|
|
|
|
detail::frame_streambuf db;
|
2017-07-14 20:25:39 -07:00
|
|
|
write_ping<flat_static_buffer_base>(
|
2017-06-08 21:01:35 -07:00
|
|
|
db, detail::opcode::pong, payload);
|
2016-11-03 17:53:32 -04:00
|
|
|
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
|
2017-07-20 13:40:34 -07:00
|
|
|
} // boost
|
2016-05-15 16:22:25 -04:00
|
|
|
|
|
|
|
|
#endif
|