Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion TNL.NET/Network/TNLSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class TNLSocket
private bool _needRun;
private readonly UdpClient _socket;

public object _lock = new object();
public Queue<Tuple<IPEndPoint, byte[]>> PacketsToBeHandled = new();

public TNLSocket()
Expand All @@ -42,7 +43,8 @@ private void OnEndReceive(IAsyncResult result)
var buff = _socket.EndReceive(result, ref ep);

if (buff != null && buff.Length > 0)
PacketsToBeHandled.Enqueue(new(ep, buff));
lock(_lock)
PacketsToBeHandled.Enqueue(new(ep, buff));
}
catch (ObjectDisposedException)
{
Expand Down
14 changes: 9 additions & 5 deletions TNL.NET/Utils/PacketStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ public NetError SendTo(TNLSocket outgoingSocket, IPEndPoint theAddress)

public NetError RecvFrom(TNLSocket incomingSocket, out IPEndPoint recvAddress)
{
if (incomingSocket.PacketsToBeHandled.Count == 0)
Tuple<IPEndPoint, byte[]> d;

lock (incomingSocket._lock)
{
recvAddress = null;
return NetError.WouldBlock;
if (incomingSocket.PacketsToBeHandled.Count == 0)
{
recvAddress = null;
return NetError.WouldBlock;
}
d = incomingSocket.PacketsToBeHandled.Dequeue();
}

var d = incomingSocket.PacketsToBeHandled.Dequeue();

var dataSize = d.Item2.Length > TNLSocket.MaxPacketDataSize ? TNLSocket.MaxPacketDataSize : (uint) d.Item2.Length;

Array.Copy(d.Item2, _buffer, dataSize);
Expand Down