Engine: initiate rcon connection from address change callback

A more user friendly approach.
This commit is contained in:
Kawe Mazidjatari 2024-04-10 16:23:48 +02:00
parent e5199d8253
commit b019307947
2 changed files with 18 additions and 17 deletions

View File

@ -29,7 +29,7 @@ static ConVar cl_rcon_inputonly("cl_rcon_inputonly", "0", FCVAR_RELEASE, "Tells
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void RCON_CmdQuery_f(const CCommand& args); static void RCON_CmdQuery_f(const CCommand& args);
static ConCommand rcon("rcon", RCON_CmdQuery_f, "Forward RCON query to remote server", FCVAR_CLIENTDLL | FCVAR_RELEASE, nullptr, "rcon \"<query>\""); static ConCommand rcon("rcon", RCON_CmdQuery_f, "Forward RCON message to remote server", FCVAR_CLIENTDLL | FCVAR_RELEASE, nullptr, "rcon \"<message>\"");
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Purpose: // Purpose:
@ -249,7 +249,9 @@ CRConClient* RCONClient() // Singleton RCON Client.
===================== =====================
RCON_AddressChanged_f RCON_AddressChanged_f
changes the address of the rcon
server and attempts to connect
to it
===================== =====================
*/ */
static void RCON_AddressChanged_f(IConVar* pConVar, const char* pOldString) static void RCON_AddressChanged_f(IConVar* pConVar, const char* pOldString)
@ -258,11 +260,6 @@ static void RCON_AddressChanged_f(IConVar* pConVar, const char* pOldString)
{ {
const char* pNewString = pConVarRef->GetString(); const char* pNewString = pConVarRef->GetString();
if (strcmp(pOldString, pNewString) == NULL)
{
return; // Same address.
}
if (!*pNewString) // Empty address means nothing to network to; shutdown client... if (!*pNewString) // Empty address means nothing to network to; shutdown client...
{ {
RCONClient()->Shutdown(); RCONClient()->Shutdown();
@ -270,6 +267,16 @@ static void RCON_AddressChanged_f(IConVar* pConVar, const char* pOldString)
else else
{ {
RCON_InitClientAndTrySyncKeys(); RCON_InitClientAndTrySyncKeys();
if (RCONClient()->IsInitialized() && !RCONClient()->IsConnected())
{
if (RCONClient()->IsConnected())
{
RCONClient()->Disconnect("address change requested");
}
RCONClient()->Connect(pNewString);
}
} }
} }
} }
@ -302,14 +309,8 @@ static void RCON_CmdQuery_f(const CCommand& args)
if (argCount < 2) if (argCount < 2)
{ {
const char* pszAddress = cl_rcon_address.GetString(); Warning(eDLL_T::CLIENT, "Failed to issue command to RCON server: %s\n", "no command provided");
return;
if (RCONClient()->IsInitialized()
&& !RCONClient()->IsConnected()
&& pszAddress[0])
{
RCONClient()->Connect(pszAddress);
}
} }
else else
{ {
@ -332,7 +333,7 @@ static void RCON_CmdQuery_f(const CCommand& args)
} }
else // Need at least 3 arguments for a password in PASS command (rcon PASS <password>) else // Need at least 3 arguments for a password in PASS command (rcon PASS <password>)
{ {
Warning(eDLL_T::CLIENT, "Failed to issue command to RCON server: %s\n", "no password given"); Warning(eDLL_T::CLIENT, "Failed to issue command to RCON server: %s\n", "no password provided");
return; return;
} }

View File

@ -81,7 +81,7 @@ bool CL_NetConConnect(CNetConBase* pBase, const char* pHostAdr, const int nHostP
string svLocalHost; string svLocalHost;
const bool bValidSocket = nHostPort != SOCKET_ERROR; const bool bValidSocket = nHostPort != SOCKET_ERROR;
if (bValidSocket && strcmp(pHostAdr, "localhost") == 0) if (bValidSocket && (strcmp(pHostAdr, "localhost") == 0))
{ {
char szHostName[512]; char szHostName[512];
if (!gethostname(szHostName, sizeof(szHostName))) if (!gethostname(szHostName, sizeof(szHostName)))