try to fix bug, and add schema to swagger
This commit is contained in:
parent
c9660633f0
commit
5fa4860bfb
|
@ -40,16 +40,17 @@ try
|
||||||
Description = "Use FPGA in the cloud",
|
Description = "Use FPGA in the cloud",
|
||||||
Version = "v1"
|
Version = "v1"
|
||||||
});
|
});
|
||||||
// Generate Doc and Exam
|
// Generate Doc and Example
|
||||||
var executingAssembly = Assembly.GetExecutingAssembly();
|
options.IncludeXmlComments(Assembly.GetExecutingAssembly());
|
||||||
var xmlFilename = $"{executingAssembly.GetName().Name}.xml";
|
// var executingAssembly = Assembly.GetExecutingAssembly();
|
||||||
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
|
// var xmlFilename = $"{executingAssembly.GetName().Name}.xml";
|
||||||
var referencedProjectsXmlDocPaths =
|
// options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
|
||||||
executingAssembly.GetReferencedAssemblies()
|
// var referencedProjectsXmlDocPaths =
|
||||||
.Where(assembly => assembly.Name != null && assembly.Name.StartsWith("server", StringComparison.InvariantCultureIgnoreCase))
|
// executingAssembly.GetReferencedAssemblies()
|
||||||
.Select(assembly => Path.Combine(AppContext.BaseDirectory, $"{assembly.Name}.xml"))
|
// .Where(assembly => assembly.Name != null && assembly.Name.StartsWith("server", StringComparison.InvariantCultureIgnoreCase))
|
||||||
.Where(path => File.Exists(path));
|
// .Select(assembly => Path.Combine(AppContext.BaseDirectory, $"{assembly.Name}.xml"))
|
||||||
foreach (var xmlDocPath in referencedProjectsXmlDocPaths) options.IncludeXmlComments(xmlDocPath);
|
// .Where(path => File.Exists(path));
|
||||||
|
// foreach (var xmlDocPath in referencedProjectsXmlDocPaths) options.IncludeXmlComments(xmlDocPath);
|
||||||
});
|
});
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
@ -83,13 +84,13 @@ try
|
||||||
// API Get
|
// API Get
|
||||||
app.MapGet("/", () => Results.Redirect("/swagger"));
|
app.MapGet("/", () => Results.Redirect("/swagger"));
|
||||||
app.MapGet("/api/GetRecvDataArray", Router.API.GetRecvDataArray);
|
app.MapGet("/api/GetRecvDataArray", Router.API.GetRecvDataArray);
|
||||||
app.MapPut("/api/SendString", Router.API.SendString);
|
app.MapPost("/api/SendString", Router.API.SendString);
|
||||||
// API Put
|
// API Put
|
||||||
app.MapPut("/api/SendAddrPackage", Router.API.SendAddrPackage);
|
app.MapPost("/api/SendAddrPackage", Router.API.SendAddrPackage);
|
||||||
app.MapPut("/api/SendDataPackage", Router.API.SendDataPackage);
|
app.MapPost("/api/SendDataPackage", Router.API.SendDataPackage);
|
||||||
// API Jtag Put
|
// API Jtag
|
||||||
app.MapPut("/api/jtag/RunCommand", Router.API.Jtag.RunCommand);
|
app.MapPost("/api/jtag/RunCommand", Router.API.Jtag.RunCommand);
|
||||||
app.MapPut("/api/jtag/GetIDCode", Router.API.Jtag.GetDeviceIDCode);
|
app.MapGet("/api/jtag/GetIDCode", Router.API.Jtag.GetDeviceIDCode);
|
||||||
|
|
||||||
app.Run("http://localhost:5000");
|
app.Run("http://localhost:5000");
|
||||||
}
|
}
|
||||||
|
|
|
@ -235,13 +235,13 @@ class Jtag
|
||||||
var ret = false;
|
var ret = false;
|
||||||
var opts = new SendAddrPackOptions();
|
var opts = new SendAddrPackOptions();
|
||||||
|
|
||||||
opts.burstType = BurstType.FixedBurst;
|
opts.BurstType = BurstType.FixedBurst;
|
||||||
opts.burstLength = 0;
|
opts.BurstLength = 0;
|
||||||
opts.commandID = 0;
|
opts.CommandID = 0;
|
||||||
opts.address = devAddr;
|
opts.Address = devAddr;
|
||||||
|
|
||||||
// Read Jtag State Register
|
// Read Jtag State Register
|
||||||
opts.isWrite = false;
|
opts.IsWrite = false;
|
||||||
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
|
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
|
||||||
if (!ret) throw new Exception("Send Address Package Failed!");
|
if (!ret) throw new Exception("Send Address Package Failed!");
|
||||||
|
|
||||||
|
@ -249,26 +249,19 @@ class Jtag
|
||||||
if (!MsgBus.IsRunning)
|
if (!MsgBus.IsRunning)
|
||||||
throw new Exception("Message Bus not Working!");
|
throw new Exception("Message Bus not Working!");
|
||||||
|
|
||||||
var data = await MsgBus.UDPServer.FindDataAsync(address);
|
var retPack = await MsgBus.UDPServer.WaitForDataAsync(address, port);
|
||||||
if (!data.HasValue)
|
if (!retPack.IsSuccessful || !retPack.Value.IsSuccessful)
|
||||||
throw new Exception("Get None after Time out!");
|
throw new Exception("Send address package failed");
|
||||||
|
|
||||||
var recvData = data.Value;
|
var retPackOpts = retPack.Value.Options;
|
||||||
if (recvData.Address != address || recvData.Port != port)
|
if (retPackOpts.Data is null)
|
||||||
throw new Exception("Receive Data From Wrong Board!");
|
throw new Exception($"Data is Null, package: {retPackOpts.ToString()}");
|
||||||
|
|
||||||
var retPack = RecvDataPackage.FromBytes(recvData.Data);
|
var retPackLen = retPackOpts.Data.Length;
|
||||||
if (!retPack.IsSuccessful)
|
|
||||||
throw new Exception("Not RecvDataPackage!", retPack.Error);
|
|
||||||
|
|
||||||
if (retPack.Value.Options.data is null)
|
|
||||||
throw new Exception($"Data is Null, package: {retPack.Value.Options.ToString()}");
|
|
||||||
|
|
||||||
var retPackLen = retPack.Value.Options.data.Length;
|
|
||||||
if (retPackLen != 4)
|
if (retPackLen != 4)
|
||||||
throw 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 (uint)(NumberProcessor.BytesToNumber(retPack.Value.Options.data));
|
return (uint)(NumberProcessor.BytesToNumber(retPackOpts.Data));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask<Result<RecvDataPackage>> RunCommand(uint devAddr, uint command)
|
public async ValueTask<Result<RecvDataPackage>> RunCommand(uint devAddr, uint command)
|
||||||
|
@ -276,59 +269,60 @@ class Jtag
|
||||||
var ret = false;
|
var ret = false;
|
||||||
var opts = new SendAddrPackOptions();
|
var opts = new SendAddrPackOptions();
|
||||||
|
|
||||||
opts.burstType = BurstType.FixedBurst;
|
|
||||||
opts.burstLength = 0;
|
opts.BurstType = BurstType.FixedBurst;
|
||||||
opts.commandID = 0;
|
opts.BurstLength = 0;
|
||||||
opts.address = devAddr;
|
opts.CommandID = 0;
|
||||||
|
opts.Address = devAddr;
|
||||||
|
|
||||||
// Write Jtag State Register
|
// Write Jtag State Register
|
||||||
opts.isWrite = true;
|
opts.IsWrite = true;
|
||||||
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
|
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
|
||||||
if (!ret) throw new Exception("Send 1st Address Package Failed!");
|
if (!ret) throw new Exception("Send 1st address package failed!");
|
||||||
|
// Check Msg Bus
|
||||||
|
if (!MsgBus.IsRunning)
|
||||||
|
throw new Exception("Message bus not working!");
|
||||||
|
// Wait for Write Ack
|
||||||
|
{
|
||||||
|
var udpResp = await MsgBus.UDPServer.WaitForAckAsync(address, port);
|
||||||
|
if (!udpResp.IsSuccessful || !udpResp.Value.IsSuccessful)
|
||||||
|
throw new Exception("Send address package failed");
|
||||||
|
}
|
||||||
|
// Send Data Package
|
||||||
ret = await UDPClientPool.SendDataPackAsync(ep,
|
ret = await UDPClientPool.SendDataPackAsync(ep,
|
||||||
new SendDataPackage(NumberProcessor.NumberToBytes(command, 4).Value));
|
new SendDataPackage(NumberProcessor.NumberToBytes(command, 4).Value));
|
||||||
if (!ret) throw new Exception("Send Data Package Failed!");
|
if (!ret) throw new Exception("Send data package failed!");
|
||||||
|
|
||||||
// Read Jtag State Register
|
// Read Jtag State Register
|
||||||
opts.isWrite = false;
|
opts.IsWrite = false;
|
||||||
opts.address = JtagAddr.STATE;
|
opts.Address = JtagAddr.STATE;
|
||||||
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
|
ret = await UDPClientPool.SendAddrPackAsync(ep, new SendAddrPackage(opts));
|
||||||
if (!ret) throw new Exception("Send 2rd Address Package Failed!");
|
if (!ret) throw new Exception("Send 2rd Address Package Failed!");
|
||||||
|
// Wait for Read Data
|
||||||
|
{
|
||||||
|
var udpResp = await MsgBus.UDPServer.WaitForDataAsync(address, port);
|
||||||
|
if (!udpResp.IsSuccessful || !udpResp.Value.IsSuccessful)
|
||||||
|
throw new Exception("Send address package failed");
|
||||||
|
|
||||||
// Wait for Ack
|
return udpResp.Value;
|
||||||
if (!MsgBus.IsRunning)
|
}
|
||||||
throw new Exception("Message Bus not Working!");
|
|
||||||
|
|
||||||
var data = await MsgBus.UDPServer.FindDataAsync(address);
|
|
||||||
if (!data.HasValue)
|
|
||||||
throw new Exception("Get None after Time out!");
|
|
||||||
|
|
||||||
var recvData = data.Value;
|
|
||||||
if (recvData.Address != address || recvData.Port != port)
|
|
||||||
throw new Exception("Receive Data From Wrong Board!");
|
|
||||||
|
|
||||||
var retPack = RecvDataPackage.FromBytes(recvData.Data);
|
|
||||||
if (!retPack.IsSuccessful)
|
|
||||||
throw new Exception("Not RecvDataPackage!", retPack.Error);
|
|
||||||
|
|
||||||
|
|
||||||
return retPack;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask<Result<bool>> RunCommand(uint devAddr, uint command, uint result, uint resultMask = 0xFF_FF_FF_FF)
|
public async ValueTask<Result<bool>> RunCommand
|
||||||
|
(uint devAddr, uint command, uint result, uint resultMask = 0xFF_FF_FF_FF)
|
||||||
{
|
{
|
||||||
var ret = false;
|
var ret = false;
|
||||||
var retPack = await RunCommand(devAddr, command);
|
var retPack = await RunCommand(devAddr, command);
|
||||||
|
|
||||||
if (retPack.Value.Options.data is null)
|
if (retPack.Value.Options.Data is null)
|
||||||
throw 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;
|
var retPackLen = retPack.Value.Options.Data.Length;
|
||||||
if (retPackLen != 4)
|
if (retPackLen != 4)
|
||||||
throw 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 (NumberProcessor.BitsCheck(
|
if (NumberProcessor.BitsCheck(
|
||||||
NumberProcessor.BytesToNumber(retPack.Value.Options.data).Value, result, resultMask))
|
NumberProcessor.BytesToNumber(retPack.Value.Options.Data).Value, result, resultMask))
|
||||||
ret = true;
|
ret = true;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
@ -2,6 +2,7 @@ using System.Net;
|
||||||
using Common;
|
using Common;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using WebProtocol;
|
||||||
|
|
||||||
namespace Router
|
namespace Router
|
||||||
{
|
{
|
||||||
|
@ -54,28 +55,24 @@ namespace Router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpPost]
|
/// <summary>
|
||||||
|
/// 发送地址包
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address" example="127.0.0.1">IP地址</param>
|
||||||
|
/// <param name="port" example="1234">UDP 端口号</param>
|
||||||
|
/// <param name="opts">地址包选项</param>
|
||||||
|
[HttpPost("{address}/{port}")]
|
||||||
|
[ProducesResponseType(200)]
|
||||||
|
[ProducesResponseType(500)]
|
||||||
public static async ValueTask<IResult> SendAddrPackage(
|
public static async ValueTask<IResult> SendAddrPackage(
|
||||||
string address,
|
string address,
|
||||||
int port,
|
int port,
|
||||||
// WebProtocol.BurstType burstType,
|
[FromBody] SendAddrPackOptions opts)
|
||||||
// byte commandID,
|
|
||||||
// bool isWrite,
|
|
||||||
// byte burstLength,
|
|
||||||
// UInt32 devAddress)
|
|
||||||
WebProtocol.SendAddrPackOptions opts)
|
|
||||||
{
|
{
|
||||||
// WebProtocol.SendAddrPackOptions opts;
|
|
||||||
// opts.burstType = burstType;
|
|
||||||
// opts.commandID = commandID;
|
|
||||||
// opts.isWrite = isWrite;
|
|
||||||
// opts.burstLength = burstLength;
|
|
||||||
// opts.address = devAddress;
|
|
||||||
|
|
||||||
var endPoint = new IPEndPoint(IPAddress.Parse(address), port);
|
var endPoint = new IPEndPoint(IPAddress.Parse(address), port);
|
||||||
var ret = await UDPClientPool.SendAddrPackAsync(endPoint, new WebProtocol.SendAddrPackage(opts));
|
var ret = await UDPClientPool.SendAddrPackAsync(endPoint, new WebProtocol.SendAddrPackage(opts));
|
||||||
if (ret) { return Results.Json(new Response() { IsSuccess = true }); }
|
if (ret) { return TypedResults.Ok(); }
|
||||||
else { return Results.Json(new Response() { IsSuccess = false }); }
|
else { return TypedResults.InternalServerError(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async ValueTask<IResult> SendDataPackage(string address, int port, string data)
|
public static async ValueTask<IResult> SendDataPackage(string address, int port, string data)
|
||||||
|
@ -87,7 +84,7 @@ namespace Router
|
||||||
else { return Results.Json(new Response() { IsSuccess = false }); }
|
else { return Results.Json(new Response() { IsSuccess = false }); }
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet()]
|
[HttpGet("{address}")]
|
||||||
public static async ValueTask<IResult> GetRecvDataArray(string address)
|
public static async ValueTask<IResult> GetRecvDataArray(string address)
|
||||||
{
|
{
|
||||||
var ret = await MsgBus.UDPServer.GetDataArrayAsync(address);
|
var ret = await MsgBus.UDPServer.GetDataArrayAsync(address);
|
||||||
|
|
|
@ -11,19 +11,23 @@ public class UDPData
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 接受到的时间
|
/// 接受到的时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DateTime DateTime { get; set; }
|
public required DateTime DateTime { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送来源的IP地址
|
/// 发送来源的IP地址
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Address { get; set; }
|
public required string Address { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送来源的端口号
|
/// 发送来源的端口号
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Port { get; set; }
|
public required int Port { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 接受到的数据
|
/// 接受到的数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte[] Data { get; set; }
|
public required byte[] Data { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 是否被读取过
|
||||||
|
/// </summary>
|
||||||
|
public required bool HasRead { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将UDP Data 转化为Json 格式字符串
|
/// 将UDP Data 转化为Json 格式字符串
|
||||||
|
@ -94,6 +98,8 @@ public class UDPServer
|
||||||
{
|
{
|
||||||
data = udpData[ipAddr][0];
|
data = udpData[ipAddr][0];
|
||||||
udpData[ipAddr].RemoveAt(0);
|
udpData[ipAddr].RemoveAt(0);
|
||||||
|
logger.Debug($"Find UDP Data: {data.ToString()}");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,6 +109,7 @@ public class UDPServer
|
||||||
|
|
||||||
if (data == null)
|
if (data == null)
|
||||||
{
|
{
|
||||||
|
logger.Trace("Get nothing even after time out");
|
||||||
return Optional.None<UDPData>();
|
return Optional.None<UDPData>();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -137,6 +144,8 @@ public class UDPServer
|
||||||
{
|
{
|
||||||
data = udpData[ipAddr][0];
|
data = udpData[ipAddr][0];
|
||||||
udpData[ipAddr].RemoveAt(0);
|
udpData[ipAddr].RemoveAt(0);
|
||||||
|
logger.Debug($"Find UDP Data: {data.ToString()}");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,6 +155,7 @@ public class UDPServer
|
||||||
|
|
||||||
if (data == null)
|
if (data == null)
|
||||||
{
|
{
|
||||||
|
logger.Trace("Get nothing even after time out");
|
||||||
return Optional.None<UDPData>();
|
return Optional.None<UDPData>();
|
||||||
}
|
}
|
||||||
else
|
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)
|
public async ValueTask<Optional<List<UDPData>>> GetDataArrayAsync(string ipAddr, int timeout = 1000)
|
||||||
{
|
{
|
||||||
List<UDPData>? data = null;
|
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)
|
private void ReceiveHandler(IAsyncResult res)
|
||||||
{
|
{
|
||||||
var remoteEP = new IPEndPoint(IPAddress.Any, listenPort);
|
var remoteEP = new IPEndPoint(IPAddress.Any, listenPort);
|
||||||
|
@ -210,16 +276,19 @@ public class UDPServer
|
||||||
Port = remotePort,
|
Port = remotePort,
|
||||||
Data = bytes,
|
Data = bytes,
|
||||||
DateTime = nowtime,
|
DateTime = nowtime,
|
||||||
|
HasRead = false,
|
||||||
});
|
});
|
||||||
logger.Trace("Receive data from old client");
|
logger.Trace("Receive data from old client");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
udpData.Add(remoteAddress, new List<UDPData>([new UDPData(){
|
udpData.Add(remoteAddress, new List<UDPData>([new UDPData()
|
||||||
|
{
|
||||||
Address = remoteAddress,
|
Address = remoteAddress,
|
||||||
Port = remotePort,
|
Port = remotePort,
|
||||||
Data = bytes,
|
Data = bytes,
|
||||||
DateTime = nowtime,
|
DateTime = nowtime,
|
||||||
|
HasRead = false,
|
||||||
}]));
|
}]));
|
||||||
logger.Trace("Receive data from new client");
|
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($"Receive Data from {remoteStr} at {nowtime.ToString()}:");
|
||||||
logger.Debug($"Original Data: {BitConverter.ToString(bytes).Replace("-", " ")}");
|
logger.Debug($"Original Data: {BitConverter.ToString(bytes).Replace("-", " ")}");
|
||||||
if (recvData.Length != 0) logger.Debug(recvData);
|
if (recvData.Length != 0) logger.Debug(recvData);
|
||||||
RecordAllData();
|
// RecordAllData();
|
||||||
|
|
||||||
|
|
||||||
listener.BeginReceive(new AsyncCallback(ReceiveHandler), null);
|
listener.BeginReceive(new AsyncCallback(ReceiveHandler), null);
|
||||||
|
|
|
@ -28,33 +28,44 @@ namespace WebProtocol
|
||||||
|
|
||||||
|
|
||||||
/// <summary> Package options which to send address to read or write </summary>
|
/// <summary> Package options which to send address to read or write </summary>
|
||||||
/// <example>
|
public class SendAddrPackOptions
|
||||||
/// {
|
|
||||||
/// "burType":0,
|
|
||||||
/// "commandID":1,
|
|
||||||
/// }
|
|
||||||
/// </example>
|
|
||||||
public struct SendAddrPackOptions
|
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary> Package Burst Type </summary>
|
/// <summary>
|
||||||
[JsonProperty("burstType")]
|
/// 突发类型
|
||||||
public BurstType burstType;
|
/// </summary>
|
||||||
/// <example>1</example>
|
/// <example>0</example>
|
||||||
public byte commandID;
|
public BurstType BurstType { get; set; }
|
||||||
/// <summary> 标识写入还是读取 </summary>
|
|
||||||
/// <example> true </example>
|
|
||||||
public bool isWrite;
|
|
||||||
/// <summary> 突发长度:0是32bits,255是32bits x 256 </summary>
|
|
||||||
/// <example> 255 </example>
|
|
||||||
public byte burstLength;
|
|
||||||
/// <example> 0x10_00_00_00 </example>
|
|
||||||
public UInt32 address;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Convert to Json String
|
/// 任务ID
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns> Json String </returns>
|
/// <example>1</example>
|
||||||
|
public byte CommandID { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 标识写入还是读取
|
||||||
|
/// </summary>
|
||||||
|
/// <example>true</example>
|
||||||
|
public bool IsWrite { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 突发长度:0是32bits,255是32bits x 256
|
||||||
|
/// </summary>
|
||||||
|
/// <example>255</example>
|
||||||
|
public byte BurstLength { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 目标地址
|
||||||
|
/// </summary>
|
||||||
|
/// <example>0</example>
|
||||||
|
public UInt32 Address { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 转换为Json格式字符串
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Json String</returns>
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return JsonConvert.SerializeObject(this);
|
return JsonConvert.SerializeObject(this);
|
||||||
|
@ -62,7 +73,7 @@ namespace WebProtocol
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Package Options which to receive from boards </summary>
|
/// <summary> Package Options which to receive from boards </summary>
|
||||||
public struct RecvPackOptions
|
public class RecvPackOptions
|
||||||
{
|
{
|
||||||
/// <summary> 数据包类型 </summary>
|
/// <summary> 数据包类型 </summary>
|
||||||
public enum PackType
|
public enum PackType
|
||||||
|
@ -76,19 +87,26 @@ namespace WebProtocol
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 数据包类型
|
/// 数据包类型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public PackType type;
|
/// <example>0</example>
|
||||||
|
public PackType Type { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Task ID
|
/// Task ID
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte commandID;
|
/// <example>0</example>
|
||||||
|
public byte CommandID { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether is succeed to finish command
|
/// Whether is succeed to finish command
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool isSuccess;
|
/// <example>true</example>
|
||||||
|
public bool IsSuccess { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Return Data
|
/// Return Data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public byte[]? data;
|
/// <example>[]</example>
|
||||||
|
public byte[]? Data { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Convert to Json String
|
/// Convert to Json String
|
||||||
|
@ -101,7 +119,7 @@ namespace WebProtocol
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Server->FPGA 地址包 </summary>
|
/// <summary> Server->FPGA 地址包 </summary>
|
||||||
public struct SendAddrPackage
|
public class SendAddrPackage
|
||||||
{
|
{
|
||||||
readonly byte sign = (byte)PackSign.SendAddr;
|
readonly byte sign = (byte)PackSign.SendAddr;
|
||||||
readonly byte commandType;
|
readonly byte commandType;
|
||||||
|
@ -115,12 +133,12 @@ namespace WebProtocol
|
||||||
/// <param name="opts"> 地址包选项 </param>
|
/// <param name="opts"> 地址包选项 </param>
|
||||||
public SendAddrPackage(SendAddrPackOptions opts)
|
public SendAddrPackage(SendAddrPackOptions opts)
|
||||||
{
|
{
|
||||||
byte byteBurstType = Convert.ToByte((byte)opts.burstType << 6);
|
byte byteBurstType = Convert.ToByte((byte)opts.BurstType << 6);
|
||||||
byte byteCommandID = Convert.ToByte((opts.commandID & 0x03) << 4);
|
byte byteCommandID = Convert.ToByte((opts.CommandID & 0x03) << 4);
|
||||||
byte byteIsWrite = (opts.isWrite ? (byte)0x01 : (byte)0x00);
|
byte byteIsWrite = (opts.IsWrite ? (byte)0x01 : (byte)0x00);
|
||||||
this.commandType = Convert.ToByte(byteBurstType | byteCommandID | byteIsWrite);
|
this.commandType = Convert.ToByte(byteBurstType | byteCommandID | byteIsWrite);
|
||||||
this.burstLength = opts.burstLength;
|
this.burstLength = opts.BurstLength;
|
||||||
this.address = opts.address;
|
this.address = opts.Address;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -178,12 +196,12 @@ namespace WebProtocol
|
||||||
/// <returns> 字符串 </returns>
|
/// <returns> 字符串 </returns>
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
SendAddrPackOptions opts;
|
var opts = new SendAddrPackOptions();
|
||||||
opts.burstType = (BurstType)(commandType >> 6);
|
opts.BurstType = (BurstType)(commandType >> 6);
|
||||||
opts.commandID = Convert.ToByte((commandType >> 4) & 0b0011);
|
opts.CommandID = Convert.ToByte((commandType >> 4) & 0b0011);
|
||||||
opts.isWrite = Convert.ToBoolean(commandType & 0x01);
|
opts.IsWrite = Convert.ToBoolean(commandType & 0x01);
|
||||||
opts.burstLength = burstLength;
|
opts.BurstLength = burstLength;
|
||||||
opts.address = address;
|
opts.Address = address;
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(opts);
|
return JsonConvert.SerializeObject(opts);
|
||||||
}
|
}
|
||||||
|
@ -287,16 +305,24 @@ namespace WebProtocol
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
RecvPackOptions opts;
|
var opts = new RecvPackOptions();
|
||||||
opts.type = RecvPackOptions.PackType.ReadResp;
|
opts.Type = RecvPackOptions.PackType.ReadResp;
|
||||||
opts.commandID = commandID;
|
opts.CommandID = commandID;
|
||||||
opts.isSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? false : true);
|
opts.IsSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? false : true);
|
||||||
opts.data = bodyData;
|
opts.Data = bodyData;
|
||||||
|
|
||||||
return opts;
|
return opts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 读取是否成功
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSuccessful
|
||||||
|
{
|
||||||
|
get { return Convert.ToBoolean((resp >> 1) == 0b01 ? false : true); }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从字节数组构建读响应包
|
/// 从字节数组构建读响应包
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -342,16 +368,24 @@ namespace WebProtocol
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
RecvPackOptions opts;
|
var opts = new RecvPackOptions();
|
||||||
opts.type = RecvPackOptions.PackType.WriteResp;
|
opts.Type = RecvPackOptions.PackType.WriteResp;
|
||||||
opts.commandID = commandID;
|
opts.CommandID = commandID;
|
||||||
opts.isSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? false : true);
|
opts.IsSuccess = Convert.ToBoolean((resp >> 1) == 0b01 ? false : true);
|
||||||
opts.data = null;
|
opts.Data = null;
|
||||||
|
|
||||||
return opts;
|
return opts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 写入是否成功
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSuccessful
|
||||||
|
{
|
||||||
|
get { return Convert.ToBoolean((resp >> 1) == 0b01 ? false : true); }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从字节数组构建写响应包
|
/// 从字节数组构建写响应包
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Reference in New Issue