Files
boost_beast/include/beast/websocket/impl/read_op.ipp
T

143 lines
3.3 KiB
C++
Raw Normal View History

2016-04-19 20:29:16 -04:00
//
// Copyright (c) 2013-2016 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)
//
2016-02-25 16:17:19 -05:00
2016-04-19 20:29:16 -04:00
#ifndef BEAST_WEBSOCKET_IMPL_READ_OP_HPP
#define BEAST_WEBSOCKET_IMPL_READ_OP_HPP
2016-02-25 16:17:19 -05:00
2016-05-07 14:57:15 -04:00
#include <beast/core/handler_alloc.hpp>
2016-02-25 16:17:19 -05:00
#include <memory>
namespace beast {
2016-04-19 20:29:16 -04:00
namespace websocket {
2016-02-25 16:17:19 -05:00
// read an entire message
//
2016-04-19 20:29:16 -04:00
template<class NextLayer>
template<class DynamicBuffer, class Handler>
2016-04-19 20:29:16 -04:00
class stream<NextLayer>::read_op
2016-02-25 16:17:19 -05:00
{
using alloc_type =
handler_alloc<char, Handler>;
struct data
{
2016-04-19 20:29:16 -04:00
stream<NextLayer>& ws;
2016-02-25 16:17:19 -05:00
opcode& op;
DynamicBuffer& db;
2016-02-25 16:17:19 -05:00
Handler h;
frame_info fi;
bool cont;
int state = 0;
template<class DeducedHandler>
data(DeducedHandler&& h_,
2016-04-19 20:29:16 -04:00
stream<NextLayer>& ws_, opcode& op_,
DynamicBuffer& sb_)
2016-02-25 16:17:19 -05:00
: ws(ws_)
, op(op_)
, db(sb_)
2016-02-25 16:17:19 -05:00
, h(std::forward<DeducedHandler>(h_))
, cont(boost_asio_handler_cont_helpers::
is_continuation(h))
{
}
};
std::shared_ptr<data> d_;
public:
read_op(read_op&&) = default;
read_op(read_op const&) = default;
template<class DeducedHandler, class... Args>
read_op(DeducedHandler&& h,
2016-04-19 20:29:16 -04:00
stream<NextLayer>& ws, Args&&... args)
2016-02-25 16:17:19 -05:00
: d_(std::allocate_shared<data>(alloc_type{h},
std::forward<DeducedHandler>(h), ws,
std::forward<Args>(args)...))
{
(*this)(error_code{}, false);
}
void operator()(
error_code const& ec, bool again = true);
friend
2016-04-22 07:34:44 -04:00
void* asio_handler_allocate(
2016-02-25 16:17:19 -05:00
std::size_t size, read_op* op)
{
return boost_asio_handler_alloc_helpers::
allocate(size, op->d_->h);
}
friend
2016-04-22 07:34:44 -04:00
void asio_handler_deallocate(
2016-02-25 16:17:19 -05:00
void* p, std::size_t size, read_op* op)
{
return boost_asio_handler_alloc_helpers::
deallocate(p, size, op->d_->h);
}
friend
2016-04-22 07:34:44 -04:00
bool asio_handler_is_continuation(read_op* op)
2016-02-25 16:17:19 -05:00
{
return op->d_->cont;
}
2016-08-26 08:01:44 -04:00
template<class Function>
2016-02-25 16:17:19 -05:00
friend
2016-04-22 07:34:44 -04:00
void asio_handler_invoke(Function&& f, read_op* op)
2016-02-25 16:17:19 -05:00
{
return boost_asio_handler_invoke_helpers::
invoke(f, op->d_->h);
}
};
2016-04-19 20:29:16 -04:00
template<class NextLayer>
template<class DynamicBuffer, class Handler>
2016-02-25 16:17:19 -05:00
void
stream<NextLayer>::read_op<DynamicBuffer, Handler>::
2016-02-25 16:17:19 -05:00
operator()(error_code const& ec, bool again)
{
auto& d = *d_;
d.cont = d.cont || again;
2016-05-15 16:22:25 -04:00
while(! ec)
2016-02-25 16:17:19 -05:00
{
switch(d.state)
{
case 0:
// read payload
d.state = 1;
2016-05-15 16:22:25 -04:00
#if 0
// VFALCO This causes dereference of null, because
// the handler is moved from the data block
// before asio_handler_deallocate is called.
2016-02-25 16:17:19 -05:00
d.ws.async_read_frame(
d.fi, d.db, std::move(*this));
2016-05-15 16:22:25 -04:00
#else
d.ws.async_read_frame(d.fi, d.db, *this);
2016-05-15 16:22:25 -04:00
#endif
2016-02-25 16:17:19 -05:00
return;
// got payload
case 1:
d.op = d.fi.op;
2016-05-15 16:22:25 -04:00
if(d.fi.fin)
goto upcall;
d.state = 0;
2016-02-25 16:17:19 -05:00
break;
}
}
2016-05-15 16:22:25 -04:00
upcall:
2016-02-25 16:17:19 -05:00
d.h(ec);
}
2016-04-19 20:29:16 -04:00
} // websocket
2016-02-25 16:17:19 -05:00
} // beast
#endif