From c1d3608c895a30a2627da1f44d984e99995afa00 Mon Sep 17 00:00:00 2001 From: Fletcher Dunn Date: Thu, 24 Jul 2025 09:38:11 -0700 Subject: [PATCH] Add a way to read jitter in SteamNetConnectionRealTimeStatus_t --- include/steam/steamnetworkingtypes.h | 11 +++++++++- .../steamnetworkingsockets_connections.cpp | 4 ++++ .../steamnetworking_statsutils.h | 20 +++++++++++++++++++ .../steamnetworkingsockets_stats.cpp | 1 + 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/include/steam/steamnetworkingtypes.h b/include/steam/steamnetworkingtypes.h index 1d252ea..42a53bd 100644 --- a/include/steam/steamnetworkingtypes.h +++ b/include/steam/steamnetworkingtypes.h @@ -801,8 +801,17 @@ struct SteamNetConnectionRealTimeStatus_t /// Nagle delay is ignored for the purposes of this calculation. SteamNetworkingMicroseconds m_usecQueueTime; + /// Highest packet jitter experienced, since the last time this information + /// was returned. (The high water mark is cleared each time you fetch the info.) + /// + /// - The units are microseconds, although the measurement precision is usually + /// not nearly this precise. + /// - A negative value means "no data available". + /// - Not all connections are able to measure jitter. + int32 m_usecMaxJitter; + // Internal stuff, room to change API easily - uint32 reserved[16]; + uint32 reserved[15]; }; /// Quick status of a particular lane diff --git a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.cpp b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.cpp index 061cb7d..cd529e8 100644 --- a/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.cpp +++ b/src/steamnetworkingsockets/clientlib/steamnetworkingsockets_connections.cpp @@ -1953,6 +1953,10 @@ EResult CSteamNetworkConnectionBase::APIGetRealTimeStatus( SteamNetConnectionRea pStatus->m_flOutBytesPerSec = m_statsEndToEnd.m_sent.m_bytes.m_flRate; pStatus->m_flInPacketsPerSec = m_statsEndToEnd.m_recv.m_packets.m_flRate; pStatus->m_flInBytesPerSec = m_statsEndToEnd.m_recv.m_bytes.m_flRate; + + // Max jitter, and clear it + pStatus->m_usecMaxJitter = m_statsEndToEnd.m_usecAPIRealtimeStatusMaxJitter; + m_statsEndToEnd.m_usecAPIRealtimeStatusMaxJitter = -1; } SNP_PopulateRealTimeStatus( pStatus, nLanes, pLanes, usecNow ); diff --git a/src/steamnetworkingsockets/steamnetworking_statsutils.h b/src/steamnetworkingsockets/steamnetworking_statsutils.h index fad7187..2aa2c75 100644 --- a/src/steamnetworkingsockets/steamnetworking_statsutils.h +++ b/src/steamnetworkingsockets/steamnetworking_statsutils.h @@ -1077,6 +1077,10 @@ struct LinkStatsTrackerEndToEnd : public LinkStatsTrackerBase /// Time when the current interval started SteamNetworkingMicroseconds m_usecSpeedIntervalStart; + /// Jitter value that is returned in GetConnectionRealTimeStatus and then + /// cleared. + int m_usecAPIRealtimeStatusMaxJitter; + ///// TX Speed, should match CMsgSteamDatagramLinkLifetimeStats //int m_nTXSpeed; //int m_nTXSpeedMax; @@ -1169,6 +1173,22 @@ protected: } } + inline void InternalProcessJitterSample( int usecJitter ) + { + LinkStatsTrackerBase::InternalProcessJitterSample( usecJitter ); + + // Update user high water mark. + // + // Use absolute value here...I think? + // If one packet is delayed and then the next packet arrives on time, the second + // jitter value will be negative, and both packets will be counted as bad, even + // though only the second one was. (Arriving early usually isn't a problem, it's + // arriving late that's bad). However, for the use cases for this is currently + // intended, it's OK. With any reasonable packet rate, we assume the app + // won't be checking this value fast enough for it to matter. + m_usecAPIRealtimeStatusMaxJitter = std::max( m_usecAPIRealtimeStatusMaxJitter, abs( usecJitter ) ); + } + private: void UpdateSpeedInterval( SteamNetworkingMicroseconds usecNow ); diff --git a/src/steamnetworkingsockets/steamnetworkingsockets_stats.cpp b/src/steamnetworkingsockets/steamnetworkingsockets_stats.cpp index 5e929a1..d8f21c2 100644 --- a/src/steamnetworkingsockets/steamnetworkingsockets_stats.cpp +++ b/src/steamnetworkingsockets/steamnetworkingsockets_stats.cpp @@ -768,6 +768,7 @@ void LinkStatsTrackerEndToEnd::InitInternal( SteamNetworkingMicroseconds usecNow m_usecWhenStartedConnectedState = 0; m_usecWhenEndedConnectedState = 0; + m_usecAPIRealtimeStatusMaxJitter = -1; //m_TXSpeedSample.Clear(); //m_nTXSpeed = 0;