Skip to content

Commit 8dcba99

Browse files
authored
fix: network time arguments (Unity-Technologies#1194)
* fix: use uint for tickrate and more rigorous argument checking for 0 tickrate * tickrate to uint in buffered interpolator * time editor tests use uint and use testcase attribute to cleanup tests
1 parent 52b66b6 commit 8dcba99

File tree

7 files changed

+97
-155
lines changed

7 files changed

+97
-155
lines changed

Components/Interpolator/BufferedLinearInterpolator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ internal interface IInterpolatorTime
1616
{
1717
double BufferedServerTime { get; }
1818
double BufferedServerFixedTime { get; }
19-
int TickRate { get; }
19+
uint TickRate { get; }
2020
}
2121

2222
private class InterpolatorTime : IInterpolatorTime
2323
{
2424
public double BufferedServerTime => NetworkManager.Singleton.ServerTime.Time;
2525
public double BufferedServerFixedTime => NetworkManager.Singleton.ServerTime.FixedTime;
26-
public int TickRate => NetworkManager.Singleton.ServerTime.TickRate;
26+
public uint TickRate => NetworkManager.Singleton.ServerTime.TickRate;
2727
}
2828

2929
internal IInterpolatorTime InterpolatorTimeProxy = new InterpolatorTime();

Runtime/Configuration/NetworkConfig.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class NetworkConfig
4949
/// The tickrate of network ticks. This value controls how often netcode runs user code and sends out data.
5050
/// </summary>
5151
[Tooltip("The tickrate. This value controls how often netcode runs user code and sends out data. The value is in 'ticks per seconds' which means a value of 50 will result in 50 ticks being executed per second or a fixed delta time of 0.02.")]
52-
public int TickRate = 30;
52+
public uint TickRate = 30;
5353

5454
/// <summary>
5555
/// The amount of seconds to wait for handshake to complete before timing out a client
@@ -162,7 +162,7 @@ public string ToBase64()
162162
using var buffer = PooledNetworkBuffer.Get();
163163
using var writer = PooledNetworkWriter.Get(buffer);
164164
writer.WriteUInt16Packed(config.ProtocolVersion);
165-
writer.WriteInt32Packed(config.TickRate);
165+
writer.WriteUInt32Packed(config.TickRate);
166166
writer.WriteInt32Packed(config.ClientConnectionBufferTimeout);
167167
writer.WriteBool(config.ConnectionApproval);
168168
writer.WriteInt32Packed(config.LoadSceneTimeOut);
@@ -192,7 +192,7 @@ public void FromBase64(string base64)
192192

193193
config.ProtocolVersion = reader.ReadUInt16Packed();
194194
ushort sceneCount = reader.ReadUInt16Packed();
195-
config.TickRate = reader.ReadInt32Packed();
195+
config.TickRate = reader.ReadUInt32Packed();
196196
config.ClientConnectionBufferTimeout = reader.ReadInt32Packed();
197197
config.ConnectionApproval = reader.ReadBool();
198198
config.LoadSceneTimeOut = reader.ReadInt32Packed();

Runtime/Timing/NetworkTickSystem.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class NetworkTickSystem
1717
/// <summary>
1818
/// The TickRate of the tick system. This is used to decide how often a fixed network tick is run.
1919
/// </summary>
20-
public int TickRate { get; }
20+
public uint TickRate { get; }
2121

2222
/// <summary>
2323
/// The current local time. This is the time at which predicted or client authoritative objects move. This value is accurate when called in Update or during the <see cref="Tick"/> event but does not work correctly for FixedUpdate.
@@ -40,8 +40,13 @@ public class NetworkTickSystem
4040
/// <param name="tickRate">The tick rate</param>
4141
/// <param name="localTimeSec">The initial local time to start at.</param>
4242
/// <param name="serverTimeSec">The initial server time to start at.</param>
43-
public NetworkTickSystem(int tickRate, double localTimeSec, double serverTimeSec)
43+
public NetworkTickSystem(uint tickRate, double localTimeSec, double serverTimeSec)
4444
{
45+
if (tickRate == 0)
46+
{
47+
throw new ArgumentException("Tickrate must be a positive value.", nameof(tickRate));
48+
}
49+
4550
TickRate = tickRate;
4651
Tick = null;
4752
LocalTime = new NetworkTime(tickRate, localTimeSec);

Runtime/Timing/NetworkTime.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public struct NetworkTime
1313
{
1414
private double m_TimeSec;
1515

16-
private int m_TickRate;
16+
private uint m_TickRate;
1717
private double m_TickInterval;
1818

1919
private int m_CachedTick;
@@ -52,13 +52,13 @@ public struct NetworkTime
5252
/// <summary>
5353
/// Gets the tickrate of the system of this <see cref="NetworkTime"/>.
5454
/// </summary>
55-
public int TickRate => m_TickRate;
55+
public uint TickRate => m_TickRate;
5656

5757
/// <summary>
5858
/// Creates a new instance of the <see cref="NetworkTime"/> struct.
5959
/// </summary>
6060
/// <param name="tickRate">The tickrate.</param>
61-
public NetworkTime(int tickRate)
61+
public NetworkTime(uint tickRate)
6262
{
6363
Assert.IsTrue(tickRate > 0, "Tickrate must be a positive value.");
6464

@@ -75,7 +75,7 @@ public NetworkTime(int tickRate)
7575
/// <param name="tickRate">The tickrate.</param>
7676
/// <param name="tick">The time will be created with a value where this many tick have already passed.</param>
7777
/// <param name="tickOffset">Can be used to create a <see cref="NetworkTime"/> with a non fixed time value by adding an offset to the given tick value.</param>
78-
public NetworkTime(int tickRate, int tick, double tickOffset = 0d)
78+
public NetworkTime(uint tickRate, int tick, double tickOffset = 0d)
7979
: this(tickRate)
8080
{
8181
Assert.IsTrue(tickOffset < 1d / tickRate);
@@ -87,7 +87,7 @@ public NetworkTime(int tickRate, int tick, double tickOffset = 0d)
8787
/// </summary>
8888
/// <param name="tickRate">The tickrate.</param>
8989
/// <param name="timeSec">The time value as a float.</param>
90-
public NetworkTime(int tickRate, double timeSec)
90+
public NetworkTime(uint tickRate, double timeSec)
9191
: this(tickRate)
9292
{
9393
this += timeSec;

Tests/Editor/InterpolatorTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ private class MockInterpolatorTime : BufferedLinearInterpolator<float>.IInterpol
1111
{
1212
public double BufferedServerTime { get; set; }
1313
public double BufferedServerFixedTime { get; }
14-
public int TickRate { get; set; }
14+
public uint TickRate { get; set; }
1515

16-
public MockInterpolatorTime(double serverTime, int tickRate)
16+
public MockInterpolatorTime(double serverTime, uint tickRate)
1717
{
1818
BufferedServerTime = serverTime;
1919
TickRate = tickRate;
@@ -22,7 +22,7 @@ public MockInterpolatorTime(double serverTime, int tickRate)
2222

2323
private const int k_MockTickRate = 1;
2424

25-
private NetworkTime T(float time, int tickRate = k_MockTickRate)
25+
private NetworkTime T(float time, uint tickRate = k_MockTickRate)
2626
{
2727
return new NetworkTime(tickRate, timeSec: time);
2828
}

0 commit comments

Comments
 (0)