Compare commits

..

No commits in common. "7f99a5be24a447857b5189b9c00606e094b2f54f" and "652620398159da392db01ef2258c3d0c805b34ee" have entirely different histories.

6 changed files with 122 additions and 169 deletions

View File

@ -3,12 +3,6 @@
pwd
echo
clean:
rm -rf "server/bin"
rm -rf "server/obj"
rm -rf "server.test/bin"
rm -rf "server.test/obj"
[working-directory: "server"]
publish: _show-dir
dotnet publish --self-contained false -t:PublishAllRids

View File

@ -18,10 +18,10 @@ namespace Common
{
if (length > 8)
{
return new(new ArgumentException(
throw new ArgumentException(
"Unsigned long number can't over 8 bytes(64 bits).",
nameof(length)
));
);
}
var arr = new byte[length];
@ -53,10 +53,10 @@ namespace Common
{
if (bytes.Length > 8)
{
return new(new ArgumentException(
throw new ArgumentException(
"Unsigned long number can't over 8 bytes(64 bits).",
nameof(bytes)
));
);
}
ulong num = 0;
@ -106,7 +106,7 @@ namespace Common
/// <returns>[TODO:return]</returns>
public static Result<ulong> MultiBitsToNumber(ulong bits1, uint bits1Len, ulong bits2, uint bits2Len)
{
if (bits1Len + bits2Len > 64) return new(new ArgumentException("Two Bits is more than 64 bits"));
if (bits1Len + bits2Len > 64) throw new ArgumentException("Two Bits is more than 64 bits");
ulong num = (bits1 << Convert.ToInt32(bits2Len)) | bits2;
return num;
@ -122,7 +122,7 @@ namespace Common
/// <returns>[TODO:return]</returns>
public static Result<uint> MultiBitsToNumber(uint bits1, uint bits1Len, uint bits2, uint bits2Len)
{
if (bits1Len + bits2Len > 64) return new(new ArgumentException("Two Bits is more than 64 bits"));
if (bits1Len + bits2Len > 64) throw new ArgumentException("Two Bits is more than 64 bits");
uint num = (bits1 << Convert.ToInt32(bits2Len)) | bits2;
return num;

View File

@ -1,6 +1,5 @@
using System.Net;
using Common;
using DotNext;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using WebProtocol;
@ -150,7 +149,6 @@ public class JtagController : ControllerBase
{
return "This is Jtag Controller";
}
/// <summary>
/// 执行一个Jtag命令
/// </summary>
@ -184,16 +182,8 @@ public class JtagController : ControllerBase
var jtagCtrl = new JtagClient.Jtag(address, port);
var ret = await jtagCtrl.ReadIDCode();
if (ret.IsSuccessful)
{
logger.Info($"Get device {address} ID code: 0x{ret.Value:X4}");
return TypedResults.Ok(ret.Value);
}
else
{
logger.Error(ret.Error);
return TypedResults.InternalServerError(ret.Error);
}
if (ret.IsSuccessful) { return TypedResults.Ok(ret.Value); }
else { return TypedResults.InternalServerError(ret.Error); }
}
/// <summary>
@ -227,8 +217,7 @@ public class JtagController : ControllerBase
await file.CopyToAsync(stream);
}
logger.Info($"Device {address} Upload Bitstream Successfully");
return TypedResults.Ok("Bitstream Upload Successfully");
return TypedResults.Ok("文件上传成功");
}
/// <summary>
@ -276,16 +265,8 @@ public class JtagController : ControllerBase
var jtagCtrl = new JtagClient.Jtag(address, port);
var ret = await jtagCtrl.DownloadBitstream(fileBytes);
if (ret.IsSuccessful)
{
logger.Info($"Device {address} dowload bitstream successfully");
return TypedResults.Ok(ret.Value);
}
else
{
logger.Error(ret.Error);
return TypedResults.InternalServerError(ret.Error);
}
if (ret.IsSuccessful) { return TypedResults.Ok(ret.Value); }
else { return TypedResults.InternalServerError(ret.Error); }
}
}
catch (Exception error)

