@@ -21,7 +21,7 @@ private class MovingWindowAverage
21
21
public float LastRTT { get ; private set ; }
22
22
Queue < float > m_MovingWindow = new Queue < float > ( ) ;
23
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 ;
24
+ const float k_MaxWindowSize = k_MaxWindowSizeSeconds / k_PingIntervalSeconds ;
25
25
26
26
public void Add ( float value )
27
27
{
@@ -31,7 +31,7 @@ public void Add(float value)
31
31
32
32
void UpdateRTTSlidingWindowAverage ( )
33
33
{
34
- if ( m_MovingWindow . Count > m_MaxWindowSize )
34
+ if ( m_MovingWindow . Count > k_MaxWindowSize )
35
35
{
36
36
m_MovingWindow . Dequeue ( ) ;
37
37
}
@@ -42,7 +42,7 @@ void UpdateRTTSlidingWindowAverage()
42
42
rttSum += singleRTT ;
43
43
}
44
44
45
- LastRTT = rttSum / m_MaxWindowSize ;
45
+ LastRTT = rttSum / k_MaxWindowSize ;
46
46
}
47
47
}
48
48
// RTT
@@ -54,10 +54,11 @@ void UpdateRTTSlidingWindowAverage()
54
54
// Note: when adding more stats, it might be worth it to abstract these in their own classes instead of having a bunch
55
55
// of attributes floating around.
56
56
57
- MovingWindowAverage BossRoomRTT = new MovingWindowAverage ( ) ;
58
- MovingWindowAverage UTP_RTT = new MovingWindowAverage ( ) ;
57
+ const float k_PingIntervalSeconds = 0.1f ;
58
+
59
+ MovingWindowAverage m_BossRoomRTT = new MovingWindowAverage ( ) ;
60
+ MovingWindowAverage m_UtpRTT = new MovingWindowAverage ( ) ;
59
61
60
- const float m_PingIntervalSeconds = 0.1f ;
61
62
float m_LastPingTime ;
62
63
Text m_TextStat ;
63
64
Text m_TextHostType ;
@@ -69,7 +70,6 @@ void UpdateRTTSlidingWindowAverage()
69
70
70
71
ClientRpcParams m_PongClientParams ;
71
72
72
-
73
73
public override void OnNetworkSpawn ( )
74
74
{
75
75
bool isClientOnly = IsClient && ! IsServer ;
@@ -78,10 +78,12 @@ public override void OnNetworkSpawn()
78
78
Destroy ( this ) ;
79
79
return ;
80
80
}
81
+
81
82
if ( IsOwner )
82
83
{
83
84
CreateNetworkStatsText ( ) ;
84
85
}
86
+
85
87
m_PongClientParams = new ClientRpcParams ( ) { Send = new ClientRpcSendParams ( ) { TargetClientIds = new [ ] { OwnerClientId } } } ;
86
88
}
87
89
@@ -96,7 +98,7 @@ void CreateNetworkStatsText()
96
98
InitializeTextLine ( "No Stat" , out m_TextStat ) ;
97
99
}
98
100
99
- private void InitializeTextLine ( string defaultText , out Text textComponent )
101
+ void InitializeTextLine ( string defaultText , out Text textComponent )
100
102
{
101
103
GameObject rootGO = new GameObject ( "UI Stat Text" ) ;
102
104
textComponent = rootGO . AddComponent < Text > ( ) ;
@@ -116,7 +118,7 @@ void FixedUpdate()
116
118
var textToDisplay = string . Empty ;
117
119
if ( ! IsServer )
118
120
{
119
- if ( Time . realtimeSinceStartup - m_LastPingTime > m_PingIntervalSeconds )
121
+ if ( Time . realtimeSinceStartup - m_LastPingTime > k_PingIntervalSeconds )
120
122
{
121
123
// We could have had a ping/pong where the ping sends the pong and the pong sends the ping. Issue with this
122
124
// is the higher the latency, the lower the sampling would be. We need pings to be sent at a regular interval
@@ -125,13 +127,12 @@ void FixedUpdate()
125
127
m_CurrentRTTPingId ++ ;
126
128
m_LastPingTime = Time . realtimeSinceStartup ;
127
129
128
- UTP_RTT . Add ( NetworkManager . NetworkConfig . NetworkTransport . GetCurrentRtt ( NetworkManager . ServerClientId ) ) ;
130
+ m_UtpRTT . Add ( NetworkManager . NetworkConfig . NetworkTransport . GetCurrentRtt ( NetworkManager . ServerClientId ) ) ;
129
131
}
130
132
131
133
if ( m_TextStat != null )
132
134
{
133
-
134
- textToDisplay = $ "{ textToDisplay } RTT: { ( BossRoomRTT . LastRTT * 1000 ) . ToString ( "0" ) } ms;\n UTP RTT { UTP_RTT . LastRTT . ToString ( "0" ) } ms";
135
+ textToDisplay = $ "{ textToDisplay } RTT: { ( m_BossRoomRTT . LastRTT * 1000 ) . ToString ( "0" ) } ms;\n UTP RTT { m_UtpRTT . LastRTT . ToString ( "0" ) } ms";
135
136
}
136
137
}
137
138
@@ -146,19 +147,18 @@ void FixedUpdate()
146
147
}
147
148
}
148
149
149
-
150
150
[ ServerRpc ]
151
- public void PingServerRPC ( int pingId , ServerRpcParams serverParams = default )
151
+ void PingServerRPC ( int pingId , ServerRpcParams serverParams = default )
152
152
{
153
153
PongClientRPC ( pingId , m_PongClientParams ) ;
154
154
}
155
155
156
156
[ ClientRpc ]
157
- public void PongClientRPC ( int pingId , ClientRpcParams clientParams = default )
157
+ void PongClientRPC ( int pingId , ClientRpcParams clientParams = default )
158
158
{
159
159
var startTime = m_PingHistoryStartTimes [ pingId ] ;
160
160
m_PingHistoryStartTimes . Remove ( pingId ) ;
161
- BossRoomRTT . Add ( Time . realtimeSinceStartup - startTime ) ;
161
+ m_BossRoomRTT . Add ( Time . realtimeSinceStartup - startTime ) ;
162
162
}
163
163
164
164
public override void OnNetworkDespawn ( )
@@ -167,6 +167,7 @@ public override void OnNetworkDespawn()
167
167
{
168
168
Destroy ( m_TextStat . gameObject ) ;
169
169
}
170
+
170
171
if ( m_TextHostType != null )
171
172
{
172
173
Destroy ( m_TextHostType . gameObject ) ;
0 commit comments