feat: udpServer使用异步处理数据包

This commit is contained in:
SikongJueluo 2025-07-13 10:53:23 +08:00
parent 0350ce8829
commit b913f58f13
No known key found for this signature in database
1 changed files with 12 additions and 10 deletions

View File

@ -287,21 +287,23 @@ public class UDPServer
var remoteEP = new IPEndPoint(IPAddress.Any, listenPort);
byte[] bytes = listener.EndReceive(res, ref remoteEP);
// 提前开始接收下一个包
listener.BeginReceive(new AsyncCallback(ReceiveHandler), null);
// Handle RemoteEP
if (remoteEP is null)
{
// logger.Debug($"Receive Data from Unknown at {DateTime.Now.ToString()}:");
// logger.Debug($" Original Data : {BitConverter.ToString(bytes).Replace("-", " ")}");
goto BEGIN_RECEIVE;
logger.Debug($"Receive Data from Unknown at {DateTime.Now.ToString()}:");
logger.Debug($" Original Data : {BitConverter.ToString(bytes).Replace("-", " ")}");
return;
}
// Handle Package
// 异步处理数据包
Task.Run(() =>
{
var udpData = RecordUDPData(bytes, remoteEP, Convert.ToInt32(bytes[1]));
PrintData(udpData);
BEGIN_RECEIVE:
listener.BeginReceive(new AsyncCallback(ReceiveHandler), null);
});
}
private bool SendBytes(IPEndPoint endPoint, byte[] buf)