From 7cc9550efce4f66b4dd9ee2460e97f92b7ebfb05 Mon Sep 17 00:00:00 2001 From: Exynox Date: Sun, 22 Sep 2024 09:55:32 +0300 Subject: [PATCH] Fixed db crash when MySQL connection wasn't possible --- src/db/src/ClientManager.h | 4 +- src/db/src/Main.cpp | 94 ++++++++++---------------------------- 2 files changed, 26 insertions(+), 72 deletions(-) diff --git a/src/db/src/ClientManager.h b/src/db/src/ClientManager.h index 9cfe36a..cb675ff 100644 --- a/src/db/src/ClientManager.h +++ b/src/db/src/ClientManager.h @@ -381,8 +381,8 @@ class CClientManager : public singleton private: int m_looping; - event_base * m_base; - evconnlistener * m_listener; + event_base * m_base = nullptr; + evconnlistener * m_listener = nullptr; TPeerList m_peerList; CPeer * m_pkAuthPeer; diff --git a/src/db/src/Main.cpp b/src/db/src/Main.cpp index add0cba..030d872 100644 --- a/src/db/src/Main.cpp +++ b/src/db/src/Main.cpp @@ -16,14 +16,12 @@ #endif #include -void SetPlayerDBName(const char* c_pszPlayerDBName); void SetTablePostfix(const char* c_pszTablePostfix); int Start(); std::string g_stTablePostfix; std::string g_stLocaleNameColumn = "name"; std::string g_stLocale = "euckr"; -std::string g_stPlayerDBName = ""; BOOL g_test_server = false; @@ -95,7 +93,7 @@ int main() DBManager.Quit(); int iCount; - while (1) + while (true) { iCount = 0; @@ -219,83 +217,56 @@ int Start() int iPort; char line[256+1]; - if (CConfig::instance().GetValue("SQL_PLAYER", line, 256)) - { + if (CConfig::instance().GetValue("SQL_PLAYER", line, 256)) { sscanf(line, " %s %s %s %s %d ", szAddr, szDB, szUser, szPassword, &iPort); SPDLOG_DEBUG("Connecting to MySQL server (player)"); - int iRetry = 5; + if (!CDBManager::instance().Connect(SQL_PLAYER, szAddr, iPort, szDB, szUser, szPassword)) { + SPDLOG_CRITICAL("Connection to MySQL server (player) failed!"); + return false; + } - do - { - if (CDBManager::instance().Connect(SQL_PLAYER, szAddr, iPort, szDB, szUser, szPassword)) - { - SPDLOG_INFO("Connected to MySQL server (player)"); - break; - } - - SPDLOG_ERROR("Connection to MySQL server (player) failed, retrying in 5 seconds"); - sleep(5); - } while (iRetry--); - SetPlayerDBName(szDB); + SPDLOG_INFO("Connected to MySQL server (player)"); } - else - { - SPDLOG_ERROR("SQL_PLAYER not configured"); + else { + SPDLOG_CRITICAL("SQL_PLAYER not configured"); return false; } - if (CConfig::instance().GetValue("SQL_ACCOUNT", line, 256)) - { + if (CConfig::instance().GetValue("SQL_ACCOUNT", line, 256)) { sscanf(line, " %s %s %s %s %d ", szAddr, szDB, szUser, szPassword, &iPort); - SPDLOG_DEBUG("connecting to MySQL server (account)"); + SPDLOG_DEBUG("Connecting to MySQL server (account)"); - int iRetry = 5; + if (!CDBManager::instance().Connect(SQL_ACCOUNT, szAddr, iPort, szDB, szUser, szPassword)) { + SPDLOG_CRITICAL("Connection to MySQL server (account) failed!"); + return false; + } - do - { - if (CDBManager::instance().Connect(SQL_ACCOUNT, szAddr, iPort, szDB, szUser, szPassword)) - { - SPDLOG_DEBUG(" OK"); - break; - } - - SPDLOG_DEBUG(" failed, retrying in 5 seconds"); - SPDLOG_ERROR(" failed, retrying in 5 seconds"); - sleep(5); - } while (iRetry--); - SPDLOG_INFO("Success ACCOUNT"); + SPDLOG_INFO("Connected to MySQL server (account)"); } else { - SPDLOG_ERROR("SQL_ACCOUNT not configured"); + SPDLOG_CRITICAL("SQL_ACCOUNT not configured"); return false; } if (CConfig::instance().GetValue("SQL_COMMON", line, 256)) { sscanf(line, " %s %s %s %s %d ", szAddr, szDB, szUser, szPassword, &iPort); - SPDLOG_DEBUG("connecting to MySQL server (common)"); + SPDLOG_DEBUG("Connecting to MySQL server (common)"); - int iRetry = 5; - do + if (!CDBManager::instance().Connect(SQL_COMMON, szAddr, iPort, szDB, szUser, szPassword)) { - if (CDBManager::instance().Connect(SQL_COMMON, szAddr, iPort, szDB, szUser, szPassword)) - { - SPDLOG_DEBUG(" OK"); - break; - } + SPDLOG_CRITICAL("Connection to MySQL server (common) failed!"); + return false; + } - SPDLOG_DEBUG(" failed, retrying in 5 seconds"); - SPDLOG_ERROR(" failed, retrying in 5 seconds"); - sleep(5); - } while (iRetry--); - SPDLOG_INFO("Success COMMON"); + SPDLOG_INFO("Connected to MySQL server (common)"); } else { - SPDLOG_ERROR("SQL_COMMON not configured"); + SPDLOG_CRITICAL("SQL_COMMON not configured"); return false; } @@ -326,20 +297,3 @@ const char * GetTablePostfix() { return g_stTablePostfix.c_str(); } - -void SetPlayerDBName(const char* c_pszPlayerDBName) -{ - if (! c_pszPlayerDBName || ! *c_pszPlayerDBName) - g_stPlayerDBName = ""; - else - { - g_stPlayerDBName = c_pszPlayerDBName; - g_stPlayerDBName += "."; - } -} - -const char * GetPlayerDBName() -{ - return g_stPlayerDBName.c_str(); -} -