59 lines
2.2 KiB
C#
59 lines
2.2 KiB
C#
namespace CatLink.Models
|
|
{
|
|
public class Msg
|
|
{
|
|
public int Version { get; set; } = 1;
|
|
public int Cmd { get; set; }
|
|
public int Proto { get; set; }
|
|
public int Sid { get; set; }
|
|
public uint Src { get; set; }
|
|
public ushort SrcPort { get; set; }
|
|
public uint Dst { get; set; }
|
|
public ushort DstPort { get; set; }
|
|
public string? Data { get; set; }
|
|
|
|
public static Msg FromString(string line)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(line))
|
|
{
|
|
throw new ArgumentException("Line cannot be null or empty", nameof(line));
|
|
}
|
|
|
|
var parts = line.Split(',');
|
|
var msg = new Msg();
|
|
|
|
if (parts.Length > 0 && !string.IsNullOrWhiteSpace(parts[0])) msg.Version = int.Parse(parts[0]);
|
|
if (parts.Length > 1 && !string.IsNullOrWhiteSpace(parts[1])) msg.Cmd = int.Parse(parts[1]);
|
|
if (parts.Length > 2 && !string.IsNullOrWhiteSpace(parts[2])) msg.Proto = int.Parse(parts[2]);
|
|
if (parts.Length > 3 && !string.IsNullOrWhiteSpace(parts[3])) msg.Sid = int.Parse(parts[3]);
|
|
if (parts.Length > 4 && !string.IsNullOrWhiteSpace(parts[4])) msg.Src = uint.Parse(parts[4]);
|
|
if (parts.Length > 5 && !string.IsNullOrWhiteSpace(parts[5])) msg.SrcPort = ushort.Parse(parts[5]);
|
|
if (parts.Length > 6 && !string.IsNullOrWhiteSpace(parts[6])) msg.Dst = uint.Parse(parts[6]);
|
|
if (parts.Length > 7 && !string.IsNullOrWhiteSpace(parts[7])) msg.DstPort = ushort.Parse(parts[7]);
|
|
if (parts.Length > 16 && !string.IsNullOrWhiteSpace(parts[16])) msg.Data = parts[16];
|
|
|
|
return msg;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{Version},{Cmd},{Proto},{Sid},{Src},{SrcPort},{Dst},{DstPort},,,,,,,,,,{Data}";
|
|
}
|
|
|
|
public Msg CloneWithNewData(string? newData)
|
|
{
|
|
return new Msg
|
|
{
|
|
Version = Version,
|
|
Cmd = Cmd,
|
|
Proto = Proto,
|
|
Sid = Sid,
|
|
Src = Src,
|
|
SrcPort = SrcPort,
|
|
Dst = Dst,
|
|
DstPort = DstPort,
|
|
Data = newData
|
|
};
|
|
}
|
|
}
|
|
} |