Skip to content

Commit

Permalink
removed System.Linq.Async dependency.
Browse files Browse the repository at this point in the history
removed Microsoft.Extensions.Hosting.Abstractions dependency.  HsmsConnection no longer inherits BackgroundService.
hidden some inappropriate members from ISecsConnection.
fixed broken unit tests.
  • Loading branch information
westjeffho committed Jan 4, 2024
1 parent cb68fda commit 4f6bff1
Show file tree
Hide file tree
Showing 20 changed files with 397 additions and 391 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.4.1</Version>
<Version>2.4.2</Version>
<RepositoryUrl>https://github.com/mkjeff/secs4net</RepositoryUrl>
<Authors>mkjeff</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ SECS-II/HSMS-SS/GEM implementation on .NET. This library provides an easy way to
## Install Nuget package
> dotnet add package Secs4Net

## Configure .NET dependency injection
## Configure via .NET dependency injection
[Sample code reference](https://github.com/mkjeff/secs4net/blob/base/samples/DeviceWorkerService/ServiceProvider.cs)
```cs
public void ConfigureServices(IServiceCollection services)
{
Expand Down
1 change: 1 addition & 0 deletions samples/DeviceWorkerService/DeviceWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
_hsmsConnection.Start(stoppingToken);
await foreach (var e in _secsGem.GetPrimaryMessageAsync(stoppingToken))
{
using var primaryMessage = e.PrimaryMessage;
Expand Down
1 change: 0 additions & 1 deletion samples/DeviceWorkerService/ServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public static class ServiceProvider
services.AddSingleton<ISecsConnection, HsmsConnection>();
services.AddSingleton<ISecsGem, SecsGem>();
services.AddSingleton<ISecsGemLogger, TLogger>();
services.AddHostedService(static s => (HsmsConnection)s.GetRequiredService<ISecsConnection>());
return services;
}
}
2 changes: 1 addition & 1 deletion samples/SecsDevice/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private async void btnEnable_Click(object sender, EventArgs e)
};

btnEnable.Enabled = false;
_ = _connector.StartAsync(_cancellationTokenSource.Token);
_connector.Start(_cancellationTokenSource.Token);
btnDisable.Enabled = true;

try
Expand Down
19 changes: 8 additions & 11 deletions src/Secs4Net/HsmsConnection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CommunityToolkit.HighPerformance.Buffers;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using PooledAwait;
using System.Buffers;
Expand All @@ -15,7 +14,7 @@ namespace Secs4Net;
#if NET
[UnsupportedOSPlatform("browser")]
#endif
public sealed class HsmsConnection : BackgroundService, ISecsConnection, IAsyncDisposable
public sealed class HsmsConnection : ISecsConnection, IAsyncDisposable
{
public event EventHandler<ConnectionState>? ConnectionChanged;
public int T5 { get; }
Expand Down Expand Up @@ -243,16 +242,12 @@ private void Disconnect()
_socket = null;
}

protected override Task ExecuteAsync(CancellationToken stoppingToken)
public void Start(CancellationToken cancellation)
{
_stoppingToken = stoppingToken;
Start(_stoppingToken);
return Task.CompletedTask;
_stoppingToken = cancellation;
Task.Run(() => _startImpl(cancellation), cancellation);
}

private void Start(CancellationToken cancellation)
=> Task.Run(() => _startImpl(cancellation), cancellation);

private async Task StartPipeDecoderConsumerAsync(CancellationToken cancellation)
{
try
Expand Down Expand Up @@ -360,8 +355,10 @@ private async Task HandleControlMessagesAsync(CancellationToken cancellation)
{
try
{
await _pipeDecoder.GetControlMessages(cancellation)
.ForEachAwaitWithCancellationAsync(ProcessControlMessageAsync, cancellation).ConfigureAwait(false);
await foreach (var item in _pipeDecoder.GetControlMessages(cancellation).WithCancellation(cancellation).ConfigureAwait(false))
{
await ProcessControlMessageAsync(item, cancellation).ConfigureAwait(continueOnCapturedContext: false);
}
}
catch (OperationCanceledException) when (cancellation.IsCancellationRequested) { }
}
Expand Down
13 changes: 4 additions & 9 deletions src/Secs4Net/ISecsConnection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel;
using System.Net;
using System.Net;

namespace Secs4Net;

Expand Down Expand Up @@ -37,14 +36,10 @@ public interface ISecsConnection
/// otherwise, return "N/A"
/// </summary>
string DeviceIpAddress { get; }

bool LinkTestEnabled { get; set; }

void Start(CancellationToken cancellation);
void Reconnect();

[EditorBrowsable(EditorBrowsableState.Advanced)]
Task SendAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken);

[EditorBrowsable(EditorBrowsableState.Advanced)]
IAsyncEnumerable<(MessageHeader header, Item? rootItem)> GetDataMessages(CancellationToken cancellation);
internal Task SendAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken);
internal IAsyncEnumerable<(MessageHeader header, Item? rootItem)> GetDataMessages(CancellationToken cancellation);
}
2 changes: 0 additions & 2 deletions src/Secs4Net/Secs4Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@
</PackageReference>
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.2.2" />
<PackageReference Include="PooledAwait" Version="1.0.49" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup>

Expand Down
12 changes: 8 additions & 4 deletions src/Secs4Net/SecsGem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ public SecsGem(IOptions<SecsGemOptions> secsGemOptions, ISecsConnection hsmsConn
_hsmsConnector = hsmsConnector;
_logger = logger;

Task.Run(() =>
_hsmsConnector.GetDataMessages(_cancellationSourceForDataMessageProcessing.Token)
.ForEachAwaitWithCancellationAsync((a, ct) =>
ProcessDataMessageAsync(a.header, a.rootItem, ct), _cancellationSourceForDataMessageProcessing.Token));
Task.Run(async () =>
{
var cancellationToken = _cancellationSourceForDataMessageProcessing.Token;
await foreach (var (header, rootItem) in _hsmsConnector.GetDataMessages(cancellationToken).WithCancellation(cancellationToken).ConfigureAwait(false))
{
await ProcessDataMessageAsync(header, rootItem, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
}
});
}

internal async Task<SecsMessage> SendDataMessageAsync(SecsMessage message, int id, CancellationToken cancellation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ BenchmarkDotNet v0.13.10, Windows 11 (10.0.22631.2861/23H2/2023Update/SunValley3
Unknown processor
.NET SDK 8.0.100
[Host] : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2
Job-LLCSMO : .NET 6.0.25 (6.0.2523.51912), X64 RyuJIT AVX2
Job-LSZLMA : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2
Job-OOEVDF : .NET 6.0.25 (6.0.2523.51912), X64 RyuJIT AVX2
Job-AKZPHK : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2
```
| Method | Runtime | ItemCount | Mean | Error | StdDev | Ratio | RatioSD | Allocated | Alloc Ratio |
|------- |--------- |---------- |-----------:|---------:|---------:|-------------:|--------:|----------:|------------:|
| **Encode** | **.NET 6.0** | **0** | **198.8 ns** | **1.90 ns** | **1.69 ns** | **baseline** | **** | **40 B** | **** |
| Encode | .NET 8.0 | 0 | 122.2 ns | 2.41 ns | 2.13 ns | 1.63x faster | 0.03x | 40 B | 1.00x more |
| **Encode** | **.NET 6.0** | **0** | **199.4 ns** | **1.67 ns** | **1.48 ns** | **baseline** | **** | **40 B** | **** |
| Encode | .NET 8.0 | 0 | 122.7 ns | 1.67 ns | 1.56 ns | 1.63x faster | 0.03x | 40 B | 1.00x more |
| | | | | | | | | | |
| Decode | .NET 6.0 | 0 | 1,411.0 ns | 19.15 ns | 16.97 ns | baseline | | 560 B | |
| Decode | .NET 8.0 | 0 | 1,269.6 ns | 21.72 ns | 20.32 ns | 1.11x faster | 0.02x | 560 B | 1.00x more |
| Decode | .NET 6.0 | 0 | 1,338.1 ns | 20.43 ns | 19.11 ns | baseline | | 560 B | |
| Decode | .NET 8.0 | 0 | 1,205.0 ns | 15.76 ns | 14.74 ns | 1.11x faster | 0.02x | 560 B | 1.00x more |
| | | | | | | | | | |
| **Encode** | **.NET 6.0** | **64** | **731.4 ns** | **8.99 ns** | **7.97 ns** | **baseline** | **** | **40 B** | **** |
| Encode | .NET 8.0 | 64 | 559.5 ns | 10.67 ns | 11.42 ns | 1.30x faster | 0.03x | 40 B | 1.00x more |
| **Encode** | **.NET 6.0** | **64** | **745.2 ns** | **9.73 ns** | **9.10 ns** | **baseline** | **** | **40 B** | **** |
| Encode | .NET 8.0 | 64 | 636.7 ns | 5.55 ns | 4.33 ns | 1.17x faster | 0.02x | 40 B | 1.00x more |
| | | | | | | | | | |
| Decode | .NET 6.0 | 64 | 2,685.3 ns | 41.63 ns | 36.90 ns | baseline | | 7248 B | |
| Decode | .NET 8.0 | 64 | 2,499.6 ns | 36.32 ns | 32.20 ns | 1.07x faster | 0.02x | 7248 B | 1.00x more |
| Decode | .NET 6.0 | 64 | 2,721.1 ns | 51.64 ns | 57.40 ns | baseline | | 7248 B | |
| Decode | .NET 8.0 | 64 | 2,445.7 ns | 48.33 ns | 47.47 ns | 1.11x faster | 0.02x | 7248 B | 1.00x more |
| | | | | | | | | | |
| **Encode** | **.NET 6.0** | **128** | **1,127.0 ns** | **19.01 ns** | **17.78 ns** | **baseline** | **** | **40 B** | **** |
| Encode | .NET 8.0 | 128 | 956.6 ns | 12.05 ns | 9.41 ns | 1.17x faster | 0.02x | 40 B | 1.00x more |
| **Encode** | **.NET 6.0** | **128** | **1,129.3 ns** | **19.60 ns** | **18.34 ns** | **baseline** | **** | **40 B** | **** |
| Encode | .NET 8.0 | 128 | 963.3 ns | 18.02 ns | 16.85 ns | 1.17x faster | 0.03x | 40 B | 1.00x more |
| | | | | | | | | | |
| Decode | .NET 6.0 | 128 | 3,297.3 ns | 41.69 ns | 36.96 ns | baseline | | 9112 B | |
| Decode | .NET 8.0 | 128 | 3,107.9 ns | 44.40 ns | 39.36 ns | 1.06x faster | 0.01x | 9112 B | 1.00x more |
| Decode | .NET 6.0 | 128 | 3,270.3 ns | 27.20 ns | 25.44 ns | baseline | | 9112 B | |
| Decode | .NET 8.0 | 128 | 3,038.6 ns | 33.79 ns | 26.38 ns | 1.08x faster | 0.01x | 9112 B | 1.00x more |
Loading

0 comments on commit 4f6bff1

Please sign in to comment.