Server: finish jtag controller; Web: finish sibebar animation

This commit is contained in:
2025-04-05 19:41:56 +08:00
parent 342a5a2436
commit 20d4fa12d8
15 changed files with 336 additions and 168 deletions

View File

@@ -2,6 +2,7 @@ using System.Net;
using System.Net.Sockets;
using System.Text;
using DotNext;
using DotNext.Threading;
public struct UDPData
{
@@ -20,7 +21,7 @@ public class UDPServer
private UdpClient listener;
private IPEndPoint groupEP;
private Dictionary<string, List<UDPData>> udpData = new Dictionary<string, List<UDPData>>();
private Mutex mutUdpData = new Mutex();
private AsyncReaderWriterLock udpDataLock = new AsyncReaderWriterLock(1);
/// <summary>
/// Construct a udp server with fixed port
@@ -50,32 +51,21 @@ public class UDPServer
/// Find UDP Receive Data According to ip address
/// </summary>
/// <param name="ipAddr"> IP Address</param>
/// <param name="rwTimeout"> Read and Write Wait for Milliseconds </param>
/// <returns>UDP Data</returns>
public Optional<UDPData> FindData(string ipAddr)
public Optional<UDPData> FindData(string ipAddr, int rwTimeout = 1000)
{
UDPData? data = null;
try
using (udpDataLock.AcquireWriteLock(TimeSpan.FromMilliseconds(rwTimeout)))
{
var isOverTime = !mutUdpData.WaitOne(1000);
if (isOverTime) return Optional.None<UDPData>();
// Get
var listData = udpData[ipAddr];
data = listData[0];
// Delete
listData.RemoveAt(0);
}
catch
{
data = null;
}
finally
{
mutUdpData.ReleaseMutex();
if (udpData.ContainsKey(ipAddr) && udpData[ipAddr].Count > 0)
{
data = udpData[ipAddr][0];
udpData[ipAddr].RemoveAt(0);
}
}
// Return
if (data is null)
if (data == null)
{
return Optional.None<UDPData>();
}
@@ -93,35 +83,24 @@ public class UDPServer
/// <returns>[TODO:return]</returns>
public async ValueTask<Optional<UDPData>> FindDataAsync(string ipAddr, int timeout = 1000)
{
var time = 0;
UDPData? data = null;
while (time < timeout)
using (await udpDataLock.AcquireWriteLockAsync(TimeSpan.FromMilliseconds(1000)))
{
await Task.Delay(1);
try
if (udpData.ContainsKey(ipAddr) && udpData[ipAddr].Count > 0)
{
var isOverTime = !mutUdpData.WaitOne(timeout - time);
if (isOverTime) break;
// Get
var listData = udpData[ipAddr];
data = listData[0];
// Delete
listData.RemoveAt(0);
data = udpData[ipAddr][0];
udpData[ipAddr].RemoveAt(0);
}
catch
{
data = null;
}
finally
{
mutUdpData.ReleaseMutex();
}
if (data is null) continue;
else break;
}
if ((time >= timeout) || (data is null)) { return Optional.None<UDPData>(); }
else { return Optional.Some((UDPData)data); }
if (data == null)
{
return Optional.None<UDPData>();
}
else
{
return Optional.Some((UDPData)data);
}
}
private void ReceiveHandler(IAsyncResult res)
@@ -131,7 +110,7 @@ public class UDPServer
var nowtime = DateTime.Now;
// Handle RemoteEP
string remoteStr;
string remoteStr = "Unknown";
if (remoteEP is not null)
{
var remoteAddress = remoteEP.Address.ToString();
@@ -160,14 +139,10 @@ public class UDPServer
remoteStr = $"{remoteAddress}:{remotePort}";
}
else
{
remoteStr = "Unknown";
}
// Handle Package
var sign = bytes[0];
string recvData;
string recvData = "";
if (sign == (byte)WebProtocol.PackSign.SendAddr)
{
var resData = WebProtocol.SendAddrPackage.FromBytes(bytes);
@@ -176,18 +151,9 @@ public class UDPServer
else
recvData = resData.Error.ToString();
}
else if (sign == (byte)WebProtocol.PackSign.SendData)
{
recvData = "";
}
else if (sign == (byte)WebProtocol.PackSign.RecvData)
{
recvData = "";
}
else if (sign == (byte)WebProtocol.PackSign.RecvResp)
{
recvData = "";
}
else if (sign == (byte)WebProtocol.PackSign.SendData) { }
else if (sign == (byte)WebProtocol.PackSign.RecvData) { }
else if (sign == (byte)WebProtocol.PackSign.RecvResp) { }
else
{
recvData = Encoding.ASCII.GetString(bytes, 0, bytes.Length);