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

155 lines
3.8 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_ACCEPT_OP_HPP
#define BEAST_WEBSOCKET_IMPL_ACCEPT_OP_HPP
2016-02-25 16:17:19 -05:00
2016-04-19 20:29:16 -04:00
#include <beast/websocket/impl/response_op.ipp>
#include <beast/http/message_v1.hpp>
#include <beast/http/parser_v1.hpp>
#include <beast/http/read.hpp>
2016-05-07 14:57:15 -04:00
#include <beast/core/handler_alloc.hpp>
#include <beast/core/prepare_buffers.hpp>
2016-09-25 12:17:32 -04:00
#include <boost/assert.hpp>
2016-02-25 16:17:19 -05:00
#include <memory>
#include <type_traits>
namespace beast {
2016-04-19 20:29:16 -04:00
namespace websocket {
2016-02-25 16:17:19 -05:00
// read and respond to an upgrade request
//
2016-04-19 20:29:16 -04:00
template<class NextLayer>
2016-02-25 16:17:19 -05:00
template<class Handler>
2016-04-19 20:29:16 -04:00
class stream<NextLayer>::accept_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-05-18 12:30:15 -04:00
http::request_v1<http::string_body> req;
2016-02-25 16:17:19 -05:00
Handler h;
bool cont;
int state = 0;
template<class DeducedHandler, class Buffers>
2016-04-19 20:29:16 -04:00
data(DeducedHandler&& h_, stream<NextLayer>& ws_,
2016-02-25 16:17:19 -05:00
Buffers const& buffers)
: ws(ws_)
, h(std::forward<DeducedHandler>(h_))
, cont(boost_asio_handler_cont_helpers::
is_continuation(h))
{
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
2016-05-15 16:22:25 -04:00
ws.reset();
2016-02-25 16:17:19 -05:00
ws.stream_.buffer().commit(buffer_copy(
ws.stream_.buffer().prepare(
buffer_size(buffers)), buffers));
}
};
std::shared_ptr<data> d_;
public:
accept_op(accept_op&&) = default;
accept_op(accept_op const&) = default;
template<class DeducedHandler, class... Args>
accept_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{}, 0, false);
}
void operator()(error_code const& ec)
{
(*this)(ec, 0);
}
void operator()(error_code const& ec,
std::size_t bytes_transferred, 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, accept_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, accept_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(accept_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, accept_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>
2016-02-25 16:17:19 -05:00
template<class Handler>
void
2016-04-19 20:29:16 -04:00
stream<NextLayer>::accept_op<Handler>::
2016-02-25 16:17:19 -05:00
operator()(error_code const& ec,
std::size_t bytes_transferred, bool again)
{
auto& d = *d_;
d.cont = d.cont || again;
while(! ec && d.state != 99)
{
switch(d.state)
{
case 0:
// read message
d.state = 1;
2016-04-30 13:00:33 -04:00
http::async_read(d.ws.next_layer(),
2016-02-25 16:17:19 -05:00
d.ws.stream_.buffer(), d.req,
std::move(*this));
return;
// got message
case 1:
// respond to request
2016-05-15 16:22:25 -04:00
#if 1
// VFALCO I have no idea why passing std::move(*this) crashes
d.state = 99;
d.ws.async_accept(d.req, *this);
#else
2016-02-25 16:17:19 -05:00
response_op<Handler>{
std::move(d.h), d.ws, d.req, true};
2016-05-15 16:22:25 -04:00
#endif
2016-02-25 16:17:19 -05:00
return;
}
}
d.h(ec);
}
2016-04-19 20:29:16 -04:00
} // websocket
2016-02-25 16:17:19 -05:00
} // beast
#endif