Refactor Server (RIPD-1120):

* Make Handler a template argument
This commit is contained in:
Vinnie Falco
2016-05-20 08:44:13 -04:00
parent 80a9a2bf5d
commit 289c8c9f09
53 changed files with 997 additions and 1176 deletions

View File

@@ -27,21 +27,25 @@
namespace ripple {
template<class Handler>
class SSLHTTPPeer
: public BaseHTTPPeer<SSLHTTPPeer>
, public std::enable_shared_from_this <SSLHTTPPeer>
: public BaseHTTPPeer<Handler, SSLHTTPPeer<Handler>>
, public std::enable_shared_from_this<SSLHTTPPeer<Handler>>
{
private:
friend class BaseHTTPPeer<SSLHTTPPeer>;
friend class BaseHTTPPeer<Handler, SSLHTTPPeer>;
using socket_type = boost::asio::ip::tcp::socket;
using stream_type = boost::asio::ssl::stream <socket_type&>;
using endpoint_type = boost::asio::ip::tcp::endpoint;
using yield_context = boost::asio::yield_context;
using error_code = boost::system::error_code;
std::unique_ptr<beast::asio::ssl_bundle> ssl_bundle_;
stream_type& stream_;
public:
template <class ConstBufferSequence>
SSLHTTPPeer (Port const& port, Handler& handler,
template<class ConstBufferSequence>
SSLHTTPPeer(Port const& port, Handler& handler,
beast::Journal journal, endpoint_type remote_address,
ConstBufferSequence const& buffers, socket_type&& socket);
@@ -53,7 +57,7 @@ public:
private:
void
do_handshake (yield_context yield);
do_handshake(yield_context do_yield);
void
do_request() override;
@@ -62,16 +66,19 @@ private:
do_close() override;
void
on_shutdown (error_code ec);
on_shutdown(error_code ec);
};
//------------------------------------------------------------------------------
template <class ConstBufferSequence>
SSLHTTPPeer::SSLHTTPPeer (Port const& port, Handler& handler,
template<class Handler>
template<class ConstBufferSequence>
SSLHTTPPeer<Handler>::
SSLHTTPPeer(Port const& port, Handler& handler,
beast::Journal journal, endpoint_type remote_address,
ConstBufferSequence const& buffers, socket_type&& socket)
: BaseHTTPPeer (port, handler, socket.get_io_service(), journal, remote_address, buffers)
: BaseHTTPPeer<Handler, SSLHTTPPeer>(port, handler,
socket.get_io_service(), journal, remote_address, buffers)
, ssl_bundle_(std::make_unique<beast::asio::ssl_bundle>(
port.context, std::move(socket)))
, stream_(ssl_bundle_->stream)
@@ -79,86 +86,99 @@ SSLHTTPPeer::SSLHTTPPeer (Port const& port, Handler& handler,
}
// Called when the acceptor accepts our socket.
template<class Handler>
void
SSLHTTPPeer::run()
SSLHTTPPeer<Handler>::
run()
{
if (!handler_.onAccept (session(), remote_address_))
if(! this->handler_.onAccept(this->session(), this->remote_address_))
{
boost::asio::spawn (strand_,
std::bind (&SSLHTTPPeer::do_close,
shared_from_this()));
boost::asio::spawn(this->strand_,
std::bind(&SSLHTTPPeer::do_close,
this->shared_from_this()));
return;
}
if (! stream_.lowest_layer().is_open())
return;
boost::asio::spawn (strand_, std::bind (&SSLHTTPPeer::do_handshake,
shared_from_this(), std::placeholders::_1));
boost::asio::spawn(this->strand_, std::bind(
&SSLHTTPPeer::do_handshake, this->shared_from_this(),
std::placeholders::_1));
}
template<class Handler>
std::shared_ptr<WSSession>
SSLHTTPPeer::websocketUpgrade()
SSLHTTPPeer<Handler>::
websocketUpgrade()
{
auto ws = ios().emplace<SSLWSPeer>(
port_, handler_, remote_address_,
std::move(message_), std::move(ssl_bundle_),
journal_);
auto ws = this->ios().template emplace<SSLWSPeer<Handler>>(
this->port_, this->handler_, this->remote_address_,
std::move(this->message_), std::move(this->ssl_bundle_),
this->journal_);
return ws;
}
template<class Handler>
void
SSLHTTPPeer::do_handshake (yield_context yield)
SSLHTTPPeer<Handler>::
do_handshake(yield_context do_yield)
{
error_code ec;
stream_.set_verify_mode (boost::asio::ssl::verify_none);
start_timer();
read_buf_.consume(stream_.async_handshake(
stream_type::server, read_buf_.data(), yield[ec]));
cancel_timer();
boost::system::error_code ec;
stream_.set_verify_mode(boost::asio::ssl::verify_none);
this->start_timer();
this->read_buf_.consume(stream_.async_handshake(
stream_type::server, this->read_buf_.data(), do_yield[ec]));
this->cancel_timer();
if (ec)
return fail(ec, "handshake");
return this->fail(ec, "handshake");
bool const http =
port().protocol.count("peer") > 0 ||
this->port().protocol.count("peer") > 0 ||
//port().protocol.count("wss") > 0 ||
port().protocol.count("wss2") > 0 ||
port().protocol.count("https") > 0;
if (http)
this->port().protocol.count("wss2") > 0 ||
this->port().protocol.count("https") > 0;
if(http)
{
boost::asio::spawn (strand_, std::bind (&SSLHTTPPeer::do_read,
shared_from_this(), std::placeholders::_1));
boost::asio::spawn(this->strand_,
std::bind(&SSLHTTPPeer::do_read,
this->shared_from_this(), std::placeholders::_1));
return;
}
// this will be destroyed
// `this` will be destroyed
}
template<class Handler>
void
SSLHTTPPeer::do_request()
SSLHTTPPeer<Handler>::
do_request()
{
++request_count_;
auto const what = handler_.onHandoff (session(),
std::move(ssl_bundle_), std::move(message_), remote_address_);
if (what.moved)
++this->request_count_;
auto const what = this->handler_.onHandoff(this->session(),
std::move(ssl_bundle_), std::move(this->message_),
this->remote_address_);
if(what.moved)
return;
if (what.response)
return write(what.response, what.keep_alive);
if(what.response)
return this->write(what.response, what.keep_alive);
// legacy
handler_.onRequest (session());
this->handler_.onRequest(this->session());
}
template<class Handler>
void
SSLHTTPPeer::do_close()
SSLHTTPPeer<Handler>::
do_close()
{
start_timer();
stream_.async_shutdown (strand_.wrap (std::bind (
&SSLHTTPPeer::on_shutdown, shared_from_this(),
this->start_timer();
stream_.async_shutdown(this->strand_.wrap(std::bind (
&SSLHTTPPeer::on_shutdown, this->shared_from_this(),
std::placeholders::_1)));
}
template<class Handler>
void
SSLHTTPPeer::on_shutdown (error_code ec)
SSLHTTPPeer<Handler>::
on_shutdown(error_code ec)
{
cancel_timer();
this->cancel_timer();
stream_.lowest_layer().close(ec);
}