Server: Revert (#484) - remove undesired locking. (#483)

This commit is contained in:
lganzzzo 2021-10-20 01:47:01 +03:00
parent 11e4cd1166
commit 6335fc7acf
4 changed files with 11 additions and 40 deletions

View File

@ -46,6 +46,7 @@ std::shared_ptr<ConnectionProvider> ConnectionProviderSwitch::getCurrentProvider
}
if(!provider) {
const char* const TAG = "[oatpp::network::ConnectionProviderSwitch::getCurrentProvider()]";
const char* const msg = "Error. Can't provide connection. There is no provider set.";
OATPP_LOGE(TAG, msg)
throw std::runtime_error(std::string(TAG) + ": " + msg);

View File

@ -35,8 +35,6 @@ namespace oatpp { namespace network {
* Ex.: to hot-reload server certificate.
*/
class ConnectionProviderSwitch : public ServerConnectionProvider, public ClientConnectionProvider {
private:
static constexpr const char* TAG = "[oatpp::network::ConnectionProviderSwitch::getCurrentProvider()]";
private:
std::shared_ptr<ConnectionProvider> getCurrentProvider();
private:

View File

@ -26,7 +26,6 @@
#include <thread>
#include <chrono>
#include <utility>
namespace oatpp { namespace network {
@ -45,20 +44,19 @@ Server::Server(const std::shared_ptr<ConnectionProvider> &connectionProvider,
// This isn't implemented as static since threading is dropped and therefore static isn't needed anymore.
void Server::conditionalMainLoop() {
setStatus(STATUS_STARTING, STATUS_RUNNING);
std::shared_ptr<const std::unordered_map<oatpp::String, oatpp::String>> params;
while (getStatus() == STATUS_RUNNING) {
if (m_condition()) {
provider::ResourceHandle<data::stream::IOStream> connectionHandle;
{
std::lock_guard<oatpp::concurrency::SpinLock> lg(m_spinlock);
connectionHandle = m_connectionProvider->get();
}
auto connectionHandle = m_connectionProvider->get();
if (connectionHandle.object) {
if (getStatus() == STATUS_RUNNING) {
if (m_condition()) {
std::lock_guard<oatpp::concurrency::SpinLock> lg(m_spinlock);
m_connectionHandler->handleConnection(connectionHandle, params /* null params */);
} else {
setStatus(STATUS_STOPPING);
@ -67,6 +65,7 @@ void Server::conditionalMainLoop() {
OATPP_LOGD("[oatpp::network::server::mainLoop()]", "Error. Server already stopped - closing connection...");
}
}
} else {
setStatus(STATUS_STOPPING);
}
@ -75,19 +74,16 @@ void Server::conditionalMainLoop() {
}
void Server::mainLoop(Server *instance) {
instance->setStatus(STATUS_STARTING, STATUS_RUNNING);
std::shared_ptr<const std::unordered_map<oatpp::String, oatpp::String>> params;
while (instance->getStatus() == STATUS_RUNNING) {
provider::ResourceHandle<data::stream::IOStream> connectionHandle;
{
std::lock_guard<oatpp::concurrency::SpinLock> lg(instance->m_spinlock);
connectionHandle = instance->m_connectionProvider->get();
}
while (instance->getStatus() == STATUS_RUNNING) {
auto connectionHandle = instance->m_connectionProvider->get();
if (connectionHandle) {
if (instance->getStatus() == STATUS_RUNNING) {
std::lock_guard<oatpp::concurrency::SpinLock> lg(instance->m_spinlock);
instance->m_connectionHandler->handleConnection(connectionHandle, params /* null params */);
} else {
OATPP_LOGD("[oatpp::network::server::mainLoop()]", "Error. Server already stopped - closing connection...");
@ -171,16 +167,6 @@ 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,7 +58,6 @@ private:
std::function<bool()> m_condition;
std::thread m_thread;
std::mutex m_mutex;
oatpp::concurrency::SpinLock m_spinlock;
std::shared_ptr<ConnectionProvider> m_connectionProvider;
std::shared_ptr<ConnectionHandler> m_connectionHandler;
@ -156,21 +155,8 @@ public:
*/
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);
};
}}
#endif /* oatpp_network_Server_hpp */