diff --git a/src/JT808.Gateway.Benchmark/JT808.Gateway.CleintBenchmark/Program.cs b/src/JT808.Gateway.Benchmark/JT808.Gateway.CleintBenchmark/Program.cs index 9bfc9e4..0061607 100644 --- a/src/JT808.Gateway.Benchmark/JT808.Gateway.CleintBenchmark/Program.cs +++ b/src/JT808.Gateway.Benchmark/JT808.Gateway.CleintBenchmark/Program.cs @@ -68,7 +68,8 @@ namespace JT808.Gateway.CleintBenchmark services.AddSingleton(); services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); services.AddJT808Configure() - .AddClient(); + .AddClient() + .AddClientReport(); services.AddHostedService(); }); await serverHostBuilder.RunConsoleAsync(); diff --git a/src/JT808.Gateway.Client/IJT808ClientBuilder.cs b/src/JT808.Gateway.Client/IJT808ClientBuilder.cs new file mode 100644 index 0000000..319a361 --- /dev/null +++ b/src/JT808.Gateway.Client/IJT808ClientBuilder.cs @@ -0,0 +1,14 @@ +using JT808.Protocol; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Text; + +namespace JT808.Gateway.Client +{ + public interface IJT808ClientBuilder + { + IJT808Builder JT808Builder { get; } + IJT808Builder Builder(); + } +} diff --git a/src/JT808.Gateway.Client/Internal/JT808ClientBuilderDefault.cs b/src/JT808.Gateway.Client/Internal/JT808ClientBuilderDefault.cs new file mode 100644 index 0000000..54b26d0 --- /dev/null +++ b/src/JT808.Gateway.Client/Internal/JT808ClientBuilderDefault.cs @@ -0,0 +1,24 @@ +using JT808.Protocol; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using System; +using System.Collections.Generic; +using System.Text; + +namespace JT808.Gateway.Client +{ + internal class JT808ClientBuilderDefault : IJT808ClientBuilder + { + public IJT808Builder JT808Builder { get; } + + public JT808ClientBuilderDefault(IJT808Builder builder) + { + JT808Builder = builder; + } + + public IJT808Builder Builder() + { + return JT808Builder; + } + } +} \ No newline at end of file diff --git a/src/JT808.Gateway.Client/JT808ClientExtensions.cs b/src/JT808.Gateway.Client/JT808ClientExtensions.cs index c673447..b5a1cb7 100644 --- a/src/JT808.Gateway.Client/JT808ClientExtensions.cs +++ b/src/JT808.Gateway.Client/JT808ClientExtensions.cs @@ -10,33 +10,34 @@ namespace JT808.Gateway.Client { public static class JT808ClientExtensions { - public static IJT808Builder AddClient(this IJT808Builder jT808Builder) + public static IJT808ClientBuilder AddClient(this IJT808Builder jT808Builder) { - jT808Builder.Services.AddSingleton(); - jT808Builder.Services.AddSingleton(); - jT808Builder.Services.AddSingleton(); - jT808Builder.Services.Configure((options)=> { }); - jT808Builder.Services.AddHostedService(); - return jT808Builder; + JT808ClientBuilderDefault jT808ClientBuilderDefault = new JT808ClientBuilderDefault(jT808Builder); + jT808ClientBuilderDefault.JT808Builder.Services.AddSingleton(); + jT808ClientBuilderDefault.JT808Builder.Services.AddSingleton(); + jT808ClientBuilderDefault.JT808Builder.Services.AddSingleton(); + return jT808ClientBuilderDefault; } - public static IJT808Builder AddClient(this IJT808Builder jT808Builder, IConfiguration Configuration) + + public static IJT808ClientBuilder AddClientReport(this IJT808ClientBuilder jT808ClientBuilder) + { + jT808ClientBuilder.JT808Builder.Services.Configure((options) => { }); + jT808ClientBuilder.JT808Builder.Services.AddHostedService(); + return jT808ClientBuilder; + } + + public static IJT808ClientBuilder AddClientReport(this IJT808ClientBuilder jT808ClientBuilder, IConfiguration Configuration) { - jT808Builder.Services.AddSingleton(); - jT808Builder.Services.AddSingleton(); - jT808Builder.Services.AddSingleton(); - jT808Builder.Services.Configure(Configuration.GetSection("JT808ReportOptions")); - jT808Builder.Services.AddHostedService(); - return jT808Builder; + jT808ClientBuilder.JT808Builder.Services.Configure(Configuration.GetSection("JT808ReportOptions")); + jT808ClientBuilder.JT808Builder.Services.AddHostedService(); + return jT808ClientBuilder; } - public static IJT808Builder AddClient(this IJT808Builder jT808Builder, Action reportOptions) + public static IJT808ClientBuilder AddClientReport(this IJT808ClientBuilder jT808ClientBuilder, Action reportOptions) { - jT808Builder.Services.AddSingleton(); - jT808Builder.Services.AddSingleton(); - jT808Builder.Services.AddSingleton(); - jT808Builder.Services.Configure(reportOptions); - jT808Builder.Services.AddHostedService(); - return jT808Builder; + jT808ClientBuilder.JT808Builder.Services.Configure(reportOptions); + jT808ClientBuilder.JT808Builder.Services.AddHostedService(); + return jT808ClientBuilder; } } } diff --git a/src/JT808.Gateway.Client/JT808TcpClient.cs b/src/JT808.Gateway.Client/JT808TcpClient.cs index f2c3860..7b8366e 100644 --- a/src/JT808.Gateway.Client/JT808TcpClient.cs +++ b/src/JT808.Gateway.Client/JT808TcpClient.cs @@ -19,7 +19,6 @@ namespace JT808.Gateway.Client public class JT808TcpClient:IDisposable { //todo: 客户端的断线重连 - //todo: 客户端的消息处理handler private bool disposed = false; private Socket clientSocket; private readonly ILogger Logger; @@ -72,7 +71,7 @@ namespace JT808.Gateway.Client { try { - Memory memory = writer.GetMemory(80960); + Memory memory = writer.GetMemory(8096); int bytesRead = await session.ReceiveAsync(memory, SocketFlags.None, cancellationToken); if (bytesRead == 0) { @@ -149,7 +148,7 @@ namespace JT808.Gateway.Client { try { - var package = JT808Serializer.HeaderDeserialize(seqReader.Sequence.Slice(totalConsumed, seqReader.Consumed - totalConsumed).FirstSpan,minBufferSize:10240); + var package = JT808Serializer.HeaderDeserialize(seqReader.Sequence.Slice(totalConsumed, seqReader.Consumed - totalConsumed).ToArray(),minBufferSize:8096); ReceiveAtomicCounterService.MsgSuccessIncrement(); if (Logger.IsEnabled(LogLevel.Debug)) Logger.LogDebug($"[Atomic Success Counter]:{ReceiveAtomicCounterService.MsgSuccessCount}"); if (Logger.IsEnabled(LogLevel.Trace)) Logger.LogTrace($"[Accept Hex {session.RemoteEndPoint}]:{package.OriginalData.ToArray().ToHexString()}"); diff --git a/src/JT808.Gateway.Client/JT808TcpClientFactory.cs b/src/JT808.Gateway.Client/JT808TcpClientFactory.cs index 5d928c6..60ba143 100644 --- a/src/JT808.Gateway.Client/JT808TcpClientFactory.cs +++ b/src/JT808.Gateway.Client/JT808TcpClientFactory.cs @@ -14,6 +14,8 @@ namespace JT808.Gateway.Client { ValueTask Create(JT808DeviceConfig deviceConfig, CancellationToken cancellationToken); + void Remove(JT808DeviceConfig deviceConfign); + List GetAll(); } @@ -55,6 +57,7 @@ namespace JT808.Gateway.Client { try { + client.Value.Close(); client.Value.Dispose(); } catch @@ -67,5 +70,21 @@ namespace JT808.Gateway.Client { return dict.Values.ToList(); } + + public void Remove(JT808DeviceConfig deviceConfig) + { + if(dict.TryRemove(deviceConfig.TerminalPhoneNo,out var client)) + { + try + { + client.Close(); + client.Dispose(); + } + catch (Exception) + { + + } + } + } } } diff --git a/src/JT808.Gateway.Tests/JT808.Gateway.QueueHosting/Program.cs b/src/JT808.Gateway.Tests/JT808.Gateway.QueueHosting/Program.cs index fa9e83e..7277d8a 100644 --- a/src/JT808.Gateway.Tests/JT808.Gateway.QueueHosting/Program.cs +++ b/src/JT808.Gateway.Tests/JT808.Gateway.QueueHosting/Program.cs @@ -54,6 +54,8 @@ namespace JT808.Gateway.QueueHosting .Builder() //添加客户端工具 .AddClient() + .AddClientReport() + .Builder() //添加客户端服务 .AddClientKafka() .AddMsgConsumer(hostContext.Configuration)