try to fix bug, and add schema to swagger

This commit is contained in:
2025-04-13 21:22:55 +08:00
parent c9660633f0
commit 5fa4860bfb
5 changed files with 239 additions and 144 deletions

View File

@@ -11,19 +11,23 @@ public class UDPData
/// <summary>
/// 接受到的时间
/// </summary>
public DateTime DateTime { get; set; }
public required DateTime DateTime { get; set; }
/// <summary>
/// 发送来源的IP地址
/// </summary>
public string Address { get; set; }
public required string Address { get; set; }
/// <summary>
/// 发送来源的端口号
/// </summary>
public int Port { get; set; }
public required int Port { get; set; }
/// <summary>
/// 接受到的数据
/// </summary>
public byte[] Data { get; set; }
public required byte[] Data { get; set; }
/// <summary>
/// 是否被读取过
/// </summary>
public required bool HasRead { get; set; }
/// <summary>
/// 将UDP Data 转化为Json 格式字符串
@@ -94,6 +98,8 @@ public class UDPServer
{
data = udpData[ipAddr][0];
udpData[ipAddr].RemoveAt(0);
logger.Debug($"Find UDP Data: {data.ToString()}");
break;
}
}
@@ -103,6 +109,7 @@ public class UDPServer
if (data == null)
{
logger.Trace("Get nothing even after time out");
return Optional.None<UDPData>();
}
else
@@ -137,6 +144,8 @@ public class UDPServer
{
data = udpData[ipAddr][0];
udpData[ipAddr].RemoveAt(0);
logger.Debug($"Find UDP Data: {data.ToString()}");
break;
}
}
@@ -146,6 +155,7 @@ public class UDPServer
if (data == null)
{
logger.Trace("Get nothing even after time out");
return Optional.None<UDPData>();
}
else
@@ -154,6 +164,12 @@ public class UDPServer
}
}
/// <summary>
/// 获取还未被读取的数据列表
/// </summary>
/// <param name="ipAddr">IP地址</param>
/// <param name="timeout">超时时间</param>
/// <returns>数据列表</returns>
public async ValueTask<Optional<List<UDPData>>> GetDataArrayAsync(string ipAddr, int timeout = 1000)
{
List<UDPData>? data = null;
@@ -186,6 +202,56 @@ public class UDPServer
}
}
/// <summary>
/// 异步等待读响应或写响应
/// </summary>
/// <param name="address">IP地址</param>
/// <param name="port">UDP 端口</param>
/// <param name="timeout">超时时间范围</param>
/// <returns>接收响应包</returns>
public async ValueTask<Result<WebProtocol.RecvRespPackage>> WaitForAckAsync
(string address, int port, int timeout = 1000)
{
var data = await FindDataAsync(address, timeout);
if (!data.HasValue)
throw new Exception("Get None even after time out!");
var recvData = data.Value;
if (recvData.Address != address || recvData.Port != port)
throw new Exception("Receive Data From Wrong Board!");
var retPack = WebProtocol.RecvRespPackage.FromBytes(recvData.Data);
if (!retPack.IsSuccessful)
throw new Exception("Not RecvDataPackage!", retPack.Error);
return retPack.Value;
}
/// <summary>
/// 异步等待数据
/// </summary>
/// <param name="address">IP地址</param>
/// <param name="port">UDP 端口</param>
/// <param name="timeout">超时时间范围</param>
/// <returns>接收数据包</returns>
public async ValueTask<Result<WebProtocol.RecvDataPackage>> WaitForDataAsync
(string address, int port, int timeout = 1000)
{
var data = await FindDataAsync(address, timeout);
if (!data.HasValue)
throw new Exception("Get None even after time out!");
var recvData = data.Value;
if (recvData.Address != address || recvData.Port != port)
throw new Exception("Receive Data From Wrong Board!");
var retPack = WebProtocol.RecvDataPackage.FromBytes(recvData.Data);
if (!retPack.IsSuccessful)
throw new Exception("Not RecvDataPackage!", retPack.Error);
return retPack.Value;
}
private void ReceiveHandler(IAsyncResult res)
{
var remoteEP = new IPEndPoint(IPAddress.Any, listenPort);
@@ -210,17 +276,20 @@ public class UDPServer
Port = remotePort,
Data = bytes,
DateTime = nowtime,
HasRead = false,
});
logger.Trace("Receive data from old client");
}
else
{
udpData.Add(remoteAddress, new List<UDPData>([new UDPData(){
Address = remoteAddress,
Port = remotePort,
Data = bytes,
DateTime = nowtime,
}]));
udpData.Add(remoteAddress, new List<UDPData>([new UDPData()
{
Address = remoteAddress,
Port = remotePort,
Data = bytes,
DateTime = nowtime,
HasRead = false,
}]));
logger.Trace("Receive data from new client");
}
@@ -268,7 +337,7 @@ public class UDPServer
logger.Debug($"Receive Data from {remoteStr} at {nowtime.ToString()}:");
logger.Debug($"Original Data: {BitConverter.ToString(bytes).Replace("-", " ")}");
if (recvData.Length != 0) logger.Debug(recvData);
RecordAllData();
// RecordAllData();
listener.BeginReceive(new AsyncCallback(ReceiveHandler), null);