Skip to content

Commit

Permalink
[FOLD] change interface for handshake response_type
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Apr 27, 2017
1 parent d24227c commit a48120c
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 58 deletions.
160 changes: 120 additions & 40 deletions include/beast/websocket/impl/handshake.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ class stream<NextLayer>::handshake_op
{
bool cont;
stream<NextLayer>& ws;
response_type* res_p;
std::string key;
request_type req;
response_type res;
int state = 0;

template<class Decorator>
data(Handler& handler, stream<NextLayer>& ws_,
boost::string_ref const& host,
boost::string_ref const& resource,
Decorator const& decorator)
response_type* res_p_,
boost::string_ref const& host,
boost::string_ref const& resource,
Decorator const& decorator)
: cont(beast_asio_helpers::
is_continuation(handler))
, ws(ws_)
, res_p(res_p_)
, req(ws.build_request(key,
host, resource, decorator))
{
Expand Down Expand Up @@ -151,15 +154,15 @@ operator()(error_code ec, bool again)
}
}
}
// args to invoke must be on the stack
auto const res = std::move(d.res);
d_.invoke(ec, res);
if(d.res_p)
swap(d.res, *d.res_p);
d_.invoke(ec);
}

template<class NextLayer>
template<class HandshakeHandler>
typename async_completion<HandshakeHandler,
void(error_code, response_type)>::result_type
void(error_code)>::result_type
stream<NextLayer>::
async_handshake(boost::string_ref const& host,
boost::string_ref const& resource,
Expand All @@ -168,17 +171,37 @@ async_handshake(boost::string_ref const& host,
static_assert(is_AsyncStream<next_layer_type>::value,
"AsyncStream requirements not met");
beast::async_completion<HandshakeHandler,
void(error_code, response_type)> completion{handler};
void(error_code)> completion{handler};
handshake_op<decltype(completion.handler)>{
completion.handler, *this, host, resource,
&default_decorate_req};
completion.handler, *this, nullptr,
host, resource, &default_decorate_req};
return completion.result.get();
}

template<class NextLayer>
template<class HandshakeHandler>
typename async_completion<HandshakeHandler,
void(error_code)>::result_type
stream<NextLayer>::
async_handshake(response_type& res,
boost::string_ref const& host,
boost::string_ref const& resource,
HandshakeHandler&& handler)
{
static_assert(is_AsyncStream<next_layer_type>::value,
"AsyncStream requirements not met");
beast::async_completion<HandshakeHandler,
void(error_code)> completion{handler};
handshake_op<decltype(completion.handler)>{
completion.handler, *this, &res,
host, resource, &default_decorate_req};
return completion.result.get();
}

template<class NextLayer>
template<class RequestDecorator, class HandshakeHandler>
typename async_completion<HandshakeHandler,
void(error_code, response_type)>::result_type
void(error_code)>::result_type
stream<NextLayer>::
async_handshake_ex(boost::string_ref const& host,
boost::string_ref const& resource,
Expand All @@ -191,32 +214,71 @@ async_handshake_ex(boost::string_ref const& host,
RequestDecorator>::value,
"RequestDecorator requirements not met");
beast::async_completion<HandshakeHandler,
void(error_code, response_type)> completion{handler};
void(error_code)> completion{handler};
handshake_op<decltype(completion.handler)>{
completion.handler, *this, nullptr,
host, resource, decorator};
return completion.result.get();
}

template<class NextLayer>
template<class RequestDecorator, class HandshakeHandler>
typename async_completion<HandshakeHandler,
void(error_code)>::result_type
stream<NextLayer>::
async_handshake_ex(response_type& res,
boost::string_ref const& host,
boost::string_ref const& resource,
RequestDecorator const& decorator,
HandshakeHandler&& handler)
{
static_assert(is_AsyncStream<next_layer_type>::value,
"AsyncStream requirements not met");
static_assert(detail::is_RequestDecorator<
RequestDecorator>::value,
"RequestDecorator requirements not met");
beast::async_completion<HandshakeHandler,
void(error_code)> completion{handler};
handshake_op<decltype(completion.handler)>{
completion.handler, *this, host, resource,
decorator};
completion.handler, *this, &res,
host, resource, decorator};
return completion.result.get();
}

