@@ -69,7 +69,7 @@ IStateStore stateStore
6969 qualityReportTimer = new ( options . QualityReportInterval ) ;
7070 qualityReportTimer . Elapsed += OnQualityReportTick ;
7171
72- networkStatsTimer = new ( options . NetworkStatsInterval ) ;
72+ networkStatsTimer = new ( options . NetworkPackageStatsInterval ) ;
7373 networkStatsTimer . Elapsed += OnNetworkStatsTick ;
7474
7575 consistencyCheckTimer = new ( options . ConsistencyCheckInterval ) ;
@@ -92,21 +92,25 @@ public void Dispose()
9292 qualityReportTimer . Elapsed -= OnQualityReportTick ;
9393 networkStatsTimer . Elapsed -= OnNetworkStatsTick ;
9494 consistencyCheckTimer . Elapsed -= OnConsistencyCheck ;
95- networkStatsTimer . Dispose ( ) ;
96- qualityReportTimer . Dispose ( ) ;
95+
9796 keepAliveTimer . Dispose ( ) ;
9897 resendInputsTimer . Dispose ( ) ;
98+ qualityReportTimer . Dispose ( ) ;
99+ networkStatsTimer . Dispose ( ) ;
99100 consistencyCheckTimer . Dispose ( ) ;
100101 }
101102
102103 void StartTimers ( )
103104 {
104105 keepAliveTimer . Start ( ) ;
105106 qualityReportTimer . Start ( ) ;
106- networkStatsTimer . Start ( ) ;
107107 resendInputsTimer . Start ( ) ;
108108
109+ if ( options . NetworkPackageStatsEnabled )
110+ networkStatsTimer . Start ( ) ;
111+
109112 if ( state . Player . IsSpectator ( ) ) return ;
113+
110114 if ( options . ConsistencyCheckEnabled )
111115 consistencyCheckTimer . Start ( ) ;
112116 }
@@ -115,10 +119,13 @@ void StopTimers()
115119 {
116120 keepAliveTimer . Stop ( ) ;
117121 qualityReportTimer . Stop ( ) ;
118- networkStatsTimer . Stop ( ) ;
119122 resendInputsTimer . Stop ( ) ;
120123
124+ if ( options . NetworkPackageStatsEnabled )
125+ networkStatsTimer . Stop ( ) ;
126+
121127 if ( state . Player . IsSpectator ( ) ) return ;
128+
122129 if ( options . ConsistencyCheckEnabled )
123130 consistencyCheckTimer . Stop ( ) ;
124131 }
@@ -153,6 +160,8 @@ public void Update()
153160 break ;
154161 case ProtocolStatus . Running :
155162 CheckDisconnection ( ) ;
163+ if ( options . CalculateRemotePlayerStats )
164+ GetNetworkStats ( ref state . Player . NetworkStats ) ;
156165 break ;
157166 case ProtocolStatus . Disconnected :
158167 break ;
@@ -209,30 +218,25 @@ public void GetNetworkStats(ref PeerNetworkStats info)
209218 info . LastAckedFrame = inbox . LastAckedFrame ;
210219 info . RemoteFramesBehind = state . Fairness . RemoteFrameAdvantage ;
211220 info . LocalFramesBehind = state . Fairness . LocalFrameAdvantage ;
212- info . Send . TotalBytes = stats . Send . TotalBytesWithHeaders ;
213- info . Send . Count = stats . Send . TotalPackets ;
214- info . Send . LastTime = Stopwatch . GetElapsedTime ( stats . Send . LastTime ) ;
215- info . Send . PackagesPerSecond = stats . Send . PackagesPerSecond ;
216- info . Send . Bandwidth = stats . Send . Bandwidth ;
217221 info . Send . LastFrame = inputBuffer . LastSent . Frame ;
218- info . Received . TotalBytes = stats . Received . TotalBytesWithHeaders ;
219- info . Received . LastTime = Stopwatch . GetElapsedTime ( stats . Received . LastTime ) ;
220- info . Received . Count = stats . Received . TotalPackets ;
221- info . Received . PackagesPerSecond = stats . Received . PackagesPerSecond ;
222- info . Received . Bandwidth = stats . Received . Bandwidth ;
223222 info . Received . LastFrame = inbox . LastReceivedInput . Frame ;
223+
224+ if ( ! options . NetworkPackageStatsEnabled ) return ;
225+ info . Send . Fill ( stats . Send ) ;
226+ info . Received . Fill ( stats . Received ) ;
224227 }
225228
226229 public bool GetPeerConnectStatus ( int id , out Frame frame )
227230 {
228- frame = state . PeerConnectStatuses [ id ] . LastFrame ;
229- return ! state . PeerConnectStatuses [ id ] . Disconnected ;
231+ var peer = state . PeerConnectStatuses [ id ] ;
232+ frame = peer . LastFrame ;
233+ return ! peer . Disconnected ;
230234 }
231235
232236 public IPeerObserver < ProtocolMessage > GetUdpObserver ( ) => inbox ;
233237 public void Synchronize ( ) => syncRequest . Synchronize ( ) ;
234238
235- public void CheckDisconnection ( )
239+ void CheckDisconnection ( )
236240 {
237241 if ( state . Stats . Received . LastTime <= 0 || ! disconnectCheckEnabled ) return ;
238242 var lastReceivedTime = Stopwatch . GetElapsedTime ( state . Stats . Received . LastTime ) ;
@@ -339,20 +343,13 @@ void OnQualityReportTick(object? sender, ElapsedEventArgs e)
339343 void OnNetworkStatsTick ( object ? sender , ElapsedEventArgs e )
340344 {
341345 const int packageHeaderSize = UdpSocket . UdpHeaderSize + UdpSocket . IpAddressHeaderSize ;
342- if ( state . CurrentStatus is not ProtocolStatus . Running )
346+ if ( state . CurrentStatus is not ProtocolStatus . Running || ! options . NetworkPackageStatsEnabled )
343347 return ;
344348
345349 var elapsed = Stopwatch . GetElapsedTime ( startedAt ) ;
346- var seconds = elapsed . TotalSeconds ;
347350 UpdateStats ( ref state . Stats . Send ) ;
348351 UpdateStats ( ref state . Stats . Received ) ;
349352
350- if ( options . LogNetworkStats )
351- {
352- logger . Write ( LogLevel . Information , $ "Network Stats(send): { state . Stats . Send } ") ;
353- logger . Write ( LogLevel . Information , $ "Network Stats(recv): { state . Stats . Received } ") ;
354- }
355-
356353 return ;
357354
358355 void UpdateStats ( ref ProtocolState . PackagesStats stats )
@@ -361,7 +358,7 @@ void UpdateStats(ref ProtocolState.PackagesStats stats)
361358 stats . TotalBytesWithHeaders = stats . TotalBytes + totalUdpHeaderSize ;
362359 stats . TotalBytesWithHeaders = stats . TotalBytes + totalUdpHeaderSize ;
363360 stats . PackagesPerSecond = ( float ) ( stats . TotalPackets * 1000f / elapsed . TotalMilliseconds ) ;
364- stats . Bandwidth = stats . TotalBytesWithHeaders / seconds ;
361+ stats . Bandwidth = stats . TotalBytesWithHeaders / elapsed . TotalSeconds ;
365362 stats . UdpOverhead =
366363 ( float ) ( 100.0 * ( packageHeaderSize * stats . TotalPackets ) / stats . TotalBytes . ByteCount ) ;
367364 }
0 commit comments