@@ -16,6 +16,35 @@ namespace Unity.Multiplayer.Samples.BossRoom
16
16
[ RequireComponent ( typeof ( NetworkObject ) ) ]
17
17
public class NetworkStats : NetworkBehaviour
18
18
{
19
+ private class MovingWindowAverage
20
+ {
21
+ public float LastRTT { get ; private set ; }
22
+ Queue < float > m_MovingWindow = new Queue < float > ( ) ;
23
+ const int k_MaxWindowSizeSeconds = 3 ; // it should take x seconds for the value to react to change
24
+ float m_MaxWindowSize => k_MaxWindowSizeSeconds / m_PingIntervalSeconds ;
25
+
26
+ public void Add ( float value )
27
+ {
28
+ m_MovingWindow . Enqueue ( value ) ;
29
+ UpdateRTTSlidingWindowAverage ( ) ;
30
+ }
31
+
32
+ void UpdateRTTSlidingWindowAverage ( )
33
+ {
34
+ if ( m_MovingWindow . Count > m_MaxWindowSize )
35
+ {
36
+ m_MovingWindow . Dequeue ( ) ;
37
+ }
38
+
39
+ float rttSum = 0 ;
40
+ foreach ( var singleRTT in m_MovingWindow )
41
+ {
42
+ rttSum += singleRTT ;
43
+ }
44
+
45
+ LastRTT = rttSum / m_MaxWindowSize ;
46
+ }
47
+ }
19
48
// RTT
20
49
// Client sends a ping RPC to the server and starts it's timer.
21
50
// The server receives the ping and sends a pong response to the client.
@@ -25,21 +54,17 @@ public class NetworkStats : NetworkBehaviour
25
54
// Note: when adding more stats, it might be worth it to abstract these in their own classes instead of having a bunch
26
55
// of attributes floating around.
27
56
28
- public float LastRTT { get ; private set ; }
57
+ MovingWindowAverage BossRoomRTT = new MovingWindowAverage ( ) ;
58
+ MovingWindowAverage UTP_RTT = new MovingWindowAverage ( ) ;
29
59
30
- [ SerializeField ]
31
- [ Tooltip ( "The interval to send ping RPCs to calculate the RTT. The bigger the number, the less reactive the stat will be to RTT changes" ) ]
32
- float m_PingIntervalSeconds = 0.1f ;
60
+ const float m_PingIntervalSeconds = 0.1f ;
33
61
float m_LastPingTime ;
34
62
Text m_TextStat ;
35
63
Text m_TextHostType ;
36
64
37
65
// When receiving pong client RPCs, we need to know when the initiating ping sent it so we can calculate its individual RTT
38
66
int m_CurrentRTTPingId ;
39
67
40
- Queue < float > m_MovingWindow = new Queue < float > ( ) ;
41
- const int k_MaxWindowSizeSeconds = 3 ; // it should take x seconds for the value to react to change
42
- float m_MaxWindowSize => k_MaxWindowSizeSeconds / m_PingIntervalSeconds ;
43
68
Dictionary < int , float > m_PingHistoryStartTimes = new Dictionary < int , float > ( ) ;
44
69
45
70
ClientRpcParams m_PongClientParams ;
@@ -99,12 +124,14 @@ void FixedUpdate()
99
124
m_PingHistoryStartTimes [ m_CurrentRTTPingId ] = Time . realtimeSinceStartup ;
100
125
m_CurrentRTTPingId ++ ;
101
126
m_LastPingTime = Time . realtimeSinceStartup ;
127
+
128
+ UTP_RTT . Add ( NetworkManager . NetworkConfig . NetworkTransport . GetCurrentRtt ( NetworkManager . ServerClientId ) ) ;
102
129
}
103
130
104
131
if ( m_TextStat != null )
105
132
{
106
- var transportRTT = NetworkManager . NetworkConfig . NetworkTransport . GetCurrentRtt ( NetworkManager . ServerClientId ) ;
107
- textToDisplay = $ "{ textToDisplay } RTT: { ( LastRTT * 1000 ) . ToString ( "0" ) } ms;\n UTP RTT { transportRTT . ToString ( "0" ) } ms";
133
+
134
+ textToDisplay = $ "{ textToDisplay } RTT: { ( BossRoomRTT . LastRTT * 1000 ) . ToString ( "0" ) } ms;\n UTP RTT { UTP_RTT . LastRTT . ToString ( "0" ) } ms";
108
135
}
109
136
}
110
137
@@ -131,24 +158,7 @@ public void PongClientRPC(int pingId, ClientRpcParams clientParams = default)
131
158
{
132
159
var startTime = m_PingHistoryStartTimes [ pingId ] ;
133
160
m_PingHistoryStartTimes . Remove ( pingId ) ;
134
- m_MovingWindow . Enqueue ( Time . realtimeSinceStartup - startTime ) ;
135
- UpdateRTTSlidingWindowAverage ( ) ;
136
- }
137
-
138
- void UpdateRTTSlidingWindowAverage ( )
139
- {
140
- if ( m_MovingWindow . Count > m_MaxWindowSize )
141
- {
142
- m_MovingWindow . Dequeue ( ) ;
143
- }
144
-
145
- float rttSum = 0 ;
146
- foreach ( var singleRTT in m_MovingWindow )
147
- {
148
- rttSum += singleRTT ;
149
- }
150
-
151
- LastRTT = rttSum / m_MaxWindowSize ;
161
+ BossRoomRTT . Add ( Time . realtimeSinceStartup - startTime ) ;
152
162
}
153
163
154
164
public override void OnNetworkDespawn ( )
0 commit comments