template<class NextLayer>
response_type
void
stream<NextLayer>::
handshake(boost::string_ref const& host,
boost::string_ref const& resource)
{
static_assert(is_SyncStream<next_layer_type>::value,
"SyncStream requirements not met");
error_code ec;
auto res = handshake_ex(
do_handshake(nullptr,
host, resource, &default_decorate_req, ec);
if(ec)
throw system_error{ec};
}

template<class NextLayer>
void
stream<NextLayer>::
handshake(response_type& res,
boost::string_ref const& host,
boost::string_ref const& resource)
{
static_assert(is_SyncStream<next_layer_type>::value,
"SyncStream requirements not met");
error_code ec;
do_handshake(&res,
host, resource, &default_decorate_req, ec);
if(ec)
throw system_error{ec};
return res;
}

template<class NextLayer>
template<class RequestDecorator>
response_type
void
stream<NextLayer>::
handshake_ex(boost::string_ref const& host,
boost::string_ref const& resource,
Expand All @@ -228,28 +290,60 @@ handshake_ex(boost::string_ref const& host,
RequestDecorator>::value,
"RequestDecorator requirements not met");
error_code ec;
auto res = handshake_ex(
host, resource, decorator, ec);
do_handshake(nullptr, host, resource, decorator, ec);
if(ec)
throw system_error{ec};
return res;
}

template<class NextLayer>
response_type
template<class RequestDecorator>
void
stream<NextLayer>::
handshake_ex(response_type& res,
boost::string_ref const& host,
boost::string_ref const& resource,
RequestDecorator const& decorator)
{
static_assert(is_SyncStream<next_layer_type>::value,
"SyncStream requirements not met");
static_assert(detail::is_RequestDecorator<
RequestDecorator>::value,
"RequestDecorator requirements not met");
error_code ec;
do_handshake(&res, host, resource, decorator, ec);
if(ec)
throw system_error{ec};
}

template<class NextLayer>
void
stream<NextLayer>::
handshake(boost::string_ref const& host,
boost::string_ref const& resource, error_code& ec)
{
static_assert(is_SyncStream<next_layer_type>::value,
"SyncStream requirements not met");
return handshake_ex(
do_handshake(nullptr,
host, resource, &default_decorate_req, ec);
}

template<class NextLayer>
void
stream<NextLayer>::
handshake(response_type& res,
boost::string_ref const& host,
boost::string_ref const& resource,
error_code& ec)
{
static_assert(is_SyncStream<next_layer_type>::value,
"SyncStream requirements not met");
do_handshake(res,
host, resource, &default_decorate_req, ec);
}

template<class NextLayer>
template<class RequestDecorator>
response_type
void
stream<NextLayer>::
handshake_ex(boost::string_ref const& host,
boost::string_ref const& resource,
Expand All @@ -261,22 +355,8 @@ handshake_ex(boost::string_ref const& host,
static_assert(detail::is_RequestDecorator<
RequestDecorator>::value,
"RequestDecorator requirements not met");
response_type res;
reset();
std::string key;
{
auto const req = build_request(
key, host, resource, decorator);
pmd_read(pmd_config_, req.fields);
http::write(stream_, req, ec);
}
if(ec)
return res;
http::read(next_layer(), stream_.buffer(), res, ec);
if(ec)
return res;
do_response(res, key, ec);
return res;
do_handshake(nullptr,
host, resource, decorator, ec);
}

//------------------------------------------------------------------------------
Expand Down
29 changes: 29 additions & 0 deletions include/beast/websocket/impl/stream.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,35 @@ do_accept(http::header<true, Fields> const& req,
open(detail::role_type::server);
}

template<class NextLayer>
template<class RequestDecorator>
void
stream<NextLayer>::
do_handshake(response_type* res_p,
boost::string_ref const& host,
boost::string_ref const& resource,
RequestDecorator const& decorator,
error_code& ec)
{
response_type res;
reset();
std::string key;
{
auto const req = build_request(
key, host, resource, decorator);
pmd_read(pmd_config_, req.fields);
http::write(stream_, req, ec);
}
if(ec)
return;
http::read(next_layer(), stream_.buffer(), res, ec);
if(ec)
return;
do_response(res, key, ec);
if(res_p)
swap(res, *res_p);
}

template<class NextLayer>
template<class Decorator>
request_type
Expand Down
Loading

0 comments on commit a48120c

Please sign in to comment.