Skip to content

Commit

Permalink
Resolved unhandled exception in UDP receive.
Browse files Browse the repository at this point in the history
  • Loading branch information
NTDLS committed Dec 22, 2023
1 parent d7308c7 commit 2ddfadf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
13 changes: 12 additions & 1 deletion NTDLS.UDPPacketFraming/FrameBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@ internal bool ReadData(UdpClient udpClient, ref IPEndPoint endPoint)
{
try
{
ReceiveBuffer = udpClient.Receive(ref endPoint);
try
{
ReceiveBuffer = udpClient.Receive(ref endPoint);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode != SocketError.Interrupted)
{
throw;
}
return false; //Graceful disconnect.
}
ReceiveBufferUsed = ReceiveBuffer.Length;

if (ReceiveBufferUsed == 0)
Expand Down
12 changes: 9 additions & 3 deletions NTDLS.UDPPacketFraming/UdpMessageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace NTDLS.UDPPacketFraming
/// </summary>
public class UdpMessageManager
{
private static Random _random = new Random();
private static readonly Random _random = new();
private bool _keepRunning = false;
private Thread? _receiveThread = null;

Expand Down Expand Up @@ -195,9 +195,15 @@ private void ListenAsync(int listenPort, ProcessFrameNotificationCallback proces

_receiveThread = new Thread(o =>
{
while (_keepRunning && Client.ReadAndProcessFrames(ref clientEndPoint, frameBuffer, processNotificationCallback))
while (_keepRunning)
{
Thread.Sleep(0);
try
{
while (Client.ReadAndProcessFrames(ref clientEndPoint, frameBuffer, processNotificationCallback))
{
}
}
catch { }
}
});

Expand Down

0 comments on commit 2ddfadf

Please sign in to comment.