Fixed db crash when MySQL connection wasn't possible

This commit is contained in:
Exynox 2024-09-22 09:55:32 +03:00
parent c6177d9c73
commit 7cc9550efc
2 changed files with 26 additions and 72 deletions

View File

@ -381,8 +381,8 @@ class CClientManager : public singleton<CClientManager>
private: private:
int m_looping; int m_looping;
event_base * m_base; event_base * m_base = nullptr;
evconnlistener * m_listener; evconnlistener * m_listener = nullptr;
TPeerList m_peerList; TPeerList m_peerList;
CPeer * m_pkAuthPeer; CPeer * m_pkAuthPeer;

View File

@ -16,14 +16,12 @@
#endif #endif
#include <signal.h> #include <signal.h>
void SetPlayerDBName(const char* c_pszPlayerDBName);
void SetTablePostfix(const char* c_pszTablePostfix); void SetTablePostfix(const char* c_pszTablePostfix);
int Start(); int Start();
std::string g_stTablePostfix; std::string g_stTablePostfix;
std::string g_stLocaleNameColumn = "name"; std::string g_stLocaleNameColumn = "name";
std::string g_stLocale = "euckr"; std::string g_stLocale = "euckr";
std::string g_stPlayerDBName = "";
BOOL g_test_server = false; BOOL g_test_server = false;
@ -95,7 +93,7 @@ int main()
DBManager.Quit(); DBManager.Quit();
int iCount; int iCount;
while (1) while (true)
{ {
iCount = 0; iCount = 0;
@ -219,83 +217,56 @@ int Start()
int iPort; int iPort;
char line[256+1]; 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); sscanf(line, " %s %s %s %s %d ", szAddr, szDB, szUser, szPassword, &iPort);
SPDLOG_DEBUG("Connecting to MySQL server (player)"); 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!");
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);
}
else
{
SPDLOG_ERROR("SQL_PLAYER not configured");
return false; return false;
} }
if (CConfig::instance().GetValue("SQL_ACCOUNT", line, 256)) SPDLOG_INFO("Connected to MySQL server (player)");
{ }
sscanf(line, " %s %s %s %s %d ", szAddr, szDB, szUser, szPassword, &iPort); else {
SPDLOG_DEBUG("connecting to MySQL server (account)"); SPDLOG_CRITICAL("SQL_PLAYER not configured");
return false;
int iRetry = 5;
do
{
if (CDBManager::instance().Connect(SQL_ACCOUNT, szAddr, iPort, szDB, szUser, szPassword))
{
SPDLOG_DEBUG(" OK");
break;
} }
SPDLOG_DEBUG(" failed, retrying in 5 seconds"); if (CConfig::instance().GetValue("SQL_ACCOUNT", line, 256)) {
SPDLOG_ERROR(" failed, retrying in 5 seconds"); sscanf(line, " %s %s %s %s %d ", szAddr, szDB, szUser, szPassword, &iPort);
sleep(5); SPDLOG_DEBUG("Connecting to MySQL server (account)");
} while (iRetry--);
SPDLOG_INFO("Success ACCOUNT"); if (!CDBManager::instance().Connect(SQL_ACCOUNT, szAddr, iPort, szDB, szUser, szPassword)) {
SPDLOG_CRITICAL("Connection to MySQL server (account) failed!");
return false;
}
SPDLOG_INFO("Connected to MySQL server (account)");
} }
else else
{ {
SPDLOG_ERROR("SQL_ACCOUNT not configured"); SPDLOG_CRITICAL("SQL_ACCOUNT not configured");
return false; return false;
} }
if (CConfig::instance().GetValue("SQL_COMMON", line, 256)) if (CConfig::instance().GetValue("SQL_COMMON", line, 256))
{ {
sscanf(line, " %s %s %s %s %d ", szAddr, szDB, szUser, szPassword, &iPort); 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_CRITICAL("Connection to MySQL server (common) failed!");
{ return false;
SPDLOG_DEBUG(" OK");
break;
} }
SPDLOG_DEBUG(" failed, retrying in 5 seconds"); SPDLOG_INFO("Connected to MySQL server (common)");
SPDLOG_ERROR(" failed, retrying in 5 seconds");
sleep(5);
} while (iRetry--);
SPDLOG_INFO("Success COMMON");
} }
else else
{ {
SPDLOG_ERROR("SQL_COMMON not configured"); SPDLOG_CRITICAL("SQL_COMMON not configured");
return false; return false;
} }
@ -326,20 +297,3 @@ const char * GetTablePostfix()
{ {
return g_stTablePostfix.c_str(); 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();
}