feat: udpServer使用异步处理数据包
This commit is contained in:
		@@ -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
 | 
			
		||||
        var udpData = RecordUDPData(bytes, remoteEP, Convert.ToInt32(bytes[1]));
 | 
			
		||||
        PrintData(udpData);
 | 
			
		||||
 | 
			
		||||
    BEGIN_RECEIVE:
 | 
			
		||||
        listener.BeginReceive(new AsyncCallback(ReceiveHandler), null);
 | 
			
		||||
        // 异步处理数据包
 | 
			
		||||
        Task.Run(() =>
 | 
			
		||||
        {
 | 
			
		||||
            var udpData = RecordUDPData(bytes, remoteEP, Convert.ToInt32(bytes[1]));
 | 
			
		||||
            PrintData(udpData);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private bool SendBytes(IPEndPoint endPoint, byte[] buf)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user