summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/core.c7
-rw-r--r--src/common/mapindex.c9
-rw-r--r--src/common/sql.c79
-rw-r--r--src/common/sql.h6
4 files changed, 90 insertions, 11 deletions
diff --git a/src/common/core.c b/src/common/core.c
index 1e3dbb3d7..b1714b7a5 100644
--- a/src/common/core.c
+++ b/src/common/core.c
@@ -1,5 +1,6 @@
-// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
-// For more information, see LICENCE in the main folder
+// Copyright (c) Hercules dev team, licensed under GNU GPL.
+// See the LICENSE file
+// Portions Copyright (c) Athena dev team
#include "../common/mmo.h"
#include "../common/showmsg.h"
@@ -11,6 +12,7 @@
#include "../common/timer.h"
#include "../common/thread.h"
#include "../common/mempool.h"
+#include "../common/sql.h"
#endif
#include <stdio.h>
@@ -319,6 +321,7 @@ int main (int argc, char **argv)
display_title();
usercheck();
+ Sql_Init();
rathread_init();
mempool_init();
db_init();
diff --git a/src/common/mapindex.c b/src/common/mapindex.c
index d46047833..cea945ac0 100644
--- a/src/common/mapindex.c
+++ b/src/common/mapindex.c
@@ -1,5 +1,6 @@
-// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
-// For more information, see LICENCE in the main folder
+// Copyright (c) Hercules dev team, licensed under GNU GPL.
+// See the LICENSE file
+// Portions Copyright (c) Athena dev team
#include "../common/mmo.h"
#include "../common/showmsg.h"
@@ -37,7 +38,7 @@ const char* mapindex_getmapname(const char* string, char* output)
len -= 4; // strip .gat extension
len = min(len, MAP_NAME_LENGTH-1);
- strncpy(dest, string, len+1);
+ safestrncpy(dest, string, len+1);
memset(&dest[len], '\0', MAP_NAME_LENGTH-len);
return dest;
@@ -61,7 +62,7 @@ const char* mapindex_getmapname_ext(const char* string, char* output)
ShowWarning("(mapindex_normalize_name) Map name '%*s' is too long!\n", 2*MAP_NAME_LENGTH, buf);
len--;
}
- strncpy(dest, buf, len+1);
+ safestrncpy(dest, buf, len+1);
if (len < 4 || stricmp(&dest[len-4], ".gat") != 0) {
strcpy(&dest[len], ".gat");
diff --git a/src/common/sql.c b/src/common/sql.c
index 800aa89b0..73c4181d9 100644
--- a/src/common/sql.c
+++ b/src/common/sql.c
@@ -1,5 +1,6 @@
-// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
-// For more information, see LICENCE in the main folder
+// Copyright (c) Hercules dev team, licensed under GNU GPL.
+// See the LICENSE file
+// Portions Copyright (c) Athena dev team
#include "../common/cbasetypes.h"
#include "../common/malloc.h"
@@ -15,7 +16,10 @@
#include <string.h>// strlen/strnlen/memcpy/memset
#include <stdlib.h>// strtoul
+void hercules_mysql_error_handler(unsigned int ecode);
+int mysql_reconnect_type;
+unsigned int mysql_reconnect_count;
/// Sql handle
struct Sql
@@ -74,7 +78,7 @@ Sql* Sql_Malloc(void)
self->lengths = NULL;
self->result = NULL;
self->keepalive = INVALID_TIMER;
-
+ self->handle.reconnect = 1;
return self;
}
@@ -266,12 +270,14 @@ int Sql_QueryV(Sql* self, const char* query, va_list args)
if( mysql_real_query(&self->handle, StringBuf_Value(&self->buf), (unsigned long)StringBuf_Length(&self->buf)) )
{
ShowSQL("DB error - %s\n", mysql_error(&self->handle));
+ hercules_mysql_error_handler(mysql_errno(&self->handle));
return SQL_ERROR;
}
self->result = mysql_store_result(&self->handle);
if( mysql_errno(&self->handle) != 0 )
{
ShowSQL("DB error - %s\n", mysql_error(&self->handle));
+ hercules_mysql_error_handler(mysql_errno(&self->handle));
return SQL_ERROR;
}
return SQL_SUCCESS;
@@ -291,12 +297,14 @@ int Sql_QueryStr(Sql* self, const char* query)
if( mysql_real_query(&self->handle, StringBuf_Value(&self->buf), (unsigned long)StringBuf_Length(&self->buf)) )
{
ShowSQL("DB error - %s\n", mysql_error(&self->handle));
+ hercules_mysql_error_handler(mysql_errno(&self->handle));
return SQL_ERROR;
}
self->result = mysql_store_result(&self->handle);
if( mysql_errno(&self->handle) != 0 )
{
ShowSQL("DB error - %s\n", mysql_error(&self->handle));
+ hercules_mysql_error_handler(mysql_errno(&self->handle));
return SQL_ERROR;
}
return SQL_SUCCESS;
@@ -639,6 +647,7 @@ int SqlStmt_PrepareV(SqlStmt* self, const char* query, va_list args)
if( mysql_stmt_prepare(self->stmt, StringBuf_Value(&self->buf), (unsigned long)StringBuf_Length(&self->buf)) )
{
ShowSQL("DB error - %s\n", mysql_stmt_error(self->stmt));
+ hercules_mysql_error_handler(mysql_stmt_errno(self->stmt));
return SQL_ERROR;
}
self->bind_params = false;
@@ -660,6 +669,7 @@ int SqlStmt_PrepareStr(SqlStmt* self, const char* query)
if( mysql_stmt_prepare(self->stmt, StringBuf_Value(&self->buf), (unsigned long)StringBuf_Length(&self->buf)) )
{
ShowSQL("DB error - %s\n", mysql_stmt_error(self->stmt));
+ hercules_mysql_error_handler(mysql_stmt_errno(self->stmt));
return SQL_ERROR;
}
self->bind_params = false;
@@ -721,12 +731,14 @@ int SqlStmt_Execute(SqlStmt* self)
mysql_stmt_execute(self->stmt) )
{
ShowSQL("DB error - %s\n", mysql_stmt_error(self->stmt));
+ hercules_mysql_error_handler(mysql_stmt_errno(self->stmt));
return SQL_ERROR;
}
self->bind_columns = false;
if( mysql_stmt_store_result(self->stmt) )// store all the data
{
ShowSQL("DB error - %s\n", mysql_stmt_error(self->stmt));
+ hercules_mysql_error_handler(mysql_stmt_errno(self->stmt));
return SQL_ERROR;
}
@@ -868,6 +880,7 @@ int SqlStmt_NextRow(SqlStmt* self)
if( err )
{
ShowSQL("DB error - %s\n", mysql_stmt_error(self->stmt));
+ hercules_mysql_error_handler(mysql_stmt_errno(self->stmt));
return SQL_ERROR;
}
@@ -946,3 +959,63 @@ void SqlStmt_Free(SqlStmt* self)
aFree(self);
}
}
+/* receives mysql error codes during runtime (not on first-time-connects) */
+void hercules_mysql_error_handler(unsigned int ecode) {
+ static unsigned int retry = 1;
+ switch( ecode ) {
+ case 2003:/* Can't connect to MySQL (this error only happens here when failing to reconnect) */
+ if( mysql_reconnect_type == 1 ) {
+ if( ++retry > mysql_reconnect_count ) {
+ ShowFatalError("MySQL has been unreachable for too long, %d reconnects were attempted. Shutting Down\n", retry);
+ exit(EXIT_FAILURE);
+ }
+ }
+ break;
+ }
+}
+void Sql_inter_server_read(const char* cfgName, bool first) {
+ int i;
+ char line[1024], w1[1024], w2[1024];
+ FILE* fp;
+
+ fp = fopen(cfgName, "r");
+ if(fp == NULL) {
+ if( first ) {
+ ShowFatalError("File not found: %s\n", cfgName);
+ exit(EXIT_FAILURE);
+ } else
+ ShowError("File not found: %s\n", cfgName);
+ return;
+ }
+
+ while(fgets(line, sizeof(line), fp)) {
+ i = sscanf(line, "%[^:]: %[^\r\n]", w1, w2);
+ if(i != 2)
+ continue;
+
+ if(!strcmpi(w1,"mysql_reconnect_type")) {
+ mysql_reconnect_type = atoi(w2);
+ switch( mysql_reconnect_type ) {
+ case 1:
+ case 2:
+ break;
+ default:
+ ShowError("%s::mysql_reconnect_type is set to %d which is not valid, defaulting to 1...\n", cfgName, mysql_reconnect_type);
+ mysql_reconnect_type = 1;
+ break;
+ }
+ } else if(!strcmpi(w1,"mysql_reconnect_count")) {
+ mysql_reconnect_count = atoi(w2);
+ if( mysql_reconnect_count < 1 )
+ mysql_reconnect_count = 1;
+ } else if(!strcmpi(w1,"import"))
+ Sql_inter_server_read(w2,false);
+ }
+ fclose(fp);
+
+ return;
+}
+
+void Sql_Init(void) {
+ Sql_inter_server_read("conf/inter-server.conf",true);
+}
diff --git a/src/common/sql.h b/src/common/sql.h
index 898e2c778..5ac92665e 100644
--- a/src/common/sql.h
+++ b/src/common/sql.h
@@ -1,5 +1,6 @@
-// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
-// For more information, see LICENCE in the main folder
+// Copyright (c) Hercules dev team, licensed under GNU GPL.
+// See the LICENSE file
+// Portions Copyright (c) Athena dev team
#ifndef _COMMON_SQL_H_
#define _COMMON_SQL_H_
@@ -339,6 +340,7 @@ void SqlStmt_ShowDebug_(SqlStmt* self, const char* debug_file, const unsigned lo
/// Frees a SqlStmt returned by SqlStmt_Malloc.
void SqlStmt_Free(SqlStmt* self);
+void Sql_Init(void);
#endif /* _COMMON_SQL_H_ */