change throw to return and fix bug: when error always throw
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Text;
|
||||
using DotNext;
|
||||
using DotNext.Threading;
|
||||
using Newtonsoft.Json;
|
||||
using WebProtocol;
|
||||
|
||||
/// <summary> UDP接受数据包格式 </summary>
|
||||
public class UDPData
|
||||
@@ -58,9 +59,6 @@ public class UDPData
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UDP Server
|
||||
/// </summary>
|
||||
/// <summary>
|
||||
/// UDP 服务器
|
||||
/// </summary>
|
||||
@@ -80,6 +78,15 @@ public class UDPServer
|
||||
/// </summary>
|
||||
public bool IsRunning { get { return isRunning; } }
|
||||
|
||||
/// <summary> UDP 服务器的错误代码 </summary>
|
||||
public enum ErrorCode
|
||||
{
|
||||
Success = 0,
|
||||
GetNoneAfterTimeout,
|
||||
ResponseWrong,
|
||||
NotRecvDataPackage,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a udp server with fixed port
|
||||
/// </summary>
|
||||
@@ -132,8 +139,9 @@ public class UDPServer
|
||||
{
|
||||
var elapsed = DateTime.Now - startTime;
|
||||
isTimeout = elapsed >= TimeSpan.FromMilliseconds(timeout);
|
||||
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
||||
if (isTimeout) break;
|
||||
|
||||
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
||||
using (await udpData.AcquireWriteLockAsync(timeleft))
|
||||
{
|
||||
if (udpData.ContainsKey(ipAddr) &&
|
||||
@@ -150,11 +158,11 @@ public class UDPServer
|
||||
if (data is null)
|
||||
{
|
||||
logger.Trace("Get nothing even after time out");
|
||||
return Optional<UDPData>.None;
|
||||
return new(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Optional.Some((UDPData)data.DeepClone());
|
||||
return new(data.DeepClone());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,8 +183,9 @@ public class UDPServer
|
||||
{
|
||||
var elapsed = DateTime.Now - startTime;
|
||||
isTimeout = elapsed >= TimeSpan.FromMilliseconds(timeout);
|
||||
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
||||
if (isTimeout) break;
|
||||
|
||||
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
|
||||
using (await udpData.AcquireReadLockAsync(timeleft))
|
||||
{
|
||||
if (udpData.ContainsKey(ipAddr) &&
|
||||
@@ -193,11 +202,11 @@ public class UDPServer
|
||||
if (data is null)
|
||||
{
|
||||
logger.Trace("Get nothing even after time out");
|
||||
return Optional<List<UDPData>>.None;
|
||||
return new(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Optional.Some((List<UDPData>)data);
|
||||
return new(data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,15 +222,15 @@ public class UDPServer
|
||||
{
|
||||
var data = await FindDataAsync(address, timeout);
|
||||
if (!data.HasValue)
|
||||
throw new Exception("Get None even after time out!");
|
||||
return new(new Exception("Get None even after time out!"));
|
||||
|
||||
var recvData = data.Value;
|
||||
if (recvData.Address != address || (port > 0 && recvData.Port != port))
|
||||
throw new Exception("Receive Data From Wrong Board!");
|
||||
return new(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 new(new Exception("Not RecvDataPackage!", retPack.Error));
|
||||
|
||||
return retPack.Value;
|
||||
}
|
||||
@@ -233,20 +242,20 @@ public class UDPServer
|
||||
/// <param name="port">UDP 端口</param>
|
||||
/// <param name="timeout">超时时间范围</param>
|
||||
/// <returns>接收数据包</returns>
|
||||
public async ValueTask<Result<WebProtocol.RecvDataPackage>> WaitForDataAsync
|
||||
public async ValueTask<Result<RecvDataPackage>> WaitForDataAsync
|
||||
(string address, int port = -1, int timeout = 1000)
|
||||
{
|
||||
var data = await FindDataAsync(address, timeout);
|
||||
if (!data.HasValue)
|
||||
throw new Exception("Get None even after time out!");
|
||||
return new(new Exception("Get None even after time out!"));
|
||||
|
||||
var recvData = data.Value;
|
||||
if (recvData.Address != address || (port >= 0 && recvData.Port != port))
|
||||
throw new Exception("Receive Data From Wrong Board!");
|
||||
return new(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 new(new Exception("Not RecvDataPackage!", retPack.Error));
|
||||
|
||||
return retPack.Value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user