Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>9.0.10</Version>
<Version>9.0.11</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,23 @@ protected virtual bool Parse(ReadOnlyMemory<byte> data, TEntity entity)
{
var value = attr.ConvertTo(data);
var valueType = value?.GetType();
if (valueType != null && p.PropertyType.IsAssignableFrom(valueType))
Comment thread
ArgoZhang marked this conversation as resolved.
if (p.PropertyType.IsAssignableFrom(valueType))
Comment thread
ArgoZhang marked this conversation as resolved.
{
p.SetValue(entity, value);
}
else
{
SocketLogging.LogInformation($"{nameof(Parse)} failed. Can't convert value from {GetValueType(valueType)} to {p.PropertyType}");
}
}
}
ret = true;
}
return ret;
}

private static string GetValueType(Type? type) => type?.FullName ?? "NULL";

private DataPropertyConverterAttribute? GetPropertyConverterAttribute(PropertyInfo propertyInfo)
{
DataPropertyConverterAttribute? attr = null;
Expand Down
26 changes: 18 additions & 8 deletions src/extensions/BootstrapBlazor.Socket/Logging/SocketLogging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,23 @@ public static void Init(ILogger logger)
/// </summary>
/// <param name="ex"></param>
/// <param name="message"></param>
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);
}
/// <summary>
/// 记录警告信息方法
/// </summary>
/// <param name="message"></param>
public static void LogWarning(string message) => _logger?.LogWarning("{message}", message);

/// <summary>
/// 记录信息方法
/// </summary>
/// <param name="message"></param>
public static void LogInformation(string message) => _logger?.LogInformation("{message}", message);

/// <summary>
/// 记录调试信息方法
/// </summary>
/// <param name="message"></param>
public static void LogDebug(string message) => _logger?.LogDebug("{message}", message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class DataByteConverter : IDataPropertyConverter
/// <param name="data"></param>
public object? Convert(ReadOnlyMemory<byte> data)
{
return data.Length > 0 ? data.Span[0] : 0x0;
return data.Length > 0 ? data.Span[0] : byte.MinValue;
}
}
5 changes: 5 additions & 0 deletions test/UnitTestTcpSocket/Assembly.cs
Original file line number Diff line number Diff line change
@@ -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]
3 changes: 3 additions & 0 deletions test/UnitTestTcpSocket/SocketLoggingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ public class SocketLoggingTest
public void Logger_Ok()
{
SocketLogging.LogError(new Exception());
SocketLogging.LogInformation("Information");
SocketLogging.LogWarning("Warning");
SocketLogging.LogDebug("Debug");
}
}
31 changes: 31 additions & 0 deletions test/UnitTestTcpSocket/TcpSocketFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<MockEntity>
Expand All @@ -1433,6 +1456,14 @@ protected override bool Parse(ReadOnlyMemory<byte> data, MockEntity entity)
}
}

class MockNullConverter : IDataPropertyConverter
{
public object? Convert(ReadOnlyMemory<byte> data)
{
return null;
}
}

class FooConverter(string name) : IDataPropertyConverter
{
public object? Convert(ReadOnlyMemory<byte> data)
Expand Down
11 changes: 11 additions & 0 deletions test/UnitTestTcpSocket/TcpSocketPropertyConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>());
Assert.Equal((byte)0x0, actual);
}
}
1 change: 1 addition & 0 deletions test/UnitTestTcpSocket/UnitTestTcpSocket.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\extensions\BootstrapBlazor.Socket\BootstrapBlazor.Socket.csproj" />
<ProjectReference Include="..\..\src\extensions\BootstrapBlazor.TcpSocket\BootstrapBlazor.TcpSocket.csproj" />
</ItemGroup>

Expand Down