View File

@ -213,34 +213,23 @@ public static class JtagCmd
public const UInt32 CMD_JTAG_IDLE_DELAY = 0b0101;
}
/// <summary>
/// Jtag控制器
/// </summary>
public class Jtag
class Jtag
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
readonly int timeout;
readonly int port;
readonly string address;
private IPEndPoint ep;
/// <summary>
/// Jtag构造函数
/// </summary>
/// <param name="address">目标IP地址</param>
/// <param name="port">目标UDP端口</param>
/// <param name="timeout">超时时间</param>
public Jtag(string address, int port, int timeout = 2000)
public Jtag(string address, int port, int outTime = 2000)
{
this.address = address;
this.port = port;
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
this.timeout = timeout;
this.timeout = outTime;
}
async ValueTask<Result<uint>> ReadFIFO(uint devAddr)
public async ValueTask<Result<uint>> ReadFIFO(uint devAddr)
{
var ret = false;
var opts = new SendAddrPackOptions();
@ -253,23 +242,23 @@ public class Jtag
// Read Jtag State Register
opts.IsWrite = false;
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
if (!ret) return new(new Exception("Send Address Package Failed!"));
if (!ret) throw new Exception("Send Address Package Failed!");
// Wait for Read Ack
if (!MsgBus.IsRunning)
return new(new Exception("Message Bus not Working!"));
throw new Exception("Message Bus not Working!");
var retPack = await MsgBus.UDPServer.WaitForDataAsync(address, port);
if (!retPack.IsSuccessful || !retPack.Value.IsSuccessful)
return new(new Exception("Send address package failed"));
throw new Exception("Send address package failed");
var retPackOpts = retPack.Value.Options;
if (retPackOpts.Data is null)
return new(new Exception($"Data is Null, package: {retPackOpts.ToString()}"));
throw new Exception($"Data is Null, package: {retPackOpts.ToString()}");
var retPackLen = retPackOpts.Data.Length;
if (retPackLen != 4)
return new(new Exception($"RecvDataPackage BodyData Length not Equal to 4: Total {retPackLen} bytes"));
throw new Exception($"RecvDataPackage BodyData Length not Equal to 4: Total {retPackLen} bytes");
return Convert.ToUInt32(Common.Number.BytesToNumber(retPackOpts.Data).Value);
}
@ -288,36 +277,36 @@ public class Jtag
// Write Jtag State Register
opts.IsWrite = true;
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
if (!ret) return new(new Exception("Send 1st address package failed!"));
if (!ret) throw new Exception("Send 1st address package failed!");
// Send Data Package
ret = await UDPClientPool.SendDataPackAsync(ep,
new SendDataPackage(Common.Number.NumberToBytes(data, 4).Value));
if (!ret) return new(new Exception("Send data package failed!"));
if (!ret) throw new Exception("Send data package failed!");
// Check Msg Bus
if (!MsgBus.IsRunning)
return new(new Exception("Message bus not working!"));
throw new Exception("Message bus not working!");
// Wait for Write Ack
var udpWriteAck = await MsgBus.UDPServer.WaitForAckAsync(address, port);
if (!udpWriteAck.IsSuccessful) return new(udpWriteAck.Error);
if (!udpWriteAck.IsSuccessful) throw udpWriteAck.Error;
else if (!udpWriteAck.Value.IsSuccessful)
return new(new Exception("Send address package failed"));
throw new Exception("Send address package failed");
// Read Jtag State Register
opts.IsWrite = false;
opts.Address = JtagAddr.STATE;
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
if (!ret) return new(new Exception("Send 2rd Address Package Failed!"));
if (!ret) throw new Exception("Send 2rd Address Package Failed!");
// Wait for Read Data
var udpDataResp = await MsgBus.UDPServer.WaitForDataAsync(address, port);
if (!udpDataResp.IsSuccessful) return new(udpDataResp.Error);
if (!udpDataResp.IsSuccessful) throw udpDataResp.Error;
else if (!udpDataResp.Value.IsSuccessful)
return new(new Exception("Send address package failed"));
throw new Exception("Send address package failed");
return udpDataResp.Value;
}
async ValueTask<Result<RecvDataPackage>> WriteFIFO(UInt32 devAddr, byte[] dataArray)
public async ValueTask<Result<RecvDataPackage>> WriteFIFO(UInt32 devAddr, byte[] dataArray)
{
var ret = false;
var opts = new SendAddrPackOptions();
@ -329,7 +318,7 @@ public class Jtag
// Check Msg Bus
if (!MsgBus.IsRunning)
return new(new Exception("Message bus not working!"));
throw new Exception("Message bus not working!");
var writeTimes = dataArray.Length / (256 * (32 / 8)) + 1;
for (var i = 0; i < writeTimes; i++)
@ -339,7 +328,7 @@ public class Jtag
opts.IsWrite = true;
opts.BurstLength = isLastData ? (byte)(dataArray.Length - i * (256 * (32 / 8)) - 1) : (byte)255;
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
if (!ret) return new(new Exception("Send 1st address package failed!"));
if (!ret) throw new Exception("Send 1st address package failed!");
// Send Data Package
ret = await UDPClientPool.SendDataPackAsync(ep,
new SendDataPackage(
@ -348,13 +337,13 @@ public class Jtag
dataArray[(i * (256 * (32 / 8)))..((i + 1) * (256 * (32 / 8)) - 1)]
)
);
if (!ret) return new(new Exception("Send data package failed!"));
if (!ret) throw new Exception("Send data package failed!");
// Wait for Write Ack
var udpWriteAck = await MsgBus.UDPServer.WaitForAckAsync(address, port);
if (!udpWriteAck.IsSuccessful) return new(udpWriteAck.Error);
if (!udpWriteAck.IsSuccessful) throw udpWriteAck.Error;
else if (!udpWriteAck.Value.IsSuccessful)
return new(new Exception("Send address package failed"));
throw new Exception("Send address package failed");
}
// Read Jtag State Register
@ -362,29 +351,29 @@ public class Jtag
opts.BurstLength = 0;
opts.Address = JtagAddr.STATE;
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
if (!ret) return new(new Exception("Send 2rd Address Package Failed!"));
if (!ret) throw new Exception("Send 2rd Address Package Failed!");
// Wait for Read Data
var udpDataResp = await MsgBus.UDPServer.WaitForDataAsync(address, port);
if (!udpDataResp.IsSuccessful) return new(udpDataResp.Error);
if (!udpDataResp.IsSuccessful) throw udpDataResp.Error;
else if (!udpDataResp.Value.IsSuccessful)
return new(new Exception("Send address package failed"));
throw new Exception("Send address package failed");
return udpDataResp.Value;
}
async ValueTask<Result<bool>> WriteFIFO
public async ValueTask<Result<bool>> WriteFIFO
(UInt32 devAddr, UInt32 data, UInt32 result, UInt32 resultMask = 0xFF_FF_FF_FF)
{
var ret = false;
var retPack = await WriteFIFO(devAddr, data);
if (!retPack.IsSuccessful) return new(retPack.Error);
if (!retPack.IsSuccessful) throw retPack.Error;
if (retPack.Value.Options.Data is null)
return new(new Exception($"Data is Null, package: {retPack.Value.Options.ToString()}"));
throw new Exception($"Data is Null, package: {retPack.Value.Options.ToString()}");
var retPackLen = retPack.Value.Options.Data.Length;
if (retPackLen != 4)
return new(new Exception($"RecvDataPackage BodyData Length not Equal to 4: Total {retPackLen} bytes"));
throw new Exception($"RecvDataPackage BodyData Length not Equal to 4: Total {retPackLen} bytes");
if (Common.Number.BitsCheck(
Common.Number.BytesToNumber(retPack.Value.Options.Data).Value, result, resultMask))
@ -393,18 +382,18 @@ public class Jtag
return ret;
}
async ValueTask<Result<bool>> WriteFIFO
public async ValueTask<Result<bool>> WriteFIFO
(UInt32 devAddr, byte[] data, UInt32 result, UInt32 resultMask = 0xFF_FF_FF_FF)
{
var ret = false;
var retPack = await WriteFIFO(devAddr, data);
if (retPack.Value.Options.Data is null)
return new(new Exception($"Data is Null, package: {retPack.Value.Options.ToString()}"));
throw new Exception($"Data is Null, package: {retPack.Value.Options.ToString()}");
var retPackLen = retPack.Value.Options.Data.Length;
if (retPackLen != 4)
return new(new Exception($"RecvDataPackage BodyData Length not Equal to 4: Total {retPackLen} bytes"));
throw new Exception($"RecvDataPackage BodyData Length not Equal to 4: Total {retPackLen} bytes");
if (Common.Number.BitsCheck(
Common.Number.BytesToNumber(retPack.Value.Options.Data).Value, result, resultMask))
@ -442,7 +431,7 @@ public class Jtag
async ValueTask<Result<bool>> IdleDelay(UInt32 milliseconds)
{
if (milliseconds > Math.Pow(2, 28)) return new(new Exception("Timespan is over 2^28 milliseconds"));
if (milliseconds > Math.Pow(2, 28)) throw new Exception("Timespan is over 2^28 milliseconds");
return await WriteFIFO(
JtagAddr.WRITE_CMD,
@ -457,8 +446,8 @@ public class Jtag
JtagAddr.WRITE_DATA,
Common.Number.MultiBitsToNumber(0, 22, command, JtagCmd.LEN_JTAG_DR).Value, 0, 0);
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Write JTAG_DR_IDCODE Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Write JTAG_DR_IDCODE Failed");
}
{
var ret = await WriteFIFO(
@ -466,8 +455,8 @@ public class Jtag
Common.Number.MultiBitsToNumber(JtagCmd.CMD_JTAG_LOAD_IR, JtagCmd.LEN_CMD_JTAG, 10, 28).Value,
0x01_00_00_00, JtagState.CMD_EXEC_FINISH);
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Write CMD_JTAG_LOAD_IR Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Write CMD_JTAG_LOAD_IR Failed");
}
return await ClearWriteDataReg();
}
@ -475,7 +464,7 @@ public class Jtag
async ValueTask<Result<bool>> LoadDRCareInput(byte[] bytesArray)
{
var bytesLen = ((uint)(bytesArray.Length * 8));
if (bytesLen > Math.Pow(2, 28)) return new(new Exception("Length is over 2^(28 - 3)"));
if (bytesLen > Math.Pow(2, 28)) throw new Exception("Length is over 2^(28 - 3)");
{
var ret = await WriteFIFO(
@ -483,21 +472,21 @@ public class Jtag
Common.Number.MultiBitsToNumber(JtagCmd.CMD_JTAG_LOAD_DR_CAREI, JtagCmd.LEN_CMD_JTAG, bytesLen, 28).Value,
0x01_00_00_00, JtagState.CMD_EXEC_FINISH);
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Write CMD_JTAG_LOAD_DR_CAREI Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Write CMD_JTAG_LOAD_DR_CAREI Failed");
}
{
var ret = await WriteFIFO(JtagAddr.WRITE_DATA, bytesArray, 0x01_00_00_00, JtagState.CMD_EXEC_FINISH);
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Write Data Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Write Data Failed");
}
return true;
}
async ValueTask<Result<UInt32>> LoadDRCareOutput(UInt32 bytesLen)
{
if (bytesLen > Math.Pow(2, 28)) return new(new Exception("Length is over 2^(28 - 3)"));
if (bytesLen > Math.Pow(2, 28)) throw new Exception("Length is over 2^(28 - 3)");
var ret = await WriteFIFO(
JtagAddr.WRITE_CMD,
@ -507,7 +496,7 @@ public class Jtag
if (ret.Value)
return await ReadFIFO(JtagAddr.READ_DATA);
else
return new(new Exception("LoadDRCareo Failed!"));
throw new Exception("LoadDRCareo Failed!");
}
public async ValueTask<Result<uint>> ReadIDCode()
@ -515,30 +504,28 @@ public class Jtag
// Clear Data
await MsgBus.UDPServer.ClearUDPData(this.address);
logger.Trace($"Clear up udp server {this.address} receive data");
Result<bool> ret;
ret = await ClearAllRegisters();
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Clear All Registers Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Clear All Registers Failed");
ret = await RunTest();
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Run Test Failed");
ret = await ExecRDCmd(JtagCmd.JTAG_DR_IDCODE);
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_IDCODE Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Execute Command JTAG_DR_IDCODE Failed");
ret = await ClearWriteDataReg();
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Clear Write Registers Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Clear Write Registers Failed");
var retData = await LoadDRCareOutput(4);
if (!retData.IsSuccessful)
{
return new(new Exception("Get ID Code Failed"));
throw new Exception("Get ID Code Failed");
}
return retData.Value;
@ -549,66 +536,66 @@ public class Jtag
// Clear Data
await MsgBus.UDPServer.ClearUDPData(this.address);
logger.Trace($"Clear up udp server {this.address} receive data");
Result<bool> ret;
ret = await CloseTest();
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Close Test Failed");
ret = await RunTest();
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Run Test Failed");
logger.Trace("Jtag initialize");
ret = await ExecRDCmd(JtagCmd.JTAG_DR_JRST);
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_JRST Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Execute Command JTAG_DR_JRST Failed");
ret = await RunTest();
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Run Test Failed");
ret = await ExecRDCmd(JtagCmd.JTAG_DR_CFGI);
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_CFGI Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Execute Command JTAG_DR_CFGI Failed");
logger.Trace("Jtag ready to write bitstream");
ret = await IdleDelay(75000);
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag IDLE Delay Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag IDLE Delay Failed");
ret = await LoadDRCareInput(bitstream);
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Load Data Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Load Data Failed");
logger.Trace("Jtag write bitstream");
ret = await CloseTest();
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Close Test Failed");
ret = await RunTest();
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Run Test Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Run Test Failed");
ret = await ExecRDCmd(JtagCmd.JTAG_DR_JWAKEUP);
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Execute Command JTAG_DR_JWAKEUP Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Execute Command JTAG_DR_JWAKEUP Failed");
logger.Trace("Jtag reset device");
ret = await IdleDelay(1000);
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag IDLE Delay Failed"));
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag IDLE Delay Failed");
ret = await CloseTest();
if (!ret.IsSuccessful) return new(ret.Error);
else if (!ret.Value) return new(new Exception("Jtag Close Test Failed"));
logger.Trace("Jtag download bitstream successfully");
if (!ret.IsSuccessful) throw ret.Error;
else if (!ret.Value) throw new Exception("Jtag Close Test Failed");
return true;
}

