using System.Net;
using DotNext;
namespace Peripherals.NetConfigClient;
static class NetConfigAddr
{
const UInt32 BASE = 0x30A7_0000;
public static readonly UInt32[] HOST_IP = { BASE + 0, BASE + 1, BASE + 2, BASE + 3 };
public static readonly UInt32[] BOARD_IP = { BASE + 4, BASE + 5, BASE + 6, BASE + 7 };
public static readonly UInt32[] HOST_MAC = { BASE + 8, BASE + 9, BASE + 10, BASE + 11, BASE + 12, BASE + 13 };
public static readonly UInt32[] BOARD_MAC = { BASE + 14, BASE + 15, BASE + 16, BASE + 17, BASE + 18, BASE + 19 };
}
///
/// [TODO:description]
///
public class NetConfig
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
readonly int timeout = 2000;
readonly int taskID;
readonly int port;
readonly string address;
private IPEndPoint ep;
///
/// [TODO:description]
///
/// [TODO:parameter]
/// [TODO:parameter]
/// [TODO:parameter]
/// [TODO:parameter]
/// [TODO:return]
public NetConfig(string address, int port, int taskID, int timeout = 2000)
{
if (timeout < 0)
throw new ArgumentException("Timeout couldn't be negative", nameof(timeout));
this.address = address;
this.taskID = taskID;
this.port = port;
this.ep = new IPEndPoint(IPAddress.Parse(address), port);
this.timeout = timeout;
}
///
/// [TODO:description]
///
/// [TODO:parameter]
/// [TODO:return]
public async ValueTask> SetHostIP(IPAddress ip)
{
// 清除UDP服务器接收缓冲区
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
var ipBytes = ip.GetAddressBytes();
{
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.HOST_IP, ipBytes, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to set host IP: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"Failed to set host IP: operation returned false");
return false;
}
}
return true;
}
///
/// [TODO:description]
///
/// [TODO:parameter]
/// [TODO:return]
public async ValueTask> SetBoardIP(IPAddress ip)
{
// 清除UDP服务器接收缓冲区
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
var ipBytes = ip.GetAddressBytes();
{
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.BOARD_IP, ipBytes, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to set board IP: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"Failed to set board IP: operation returned false");
return false;
}
}
return true;
}
///
/// [TODO:description]
///
/// [TODO:parameter]
/// [TODO:return]
public async ValueTask> SetHostMAC(byte[] macAddress)
{
if (macAddress == null)
throw new ArgumentNullException(nameof(macAddress));
if (macAddress.Length != 6)
throw new ArgumentException("MAC address must be 6 bytes", nameof(macAddress));
// 清除UDP服务器接收缓冲区
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
{
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.HOST_MAC, macAddress, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to set host MAC address: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"Failed to set host MAC address: operation returned false");
return false;
}
}
return true;
}
///
/// [TODO:description]
///
/// [TODO:parameter]
/// [TODO:return]
public async ValueTask> SetBoardMAC(byte[] macAddress)
{
if (macAddress == null)
throw new ArgumentNullException(nameof(macAddress));
if (macAddress.Length != 6)
throw new ArgumentException("MAC address must be 6 bytes", nameof(macAddress));
// 清除UDP服务器接收缓冲区
MsgBus.UDPServer.ClearUDPData(this.address, this.taskID);
{
var ret = await UDPClientPool.WriteAddrSeq(this.ep, this.taskID, NetConfigAddr.BOARD_MAC, macAddress, this.timeout);
if (!ret.IsSuccessful)
{
logger.Error($"Failed to set board MAC address: {ret.Error}");
return new(ret.Error);
}
if (!ret.Value)
{
logger.Error($"Failed to set board MAC address: operation returned false");
return false;
}
}
return true;
}
}