Skip to content

Commit

Permalink
make ntp requests concurrent
Browse files Browse the repository at this point in the history
  • Loading branch information
RevenantX committed Oct 12, 2023
1 parent 373dfc7 commit 3220c1c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions LiteNetLib/NetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private struct IncomingData

private readonly Dictionary<IPEndPoint, NetPeer> _peersDict = new Dictionary<IPEndPoint, NetPeer>(new IPEndPointComparer());
private readonly Dictionary<IPEndPoint, ConnectionRequest> _requestsDict = new Dictionary<IPEndPoint, ConnectionRequest>(new IPEndPointComparer());
private readonly Dictionary<IPEndPoint, NtpRequest> _ntpRequests = new Dictionary<IPEndPoint, NtpRequest>(new IPEndPointComparer());
private readonly ConcurrentDictionary<IPEndPoint, NtpRequest> _ntpRequests = new ConcurrentDictionary<IPEndPoint, NtpRequest>(new IPEndPointComparer());
private readonly ReaderWriterLockSlim _peersLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
private volatile NetPeer _headPeer;
private int _connectedPeersCount;
Expand Down Expand Up @@ -734,7 +734,7 @@ private void ProcessNtpRequests(int elapsedMilliseconds)
{
foreach (var ipEndPoint in requestsToRemove)
{
_ntpRequests.Remove(ipEndPoint);
_ntpRequests.TryRemove(ipEndPoint, out _);
}
}
}
Expand Down Expand Up @@ -941,7 +941,7 @@ private void DebugMessageReceived(NetPacket packet, IPEndPoint remoteEndPoint)

if (ntpPacket != null)
{
_ntpRequests.Remove(remoteEndPoint);
_ntpRequests.TryRemove(remoteEndPoint, out _);
_ntpEventListener?.OnNtpResponse(ntpPacket);
}
return;
Expand Down Expand Up @@ -1776,7 +1776,7 @@ public void DisconnectPeer(NetPeer peer, byte[] data, int start, int count)
/// <param name="endPoint">NTP Server address.</param>
public void CreateNtpRequest(IPEndPoint endPoint)
{
_ntpRequests.Add(endPoint, new NtpRequest(endPoint));
_ntpRequests.TryAdd(endPoint, new NtpRequest(endPoint));
}

/// <summary>
Expand All @@ -1787,7 +1787,7 @@ public void CreateNtpRequest(IPEndPoint endPoint)
public void CreateNtpRequest(string ntpServerAddress, int port)
{
IPEndPoint endPoint = NetUtils.MakeEndPoint(ntpServerAddress, port);
_ntpRequests.Add(endPoint, new NtpRequest(endPoint));
_ntpRequests.TryAdd(endPoint, new NtpRequest(endPoint));
}

/// <summary>
Expand All @@ -1797,7 +1797,7 @@ public void CreateNtpRequest(string ntpServerAddress, int port)
public void CreateNtpRequest(string ntpServerAddress)
{
IPEndPoint endPoint = NetUtils.MakeEndPoint(ntpServerAddress, NtpRequest.DefaultPort);
_ntpRequests.Add(endPoint, new NtpRequest(endPoint));
_ntpRequests.TryAdd(endPoint, new NtpRequest(endPoint));
}

public NetPeerEnumerator GetEnumerator()
Expand Down

0 comments on commit 3220c1c

Please sign in to comment.