Browse Source

DB async ping

Fixes #93
Image 4 years ago
parent
commit
afd45c4cfc

+ 1 - 0
EQ2/source/WorldServer/net.cpp

@@ -464,6 +464,7 @@ int main(int argc, char** argv) {
 			if (InterserverTimer.Check()) {
 				InterserverTimer.Start();
 				database.ping();
+				database.PingAsyncDatabase();
 
 				if (net.LoginServerInfo && loginserver.Connected() == false && loginserver.CanReconnect()) {
 					LogWrite(WORLD__DEBUG, 0, "Thread", "Starting autoinit loginserver thread...");

+ 2 - 0
EQ2/source/common/DatabaseNew.cpp

@@ -38,6 +38,8 @@
 
 DatabaseNew::DatabaseNew() {
 	mysql_init(&mysql);
+	int timeout = 10;
+	mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, &timeout);
 	MMysql.SetName("DatabaseNew::mysql");
 }
 

+ 12 - 0
EQ2/source/common/database.cpp

@@ -471,6 +471,18 @@ void Database::PurgeDBInstances()
 	DBInstanceMutex.releasewritelock(__FUNCTION__, __LINE__);
 }
 
+
+void Database::PingAsyncDatabase()
+{
+	map<Database*, bool>::iterator itr;
+	DBInstanceMutex.readlock(__FUNCTION__, __LINE__);
+	for (itr = dbInstances.begin(); itr != dbInstances.end(); itr++) {
+		Database* tmpInst = itr->first;
+		tmpInst->ping();
+	}
+	DBInstanceMutex.releasereadlock(__FUNCTION__, __LINE__);
+}
+
 void Database::FreeDBInstance(Database* cur)
 {
 	DBInstanceMutex.writelock(__FUNCTION__, __LINE__);

+ 1 - 0
EQ2/source/common/database.h

@@ -57,6 +57,7 @@ public:
 	void RemoveActiveQuery(Query* query);
 	void AddActiveQuery(Query* query);
 	bool IsActiveQuery(int32 id, Query* skip=0);
+	void PingAsyncDatabase();
 #endif
 protected:
 

+ 21 - 5
EQ2/source/common/dbcore.cpp

@@ -56,8 +56,8 @@ DBcore::DBcore() {
 	pPassword = 0;
 	pDatabase = 0;
 	pCompress = false;
-	pSSL = false;
-	pStatus = Closed;
+pSSL = false;
+pStatus = Closed;
 }
 
 DBcore::~DBcore() {
@@ -75,10 +75,10 @@ DBcore::~DBcore() {
 }
 
 
-bool DBcore::ReadDBINI(char *host, char *user, char *passwd, char *database, int32 &port, bool &compress, bool *items) {
-	char line[256], *key, *val;
+bool DBcore::ReadDBINI(char* host, char* user, char* passwd, char* database, int32& port, bool& compress, bool* items) {
+	char line[256], * key, * val;
 	bool on_database_section = false;
-	FILE *f;
+	FILE* f;
 
 	if ((f = fopen(DB_INI_FILE, "r")) == NULL) {
 		LogWrite(DATABASE__ERROR, 0, "DBCore", "Unable to open '%s' for reading", DB_INI_FILE);
@@ -154,6 +154,22 @@ void DBcore::ping() {
 		return;
 	}
 	mysql_ping(&mysql);
+
+	int32* errnum = new int32;
+	*errnum = mysql_errno(&mysql);
+	
+	switch (*errnum)
+	{
+		case CR_COMMANDS_OUT_OF_SYNC:
+		case CR_SERVER_GONE_ERROR:
+		case CR_UNKNOWN_ERROR:
+		{
+			LogWrite(DATABASE__ERROR, 0, "DBCore", "[Database] We lost connection to the database., errno: %i", errno);
+			break;
+		}
+	}
+
+	safe_delete(errnum);
 	MDatabase.unlock();
 }