From 50df89b4e54fdcb9a45708e0f5c423452c17fa03 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 31 Aug 2025 11:39:00 +0800 Subject: [PATCH 01/10] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataConverter/DataConverter.cs | 4 +++ .../Logging/SocketLogging.cs | 26 +++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs b/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs index 44425a29..4027bc09 100644 --- a/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs +++ b/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs @@ -76,6 +76,10 @@ protected virtual bool Parse(ReadOnlyMemory data, TEntity entity) { p.SetValue(entity, value); } + else + { + SocketLogging.LogInformation($"{nameof(Parse)} failed. Can't convert value type {valueType?.Name} to {p.PropertyType.Name}"); + } } } ret = true; diff --git a/src/extensions/BootstrapBlazor.Socket/Logging/SocketLogging.cs b/src/extensions/BootstrapBlazor.Socket/Logging/SocketLogging.cs index 07b46c03..7f7a2ce1 100644 --- a/src/extensions/BootstrapBlazor.Socket/Logging/SocketLogging.cs +++ b/src/extensions/BootstrapBlazor.Socket/Logging/SocketLogging.cs @@ -34,13 +34,23 @@ public static void Init(ILogger logger) /// /// /// - public static void LogError(Exception ex, string? message = null) - { - if (_logger == null) - { - return; - } + public static void LogError(Exception ex, string? message = null) => _logger?.LogError(ex, "{message}", message); - _logger.LogError(ex, "{message}", message); - } + /// + /// 记录警告信息方法 + /// + /// + public static void LogWarning(string message) => _logger?.LogWarning("{message}", message); + + /// + /// 记录信息方法 + /// + /// + public static void LogInformation(string message) => _logger?.LogInformation("{message}", message); + + /// + /// 记录调试信息方法 + /// + /// + public static void LogDebug(string message) => _logger?.LogDebug("{message}", message); } From f94ad6f1feaf08ffe8e1eb96b3cb2cb16f9c734d Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 31 Aug 2025 11:44:19 +0800 Subject: [PATCH 02/10] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20Logging=20?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTestTcpSocket/SocketLoggingTest.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/UnitTestTcpSocket/SocketLoggingTest.cs b/test/UnitTestTcpSocket/SocketLoggingTest.cs index 34292790..b2200a56 100644 --- a/test/UnitTestTcpSocket/SocketLoggingTest.cs +++ b/test/UnitTestTcpSocket/SocketLoggingTest.cs @@ -3,6 +3,7 @@ // Website: https://www.blazor.zone or https://argozhang.github.io/ using BootstrapBlazor.Socket.Logging; +using Microsoft.Extensions.Logging; namespace UnitTestTcpSocket; @@ -12,5 +13,13 @@ public class SocketLoggingTest public void Logger_Ok() { SocketLogging.LogError(new Exception()); + SocketLogging.LogInformation("Information"); + SocketLogging.LogWarning("Warning"); + SocketLogging.LogDebug("Debug"); + + SocketLogging.Init(new LoggerFactory().CreateLogger("SocketLoggingTest")); + SocketLogging.LogInformation("Information"); + SocketLogging.LogWarning("Warning"); + SocketLogging.LogDebug("Debug"); } } From 9c7ddddcf7d8a19621a313c992497e31256a8774 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 31 Aug 2025 11:57:43 +0800 Subject: [PATCH 03/10] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PropertyConverter/DataByteConverter.cs | 2 +- test/UnitTestTcpSocket/TcpSocketFactoryTest.cs | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/extensions/BootstrapBlazor.Socket/PropertyConverter/DataByteConverter.cs b/src/extensions/BootstrapBlazor.Socket/PropertyConverter/DataByteConverter.cs index afd1ad2b..c16c9566 100644 --- a/src/extensions/BootstrapBlazor.Socket/PropertyConverter/DataByteConverter.cs +++ b/src/extensions/BootstrapBlazor.Socket/PropertyConverter/DataByteConverter.cs @@ -15,6 +15,6 @@ public class DataByteConverter : IDataPropertyConverter /// public object? Convert(ReadOnlyMemory data) { - return data.Length > 0 ? data.Span[0] : 0x0; + return data.Length > 0 ? data.Span[0] : byte.MinValue; } } diff --git a/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs b/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs index 4ab1a4c2..ca22de54 100644 --- a/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs +++ b/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs @@ -670,6 +670,9 @@ public async Task TryConvertTo_Ok() Assert.Equal([1, 2, 3, 4, 5], entity.Header); Assert.Equal([3, 4], entity.Body); + // byte + Assert.Equal(0x1, entity.Value15); + // string Assert.Equal("1", entity.Value1); @@ -1415,6 +1418,9 @@ class MockEntity public string? Value14 { get; set; } public string? Value13 { get; set; } + + [DataPropertyConverter(Type = typeof(byte), Offset = 0, Length = 1)] + public byte Value15 { get; set; } } class MockSocketDataConverter : DataConverter From 09ced9d556774b7e46c93ed89136d452b84ddf66 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 31 Aug 2025 12:04:07 +0800 Subject: [PATCH 04/10] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=20ByteConverte?= =?UTF-8?q?r=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TcpSocketPropertyConverterTest.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/UnitTestTcpSocket/TcpSocketPropertyConverterTest.cs b/test/UnitTestTcpSocket/TcpSocketPropertyConverterTest.cs index b7babc65..09dfc439 100644 --- a/test/UnitTestTcpSocket/TcpSocketPropertyConverterTest.cs +++ b/test/UnitTestTcpSocket/TcpSocketPropertyConverterTest.cs @@ -69,4 +69,15 @@ public void DoubleConverter_Ok() var actual = converter.Convert(new byte[] { 0x1F, 0x85, 0xEB, 0x51, 0xB8, 0x1E, 0x09, 0x40 }); Assert.Equal(3.14, actual); } + + [Fact] + public void ByteConverter_Ok() + { + var converter = new DataByteConverter(); + var actual = converter.Convert(new byte[] { 0xFF }); + Assert.Equal((byte)0xFF, actual); + + actual = converter.Convert(Array.Empty()); + Assert.Equal((byte)0x0, actual); + } } From 9dcc6e870385472538e9a400c35e5794a9c84e7e Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 31 Aug 2025 12:07:47 +0800 Subject: [PATCH 05/10] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BootstrapBlazor.Socket/DataConverter/DataConverter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs b/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs index 4027bc09..27531090 100644 --- a/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs +++ b/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs @@ -72,13 +72,13 @@ protected virtual bool Parse(ReadOnlyMemory data, TEntity entity) { var value = attr.ConvertTo(data); var valueType = value?.GetType(); - if (valueType != null && p.PropertyType.IsAssignableFrom(valueType)) + if (p.PropertyType.IsAssignableFrom(valueType)) { p.SetValue(entity, value); } else { - SocketLogging.LogInformation($"{nameof(Parse)} failed. Can't convert value type {valueType?.Name} to {p.PropertyType.Name}"); + SocketLogging.LogInformation($"{nameof(Parse)} failed. Can't convert value from {valueType} to {p.PropertyType}"); } } } From 0f2d6913d50eabc9d5a51241be3100883995508e Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 31 Aug 2025 12:08:03 +0800 Subject: [PATCH 06/10] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=20Logging=20?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTestTcpSocket/SocketLoggingTest.cs | 6 ------ test/UnitTestTcpSocket/TcpSocketFactoryTest.cs | 5 +++++ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/test/UnitTestTcpSocket/SocketLoggingTest.cs b/test/UnitTestTcpSocket/SocketLoggingTest.cs index b2200a56..9fd34ec2 100644 --- a/test/UnitTestTcpSocket/SocketLoggingTest.cs +++ b/test/UnitTestTcpSocket/SocketLoggingTest.cs @@ -3,7 +3,6 @@ // Website: https://www.blazor.zone or https://argozhang.github.io/ using BootstrapBlazor.Socket.Logging; -using Microsoft.Extensions.Logging; namespace UnitTestTcpSocket; @@ -16,10 +15,5 @@ public void Logger_Ok() SocketLogging.LogInformation("Information"); SocketLogging.LogWarning("Warning"); SocketLogging.LogDebug("Debug"); - - SocketLogging.Init(new LoggerFactory().CreateLogger("SocketLoggingTest")); - SocketLogging.LogInformation("Information"); - SocketLogging.LogWarning("Warning"); - SocketLogging.LogDebug("Debug"); } } diff --git a/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs b/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs index ca22de54..63c503d2 100644 --- a/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs +++ b/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ +using BootstrapBlazor.Socket.Logging; using Microsoft.Extensions.Logging; using System.Buffers; using System.Net; @@ -43,6 +44,10 @@ public async Task GetOrCreate_Ok() await client5.DisposeAsync(); await factory.DisposeAsync(); + + SocketLogging.LogWarning("Warning"); + SocketLogging.LogDebug("Debug"); + SocketLogging.LogInformation("Information"); } [Fact] From b46f87405cd05d9fcb68ad1b282d7c9495a7f358 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 31 Aug 2025 12:08:14 +0800 Subject: [PATCH 07/10] chore: bump version 9.0.11 --- .../BootstrapBlazor.Socket/BootstrapBlazor.Socket.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/BootstrapBlazor.Socket/BootstrapBlazor.Socket.csproj b/src/extensions/BootstrapBlazor.Socket/BootstrapBlazor.Socket.csproj index 2b4cf7f9..4f0ed118 100644 --- a/src/extensions/BootstrapBlazor.Socket/BootstrapBlazor.Socket.csproj +++ b/src/extensions/BootstrapBlazor.Socket/BootstrapBlazor.Socket.csproj @@ -1,7 +1,7 @@  - 9.0.10 + 9.0.11 From 192815c0733dcd45c522e4d6bff742143b5d757c Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 31 Aug 2025 12:25:09 +0800 Subject: [PATCH 08/10] =?UTF-8?q?test:=20=E6=8F=90=E9=AB=98=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=A6=86=E7=9B=96=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataConverter/DataConverter.cs | 4 +++- .../UnitTestTcpSocket/TcpSocketFactoryTest.cs | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs b/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs index 27531090..730bb469 100644 --- a/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs +++ b/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs @@ -78,7 +78,7 @@ protected virtual bool Parse(ReadOnlyMemory data, TEntity entity) } else { - SocketLogging.LogInformation($"{nameof(Parse)} failed. Can't convert value from {valueType} to {p.PropertyType}"); + SocketLogging.LogInformation($"{nameof(Parse)} failed. Can't convert value from {GetValueType(valueType)} to {p.PropertyType}"); } } } @@ -87,6 +87,8 @@ protected virtual bool Parse(ReadOnlyMemory data, TEntity entity) return ret; } + private static string GetValueType(Type? type) => type?.FullName ?? "NULL"; + private DataPropertyConverterAttribute? GetPropertyConverterAttribute(PropertyInfo propertyInfo) { DataPropertyConverterAttribute? attr = null; diff --git a/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs b/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs index 63c503d2..108d0bc4 100644 --- a/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs +++ b/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs @@ -675,6 +675,12 @@ public async Task TryConvertTo_Ok() Assert.Equal([1, 2, 3, 4, 5], entity.Header); Assert.Equal([3, 4], entity.Body); + // null + Assert.Equal((byte)0x0, entity.Value16); + + // null + Assert.Equal((byte)0x0, entity.Value17); + // byte Assert.Equal(0x1, entity.Value15); @@ -1426,6 +1432,12 @@ class MockEntity [DataPropertyConverter(Type = typeof(byte), Offset = 0, Length = 1)] public byte Value15 { get; set; } + + [DataPropertyConverter(Type = typeof(byte), ConverterType = typeof(MockNullConverter), Offset = 0, Length = 1)] + public byte Value16 { get; set; } + + [DataPropertyConverter(Type = typeof(byte[]), Offset = 0, Length = 1)] + public byte Value17 { get; set; } } class MockSocketDataConverter : DataConverter @@ -1444,6 +1456,14 @@ protected override bool Parse(ReadOnlyMemory data, MockEntity entity) } } + class MockNullConverter : IDataPropertyConverter + { + public object? Convert(ReadOnlyMemory data) + { + return null; + } + } + class FooConverter(string name) : IDataPropertyConverter { public object? Convert(ReadOnlyMemory data) From e6b04a45a417d643b3efa738d4e34a81203ef590 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 31 Aug 2025 12:26:11 +0800 Subject: [PATCH 09/10] =?UTF-8?q?chore:=20=E5=A2=9E=E5=8A=A0=E4=BE=9D?= =?UTF-8?q?=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTestTcpSocket/UnitTestTcpSocket.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/test/UnitTestTcpSocket/UnitTestTcpSocket.csproj b/test/UnitTestTcpSocket/UnitTestTcpSocket.csproj index 92146715..fdb9ea2c 100644 --- a/test/UnitTestTcpSocket/UnitTestTcpSocket.csproj +++ b/test/UnitTestTcpSocket/UnitTestTcpSocket.csproj @@ -14,6 +14,7 @@ + From 98cdc4dfd1f5a4aeb52fca519cf3f949bf05400d Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 31 Aug 2025 12:31:57 +0800 Subject: [PATCH 10/10] =?UTF-8?q?chore:=20=E5=A2=9E=E5=8A=A0=E6=8E=92?= =?UTF-8?q?=E9=99=A4=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTestTcpSocket/Assembly.cs | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 test/UnitTestTcpSocket/Assembly.cs diff --git a/test/UnitTestTcpSocket/Assembly.cs b/test/UnitTestTcpSocket/Assembly.cs new file mode 100644 index 00000000..2eac8083 --- /dev/null +++ b/test/UnitTestTcpSocket/Assembly.cs @@ -0,0 +1,5 @@ +// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +[assembly: ExcludeFromCodeCoverage]