Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,13 @@ public virtual void Reset()
ResetState();
// default to ClientToServer so this works immediately for users
syncDirection = SyncDirection.ClientToServer;
NetworkTime.activeNTs = 0;
}

protected virtual void OnEnable()
{
ResetState();
NetworkTime.activeNTs++;

if (NetworkServer.active)
NetworkIdentity.clientAuthorityCallback += OnClientAuthorityChanged;
Expand All @@ -447,6 +449,7 @@ protected virtual void OnEnable()
protected virtual void OnDisable()
{
ResetState();
NetworkTime.activeNTs--;

if (NetworkServer.active)
NetworkIdentity.clientAuthorityCallback -= OnClientAuthorityChanged;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ public override void Reset()

// default to ClientToServer so this works immediately for users
syncDirection = SyncDirection.ClientToServer;
NetworkTime.activeNTs = 0;

// disabled objects aren't updated anymore.
// so let's clear the buffers.
Expand All @@ -548,8 +549,17 @@ public override void Reset()
// Debug.Log($"[{name}] Reset to baselineTick=0");
}

protected virtual void OnDisable() => Reset();
protected virtual void OnEnable() => Reset();
protected virtual void OnEnable()
{
Reset();
NetworkTime.activeNTs++;
}

protected virtual void OnDisable()
{
Reset();
NetworkTime.activeNTs--;
}

public override void OnSerialize(NetworkWriter writer, bool initialState)
{
Expand Down
10 changes: 10 additions & 0 deletions Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ protected virtual void Awake()
positionCorrectionThresholdSqr = positionCorrectionThreshold * positionCorrectionThreshold;
}

protected virtual void OnEnable()
{
NetworkTime.activeNTs++;
}

protected virtual void OnDisable()
{
NetworkTime.activeNTs--;
}

protected virtual void CopyRenderersAsGhost(GameObject destination, Material material)
{
// find the MeshRenderer component, which sometimes is on a child.
Expand Down
3 changes: 2 additions & 1 deletion Assets/Mirror/Core/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,8 @@ static void Broadcast()
if (NetworkServer.active) return;

// send time snapshot every sendInterval.
Send(new TimeSnapshotMessage(), Channels.Unreliable);
if (NetworkTime.activeNTs > 0)
Send(new TimeSnapshotMessage(), Channels.Unreliable);

// broadcast client state to server
BroadcastToServer();
Expand Down
2 changes: 2 additions & 0 deletions Assets/Mirror/Core/NetworkConnectionToClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ protected virtual void UpdatePing()
// localTime (double) instead of Time.time for accuracy over days
if (NetworkTime.localTime >= lastPingTime + NetworkTime.PingInterval)
{
//Debug.Log("NetworkConnectionToClient SendPing");

// TODO it would be safer for the server to store the last N
// messages' timestamp and only send a message number.
// This way client's can't just modify the timestamp.
Expand Down
3 changes: 2 additions & 1 deletion Assets/Mirror/Core/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,8 @@ static void Broadcast()
// make sure Broadcast() is only called every sendInterval,
// even if targetFrameRate isn't set in host mode (!)
// (done via AccurateInterval)
connection.Send(new TimeSnapshotMessage(), Channels.Unreliable);
if (NetworkTime.activeNTs > 0)
connection.Send(new TimeSnapshotMessage(), Channels.Unreliable);

// broadcast world state to this connection
BroadcastToConnection(connection);
Expand Down
11 changes: 8 additions & 3 deletions Assets/Mirror/Core/NetworkTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ public static class NetworkTime
/// <summary>Ping message interval, used to calculate latency / RTT and predicted time.</summary>
// 2s was enough to get a good average RTT.
// for prediction, we want to react to latency changes more rapidly.
const float DefaultPingInterval = 0.1f; // for resets
public static float PingInterval = DefaultPingInterval;
internal static float DefaultPingInterval = 2.0f; // internal for tests
public static float PingInterval => activeNTs > 0 ? 0.1f : DefaultPingInterval;

/// <summary>Average out the last few results from Ping</summary>
// const because it's used immediately in _rtt constructor.
public const int PingWindowSize = 50; // average over 50 * 100ms = 5s

static double lastPingTime;

internal static ulong activeNTs = 0;

static ExponentialMovingAverage _rtt = new ExponentialMovingAverage(PingWindowSize);

/// <summary>Returns double precision clock time _in this system_, unaffected by the network.</summary>
Expand Down Expand Up @@ -129,7 +131,8 @@ public static double predictedTime
[RuntimeInitializeOnLoadMethod]
public static void ResetStatics()
{
PingInterval = DefaultPingInterval;
DefaultPingInterval = 2.0f;
activeNTs = 0;
lastPingTime = 0;
_rtt = new ExponentialMovingAverage(PingWindowSize);
#if !UNITY_2020_3_OR_NEWER
Expand All @@ -147,6 +150,8 @@ internal static void UpdateClient()
// Separate method so we can call it from NetworkClient directly.
internal static void SendPing()
{
//Debug.Log("NetworkTime SendPing");

// send raw predicted time without the offset applied yet.
// we then apply the offset to it after.
NetworkPingMessage pingMessage = new NetworkPingMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Send_BatchesUntilUpdate()
{
// create connection and send
NetworkConnectionToClient connection = new NetworkConnectionToClient(42);
NetworkTime.PingInterval = float.MaxValue; // disable ping for this test
NetworkTime.DefaultPingInterval = float.MaxValue; // disable ping for this test
byte[] message = {0x01, 0x02};
connection.Send(new ArraySegment<byte>(message));

Expand Down Expand Up @@ -64,7 +64,7 @@ public void SendBatchingResetsPreviousWriter()

// create connection
NetworkConnectionToClient connection = new NetworkConnectionToClient(42);
NetworkTime.PingInterval = float.MaxValue; // disable ping for this test
NetworkTime.DefaultPingInterval = float.MaxValue; // disable ping for this test

// send and update big message
byte[] message = {0x01, 0x02};
Expand Down
3 changes: 1 addition & 2 deletions Assets/Mirror/Tests/Runtime/NetworkServerRuntimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ NetworkIdentity SpawnPrefab(GameObject prefab)
[UnityTest]
public IEnumerator DisconnectTimeoutTest()
{
// Set low ping frequency so no NetworkPingMessage is generated
NetworkTime.PingInterval = 5f;
NetworkTime.DefaultPingInterval = float.MaxValue; // disable ping for this test

// Set a short timeout for this test and enable disconnectInactiveConnections
NetworkServer.disconnectInactiveConnections = true;
Expand Down