From abfb6ea22d61d696f838ab0230e300b191611172 Mon Sep 17 00:00:00 2001 From: k1-801 Date: Wed, 28 Apr 2021 01:45:27 +0400 Subject: [PATCH] Tiny fixups (server conn info leak closed, log va leak closed, config dir optimized) Commit amended for code style by Adam Honse --- LogManager.cpp | 3 ++- NetworkServer.cpp | 25 ++++++++++++++++++------- NetworkServer.h | 5 ++++- ResourceManager.cpp | 9 +++++++-- ResourceManager.h | 3 +++ 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/LogManager.cpp b/LogManager.cpp index 5de38a63..6ec1fc44 100644 --- a/LogManager.cpp +++ b/LogManager.cpp @@ -186,8 +186,9 @@ void LogManager::_append(const char* filename, int line, unsigned int level, con va_list va2; va_copy(va2, va); int len = vsnprintf(nullptr, 0, fmt, va); - mes->buffer.resize(len + 1); + mes->buffer.resize(len); vsnprintf(&(mes->buffer[0]), len + 1, fmt, va2); + va_end(va2); /*-------------------------------------------------*\ | Fill in message information | diff --git a/NetworkServer.cpp b/NetworkServer.cpp index 0660f555..035259d5 100644 --- a/NetworkServer.cpp +++ b/NetworkServer.cpp @@ -7,6 +7,8 @@ \*-----------------------------------------*/ #include "NetworkServer.h" +#include "ResourceManager.h" +#include "LogManager.h" #include #ifndef WIN32 @@ -33,6 +35,17 @@ const char yes = 1; using namespace std::chrono_literals; +NetworkClientInfo::~NetworkClientInfo() +{ + if(client_sock != INVALID_SOCKET) + { + LOG_NOTICE("Closing server connection: %s", client_ip); + delete client_listen_thread; + shutdown(client_sock, SD_RECEIVE); + closesocket(client_sock); + } +} + NetworkServer::NetworkServer(std::vector& control) : controllers(control) { port_num = OPENRGB_SDK_PORT; @@ -280,17 +293,17 @@ void NetworkServer::StopServer() server_online = false; ServerClientsMutex.lock(); + for(unsigned int client_idx = 0; client_idx < ServerClients.size(); client_idx++) { - shutdown(ServerClients[client_idx]->client_sock, SD_RECEIVE); - closesocket(ServerClients[client_idx]->client_sock); delete ServerClients[client_idx]; } + ServerClients.clear(); + shutdown(server_sock, SD_RECEIVE); closesocket(server_sock); - ServerClients.clear(); ServerClientsMutex.unlock(); if(ConnectionThread) @@ -726,9 +739,6 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info) } listen_done: - printf("Server connection closed\r\n"); - shutdown(client_info->client_sock, SD_RECEIVE); - closesocket(client_info->client_sock); ServerClientsMutex.lock(); @@ -736,13 +746,14 @@ listen_done: { if(ServerClients[this_idx] == client_info) { - delete client_info->client_listen_thread; delete client_info; ServerClients.erase(ServerClients.begin() + this_idx); break; } } + client_info = nullptr; + ServerClientsMutex.unlock(); /*-------------------------------------------------*\ diff --git a/NetworkServer.h b/NetworkServer.h index b6328510..32e8a8cc 100644 --- a/NetworkServer.h +++ b/NetworkServer.h @@ -21,8 +21,11 @@ typedef void (*NetServerCallback)(void *); -struct NetworkClientInfo +class NetworkClientInfo { +public: + ~NetworkClientInfo(); + SOCKET client_sock; std::thread * client_listen_thread; std::string client_string; diff --git a/ResourceManager.cpp b/ResourceManager.cpp index efe259c3..bab4ebce 100644 --- a/ResourceManager.cpp +++ b/ResourceManager.cpp @@ -43,6 +43,8 @@ ResourceManager::ResourceManager() detection_is_required = false; DetectDevicesThread = nullptr; + SetupConfigurationDirectory(); + /*-------------------------------------------------------------------------*\ | Load settings from file | \*-------------------------------------------------------------------------*/ @@ -309,9 +311,9 @@ void ResourceManager::I2CBusListChanged() I2CBusListChangeMutex.unlock(); } -std::string ResourceManager::GetConfigurationDirectory() +void ResourceManager::SetupConfigurationDirectory() { - std::string config_dir = ""; + config_dir.clear(); const char* xdg_config_home = getenv("XDG_CONFIG_HOME"); const char* home = getenv("HOME"); const char* appdata = getenv("APPDATA"); @@ -357,7 +359,10 @@ std::string ResourceManager::GetConfigurationDirectory() { config_dir = "./"; } +} +std::string ResourceManager::GetConfigurationDirectory() +{ return(config_dir); } diff --git a/ResourceManager.h b/ResourceManager.h index d4f5a462..ec97bc56 100644 --- a/ResourceManager.h +++ b/ResourceManager.h @@ -153,6 +153,7 @@ public: private: void DetectDevicesThreadFunction(); void UpdateDetectorSettings(); + void SetupConfigurationDirectory(); /*-------------------------------------------------------------------------------------*\ | Static pointer to shared instance of ResourceManager | @@ -243,4 +244,6 @@ private: std::mutex I2CBusListChangeMutex; std::vector I2CBusListChangeCallbacks; std::vector I2CBusListChangeCallbackArgs; + + std::string config_dir; };