Skip to content

Commit

Permalink
[FOLD] ~response_op.ip
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Oct 24, 2016
1 parent 1793454 commit 4aef484
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 141 deletions.
121 changes: 120 additions & 1 deletion include/beast/websocket/impl/accept.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
#ifndef BEAST_WEBSOCKET_IMPL_ACCEPT_IPP
#define BEAST_WEBSOCKET_IMPL_ACCEPT_IPP

#include <beast/websocket/impl/response_op.ipp>
#include <beast/http/message.hpp>
#include <beast/http/parser_v1.hpp>
#include <beast/http/read.hpp>
#include <beast/http/string_body.hpp>
#include <beast/http/write.hpp>
#include <beast/core/handler_alloc.hpp>
#include <beast/core/prepare_buffers.hpp>
#include <beast/core/detail/type_traits.hpp>
Expand All @@ -24,6 +25,124 @@ namespace websocket {

//------------------------------------------------------------------------------

// Respond to an upgrade HTTP request
template<class NextLayer>
template<class Handler>
class stream<NextLayer>::response_op
{
using alloc_type =
handler_alloc<char, Handler>;

struct data
{
stream<NextLayer>& ws;
http::response<http::string_body> resp;
Handler h;
error_code final_ec;
bool cont;
int state = 0;

template<class DeducedHandler,
class Body, class Headers>
data(DeducedHandler&& h_, stream<NextLayer>& ws_,
http::request<Body, Headers> const& req,
bool cont_)
: ws(ws_)
, resp(ws_.build_response(req))
, h(std::forward<DeducedHandler>(h_))
, cont(cont_)
{
// can't call stream::reset() here
// otherwise accept_op will malfunction
//
if(resp.status != 101)
final_ec = error::handshake_failed;
}
};

std::shared_ptr<data> d_;

public:
response_op(response_op&&) = default;
response_op(response_op const&) = default;

template<class DeducedHandler, class... Args>
response_op(DeducedHandler&& h,
stream<NextLayer>& ws, Args&&... args)
: 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 ec, bool again = true);

friend
void* asio_handler_allocate(
std::size_t size, response_op* op)
{
return boost_asio_handler_alloc_helpers::
allocate(size, op->d_->h);
}

friend
void asio_handler_deallocate(
void* p, std::size_t size, response_op* op)
{
return boost_asio_handler_alloc_helpers::
deallocate(p, size, op->d_->h);
}

friend
bool asio_handler_is_continuation(response_op* op)
{
return op->d_->cont;
}

template<class Function>
friend
void asio_handler_invoke(Function&& f, response_op* op)
{
return boost_asio_handler_invoke_helpers::
invoke(f, op->d_->h);
}
};

template<class NextLayer>
template<class Handler>
void
stream<NextLayer>::response_op<Handler>::
operator()(error_code ec, bool again)
{
auto& d = *d_;
d.cont = d.cont || again;
while(! ec && d.state != 99)
{
switch(d.state)
{
case 0:
// send response
d.state = 1;
http::async_write(d.ws.next_layer(),
d.resp, std::move(*this));
return;

// sent response
case 1:
d.state = 99;
ec = d.final_ec;
if(! ec)
d.ws.open(detail::role_type::server);
break;
}
}
d.h(ec);
}

//------------------------------------------------------------------------------

// read and respond to an upgrade request
//
template<class NextLayer>
Expand Down
139 changes: 0 additions & 139 deletions include/beast/websocket/impl/response_op.ipp

This file was deleted.

1 change: 0 additions & 1 deletion include/beast/websocket/impl/stream.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <beast/websocket/teardown.hpp>
#include <beast/websocket/detail/hybi13.hpp>
#include <beast/websocket/impl/response_op.ipp>
#include <beast/http/read.hpp>
#include <beast/http/write.hpp>
#include <beast/http/reason.hpp>
Expand Down

0 comments on commit 4aef484

Please sign in to comment.