engine: server: move master announce logic to masterlist, keep unique heartbeat challenge and heartbeat timer for each master

This commit is contained in:
Alibek Omarov
2023-04-03 00:57:47 +03:00
parent 93a7ccd14f
commit 01e0542223
7 changed files with 195 additions and 117 deletions

View File

@@ -374,9 +374,7 @@ typedef struct
entity_state_t *baselines; // [GI->max_edicts]
entity_state_t *static_entities; // [MAX_STATIC_ENTITIES];
double last_heartbeat;
challenge_t challenges[MAX_CHALLENGES]; // to prevent invalid IPs from connecting
uint heartbeat_challenge;
} server_static_t;
//=============================================================================
@@ -444,6 +442,7 @@ extern convar_t skill;
extern convar_t coop;
extern convar_t sv_cheats;
extern convar_t public_server;
extern convar_t sv_nat;
extern convar_t *sv_pausable; // allows pause in multiplayer
extern convar_t *sv_check_errors;

View File

@@ -475,7 +475,7 @@ void SV_ConnectClient( netadr_t from )
Log_Printf( "\"%s<%i><%i><>\" connected, address \"%s\"\n", newcl->name, newcl->userid, i, NET_AdrToString( newcl->netchan.remote_address ));
if( count == 1 || count == svs.maxclients )
svs.last_heartbeat = MAX_HEARTBEAT;
NET_MasterClear();
}
/*
@@ -542,7 +542,7 @@ edict_t *SV_FakeConnect( const char *netname )
cl->state = cs_spawned;
if( count == 1 || count == svs.maxclients )
svs.last_heartbeat = MAX_HEARTBEAT;
NET_MasterClear();
return cl->edict;
}
@@ -619,7 +619,7 @@ void SV_DropClient( sv_client_t *cl, qboolean crash )
}
if( i == svs.maxclients )
svs.last_heartbeat = MAX_HEARTBEAT;
NET_MasterClear();
}
/*
@@ -3116,7 +3116,7 @@ void SV_ConnectionlessPacket( netadr_t from, sizebuf_t *msg )
else if( !Q_strcmp( pcmd, "s" )) SV_AddToMaster( from, msg );
else if( !Q_strcmp( pcmd, "T" "Source" )) SV_TSourceEngineQuery( from );
else if( !Q_strcmp( pcmd, "i" )) NET_SendPacket( NS_SERVER, 5, "\xFF\xFF\xFF\xFFj", from ); // A2A_PING
else if( !Q_strcmp( pcmd, "c" ) && Cvar_VariableInteger( "sv_nat" ) && NET_IsMasterAdr( from ))
else if( !Q_strcmp( pcmd, "c" ) && sv_nat.value && NET_IsMasterAdr( from ))
{
netadr_t to;
if( NET_StringToAdr( Cmd_Argv( 1 ), &to ) && !NET_IsReservedAdr( to ))

View File

@@ -749,9 +749,9 @@ void SV_ConSay_f( void )
SV_Heartbeat_f
==================
*/
void SV_Heartbeat_f( void )
static void SV_Heartbeat_f( void )
{
svs.last_heartbeat = MAX_HEARTBEAT;
NET_MasterClear();
}
/*

View File

@@ -1012,7 +1012,7 @@ qboolean SV_SpawnServer( const char *mapname, const char *startspot, qboolean ba
}
// heartbeats will always be sent to the id master
svs.last_heartbeat = MAX_HEARTBEAT; // send immediately
NET_MasterClear();
// get actual movevars
SV_UpdateMovevars( true );

View File

@@ -18,8 +18,6 @@ GNU General Public License for more details.
#include "net_encode.h"
#include "platform/platform.h"
#define HEARTBEAT_SECONDS ((sv_nat.value > 0.0f) ? 60.0f : 300.0f) // 1 or 5 minutes
// server cvars
CVAR_DEFINE_AUTO( sv_lan, "0", 0, "server is a lan server ( no heartbeat, no authentication, no non-class C addresses, 9999.0 rate, etc." );
CVAR_DEFINE_AUTO( sv_lan_rate, "20000.0", 0, "rate for lan server" );
@@ -142,8 +140,6 @@ convar_t *sv_allow_mouse;
convar_t *sv_allow_joystick;
convar_t *sv_allow_vr;
static void Master_Heartbeat( void );
//============================================================================
/*
================
@@ -668,7 +664,7 @@ void Host_ServerFrame( void )
Platform_UpdateStatusLine ();
// send a heartbeat to the master if needed
Master_Heartbeat ();
NET_MasterHeartbeat ();
}
/*
@@ -683,68 +679,6 @@ void Host_SetServerState( int state )
}
//============================================================================
/*
=================
Master_Add
=================
*/
static void Master_Add( void )
{
sizebuf_t msg;
char buf[16];
uint challenge;
NET_Config( true, false ); // allow remote
svs.heartbeat_challenge = challenge = COM_RandomLong( 0, INT_MAX );
MSG_Init( &msg, "Master Join", buf, sizeof( buf ));
MSG_WriteBytes( &msg, "q\xFF", 2 );
MSG_WriteDword( &msg, challenge );
if( NET_SendToMasters( NS_SERVER, MSG_GetNumBytesWritten( &msg ), MSG_GetBuf( &msg )))
svs.last_heartbeat = MAX_HEARTBEAT;
}
/*
================
Master_Heartbeat
Send a message to the master every few minutes to
let it know we are alive, and log information
================
*/
static void Master_Heartbeat( void )
{
if(( !public_server.value && !sv_nat.value ) || svs.maxclients == 1 )
return; // only public servers send heartbeats
// check for time wraparound
if( svs.last_heartbeat > host.realtime )
svs.last_heartbeat = host.realtime;
if(( host.realtime - svs.last_heartbeat ) < HEARTBEAT_SECONDS )
return; // not time to send yet
svs.last_heartbeat = host.realtime;
Master_Add();
}
/*
=================
Master_Shutdown
Informs all masters that this server is going down
=================
*/
static void Master_Shutdown( void )
{
NET_Config( true, false ); // allow remote
while( NET_SendToMasters( NS_SERVER, 2, "\x62\x0A" ));
}
/*
=================
SV_AddToMaster
@@ -755,18 +689,19 @@ Master will validate challenge and this server to public list
*/
void SV_AddToMaster( netadr_t from, sizebuf_t *msg )
{
uint challenge, challenge2;
uint challenge, challenge2, heartbeat_challenge;
char s[MAX_INFO_STRING] = "0\n"; // skip 2 bytes of header
int clients, bots;
double last_heartbeat;
const int len = sizeof( s );
if( !NET_IsMasterAdr( from ))
if( !NET_GetMaster( from, &heartbeat_challenge, &last_heartbeat ))
{
Con_Printf( S_WARN "unexpected master server info query packet from %s\n", NET_AdrToString( from ));
return;
}
if( svs.last_heartbeat + sv_master_response_timeout.value < host.realtime )
if( last_heartbeat + sv_master_response_timeout.value < host.realtime )
{
Con_Printf( S_WARN "unexpected master server info query packet (too late? try increasing sv_master_response_timeout value)\n");
return;
@@ -775,7 +710,7 @@ void SV_AddToMaster( netadr_t from, sizebuf_t *msg )
challenge = MSG_ReadDword( msg );
challenge2 = MSG_ReadDword( msg );
if( challenge2 != svs.heartbeat_challenge )
if( challenge2 != heartbeat_challenge )
{
Con_Printf( S_WARN "unexpected master server info query packet (wrong challenge!)\n" );
return;
@@ -1114,7 +1049,7 @@ void SV_Shutdown( const char *finalmsg )
SV_FinalMessage( finalmsg, false );
if( public_server.value && svs.maxclients != 1 )
Master_Shutdown();
NET_MasterShutdown();
NET_Config( false, false );
SV_UnloadProgs ();