feat: 优化Common函数以提高性能
This commit is contained in:
parent
8c404d4072
commit
24924c29f4
|
@ -28,7 +28,6 @@ DebuggerCmd.md
|
||||||
*.suo
|
*.suo
|
||||||
*.ntvs*
|
*.ntvs*
|
||||||
*.njsproj
|
*.njsproj
|
||||||
*.sln
|
|
||||||
*.sw?
|
*.sw?
|
||||||
|
|
||||||
*.tsbuildinfo
|
*.tsbuildinfo
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.0.31903.59
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "server", "server\server.csproj", "{F31D6A0D-0407-41CE-A67E-01B847488EFB}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "server.test", "server.test\server.test.csproj", "{CC274582-AC3C-4FD1-977C-96F1BC2760BD}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Debug|x64 = Debug|x64
|
||||||
|
Debug|x86 = Debug|x86
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
Release|x64 = Release|x64
|
||||||
|
Release|x86 = Release|x86
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{F31D6A0D-0407-41CE-A67E-01B847488EFB}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{CC274582-AC3C-4FD1-977C-96F1BC2760BD}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
|
@ -1,61 +0,0 @@
|
||||||
using System.Buffers.Binary;
|
|
||||||
using Common;
|
|
||||||
|
|
||||||
namespace server.test;
|
|
||||||
|
|
||||||
public class CommonTest
|
|
||||||
{
|
|
||||||
[Fact]
|
|
||||||
public void ReverseBytesTest()
|
|
||||||
{
|
|
||||||
var rnd = new Random();
|
|
||||||
var bytesLen = 8;
|
|
||||||
var bytesArray = new byte[bytesLen];
|
|
||||||
rnd.NextBytes(bytesArray);
|
|
||||||
|
|
||||||
var rev2Bytes = new byte[] {
|
|
||||||
bytesArray[1],
|
|
||||||
bytesArray[0],
|
|
||||||
bytesArray[3],
|
|
||||||
bytesArray[2],
|
|
||||||
bytesArray[5],
|
|
||||||
bytesArray[4],
|
|
||||||
bytesArray[7],
|
|
||||||
bytesArray[6],
|
|
||||||
};
|
|
||||||
Assert.Equal(Number.ReverseBytes(bytesArray, 2).Value, rev2Bytes);
|
|
||||||
|
|
||||||
var rev4Bytes = new byte[] {
|
|
||||||
bytesArray[3],
|
|
||||||
bytesArray[2],
|
|
||||||
bytesArray[1],
|
|
||||||
bytesArray[0],
|
|
||||||
bytesArray[7],
|
|
||||||
bytesArray[6],
|
|
||||||
bytesArray[5],
|
|
||||||
bytesArray[4],
|
|
||||||
};
|
|
||||||
Assert.Equal(Number.ReverseBytes(bytesArray, 4).Value, rev4Bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ToBitTest()
|
|
||||||
{
|
|
||||||
Assert.Equal(true, Number.ToBit(0xFF, 3).Value);
|
|
||||||
Assert.Equal(false, Number.ToBit(0x00, 3).Value);
|
|
||||||
Assert.Equal(true, Number.ToBit(0xAA, 3).Value);
|
|
||||||
Assert.Equal(false, Number.ToBit(0xAA, 2).Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ReverseBits()
|
|
||||||
{
|
|
||||||
Assert.Equal((byte)0x05, Common.Number.ReverseBits((byte)0xA0));
|
|
||||||
|
|
||||||
var bytesSrc = new byte[] { 0xAB, 0x00, 0x00, 0x01 };
|
|
||||||
var bytes = new byte[4];
|
|
||||||
for (int i = 0; i < 4; i++)
|
|
||||||
bytes[i] = Common.Number.ReverseBits(bytesSrc[i]);
|
|
||||||
Assert.Equal(new byte[] { 0xD5, 0x00, 0x00, 0x80 }, bytes);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,286 @@
|
||||||
|
using System.Collections;
|
||||||
|
using Common;
|
||||||
|
|
||||||
|
namespace CommonTest;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 针对 Common.Number 的单元测试,覆盖所有公开方法
|
||||||
|
/// </summary>
|
||||||
|
public class NumberTest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 NumberToBytes 的正常与异常情况
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_NumberToBytes()
|
||||||
|
{
|
||||||
|
// 测试大端(isLowNumHigh=false)
|
||||||
|
var result1 = Number.NumberToBytes(0x12345678ABCDEF01, 8, false);
|
||||||
|
Assert.True(result1.IsSuccessful);
|
||||||
|
Assert.Equal(new byte[] { 0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x01 }, result1.Value);
|
||||||
|
|
||||||
|
// 测试小端(isLowNumHigh=true)
|
||||||
|
var result2 = Number.NumberToBytes(0x12345678ABCDEF01, 8, true);
|
||||||
|
Assert.True(result2.IsSuccessful);
|
||||||
|
Assert.Equal(new byte[] { 0x01, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12 }, result2.Value);
|
||||||
|
|
||||||
|
// 测试长度不足(4字节)
|
||||||
|
var result3 = Number.NumberToBytes(0x12345678, 4, false);
|
||||||
|
Assert.True(result3.IsSuccessful);
|
||||||
|
Assert.Equal(new byte[] { 0x12, 0x34, 0x56, 0x78 }, result3.Value);
|
||||||
|
|
||||||
|
// 测试超长
|
||||||
|
var result4 = Number.NumberToBytes(0x1, 9, false);
|
||||||
|
Assert.False(result4.IsSuccessful);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 BytesToUInt64 的正常与异常情况
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_BytesToUInt64()
|
||||||
|
{
|
||||||
|
// 正常大端
|
||||||
|
var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x01 };
|
||||||
|
var result = Number.BytesToUInt64((byte[])bytes.Clone(), false);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal(0x12345678ABCDEF01UL, result.Value);
|
||||||
|
|
||||||
|
// 正常小端
|
||||||
|
var bytes2 = new byte[] { 0x01, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12 };
|
||||||
|
var result2 = Number.BytesToUInt64((byte[])bytes2.Clone(), true);
|
||||||
|
Assert.True(result2.IsSuccessful);
|
||||||
|
Assert.Equal(0x12345678ABCDEF01UL, result2.Value);
|
||||||
|
|
||||||
|
// 异常:长度超限
|
||||||
|
var result3 = Number.BytesToUInt64(new byte[9], false);
|
||||||
|
Assert.False(result3.IsSuccessful);
|
||||||
|
|
||||||
|
// 异常:不足8字节
|
||||||
|
var result4 = Number.BytesToUInt64(new byte[] { 0x01, 0x02 }, false);
|
||||||
|
Assert.False(result4.IsSuccessful); // BitConverter.ToUInt64 需要8字节
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 BytesToUInt32 的正常与异常情况
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_BytesToUInt32()
|
||||||
|
{
|
||||||
|
// 正常大端
|
||||||
|
var bytes = new byte[] { 0x12, 0x34, 0x56, 0x78 };
|
||||||
|
var result = Number.BytesToUInt32((byte[])bytes.Clone(), false);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal(0x12345678U, result.Value);
|
||||||
|
|
||||||
|
// 正常小端
|
||||||
|
var bytes2 = new byte[] { 0x78, 0x56, 0x34, 0x12 };
|
||||||
|
var result2 = Number.BytesToUInt32((byte[])bytes2.Clone(), true);
|
||||||
|
Assert.True(result2.IsSuccessful);
|
||||||
|
Assert.Equal(0x12345678U, result2.Value);
|
||||||
|
|
||||||
|
// 异常:长度超限
|
||||||
|
var result3 = Number.BytesToUInt32(new byte[5], false);
|
||||||
|
Assert.False(result3.IsSuccessful);
|
||||||
|
|
||||||
|
// 异常:不足4字节
|
||||||
|
var result4 = Number.BytesToUInt32(new byte[] { 0x01, 0x02 }, false);
|
||||||
|
Assert.False(result4.IsSuccessful); // BitConverter.ToUInt32 需要4字节
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 UInt32ArrayToBytes 的正常与异常情况
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_UInt32ArrayToBytes()
|
||||||
|
{
|
||||||
|
// 正常情况
|
||||||
|
var arr = new UInt32[] { 0x12345678, 0xABCDEF01 };
|
||||||
|
var result = Number.UInt32ArrayToBytes(arr);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
// BlockCopy 按小端序
|
||||||
|
Assert.Equal(new byte[] { 0x78, 0x56, 0x34, 0x12, 0x01, 0xEF, 0xCD, 0xAB }, result.Value);
|
||||||
|
|
||||||
|
// 空数组
|
||||||
|
var result2 = Number.UInt32ArrayToBytes(new UInt32[0]);
|
||||||
|
Assert.True(result2.IsSuccessful);
|
||||||
|
Assert.Empty(result2.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 MultiBitsToBytes 和 MultiBitsToNumber (ulong)
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_MultiBitsToBytesAndNumber_Ulong()
|
||||||
|
{
|
||||||
|
// 合并两个比特段
|
||||||
|
var result = Number.MultiBitsToNumber(0b101UL, 3, 0b11UL, 2);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal((ulong)0b10111, result.Value);
|
||||||
|
|
||||||
|
// 合并为字节数组
|
||||||
|
var bytesResult = Number.MultiBitsToBytes(0b101UL, 3, 0b11UL, 2);
|
||||||
|
Assert.True(bytesResult.IsSuccessful);
|
||||||
|
Assert.Equal(new byte[] { 0b10111 }, bytesResult.Value);
|
||||||
|
|
||||||
|
// 超过64位
|
||||||
|
var failResult = Number.MultiBitsToNumber(0xFFFFFFFFFFFFFFFF, 64, 1, 1);
|
||||||
|
Assert.False(failResult.IsSuccessful);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 MultiBitsToNumber (uint)
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_MultiBitsToNumber_Uint()
|
||||||
|
{
|
||||||
|
var result = Number.MultiBitsToNumber(0b101U, 3, 0b11U, 2);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal((uint)0b10111, result.Value);
|
||||||
|
|
||||||
|
// 超过64位
|
||||||
|
var failResult = Number.MultiBitsToNumber(uint.MaxValue, 64, 1, 1);
|
||||||
|
Assert.False(failResult.IsSuccessful);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 BitsCheck (ulong)
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_BitsCheck_Ulong()
|
||||||
|
{
|
||||||
|
// 完全匹配
|
||||||
|
Assert.True(Number.BitsCheck(0b1101UL, 0b1101UL));
|
||||||
|
// 不匹配
|
||||||
|
Assert.False(Number.BitsCheck(0b1101UL, 0b1001UL));
|
||||||
|
// 掩码
|
||||||
|
Assert.True(Number.BitsCheck(0b1101UL, 0b1001UL, 0b1001UL));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 BitsCheck (uint)
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_BitsCheck_Uint()
|
||||||
|
{
|
||||||
|
Assert.True(Number.BitsCheck(0b1011U, 0b1011U));
|
||||||
|
Assert.False(Number.BitsCheck(0b1011U, 0b1001U));
|
||||||
|
Assert.True(Number.BitsCheck(0b1011U, 0b1001U, 0b1001U));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 ToBit
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_ToBit()
|
||||||
|
{
|
||||||
|
// 取第0位
|
||||||
|
var result = Number.ToBit(0b1010U, 0);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.False(result.Value);
|
||||||
|
|
||||||
|
// 取第1位
|
||||||
|
var result2 = Number.ToBit(0b1010U, 1);
|
||||||
|
Assert.True(result2.IsSuccessful);
|
||||||
|
Assert.True(result2.Value);
|
||||||
|
|
||||||
|
// 负数位置
|
||||||
|
var result3 = Number.ToBit(0b1010U, -1);
|
||||||
|
Assert.False(result3.IsSuccessful);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 BitsToNumber
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_BitsToNumber()
|
||||||
|
{
|
||||||
|
// 5位BitArray
|
||||||
|
var bits = new BitArray(new bool[] { true, true, false, true, false }); // 0b01011
|
||||||
|
var result = Number.BitsToNumber(bits);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal((uint)0b01011, result.Value);
|
||||||
|
|
||||||
|
// 超过32位
|
||||||
|
var bits2 = new BitArray(33);
|
||||||
|
Assert.Throws<ArgumentException>(() => Number.BitsToNumber(bits2));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 StringToBytes
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_StringToBytes()
|
||||||
|
{
|
||||||
|
// 16进制字符串
|
||||||
|
var bytes = Number.StringToBytes("1234ABCD");
|
||||||
|
Assert.Equal(new byte[] { 0x12, 0x34, 0xAB, 0xCD }, bytes);
|
||||||
|
|
||||||
|
// 8位字符串
|
||||||
|
var bytes2 = Number.StringToBytes("01020304");
|
||||||
|
Assert.Equal(new byte[] { 0x01, 0x02, 0x03, 0x04 }, bytes2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 ReverseBytes
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_ReverseBytes()
|
||||||
|
{
|
||||||
|
// 步长为2
|
||||||
|
var src = new byte[] { 0x01, 0x02, 0x03, 0x04 };
|
||||||
|
var result = Number.ReverseBytes(src, 2);
|
||||||
|
Assert.True(result.IsSuccessful);
|
||||||
|
Assert.Equal(new byte[] { 0x02, 0x01, 0x04, 0x03 }, result.Value);
|
||||||
|
|
||||||
|
// 步长为4
|
||||||
|
var src2 = new byte[] { 0x01, 0x02, 0x03, 0x04 };
|
||||||
|
var result2 = Number.ReverseBytes(src2, 4);
|
||||||
|
Assert.True(result2.IsSuccessful);
|
||||||
|
Assert.Equal(new byte[] { 0x04, 0x03, 0x02, 0x01 }, result2.Value);
|
||||||
|
|
||||||
|
// 步长为1(无变化)
|
||||||
|
var src3 = new byte[] { 0x01, 0x02, 0x03, 0x04 };
|
||||||
|
var result3 = Number.ReverseBytes(src3, 1);
|
||||||
|
Assert.True(result3.IsSuccessful);
|
||||||
|
Assert.Equal(src3, result3.Value);
|
||||||
|
|
||||||
|
// 步长为0(异常)
|
||||||
|
var result4 = Number.ReverseBytes(src3, 0);
|
||||||
|
Assert.False(result4.IsSuccessful);
|
||||||
|
|
||||||
|
// 步长不能整除
|
||||||
|
var result5 = Number.ReverseBytes(src3, 3);
|
||||||
|
Assert.False(result5.IsSuccessful);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 ReverseBits (byte)
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_ReverseBits_Byte()
|
||||||
|
{
|
||||||
|
// 0b00010010 -> 0b01001000
|
||||||
|
byte src = 0b00010010;
|
||||||
|
byte reversed = Number.ReverseBits(src);
|
||||||
|
Assert.Equal(0b01001000, reversed);
|
||||||
|
|
||||||
|
// 0b11110000 -> 0b00001111
|
||||||
|
Assert.Equal(0b00001111, Number.ReverseBits(0b11110000));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 测试 ReverseBits (byte[])
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void Test_ReverseBits_ByteArray()
|
||||||
|
{
|
||||||
|
var src = new byte[] { 0b00010010, 0b11110000 };
|
||||||
|
var reversed = Number.ReverseBits(src);
|
||||||
|
Assert.Equal(new byte[] { 0b01001000, 0b00001111 }, reversed);
|
||||||
|
|
||||||
|
// 空数组
|
||||||
|
var reversed2 = Number.ReverseBits(new byte[0]);
|
||||||
|
Assert.Empty(reversed2);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,138 +0,0 @@
|
||||||
using System.Net;
|
|
||||||
using System.Text;
|
|
||||||
using Common;
|
|
||||||
using Xunit.Abstractions;
|
|
||||||
|
|
||||||
namespace server.test;
|
|
||||||
|
|
||||||
|
|
||||||
public class UDPServerTest
|
|
||||||
{
|
|
||||||
const string address = "127.0.0.1";
|
|
||||||
const int port = 33000;
|
|
||||||
private static readonly UDPServer udpServer = new UDPServer(port);
|
|
||||||
|
|
||||||
private readonly ITestOutputHelper output;
|
|
||||||
|
|
||||||
public UDPServerTest(ITestOutputHelper output)
|
|
||||||
{
|
|
||||||
this.output = output;
|
|
||||||
udpServer.Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void UDPDataDeepClone()
|
|
||||||
{
|
|
||||||
var udpData = new UDPData()
|
|
||||||
{
|
|
||||||
DateTime = DateTime.Now,
|
|
||||||
Address = "127.0.0.1",
|
|
||||||
Port = 1234,
|
|
||||||
Data = new byte[] { 0xf0, 00, 00, 00 },
|
|
||||||
HasRead = false
|
|
||||||
};
|
|
||||||
var cloneUdpData = udpData.DeepClone();
|
|
||||||
|
|
||||||
Assert.Equal(udpData.DateTime, cloneUdpData.DateTime);
|
|
||||||
Assert.Equal(udpData.Address, cloneUdpData.Address);
|
|
||||||
Assert.Equal(udpData.Port, cloneUdpData.Port);
|
|
||||||
Assert.Equal(udpData.Data, cloneUdpData.Data);
|
|
||||||
Assert.Equal(udpData.HasRead, cloneUdpData.HasRead);
|
|
||||||
|
|
||||||
udpData.DateTime = DateTime.Now;
|
|
||||||
udpData.Address = "192.168.1.1";
|
|
||||||
udpData.Port = 33000;
|
|
||||||
udpData.Data = new byte[] { 0xFF, 00, 00, 00 };
|
|
||||||
udpData.HasRead = true;
|
|
||||||
|
|
||||||
Assert.NotNull(cloneUdpData.DateTime);
|
|
||||||
Assert.NotNull(cloneUdpData.Address);
|
|
||||||
Assert.NotNull(cloneUdpData.Port);
|
|
||||||
Assert.NotNull(cloneUdpData.Data);
|
|
||||||
Assert.NotNull(cloneUdpData.HasRead);
|
|
||||||
|
|
||||||
Assert.NotEqual(udpData.DateTime, cloneUdpData.DateTime);
|
|
||||||
Assert.NotEqual(udpData.Address, cloneUdpData.Address);
|
|
||||||
Assert.NotEqual(udpData.Port, cloneUdpData.Port);
|
|
||||||
Assert.NotEqual(udpData.Data, cloneUdpData.Data);
|
|
||||||
Assert.NotEqual(udpData.HasRead, cloneUdpData.HasRead);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[InlineData(new object[] { new string[] { "Hello World!", "Hello Server!", "What is your problem?" } })]
|
|
||||||
public async Task UDPServerFindString(string[] textArray)
|
|
||||||
{
|
|
||||||
Assert.True(udpServer.IsRunning);
|
|
||||||
|
|
||||||
var serverEP = new IPEndPoint(IPAddress.Parse(address), port);
|
|
||||||
|
|
||||||
foreach (var text in textArray)
|
|
||||||
{
|
|
||||||
Assert.True(await UDPClientPool.SendStringAsync(serverEP, [text]));
|
|
||||||
var ret = await udpServer.FindDataAsync(address);
|
|
||||||
Assert.True(ret.HasValue);
|
|
||||||
var data = ret.Value;
|
|
||||||
Assert.Equal(address, data.Address);
|
|
||||||
Assert.Equal(text, Encoding.ASCII.GetString(data.Data));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[InlineData(new object[] { new UInt32[] { 0xF0_00_00_00, 0xFF_00_00_00, 0xFF_FF_FF_FF } })]
|
|
||||||
public async Task UDPServerFindBytes(UInt32[] bytesArray)
|
|
||||||
{
|
|
||||||
Assert.True(udpServer.IsRunning);
|
|
||||||
|
|
||||||
var serverEP = new IPEndPoint(IPAddress.Parse(address), port);
|
|
||||||
|
|
||||||
foreach (var number in bytesArray)
|
|
||||||
{
|
|
||||||
Assert.True(await UDPClientPool.SendBytesAsync(serverEP, Number.NumberToBytes(number, 4).Value));
|
|
||||||
var ret = await udpServer.FindDataAsync(address);
|
|
||||||
Assert.True(ret.HasValue);
|
|
||||||
var data = ret.Value;
|
|
||||||
Assert.Equal(address, data.Address);
|
|
||||||
Assert.Equal(number, Number.BytesToUInt32(data.Data).Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[InlineData(new object[] { new UInt32[] { 0xF0_00_00_00, 0xF0_01_00_00 } })]
|
|
||||||
public async Task UDPServerWaitResp(UInt32[] bytesArray)
|
|
||||||
{
|
|
||||||
Assert.True(udpServer.IsRunning);
|
|
||||||
|
|
||||||
var serverEP = new IPEndPoint(IPAddress.Parse(address), port);
|
|
||||||
|
|
||||||
foreach (var number in bytesArray)
|
|
||||||
{
|
|
||||||
Assert.True(await UDPClientPool.SendBytesAsync(serverEP, Number.NumberToBytes(number, 4).Value));
|
|
||||||
|
|
||||||
var ret = await udpServer.WaitForAckAsync(address);
|
|
||||||
Assert.True(ret.IsSuccessful);
|
|
||||||
var data = ret.Value;
|
|
||||||
Assert.True(data.IsSuccessful);
|
|
||||||
Assert.Equal(number, Number.BytesToUInt32(data.ToBytes()).Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[InlineData(new object[] { new UInt64[] { 0x0F_00_00_00_01_02_02_02, 0x0F_01_00_00_FF_FF_FF_FF } })]
|
|
||||||
public async Task UDPServerWaitData(UInt64[] bytesArray)
|
|
||||||
{
|
|
||||||
Assert.True(udpServer.IsRunning);
|
|
||||||
|
|
||||||
var serverEP = new IPEndPoint(IPAddress.Parse(address), port);
|
|
||||||
|
|
||||||
foreach (var number in bytesArray)
|
|
||||||
{
|
|
||||||
Assert.True(await UDPClientPool.SendBytesAsync(serverEP, Number.NumberToBytes(number, 8).Value));
|
|
||||||
|
|
||||||
var ret = await udpServer.WaitForDataAsync(address);
|
|
||||||
Assert.True(ret.IsSuccessful);
|
|
||||||
var data = ret.Value;
|
|
||||||
Assert.True(data.IsSuccessful);
|
|
||||||
Assert.Equal((UInt64)number, Number.BytesToUInt64(data.ToBytes()).Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -99,20 +99,11 @@ public class Number
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (isLowNumHigh)
|
if (!isLowNumHigh)
|
||||||
{
|
{
|
||||||
for (var i = 0; i < len; i++)
|
Array.Reverse(bytes);
|
||||||
{
|
|
||||||
num += Convert.ToUInt64((UInt64)bytes[len - 1 - i] << (i << 3));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (var i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
num += Convert.ToUInt64((UInt64)bytes[i] << ((int)(len - 1 - i) << 3));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
num = BitConverter.ToUInt64(bytes, 0);
|
||||||
|
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
@ -143,20 +134,11 @@ public class Number
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (isLowNumHigh)
|
if (!isLowNumHigh)
|
||||||
{
|
{
|
||||||
for (var i = 0; i < len; i++)
|
Array.Reverse(bytes);
|
||||||
{
|
|
||||||
num += Convert.ToUInt32((UInt32)bytes[len - 1 - i] << (i << 3));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (var i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
num += Convert.ToUInt32((UInt32)bytes[i] << ((int)(len - 1 - i) << 3));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
num = BitConverter.ToUInt32(bytes, 0);
|
||||||
|
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
@ -333,10 +315,9 @@ public class Number
|
||||||
|
|
||||||
for (int i = 0; i < srcBytesLen; i += distance)
|
for (int i = 0; i < srcBytesLen; i += distance)
|
||||||
{
|
{
|
||||||
var end = i + distance;
|
Buffer.BlockCopy(srcBytes, i, buffer, 0, distance);
|
||||||
buffer = srcBytes[i..end];
|
|
||||||
Array.Reverse(buffer);
|
Array.Reverse(buffer);
|
||||||
Array.Copy(buffer, 0, dstBytes, i, distance);
|
Buffer.BlockCopy(buffer, 0, dstBytes, i, distance);
|
||||||
}
|
}
|
||||||
|
|
||||||
return dstBytes;
|
return dstBytes;
|
||||||
|
|
|
@ -413,6 +413,7 @@ public class DebuggerController : ControllerBase
|
||||||
}
|
}
|
||||||
var channelBytes = new byte[4 * depth];
|
var channelBytes = new byte[4 * depth];
|
||||||
Buffer.BlockCopy(channelUintArr, 0, channelBytes, 0, channelBytes.Length);
|
Buffer.BlockCopy(channelUintArr, 0, channelBytes, 0, channelBytes.Length);
|
||||||
|
channelBytes = Common.Number.ReverseBytes(channelBytes, 4).Value;
|
||||||
logger.Debug($"{channel.name} HexData: {BitConverter.ToString(channelBytes)}");
|
logger.Debug($"{channel.name} HexData: {BitConverter.ToString(channelBytes)}");
|
||||||
var base64 = Convert.ToBase64String(channelBytes);
|
var base64 = Convert.ToBase64String(channelBytes);
|
||||||
channelDataList.Add(new ChannelCaptureData { name = channel.name, data = base64 });
|
channelDataList.Add(new ChannelCaptureData { name = channel.name, data = base64 });
|
||||||
|
|
Loading…
Reference in New Issue