pass parameter map to ConnectionHandler::handleConnection method

This commit is contained in:
lganzzzo 2019-06-19 02:13:53 +03:00
parent 49b63f5611
commit 4f705c18ed
9 changed files with 59 additions and 15 deletions

View File

@ -26,7 +26,7 @@
#define network_server_ConnectionHandler_hpp
#include "oatpp/core/data/stream/Stream.hpp"
#include <unordered_map>
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<oatpp::String, oatpp::String> 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<oatpp::data::stream::IOStream>& connection) = 0;
virtual void handleConnection(const std::shared_ptr<IOStream>& connection, const std::shared_ptr<const ParameterMap>& params) = 0;
/**
* Stop all threads here

View File

@ -44,14 +44,16 @@ Server::Server(const std::shared_ptr<ServerConnectionProvider>& connectionProvid
void Server::mainLoop(){
setStatus(STATUS_CREATED, STATUS_RUNNING);
std::shared_ptr<const std::unordered_map<oatpp::String, oatpp::String>> 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...");
}

View File

@ -68,6 +68,14 @@ std::shared_ptr<oatpp::network::server::ConnectionHandler> Response::getConnecti
return m_connectionUpgradeHandler;
}
void Response::setConnectionUpgradeParameters(const std::shared_ptr<const ConnectionHandler::ParameterMap>& parameters) {
m_connectionUpgradeParameters = parameters;
}
std::shared_ptr<const Response::ConnectionHandler::ParameterMap> Response::getConnectionUpgradeParameters() {
return m_connectionUpgradeParameters;
}
void Response::send(const std::shared_ptr<data::stream::OutputStream>& stream) {
if(m_body){

View File

@ -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<Body> m_body;
std::shared_ptr<oatpp::network::server::ConnectionHandler> m_connectionUpgradeHandler;
std::shared_ptr<ConnectionHandler> m_connectionUpgradeHandler;
std::shared_ptr<const ConnectionHandler::ParameterMap> m_connectionUpgradeParameters;
public:
/**
* Constructor.
@ -100,13 +106,26 @@ public:
* Response(&id:oatpp::web::protocol::http::Status::CODE_101;, nullptr);<br>
* @param handler - `std::shared_ptr` to &id:oatpp::network::server::ConnectionHandler;.
*/
void setConnectionUpgradeHandler(const std::shared_ptr<oatpp::network::server::ConnectionHandler>& handler);
void setConnectionUpgradeHandler(const std::shared_ptr<ConnectionHandler>& 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<oatpp::network::server::ConnectionHandler> getConnectionUpgradeHandler();
std::shared_ptr<ConnectionHandler> getConnectionUpgradeHandler();
/**
* Set connection upgrade parameters. <br>
* 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<const ConnectionHandler::ParameterMap>& parameters);
/**
* Get connection upgrade parameters.
* @return - `std::shared_ptr` to const &id:oatpp::network::server::ConnectionHandler::ParametersMap;.
*/
std::shared_ptr<const ConnectionHandler::ParameterMap> getConnectionUpgradeParameters();
/**
* Write this Response to stream.

View File

@ -65,7 +65,9 @@ void AsyncHttpConnectionHandler::addRequestInterceptor(const std::shared_ptr<han
m_requestInterceptors.pushBack(interceptor);
}
void AsyncHttpConnectionHandler::handleConnection(const std::shared_ptr<oatpp::data::stream::IOStream>& connection){
void AsyncHttpConnectionHandler::handleConnection(const std::shared_ptr<IOStream>& connection,
const std::shared_ptr<const ParameterMap>& params)
{
connection->setOutputStreamIOMode(oatpp::data::stream::IOMode::NON_BLOCKING);
connection->setInputStreamIOMode(oatpp::data::stream::IOMode::NON_BLOCKING);

View File

@ -67,7 +67,7 @@ public:
void addRequestInterceptor(const std::shared_ptr<handler::RequestInterceptor>& interceptor);
void handleConnection(const std::shared_ptr<oatpp::data::stream::IOStream>& connection) override;
void handleConnection(const std::shared_ptr<IOStream>& connection, const std::shared_ptr<const ParameterMap>& params) override;
/**
* Will call m_executor.stop()

View File

@ -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<handler:
m_requestInterceptors.pushBack(interceptor);
}
void HttpConnectionHandler::handleConnection(const std::shared_ptr<oatpp::data::stream::IOStream>& connection){
void HttpConnectionHandler::handleConnection(const std::shared_ptr<oatpp::data::stream::IOStream>& connection,
const std::shared_ptr<const ParameterMap>& params)
{
connection->setOutputStreamIOMode(oatpp::data::stream::IOMode::BLOCKING);
connection->setInputStreamIOMode(oatpp::data::stream::IOMode::BLOCKING);

View File

@ -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<oatpp::data::stream::IOStream>& connection) override;
void handleConnection(const std::shared_ptr<IOStream>& connection, const std::shared_ptr<const ParameterMap>& params) override;
/**
* Tell all worker threads to exit when done.

View File

@ -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!");
}