Skip to content

Commit 483f917

Browse files
committed
fix: round networking rates to nearest tick interval on boundaries
This fixes an alignment issue with tickrate vs. networking rate. Now, if our networking rate is within 1 unit of a tick interval, we just jump to that tick interval. This makes for slightly faster/slower networking packet rates inaccurate to the set integer rate, but the difference is miniscule and provides many benefits for synchronization purposes. In CS:GO, the tickrate was changed to a value expressable by an integer (64), this is the next best thing.
1 parent 1a3d8b3 commit 483f917

File tree

8 files changed

+46
-17
lines changed

8 files changed

+46
-17
lines changed

src/engine/baseclient.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,18 @@ void CBaseClient::SetUserCVar( const char *pchCvar, const char *value)
201201

202202
void CBaseClient::SetUpdateRate(int udpaterate, bool bForce)
203203
{
204-
udpaterate = clamp( udpaterate, 1.0f, 133.0f );
204+
udpaterate = clamp( udpaterate, 1, 133 );
205205

206-
m_fSnapshotInterval = MAX( 1.0f / udpaterate, host_state.interval_per_tick );
206+
float fSnapshotInterval = MAX( 1.0f / udpaterate, host_state.interval_per_tick );
207+
float fTickInterval = host_state.interval_per_tick * ( TIME_TO_TICKS( fSnapshotInterval ) );
208+
209+
// If we're near a tick interval, round to it (since we're clamped to int)
210+
if ( fSnapshotInterval - fTickInterval < 1.0f )
211+
{
212+
fSnapshotInterval = fTickInterval;
213+
}
214+
215+
m_fSnapshotInterval = fSnapshotInterval;
207216
}
208217

209218
int CBaseClient::GetUpdateRate(void) const
@@ -1431,7 +1440,7 @@ void CBaseClient::UpdateUserSettings()
14311440
SetRate( rate, false );
14321441

14331442
// set server to client update rate
1434-
SetUpdateRate( m_ConVars->GetInt( "cl_updaterate", 33), false );
1443+
SetUpdateRate( m_ConVars->GetInt( "cl_updaterate", 66), false );
14351444

14361445
SetMaxRoutablePayloadSize( m_ConVars->GetInt( "net_maxroutable", MAX_ROUTABLE_PAYLOAD ) );
14371446

src/engine/baseserver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,8 +2083,8 @@ CBaseClient *CBaseServer::CreateFakeClient( const char *name )
20832083

20842084
// fake some cvar settings
20852085
//fakeclient->SetUserCVar( "name", name ); // set already by Connect()
2086-
fakeclient->SetUserCVar( "rate", "30000" );
2087-
fakeclient->SetUserCVar( "cl_updaterate", "33" );
2086+
fakeclient->SetUserCVar( "rate", va( "%d", DEFAULT_RATE ) );
2087+
fakeclient->SetUserCVar( "cl_updaterate", "66" );
20882088
fakeclient->SetUserCVar( "cl_interp_ratio", "1.0" );
20892089
fakeclient->SetUserCVar( "cl_interp", "0.1" );
20902090
fakeclient->SetUserCVar( "cl_interpolate", "0" );

src/engine/cl_bounded_cvars.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,17 @@ class CBoundedCvar_CmdRate : public ConVar_ServerBounded
9595
}
9696

9797
// Then we clamp to the min/max values the server has set.
98-
return clamp( flCmdRate, sv_mincmdrate.GetFloat(), sv_maxcmdrate.GetFloat() );
98+
float fCmdRate = clamp( flCmdRate, sv_mincmdrate.GetFloat(), sv_maxcmdrate.GetFloat() );
99+
// If we're near a tick interval, round to it (since we're clamped to int)
100+
float fSnapshotInterval = 1.0f / fCmdRate;
101+
float fTickInterval = host_state.interval_per_tick * ( TIME_TO_TICKS( fSnapshotInterval ) );
102+
103+
if ( fSnapshotInterval - fTickInterval < 1.0f )
104+
{
105+
fSnapshotInterval = fTickInterval;
106+
}
107+
108+
return 1.0f / fSnapshotInterval;
99109
}
100110
else
101111
{
@@ -119,7 +129,7 @@ class CBoundedCvar_UpdateRate : public ConVar_ServerBounded
119129
CBoundedCvar_UpdateRate() :
120130
ConVar_ServerBounded(
121131
"cl_updaterate",
122-
"33",
132+
"66",
123133
FCVAR_ARCHIVE | FCVAR_USERINFO | FCVAR_NOT_CONNECTED,
124134
"Number of packets per second of updates you are requesting from the server" )
125135
{
@@ -132,7 +142,17 @@ class CBoundedCvar_UpdateRate : public ConVar_ServerBounded
132142
// This cvar only takes effect on the server anyway, and this is done there too,
133143
// but we have this here so they'll get the **note thing telling them the value
134144
// isn't functioning the way they set it.
135-
return clamp( GetBaseFloatValue(), sv_minupdaterate.GetFloat(), sv_maxupdaterate.GetFloat() );
145+
float fUpdateRate = clamp( GetBaseFloatValue(), sv_minupdaterate.GetFloat(), sv_maxupdaterate.GetFloat() );
146+
// If we're near a tick interval, round to it (since we're clamped to int)
147+
float fSnapshotInterval = 1.0f / fUpdateRate;
148+
float fTickInterval = host_state.interval_per_tick * ( TIME_TO_TICKS( fSnapshotInterval ) );
149+
150+
if ( fSnapshotInterval - fTickInterval < 1.0f )
151+
{
152+
fSnapshotInterval = fTickInterval;
153+
}
154+
155+
return 1.0f / fSnapshotInterval;
136156
}
137157
};
138158