View File

@ -5,7 +5,6 @@ using System.Text;
using DotNext;
using DotNext.Threading;
using Newtonsoft.Json;
using WebProtocol;
/// <summary> UDP接受数据包格式 </summary>
public class UDPData
@ -59,6 +58,9 @@ public class UDPData
}
}
/// <summary>
/// UDP Server
/// </summary>
/// <summary>
/// UDP 服务器
/// </summary>
@ -78,15 +80,6 @@ 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>
@ -139,9 +132,8 @@ public class UDPServer
{
var elapsed = DateTime.Now - startTime;
isTimeout = elapsed >= TimeSpan.FromMilliseconds(timeout);
if (isTimeout) break;
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
using (await udpData.AcquireWriteLockAsync(timeleft))
{
if (udpData.ContainsKey(ipAddr) &&
@ -158,11 +150,11 @@ public class UDPServer
if (data is null)
{
logger.Trace("Get nothing even after time out");
return new(null);
return Optional<UDPData>.None;
}
else
{
return new(data.DeepClone());
return Optional.Some((UDPData)data.DeepClone());
}
}
@ -183,9 +175,8 @@ public class UDPServer
{
var elapsed = DateTime.Now - startTime;
isTimeout = elapsed >= TimeSpan.FromMilliseconds(timeout);
if (isTimeout) break;
timeleft = TimeSpan.FromMilliseconds(timeout) - elapsed;
using (await udpData.AcquireReadLockAsync(timeleft))
{
if (udpData.ContainsKey(ipAddr) &&
@ -202,11 +193,11 @@ public class UDPServer
if (data is null)
{
logger.Trace("Get nothing even after time out");
return new(null);
return Optional<List<UDPData>>.None;
}
else
{
return new(data);
return Optional.Some((List<UDPData>)data);
}
}
@ -222,15 +213,15 @@ public class UDPServer
{
var data = await FindDataAsync(address, timeout);
if (!data.HasValue)
return new(new Exception("Get None even after time out!"));
throw new Exception("Get None even after time out!");
var recvData = data.Value;
if (recvData.Address != address || (port > 0 && recvData.Port != port))
return new(new Exception("Receive Data From Wrong Board!"));
throw new Exception("Receive Data From Wrong Board!");
var retPack = WebProtocol.RecvRespPackage.FromBytes(recvData.Data);
if (!retPack.IsSuccessful)
return new(new Exception("Not RecvDataPackage!", retPack.Error));
throw new Exception("Not RecvDataPackage!", retPack.Error);
return retPack.Value;
}
@ -242,20 +233,20 @@ public class UDPServer
/// <param name="port">UDP 端口</param>
/// <param name="timeout">超时时间范围</param>
/// <returns>接收数据包</returns>
public async ValueTask<Result<RecvDataPackage>> WaitForDataAsync
public async ValueTask<Result<WebProtocol.RecvDataPackage>> WaitForDataAsync
(string address, int port = -1, int timeout = 1000)
{
var data = await FindDataAsync(address, timeout);
if (!data.HasValue)
return new(new Exception("Get None even after time out!"));
throw new Exception("Get None even after time out!");
var recvData = data.Value;
if (recvData.Address != address || (port >= 0 && recvData.Port != port))
return new(new Exception("Receive Data From Wrong Board!"));
throw new Exception("Receive Data From Wrong Board!");
var retPack = WebProtocol.RecvDataPackage.FromBytes(recvData.Data);
if (!retPack.IsSuccessful)
return new(new Exception("Not RecvDataPackage!", retPack.Error));
throw new Exception("Not RecvDataPackage!", retPack.Error);
return retPack.Value;
}

View File

@ -234,18 +234,18 @@ namespace WebProtocol
{
if (bytes.Length != 8)
{
return new(new ArgumentException(
throw new ArgumentException(
"Bytes are not equal to 8 bytes.",
nameof(bytes)
));
);
}
if (checkSign && bytes[0] != (byte)PackSign.SendAddr)
{
return new(new ArgumentException(
"The sign of bytes is not SendAddr Package, but you can disable it by set checkSign false.",
nameof(bytes)
));
throw new ArgumentException(
"The sign of bytes is not SendAddr Package, but you can disable it by set checkSign false.",
nameof(bytes)
);
}
var address = Common.Number.BytesToNumber(bytes[4..]).Value;
@ -377,10 +377,10 @@ namespace WebProtocol
public static Result<RecvDataPackage> FromBytes(byte[] bytes)
{
if (bytes[0] != (byte)PackSign.RecvData)
return new(new ArgumentException(
throw new ArgumentException(
$"The sign of bytes is not RecvData Package, Sign: 0x{BitConverter.ToString([bytes[0]])}",
nameof(bytes)
));
);
return new RecvDataPackage(bytes[1], bytes[2], bytes[4..]);
}
@ -480,10 +480,10 @@ namespace WebProtocol
public static Result<RecvRespPackage> FromBytes(byte[] bytes)
{
if (bytes[0] != (byte)PackSign.RecvResp)
return new(new ArgumentException(
throw new ArgumentException(
$"The sign of bytes is not RecvResp Package, Sign: 0x{BitConverter.ToString([bytes[0]])}",
nameof(bytes)
));
);
return new RecvRespPackage(bytes[1], bytes[2]);
}