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 diff --git a/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs b/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs index 44425a29..730bb469 100644 --- a/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs +++ b/src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs @@ -72,10 +72,14 @@ 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 from {GetValueType(valueType)} to {p.PropertyType}"); + } } } ret = true; @@ -83,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/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); } 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/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] diff --git a/test/UnitTestTcpSocket/SocketLoggingTest.cs b/test/UnitTestTcpSocket/SocketLoggingTest.cs index 34292790..9fd34ec2 100644 --- a/test/UnitTestTcpSocket/SocketLoggingTest.cs +++ b/test/UnitTestTcpSocket/SocketLoggingTest.cs @@ -12,5 +12,8 @@ public class SocketLoggingTest public void Logger_Ok() { SocketLogging.LogError(new Exception()); + SocketLogging.LogInformation("Information"); + SocketLogging.LogWarning("Warning"); + SocketLogging.LogDebug("Debug"); } } diff --git a/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs b/test/UnitTestTcpSocket/TcpSocketFactoryTest.cs index 4ab1a4c2..108d0bc4 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] @@ -670,6 +675,15 @@ 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); + // string Assert.Equal("1", entity.Value1); @@ -1415,6 +1429,15 @@ 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; } + + [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 @@ -1433,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) 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); + } } 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 @@ +