From 4f705c18ed66941ba8776d3e9ffa5de8d9ef2b81 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Wed, 19 Jun 2019 02:13:53 +0300 Subject: [PATCH] pass parameter map to ConnectionHandler::handleConnection method --- .../network/server/ConnectionHandler.hpp | 17 +++++++++--- src/oatpp/network/server/Server.cpp | 6 +++-- .../web/protocol/http/outgoing/Response.cpp | 8 ++++++ .../web/protocol/http/outgoing/Response.hpp | 27 ++++++++++++++++--- .../web/server/AsyncHttpConnectionHandler.cpp | 4 ++- .../web/server/AsyncHttpConnectionHandler.hpp | 2 +- .../web/server/HttpConnectionHandler.cpp | 6 +++-- .../web/server/HttpConnectionHandler.hpp | 2 +- src/oatpp/web/server/HttpProcessor.cpp | 2 +- 9 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/oatpp/network/server/ConnectionHandler.hpp b/src/oatpp/network/server/ConnectionHandler.hpp index 5d77f8a7..a17cd5d5 100644 --- a/src/oatpp/network/server/ConnectionHandler.hpp +++ b/src/oatpp/network/server/ConnectionHandler.hpp @@ -26,7 +26,7 @@ #define network_server_ConnectionHandler_hpp #include "oatpp/core/data/stream/Stream.hpp" - +#include namespace oatpp { namespace network { namespace server { @@ -34,6 +34,16 @@ namespace oatpp { namespace network { namespace server { * Abstract ConnectionHandler. */ class ConnectionHandler { +public: + /** + * Convenience typedef for &id:oatpp::data::stream::IOStream;. + */ + typedef oatpp::data::stream::IOStream IOStream; + + /** + * Convenience typedef for accompanying parameters of connection handling. + */ + typedef std::unordered_map ParameterMap; public: /** @@ -42,10 +52,11 @@ public: virtual ~ConnectionHandler() = default; /** - * Handle provided connection here + * Handle provided connection. * @param connection - see &id:oatpp::data::stream::IOStream;. + * @param params - accompanying parameters. */ - virtual void handleConnection(const std::shared_ptr& connection) = 0; + virtual void handleConnection(const std::shared_ptr& connection, const std::shared_ptr& params) = 0; /** * Stop all threads here diff --git a/src/oatpp/network/server/Server.cpp b/src/oatpp/network/server/Server.cpp index 258b904d..3cca618f 100644 --- a/src/oatpp/network/server/Server.cpp +++ b/src/oatpp/network/server/Server.cpp @@ -44,14 +44,16 @@ Server::Server(const std::shared_ptr& connectionProvid void Server::mainLoop(){ setStatus(STATUS_CREATED, STATUS_RUNNING); - + + std::shared_ptr> params; + while(getStatus() == STATUS_RUNNING) { auto connection = m_connectionProvider->getConnection(); if (connection) { if(getStatus() == STATUS_RUNNING){ - m_connectionHandler->handleConnection(connection); + m_connectionHandler->handleConnection(connection, params /* null params */); } else { OATPP_LOGD("Server", "Already stopped. Closing connection..."); } diff --git a/src/oatpp/web/protocol/http/outgoing/Response.cpp b/src/oatpp/web/protocol/http/outgoing/Response.cpp index 387a4a26..e87794c6 100644 --- a/src/oatpp/web/protocol/http/outgoing/Response.cpp +++ b/src/oatpp/web/protocol/http/outgoing/Response.cpp @@ -68,6 +68,14 @@ std::shared_ptr Response::getConnecti return m_connectionUpgradeHandler; } +void Response::setConnectionUpgradeParameters(const std::shared_ptr& parameters) { + m_connectionUpgradeParameters = parameters; +} + +std::shared_ptr Response::getConnectionUpgradeParameters() { + return m_connectionUpgradeParameters; +} + void Response::send(const std::shared_ptr& stream) { if(m_body){ diff --git a/src/oatpp/web/protocol/http/outgoing/Response.hpp b/src/oatpp/web/protocol/http/outgoing/Response.hpp index 8d63079c..645b2e39 100644 --- a/src/oatpp/web/protocol/http/outgoing/Response.hpp +++ b/src/oatpp/web/protocol/http/outgoing/Response.hpp @@ -42,6 +42,11 @@ public: * See &id:oatpp::web::protocol::http::Headers; */ typedef http::Headers Headers; + + /** + * Convenience typedef for &id:oatpp::network::server::ConnectionHandler;. + */ + typedef oatpp::network::server::ConnectionHandler ConnectionHandler; public: OBJECT_POOL(Outgoing_Response_Pool, Response, 32) SHARED_OBJECT_POOL(Shared_Outgoing_Response_Pool, Response, 32) @@ -49,7 +54,8 @@ private: Status m_status; Headers m_headers; std::shared_ptr m_body; - std::shared_ptr m_connectionUpgradeHandler; + std::shared_ptr m_connectionUpgradeHandler; + std::shared_ptr m_connectionUpgradeParameters; public: /** * Constructor. @@ -100,13 +106,26 @@ public: * Response(&id:oatpp::web::protocol::http::Status::CODE_101;, nullptr);
* @param handler - `std::shared_ptr` to &id:oatpp::network::server::ConnectionHandler;. */ - void setConnectionUpgradeHandler(const std::shared_ptr& handler); + void setConnectionUpgradeHandler(const std::shared_ptr& handler); /** - * Get currently set connection upgrade handler + * Get currently set connection upgrade handler. * @return - `std::shared_ptr` to &id:oatpp::network::server::ConnectionHandler;. */ - std::shared_ptr getConnectionUpgradeHandler(); + std::shared_ptr getConnectionUpgradeHandler(); + + /** + * Set connection upgrade parameters.
+ * Use it to set additional parameters for upgraded connection handling. See &l:Response::setConnectionUpgradeHandler ();. + * @param parameters - `std::shared_ptr` to const &id:oatpp::network::server::ConnectionHandler::ParameterMap;. + */ + void setConnectionUpgradeParameters(const std::shared_ptr& parameters); + + /** + * Get connection upgrade parameters. + * @return - `std::shared_ptr` to const &id:oatpp::network::server::ConnectionHandler::ParametersMap;. + */ + std::shared_ptr getConnectionUpgradeParameters(); /** * Write this Response to stream. diff --git a/src/oatpp/web/server/AsyncHttpConnectionHandler.cpp b/src/oatpp/web/server/AsyncHttpConnectionHandler.cpp index 8519ab28..63b34030 100644 --- a/src/oatpp/web/server/AsyncHttpConnectionHandler.cpp +++ b/src/oatpp/web/server/AsyncHttpConnectionHandler.cpp @@ -65,7 +65,9 @@ void AsyncHttpConnectionHandler::addRequestInterceptor(const std::shared_ptr& connection){ +void AsyncHttpConnectionHandler::handleConnection(const std::shared_ptr& connection, + const std::shared_ptr& params) +{ connection->setOutputStreamIOMode(oatpp::data::stream::IOMode::NON_BLOCKING); connection->setInputStreamIOMode(oatpp::data::stream::IOMode::NON_BLOCKING); diff --git a/src/oatpp/web/server/AsyncHttpConnectionHandler.hpp b/src/oatpp/web/server/AsyncHttpConnectionHandler.hpp index f042c87c..508574ae 100644 --- a/src/oatpp/web/server/AsyncHttpConnectionHandler.hpp +++ b/src/oatpp/web/server/AsyncHttpConnectionHandler.hpp @@ -67,7 +67,7 @@ public: void addRequestInterceptor(const std::shared_ptr& interceptor); - void handleConnection(const std::shared_ptr& connection) override; + void handleConnection(const std::shared_ptr& connection, const std::shared_ptr& params) override; /** * Will call m_executor.stop() diff --git a/src/oatpp/web/server/HttpConnectionHandler.cpp b/src/oatpp/web/server/HttpConnectionHandler.cpp index 906d825f..7a649b13 100644 --- a/src/oatpp/web/server/HttpConnectionHandler.cpp +++ b/src/oatpp/web/server/HttpConnectionHandler.cpp @@ -81,7 +81,7 @@ void HttpConnectionHandler::Task::run(){ if(connectionState == oatpp::web::protocol::http::outgoing::CommunicationUtils::CONNECTION_STATE_UPGRADE) { auto handler = response->getConnectionUpgradeHandler(); if(handler) { - handler->handleConnection(m_connection); + handler->handleConnection(m_connection, response->getConnectionUpgradeParameters()); } else { OATPP_LOGD("[oatpp::web::server::HttpConnectionHandler::Task::run()]", "Warning. ConnectionUpgradeHandler not set!"); } @@ -110,7 +110,9 @@ void HttpConnectionHandler::addRequestInterceptor(const std::shared_ptr& connection){ +void HttpConnectionHandler::handleConnection(const std::shared_ptr& connection, + const std::shared_ptr& params) +{ connection->setOutputStreamIOMode(oatpp::data::stream::IOMode::BLOCKING); connection->setInputStreamIOMode(oatpp::data::stream::IOMode::BLOCKING); diff --git a/src/oatpp/web/server/HttpConnectionHandler.hpp b/src/oatpp/web/server/HttpConnectionHandler.hpp index ef77a4e3..faaa232e 100644 --- a/src/oatpp/web/server/HttpConnectionHandler.hpp +++ b/src/oatpp/web/server/HttpConnectionHandler.hpp @@ -108,7 +108,7 @@ public: * Implementation of &id:oatpp::network::server::ConnectionHandler::handleConnection;. * @param connection - &id:oatpp::data::stream::IOStream; representing connection. */ - void handleConnection(const std::shared_ptr& connection) override; + void handleConnection(const std::shared_ptr& connection, const std::shared_ptr& params) override; /** * Tell all worker threads to exit when done. diff --git a/src/oatpp/web/server/HttpProcessor.cpp b/src/oatpp/web/server/HttpProcessor.cpp index e0bc3847..dffd672a 100644 --- a/src/oatpp/web/server/HttpProcessor.cpp +++ b/src/oatpp/web/server/HttpProcessor.cpp @@ -164,7 +164,7 @@ HttpProcessor::Coroutine::Action HttpProcessor::Coroutine::onRequestDone() { if(m_connectionState == oatpp::web::protocol::http::outgoing::CommunicationUtils::CONNECTION_STATE_UPGRADE) { auto handler = m_currentResponse->getConnectionUpgradeHandler(); if(handler) { - handler->handleConnection(m_connection); + handler->handleConnection(m_connection, m_currentResponse->getConnectionUpgradeParameters()); } else { OATPP_LOGD("[oatpp::web::server::HttpProcessor::Coroutine::onRequestDone()]", "Warning. ConnectionUpgradeHandler not set!"); }