Browse Source

Tiny fixups (server conn info leak closed, log va leak closed, config dir optimized)

Commit amended for code style by Adam Honse <calcprogrammer1@gmail.com>
merge-requests/471/merge
k1-801 1 year ago committed by Adam Honse
parent
commit
abfb6ea22d
  1. 3
      LogManager.cpp
  2. 25
      NetworkServer.cpp
  3. 5
      NetworkServer.h
  4. 9
      ResourceManager.cpp
  5. 3
      ResourceManager.h

3
LogManager.cpp

@ -186,8 +186,9 @@ void LogManager::_append(const char* filename, int line, unsigned int level, con @@ -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 |

25
NetworkServer.cpp

@ -7,6 +7,8 @@ @@ -7,6 +7,8 @@
\*-----------------------------------------*/
#include "NetworkServer.h"
#include "ResourceManager.h"
#include "LogManager.h"
#include <cstring>
#ifndef WIN32
@ -33,6 +35,17 @@ const char yes = 1; @@ -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<RGBController *>& control) : controllers(control)
{
port_num = OPENRGB_SDK_PORT;
@ -280,17 +293,17 @@ void NetworkServer::StopServer() @@ -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) @@ -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: @@ -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();
/*-------------------------------------------------*\

5
NetworkServer.h

@ -21,8 +21,11 @@ @@ -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;

9
ResourceManager.cpp

@ -43,6 +43,8 @@ ResourceManager::ResourceManager() @@ -43,6 +43,8 @@ ResourceManager::ResourceManager()
detection_is_required = false;
DetectDevicesThread = nullptr;
SetupConfigurationDirectory();
/*-------------------------------------------------------------------------*\
| Load settings from file |
\*-------------------------------------------------------------------------*/
@ -309,9 +311,9 @@ void ResourceManager::I2CBusListChanged() @@ -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() @@ -357,7 +359,10 @@ std::string ResourceManager::GetConfigurationDirectory()
{
config_dir = "./";
}
}
std::string ResourceManager::GetConfigurationDirectory()
{
return(config_dir);
}

3
ResourceManager.h

@ -153,6 +153,7 @@ public: @@ -153,6 +153,7 @@ public:
private:
void DetectDevicesThreadFunction();
void UpdateDetectorSettings();
void SetupConfigurationDirectory();
/*-------------------------------------------------------------------------------------*\
| Static pointer to shared instance of ResourceManager |
@ -243,4 +244,6 @@ private: @@ -243,4 +244,6 @@ private:
std::mutex I2CBusListChangeMutex;
std::vector<I2CBusListChangeCallback> I2CBusListChangeCallbacks;
std::vector<void *> I2CBusListChangeCallbackArgs;
std::string config_dir;
};

Loading…
Cancel
Save