diff --git a/include/advertiser.h b/include/advertiser.h index b2c5752..6c09caa 100644 --- a/include/advertiser.h +++ b/include/advertiser.h @@ -32,9 +32,10 @@ class Advertiser : public QObject { Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port, QString p_name, QString p_description, QObject* parent = nullptr); + ~Advertiser(); void contactMasterServer(); - signals: +signals: public slots: void readData(); diff --git a/include/aoclient.h b/include/aoclient.h index e312b9c..7139da4 100644 --- a/include/aoclient.h +++ b/include/aoclient.h @@ -31,6 +31,7 @@ class AOClient : public QObject { public: AOClient(Server* p_server, QTcpSocket* p_socket, QObject* parent = nullptr); ~AOClient(); + void cleanup(); QString getHwid(); void setHwid(QString p_hwid); diff --git a/include/config_manager.h b/include/config_manager.h index cd5b8ae..9311fbb 100644 --- a/include/config_manager.h +++ b/include/config_manager.h @@ -45,7 +45,6 @@ class ConfigManager { bool loadServerSettings(server_settings* settings); private: - QSettings* config; bool fileExists(QFileInfo *file); }; diff --git a/include/server.h b/include/server.h index ff5be5a..3245883 100644 --- a/include/server.h +++ b/include/server.h @@ -39,6 +39,8 @@ class Server : public QObject { public: Server(int p_port, int p_ws_port, QObject* parent = nullptr); + ~Server(); + void start(); AOClient* getClient(QString ipid); void updateCharsTaken(AreaData* area); diff --git a/include/ws_client.h b/include/ws_client.h index 0dfee73..8f2ca17 100644 --- a/include/ws_client.h +++ b/include/ws_client.h @@ -27,7 +27,7 @@ class WSClient : public QObject { Q_OBJECT public: WSClient(QTcpSocket* p_tcp_socket, QWebSocket* p_web_socket, QObject* parent = nullptr); - + ~WSClient(); public slots: void onTcpData(); void onWsData(QString message); diff --git a/include/ws_proxy.h b/include/ws_proxy.h index b27fe9c..440d453 100644 --- a/include/ws_proxy.h +++ b/include/ws_proxy.h @@ -29,9 +29,10 @@ class WSProxy : public QObject { Q_OBJECT public: WSProxy(int p_local_port, int p_ws_port, QObject* parent); - void start(); + ~WSProxy(); - public slots: + void start(); +public slots: void wsConnected(); private: diff --git a/src/advertiser.cpp b/src/advertiser.cpp index 79af152..8fe4352 100644 --- a/src/advertiser.cpp +++ b/src/advertiser.cpp @@ -77,3 +77,8 @@ void Advertiser::socketDisconnected() // TODO: fire a signal here, i18n qDebug("Connection to master server lost"); } + +Advertiser::~Advertiser() +{ + socket->deleteLater(); +} diff --git a/src/aoclient.cpp b/src/aoclient.cpp index c7b35fa..257410d 100644 --- a/src/aoclient.cpp +++ b/src/aoclient.cpp @@ -301,4 +301,9 @@ void AOClient::setHwid(QString p_hwid) QString AOClient::getIpid() { return ipid; } +void AOClient::cleanup() { + socket->disconnectFromHost(); + socket->deleteLater(); +} + AOClient::~AOClient() {} diff --git a/src/config_manager.cpp b/src/config_manager.cpp index 086ec90..3d5b75c 100644 --- a/src/config_manager.cpp +++ b/src/config_manager.cpp @@ -17,14 +17,12 @@ ////////////////////////////////////////////////////////////////////////////////////// #include "include/config_manager.h" -ConfigManager::ConfigManager() -{ - config = new QSettings("config/config.ini", QSettings::IniFormat); -} +ConfigManager::ConfigManager() { } // Validate and set up the config bool ConfigManager::initConfig() { + QSettings config("config/config.ini", QSettings::IniFormat); QFileInfo config_dir_info("config/"); if (!config_dir_info.exists() || !config_dir_info.isDir()) { qCritical() << "Config directory doesn't exist!"; @@ -56,9 +54,9 @@ bool ConfigManager::initConfig() return false; } - config->beginGroup("Info"); - QString config_version = config->value("version", "none").toString(); - config->endGroup(); + config.beginGroup("Info"); + QString config_version = config.value("version", "none").toString(); + config.endGroup(); if (config_version == "none") { QFileInfo check_file("config/config.ini"); if (!fileExists(&check_file)) { @@ -92,6 +90,7 @@ bool ConfigManager::initConfig() // Ensure version continuity with config versions bool ConfigManager::updateConfig(int current_version) { + QSettings config("config/config.ini", QSettings::IniFormat); if (current_version > CONFIG_VERSION) { // Config version is newer than the latest version, and the config is // invalid This could also mean the server is out of date, and the user @@ -111,9 +110,9 @@ bool ConfigManager::updateConfig(int current_version) case 0: // Version 0 doesn't actually exist, but we should check for it // just in case case 1: - config->beginGroup("Info"); - config->setValue("version", CONFIG_VERSION); - config->endGroup(); + config.beginGroup("Info"); + config.setValue("version", CONFIG_VERSION); + config.endGroup(); break; // This is the newest version, and nothing more needs to be // done } @@ -124,36 +123,37 @@ bool ConfigManager::updateConfig(int current_version) // Validate and retriever settings related to advertising and the server bool ConfigManager::loadServerSettings(server_settings* settings) { + QSettings config("config/config.ini", QSettings::IniFormat); bool port_conversion_success; bool ws_port_conversion_success; bool local_port_conversion_success; - config->beginGroup("Options"); + config.beginGroup("Options"); settings->ms_ip = - config->value("ms_ip", "master.aceattorneyonline.com").toString(); + config.value("ms_ip", "master.aceattorneyonline.com").toString(); settings->port = - config->value("port", "27016").toInt(&port_conversion_success); + config.value("port", "27016").toInt(&port_conversion_success); settings->ws_port = - config->value("webao_port", "27017").toInt(&ws_port_conversion_success); + config.value("webao_port", "27017").toInt(&ws_port_conversion_success); settings->local_port = - config->value("port", "27016").toInt(&local_port_conversion_success); - settings->name = config->value("server_name", "My First Server").toString(); + config.value("port", "27016").toInt(&local_port_conversion_success); + settings->name = config.value("server_name", "My First Server").toString(); settings->description = - config->value("server_description", "This is my flashy new server") + config.value("server_description", "This is my flashy new server") .toString(); - config->endGroup(); + config.endGroup(); if (!port_conversion_success || !ws_port_conversion_success || !local_port_conversion_success) { return false; } else { - config->beginGroup("Options"); + config.beginGroup("Options"); // Will be true of false depending on the key settings->advertise_server = - (config->value("advertise", "true").toString() == "true"); + (config.value("advertise", "true").toString() == "true"); - if (config->value("webao_enable", "true").toString() != "true") + if (config.value("webao_enable", "true").toString() != "true") settings->ws_port = -1; - config->endGroup(); + config.endGroup(); return true; } diff --git a/src/main.cpp b/src/main.cpp index 1854a77..1ba06ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,17 +19,25 @@ #include "include/server.h" #include "include/config_manager.h" +#include + #include #include Advertiser* advertiser; Server* server; +void cleanup() { + server->deleteLater(); + advertiser->deleteLater(); +} + int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); QCoreApplication::setApplicationName("akashi"); QCoreApplication::setApplicationVersion("0.0.1"); + std::atexit(cleanup); qDebug("Main application started"); @@ -42,7 +50,8 @@ int main(int argc, char* argv[]) if (!config_valid) { qCritical() << "config.ini is invalid!"; qCritical() << "Exiting server due to configuration issue."; - return EXIT_FAILURE; + exit(EXIT_FAILURE); + QCoreApplication::quit(); } else { @@ -62,7 +71,8 @@ int main(int argc, char* argv[]) } } else { qCritical() << "Exiting server due to configuration issue."; - return EXIT_FAILURE; + exit(EXIT_FAILURE); + QCoreApplication::quit(); } return app.exec(); diff --git a/src/server.cpp b/src/server.cpp index 51ef6ea..74c43b0 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -132,3 +132,13 @@ AOClient* Server::getClient(QString ipid) } return nullptr; } + +Server::~Server() +{ + for (AOClient* client : clients) { + client->cleanup(); + client->deleteLater(); + } + server->deleteLater(); + proxy->deleteLater(); +} diff --git a/src/ws_client.cpp b/src/ws_client.cpp index d77321d..4ce14c3 100644 --- a/src/ws_client.cpp +++ b/src/ws_client.cpp @@ -49,8 +49,11 @@ void WSClient::onWsDisconnect() void WSClient::onTcpDisconnect() { - qDebug() << "deleted"; web_socket->close(); +} + +WSClient::~WSClient() +{ tcp_socket->deleteLater(); web_socket->deleteLater(); } diff --git a/src/ws_proxy.cpp b/src/ws_proxy.cpp index e0862aa..766f787 100644 --- a/src/ws_proxy.cpp +++ b/src/ws_proxy.cpp @@ -54,3 +54,8 @@ void WSProxy::wsConnected() new_tcp->connectToHost(QHostAddress::LocalHost, local_port); } + +WSProxy::~WSProxy() +{ + server->deleteLater(); +}