Skip to content

Commit

Permalink
Make PeerTimers and ElapsedMilliseconds as float fix #521
Browse files Browse the repository at this point in the history
  • Loading branch information
RevenantX committed Aug 2, 2024
1 parent 1d245ab commit 9fb2546
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions LiteNetLib/NetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,8 @@ private void UpdateLogic()
try
{
ProcessDelayedPackets();
int elapsed = (int)stopwatch.ElapsedMilliseconds;
elapsed = elapsed <= 0 ? 1 : elapsed;
float elapsed = (float)(stopwatch.ElapsedTicks / (double)Stopwatch.Frequency * 1000.0);
elapsed = elapsed <= 0.0f ? 0.001f : elapsed;
stopwatch.Restart();

for (var netPeer = _headPeer; netPeer != null; netPeer = netPeer.NextPeer)
Expand Down Expand Up @@ -625,7 +625,7 @@ private void ProcessDelayedPackets()
}
}

private void ProcessNtpRequests(int elapsedMilliseconds)
private void ProcessNtpRequests(float elapsedMilliseconds)
{
List<IPEndPoint> requestsToRemove = null;
foreach (var ntpRequest in _ntpRequests)
Expand All @@ -652,7 +652,7 @@ private void ProcessNtpRequests(int elapsedMilliseconds)
/// Update and send logic. Use this only when NetManager started in manual mode
/// </summary>
/// <param name="elapsedMilliseconds">elapsed milliseconds since last update call</param>
public void ManualUpdate(int elapsedMilliseconds)
public void ManualUpdate(float elapsedMilliseconds)
{
if (!_manualMode)
return;
Expand Down
22 changes: 11 additions & 11 deletions LiteNetLib/NetPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public class NetPeer : IPEndPoint
private int _avgRtt;
private int _rttCount;
private double _resendDelay = 27.0;
private int _pingSendTimer;
private int _rttResetTimer;
private float _pingSendTimer;
private float _rttResetTimer;
private readonly Stopwatch _pingTimer = new Stopwatch();
private int _timeSinceLastPacket;
private volatile float _timeSinceLastPacket;
private long _remoteDelta;

//Common
Expand Down Expand Up @@ -90,7 +90,7 @@ private set
private int _mtu;
private int _mtuIdx;
private bool _finishMtu;
private int _mtuCheckTimer;
private float _mtuCheckTimer;
private int _mtuCheckAttempts;
private const int MtuCheckDelay = 1000;
private const int MaxMtuCheckAttempts = 4;
Expand All @@ -115,13 +115,13 @@ private class IncomingFragments

//Connection
private int _connectAttempts;
private int _connectTimer;
private float _connectTimer;
private long _connectTime;
private byte _connectNum;
private ConnectionState _connectionState;
private NetPacket _shutdownPacket;
private const int ShutdownDelay = 300;
private int _shutdownTimer;
private float _shutdownTimer;
private readonly NetPacket _pingPacket;
private readonly NetPacket _pongPacket;
private readonly NetPacket _connectRequestPacket;
Expand Down Expand Up @@ -179,9 +179,9 @@ private class IncomingFragments
public DateTime RemoteUtcTime => new DateTime(DateTime.UtcNow.Ticks + _remoteDelta);

/// <summary>
/// Time since last packet received (including internal library packets)
/// Time since last packet received (including internal library packets) in milliseconds
/// </summary>
public int TimeSinceLastPacket => _timeSinceLastPacket;
public float TimeSinceLastPacket => _timeSinceLastPacket;

internal double ResendDelay => _resendDelay;

Expand Down Expand Up @@ -1060,7 +1060,7 @@ private void ProcessMtuPacket(NetPacket packet)
}
}

private void UpdateMtuLogic(int deltaTime)
private void UpdateMtuLogic(float deltaTime)
{
if (_finishMtu)
return;
Expand Down Expand Up @@ -1304,9 +1304,9 @@ internal void SendUserData(NetPacket packet)
//DebugWriteForce("Merged: " + _mergePos + "/" + (_mtu - 2) + ", count: " + _mergeCount);
}

internal void Update(int deltaTime)
internal void Update(float deltaTime)
{
Interlocked.Add(ref _timeSinceLastPacket, deltaTime);
_timeSinceLastPacket = _timeSinceLastPacket + deltaTime;
switch (_connectionState)
{
case ConnectionState.Connected:
Expand Down
6 changes: 3 additions & 3 deletions LiteNetLib/Utils/NtpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ internal sealed class NtpRequest
private const int KillTimer = 10000;
public const int DefaultPort = 123;
private readonly IPEndPoint _ntpEndPoint;
private int _resendTime = ResendTimer;
private int _killTime = 0;
private float _resendTime = ResendTimer;
private float _killTime = 0;

public NtpRequest(IPEndPoint endPoint)
{
Expand All @@ -19,7 +19,7 @@ public NtpRequest(IPEndPoint endPoint)

public bool NeedToKill => _killTime >= KillTimer;

public bool Send(Socket socket, int time)
public bool Send(Socket socket, float time)
{
_resendTime += time;
_killTime += time;
Expand Down

0 comments on commit 9fb2546

Please sign in to comment.