Merge pull request #484 from bamkrs/server_replace_connectionprovider

Add setter for `Server`s `ConnectionProvider`.
This commit is contained in:
Benedikt-Alexander Mokroß 2021-09-15 17:06:39 +02:00 committed by GitHub
commit 0dee902a69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 3 deletions

View File

@ -50,11 +50,15 @@ void Server::conditionalMainLoop() {
while (getStatus() == STATUS_RUNNING) {
if (m_condition()) {
auto connection = m_connectionProvider->get();
std::shared_ptr<data::stream::IOStream> connection;
{
std::lock_guard<oatpp::concurrency::SpinLock> lg(m_spinlock);
connection = m_connectionProvider->get();
}
if (connection) {
if (getStatus() == STATUS_RUNNING) {
if (m_condition()) {
std::lock_guard<oatpp::concurrency::SpinLock> lg(m_spinlock);
m_connectionHandler->handleConnection(connection, params /* null params */);
} else {
setStatus(STATUS_STOPPING);
@ -75,10 +79,15 @@ void Server::mainLoop(Server *instance) {
std::shared_ptr<const std::unordered_map<oatpp::String, oatpp::String>> params;
while (instance->getStatus() == STATUS_RUNNING) {
auto connection = instance->m_connectionProvider->get();
std::shared_ptr<data::stream::IOStream> connection;
{
std::lock_guard<oatpp::concurrency::SpinLock> lg(instance->m_spinlock);
connection = instance->m_connectionProvider->get();
}
if (connection) {
if (instance->getStatus() == STATUS_RUNNING) {
std::lock_guard<oatpp::concurrency::SpinLock> lg(instance->m_spinlock);
instance->m_connectionHandler->handleConnection(connection, params /* null params */);
} else {
OATPP_LOGD("[oatpp::network::server::mainLoop()]", "Error. Server already stopped - closing connection...");
@ -163,6 +172,16 @@ v_int32 Server::getStatus() {
return m_status.load();
}
void Server::setConnectionProvider(const std::shared_ptr<ServerConnectionProvider> &connectionProvider) {
std::lock_guard<oatpp::concurrency::SpinLock> lg(m_spinlock);
m_connectionProvider = connectionProvider;
}
void Server::setConnectionHandler(const std::shared_ptr<ConnectionHandler> &connectionHandler) {
std::lock_guard<oatpp::concurrency::SpinLock> lg(m_spinlock);
m_connectionHandler = connectionHandler;
}
Server::~Server() {
stop();
}

View File

@ -58,6 +58,7 @@ private:
std::function<bool()> m_condition;
std::thread m_thread;
std::mutex m_mutex;
oatpp::concurrency::SpinLock m_spinlock;
std::shared_ptr<ServerConnectionProvider> m_connectionProvider;
std::shared_ptr<ConnectionHandler> m_connectionHandler;
@ -154,6 +155,18 @@ public:
* </ul>
*/
v_int32 getStatus();
/**
* Replaces the internal connection-provider
* @param connectionProvider - &id:oatpp::network::ConnectionProvider;.
*/
void setConnectionProvider(const std::shared_ptr<ServerConnectionProvider>& connectionProvider);
/**
* Replaces the internal connection-handler
* @param connectionHandler - &id:oatpp::network::ConnectionHandler;.
*/
void setConnectionHandler(const std::shared_ptr<ConnectionHandler>& connectionHandler);
};