Skip to content

Commit

Permalink
fix: #3576 Pings are now stamped with a scene hash so we can drop mes…
Browse files Browse the repository at this point in the history
…sages before a (potentially long) scene load. fixes a bug where RTT would be very high after a long scene load. (#3650)

* fix: #3576 Pings are now stamped with a scene hash so we can drop messages before a (potentially long) scene load. fixes a bug where RTT would be very high after a long scene load.

* 16 bit hash fakebyte
  • Loading branch information
miwarnec committed Nov 15, 2023
1 parent 7a69c42 commit c729fe1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
20 changes: 18 additions & 2 deletions Assets/Mirror/Core/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,23 @@ public struct EntityStateMessage : NetworkMessage
// whoever wants to measure rtt, sends this to the other end.
public struct NetworkPingMessage : NetworkMessage
{
// ping messages are stamped with scene name (as hash).
// this way we can disregard messages from before a scene change.
// otherwise a 30s loading pause would cause super high RTT after:
// https://github.com/MirrorNetworking/Mirror/issues/3576
// (2 byte hash instead of N byte string to minimize bandwidth)
public ushort sceneHash;

// local time is used to calculate round trip time,
// and to calculate the predicted time offset.
public double localTime;

// predicted time is sent to compare the final error, for debugging only
public double predictedTimeAdjusted;

public NetworkPingMessage(double localTime, double predictedTimeAdjusted)
public NetworkPingMessage(ushort sceneHash, double localTime, double predictedTimeAdjusted)
{
this.sceneHash = sceneHash;
this.localTime = localTime;
this.predictedTimeAdjusted = predictedTimeAdjusted;
}
Expand All @@ -123,15 +131,23 @@ public NetworkPingMessage(double localTime, double predictedTimeAdjusted)
// we can use this to calculate rtt.
public struct NetworkPongMessage : NetworkMessage
{
// ping messages are stamped with scene name (as hash).
// this way we can disregard messages from before a scene change.
// otherwise a 30s loading pause would cause super high RTT after:
// https://github.com/MirrorNetworking/Mirror/issues/3576
// (2 byte hash instead of N byte string to minimize bandwidth)
public ushort sceneHash;

// local time is used to calculate round trip time.
public double localTime;

// predicted error is used to adjust the predicted timeline.
public double predictionErrorUnadjusted;
public double predictionErrorAdjusted; // for debug purposes

public NetworkPongMessage(double localTime, double predictionErrorUnadjusted, double predictionErrorAdjusted)
public NetworkPongMessage(ushort sceneHash, double localTime, double predictionErrorUnadjusted, double predictionErrorAdjusted)
{
this.sceneHash = sceneHash;
this.localTime = localTime;
this.predictionErrorUnadjusted = predictionErrorUnadjusted;
this.predictionErrorAdjusted = predictionErrorAdjusted;
Expand Down
4 changes: 3 additions & 1 deletion Assets/Mirror/Core/NetworkConnectionToClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Mirror
{
Expand Down Expand Up @@ -129,7 +130,8 @@ protected virtual void UpdatePing()
// messages' timestamp and only send a message number.
// This way client's can't just modify the timestamp.
// predictedTime parameter is 0 because the server doesn't predict.
NetworkPingMessage pingMessage = new NetworkPingMessage(NetworkTime.localTime, 0);
ushort sceneHash = SceneManager.GetActiveScene().name.GetStableHashCode16();
NetworkPingMessage pingMessage = new NetworkPingMessage(sceneHash, NetworkTime.localTime, 0);
Send(pingMessage, Channels.Unreliable);
lastPingTime = NetworkTime.localTime;
}
Expand Down
13 changes: 13 additions & 0 deletions Assets/Mirror/Core/NetworkTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.SceneManagement;
#if !UNITY_2020_3_OR_NEWER
using Stopwatch = System.Diagnostics.Stopwatch;
#endif
Expand Down Expand Up @@ -144,8 +145,11 @@ internal static void UpdateClient()
{
// send raw predicted time without the offset applied yet.
// we then apply the offset to it after.
// include scene name (as hash)
ushort sceneHash = SceneManager.GetActiveScene().name.GetStableHashCode16();
NetworkPingMessage pingMessage = new NetworkPingMessage
(
sceneHash,
localTime,
predictedTime
);
Expand Down Expand Up @@ -175,6 +179,7 @@ internal static void OnServerPing(NetworkConnectionToClient conn, NetworkPingMes
// Debug.Log($"OnServerPing conn:{conn}");
NetworkPongMessage pongMessage = new NetworkPongMessage
(
message.sceneHash,
message.localTime,
unadjustedError,
adjustedError
Expand All @@ -190,6 +195,13 @@ internal static void OnClientPong(NetworkPongMessage message)
// prevent attackers from sending timestamps which are in the future
if (message.localTime > localTime) return;

// ping messages are stamped with scene name (as hash).
// this way we can disregard messages from before a scene change.
// otherwise a 30s loading pause would cause super high RTT after:
// https://github.com/MirrorNetworking/Mirror/issues/3576
int sceneHash = SceneManager.GetActiveScene().name.GetStableHashCode();
if (message.sceneHash != sceneHash) return;

// how long did this message take to come back
double newRtt = localTime - message.localTime;
_rtt.Add(newRtt);
Expand All @@ -210,6 +222,7 @@ internal static void OnClientPing(NetworkPingMessage message)
// Debug.Log($"OnClientPing conn:{conn}");
NetworkPongMessage pongMessage = new NetworkPongMessage
(
message.sceneHash,
message.localTime,
0, 0 // server doesn't predict
);
Expand Down

0 comments on commit c729fe1

Please sign in to comment.