Skip to content

Commit

Permalink
Fixed connection checking in SB_Connect
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr D committed Jan 25, 2014
1 parent 805cf1f commit 31612fd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Binary file modified game/addons/sourcemod/plugins/sourcebans.smx
Binary file not shown.
34 changes: 34 additions & 0 deletions game/addons/sourcemod/scripting/sourcebans.sp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ enum DatabaseState
DatabaseState_Connecting,
DatabaseState_Connected
}
enum CheckingState
{
CheckingState_None = 0,
CheckingState_Active
}

new ConfigState:g_iConfigState;
new g_iConnectLock = 0;
new DatabaseState:g_iDatabaseState;
new CheckingState:g_iCheckingState;
new g_iSequence = 0;
new g_iServerPort;
new Handle:g_hConfig;
Expand Down Expand Up @@ -267,6 +273,8 @@ public Query_ServerInsert(Handle:owner, Handle:hndl, const String:error[], any:d

public Query_ExecuteCallback(Handle:owner, Handle:hndl, const String:error[], any:pack)
{
CheckConnection(hndl);

ResetPack(pack);
new Handle:plugin = Handle:ReadPackCell(pack);
new SQLTCallback:callback = SQLTCallback:ReadPackCell(pack);
Expand All @@ -283,10 +291,23 @@ public Query_ExecuteCallback(Handle:owner, Handle:hndl, const String:error[], an

public Query_ErrorCheck(Handle:owner, Handle:hndl, const String:error[], any:data)
{
CheckConnection(hndl);

if(error[0])
LogError("%T (%s)", "Failed to query database", LANG_SERVER, error);
}

public Query_PingDBCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if(!hndl || error[0])
{
LogError("Lost connection to database");
CloseHandle(g_hDatabase);
g_hDatabase = INVALID_HANDLE;
g_iDatabaseState = DatabaseState_None;
}
g_iCheckingState = CheckingState_None;
}

/**
* Connect Callback
Expand Down Expand Up @@ -515,3 +536,16 @@ ExecuteQuery(SQLTCallback:callback, String:sQuery[4096], any:data = 0, DBPriorit

SQL_TQuery(g_hDatabase, callback, sQuery, data, prio);
}

CheckConnection(Handle:hndl)
{
if(hndl == INVALID_HANDLE && g_iDatabaseState == DatabaseState_Connected)
{
// Possible we lost connection to db. Also it could be an SQL-error.
if(g_iCheckingState == CheckingState_None)
{
g_iCheckingState = CheckingState_Active;
SQL_TQuery(g_hDatabase, Query_PingDBCallback, "SELECT 1", _, DBPrio_High);
}
}
}

0 comments on commit 31612fd

Please sign in to comment.