add nlog for server
This commit is contained in:
@@ -4,11 +4,24 @@ using System.Text;
|
||||
using DotNext;
|
||||
using DotNext.Threading;
|
||||
|
||||
/// <summary> UDP接受数据包格式 </summary>
|
||||
public struct UDPData
|
||||
{
|
||||
/// <summary>
|
||||
/// 接受到的时间
|
||||
/// </summary>
|
||||
public DateTime datetime;
|
||||
/// <summary>
|
||||
/// 发送来源的IP地址
|
||||
/// </summary>
|
||||
public string addr;
|
||||
/// <summary>
|
||||
/// 发送来源的端口号
|
||||
/// </summary>
|
||||
public int port;
|
||||
/// <summary>
|
||||
/// 接受到的数据
|
||||
/// </summary>
|
||||
public byte[] data;
|
||||
}
|
||||
|
||||
@@ -51,18 +64,29 @@ 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>
|
||||
/// <param name="timeout"> Read and Write Wait for Milliseconds </param>
|
||||
/// <returns>UDP Data</returns>
|
||||
public Optional<UDPData> FindData(string ipAddr, int rwTimeout = 1000)
|
||||
public Optional<UDPData> FindData(string ipAddr, int timeout = 1000)
|
||||
{
|
||||
UDPData? data = null;
|
||||
using (udpDataLock.AcquireWriteLock(TimeSpan.FromMilliseconds(rwTimeout)))
|
||||
|
||||
var startTime = DateTime.Now;
|
||||
var isTimeout = false;
|
||||
var timeleft = TimeSpan.FromMilliseconds(timeout);
|
||||
while (!isTimeout)
|
||||
{
|
||||
if (udpData.ContainsKey(ipAddr) && udpData[ipAddr].Count > 0)
|
||||
|
||||
using (udpDataLock.AcquireWriteLock(timeleft))
|
||||
{
|
||||
data = udpData[ipAddr][0];
|
||||
udpData[ipAddr].RemoveAt(0);
|
||||
if (udpData.ContainsKey(ipAddr) && udpData[ipAddr].Count > 0)
|
||||
{
|
||||
data = udpData[ipAddr][0];
|
||||
udpData[ipAddr].RemoveAt(0);
|
||||
}
|
||||
}
|
||||
|
||||
timeleft = DateTime.Now.Subtract(startTime);
|
||||
isTimeout = timeleft >= TimeSpan.FromMilliseconds(timeout);
|
||||
}
|
||||
|
||||
if (data == null)
|
||||
@@ -76,21 +100,36 @@ public class UDPServer
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find UDP Receive Data Async
|
||||
/// 异步寻找目标发送的内容
|
||||
/// </summary>
|
||||
/// <param name="ipAddr">[TODO:parameter]</param>
|
||||
/// <param name="timeout">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
/// <param name="ipAddr"> 目标IP地址 </param>
|
||||
/// <param name="timeout">超时时间</param>
|
||||
/// <returns>
|
||||
/// 异步Optional 数据包:
|
||||
/// Optional 为空时,表明找不到数据;
|
||||
/// Optional 存在时,为最先收到的数据
|
||||
/// </returns>
|
||||
public async ValueTask<Optional<UDPData>> FindDataAsync(string ipAddr, int timeout = 1000)
|
||||
{
|
||||
UDPData? data = null;
|
||||
using (await udpDataLock.AcquireWriteLockAsync(TimeSpan.FromMilliseconds(1000)))
|
||||
|
||||
var startTime = DateTime.Now;
|
||||
var isTimeout = false;
|
||||
var timeleft = TimeSpan.FromMilliseconds(timeout);
|
||||
while (!isTimeout)
|
||||
{
|
||||
if (udpData.ContainsKey(ipAddr) && udpData[ipAddr].Count > 0)
|
||||
|
||||
using (await udpDataLock.AcquireWriteLockAsync(timeleft))
|
||||
{
|
||||
data = udpData[ipAddr][0];
|
||||
udpData[ipAddr].RemoveAt(0);
|
||||
if (udpData.ContainsKey(ipAddr) && udpData[ipAddr].Count > 0)
|
||||
{
|
||||
data = udpData[ipAddr][0];
|
||||
udpData[ipAddr].RemoveAt(0);
|
||||
}
|
||||
}
|
||||
|
||||
timeleft = DateTime.Now.Subtract(startTime);
|
||||
isTimeout = timeleft >= TimeSpan.FromMilliseconds(timeout);
|
||||
}
|
||||
|
||||
if (data == null)
|
||||
@@ -152,8 +191,22 @@ public class UDPServer
|
||||
recvData = resData.Error.ToString();
|
||||
}
|
||||
else if (sign == (byte)WebProtocol.PackSign.SendData) { }
|
||||
else if (sign == (byte)WebProtocol.PackSign.RecvData) { }
|
||||
else if (sign == (byte)WebProtocol.PackSign.RecvResp) { }
|
||||
else if (sign == (byte)WebProtocol.PackSign.RecvData)
|
||||
{
|
||||
var resData = WebProtocol.RecvDataPackage.FromBytes(bytes);
|
||||
if (resData.IsSuccessful)
|
||||
recvData = resData.Value.Options.ToString();
|
||||
else
|
||||
recvData = resData.Error.ToString();
|
||||
}
|
||||
else if (sign == (byte)WebProtocol.PackSign.RecvResp)
|
||||
{
|
||||
var resData = WebProtocol.RecvRespPackage.FromBytes(bytes);
|
||||
if (resData.IsSuccessful)
|
||||
recvData = resData.Value.Options.ToString();
|
||||
else
|
||||
recvData = resData.Error.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
recvData = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
|
||||
|
||||
Reference in New Issue
Block a user