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