src/engine/hltvserver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,8 @@ void CHLTVServer::StartMaster(CGameClient *client)
531531
// set default user settings
532532
m_MasterClient->m_ConVars->SetString( "name", tv_name.GetString() );
533533
m_MasterClient->m_ConVars->SetString( "cl_team", "1" );
534-
m_MasterClient->m_ConVars->SetString( "rate", "30000" );
535-
m_MasterClient->m_ConVars->SetString( "cl_updaterate", "22" );
534+
m_MasterClient->m_ConVars->SetString( "rate", va( "%d", DEFAULT_RATE ) );
535+
m_MasterClient->m_ConVars->SetString( "cl_updaterate", "33" ); // this may not be necessary...
536536
m_MasterClient->m_ConVars->SetString( "cl_interp_ratio", "1.0" );
537537
m_MasterClient->m_ConVars->SetString( "cl_predict", "0" );
538538

src/engine/replayserver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,8 @@ void CReplayServer::StartMaster(CGameClient *client)
471471
ConVarRef replay_name( "replay_name" );
472472
m_MasterClient->m_ConVars->SetString( "name", replay_name.GetString() );
473473
m_MasterClient->m_ConVars->SetString( "cl_team", "1" );
474-
m_MasterClient->m_ConVars->SetString( "rate", "30000" );
475-
m_MasterClient->m_ConVars->SetString( "cl_updaterate", "22" );
474+
m_MasterClient->m_ConVars->SetString( "rate", va( "%d", DEFAULT_RATE ));
475+
m_MasterClient->m_ConVars->SetString( "cl_updaterate", "33" );
476476
m_MasterClient->m_ConVars->SetString( "cl_interp_ratio", "1.0" );
477477
m_MasterClient->m_ConVars->SetString( "cl_predict", "0" );
478478

src/engine/sv_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static ConVar sv_timeout( "sv_timeout", "65", 0, "After this many seconds withou
4141
static ConVar sv_maxrate( "sv_maxrate", "0", FCVAR_REPLICATED, "Max bandwidth rate allowed on server, 0 == unlimited" );
4242
static ConVar sv_minrate( "sv_minrate", "80000", FCVAR_REPLICATED, "Min bandwidth rate allowed on server, 0 == unlimited" );
4343

44-
ConVar sv_maxupdaterate( "sv_maxupdaterate", "66", FCVAR_REPLICATED, "Maximum updates per second that the server will allow" );
44+
ConVar sv_maxupdaterate( "sv_maxupdaterate", "66.67", FCVAR_REPLICATED, "Maximum updates per second that the server will allow" );
4545
ConVar sv_minupdaterate( "sv_minupdaterate", "10", FCVAR_REPLICATED, "Minimum updates per second that the server will allow" );
4646

4747
ConVar sv_stressbots("sv_stressbots", "0", FCVAR_DEVELOPMENTONLY, "If set to 1, the server calculates data and fills packets to bots. Used for perf testing.");

src/game/client/cdll_bounded_cvars.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ class CBoundedCvar_Interp : public ConVar_ServerBounded
9999
public:
100100
CBoundedCvar_Interp() :
101101
ConVar_ServerBounded( "cl_interp",
102-
"0.1",
102+
"0.0",
103103
FCVAR_USERINFO | FCVAR_NOT_CONNECTED | FCVAR_ARCHIVE,
104104
"Sets the interpolation amount (bounded by server interp ratio settings).", true, 0.0f, true, 0.2f )
105105
{
106106
}
107107

108108
virtual float GetFloat() const
109109
{
110-
static const ConVar *pUpdateRate = g_pCVar->FindVar( "cl_updaterate" );
110+
static const ConVar_ServerBounded *pUpdateRate = static_cast<const ConVar_ServerBounded*>( g_pCVar->FindVar( "cl_updaterate" ) );
111111
static const ConVar *pMin = g_pCVar->FindVar( "sv_client_min_interp_ratio" );
112112
static const ConVar *pMax = g_pCVar->FindVar( "sv_client_max_interp_ratio" );
113113
if ( pUpdateRate && pMin && pMin->GetFloat() != -1 && pMax )
@@ -126,7 +126,7 @@ ConVar_ServerBounded *cl_interp = &cl_interp_var;
126126

127127
float GetClientInterpAmount()
128128
{
129-
static const ConVar *pUpdateRate = g_pCVar->FindVar( "cl_updaterate" );
129+
static const ConVar_ServerBounded *pUpdateRate = static_cast<const ConVar_ServerBounded*>( g_pCVar->FindVar( "cl_updaterate" ) );
130130
if ( pUpdateRate )
131131
{
132132
// #define FIXME_INTERP_RATIO

src/game/server/player.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ CBasePlayer::CBasePlayer( )
593593

594594
m_hZoomOwner = NULL;
595595

596-
m_nUpdateRate = 33; // cl_updaterate defualt
596+
m_nUpdateRate = 66; // cl_updaterate defualt
597597
m_fLerpTime = 0.1f; // cl_interp default
598598
m_bPredictWeapons = true;
599599
m_bLagCompensation = false;

0 commit comments

Comments
 (0)