Selaa lähdekoodia

1.完善客户端的心跳、断线重连、多网卡IP绑定

2.修改升级新版例子
tags/pipeline-1.1.0
SmallChi(Koike) 4 vuotta sitten
vanhempi
commit
0060ba5579
23 muutettua tiedostoa jossa 195 lisäystä ja 242 poistoa
  1. +8
    -8
      simples/JT808.Gateway.SimpleClient/JT808.Gateway.SimpleClient.csproj
  2. +0
    -67
      simples/JT808.Gateway.SimpleClient/Jobs/CallGrpcClientJob.cs
  3. +54
    -0
      simples/JT808.Gateway.SimpleClient/Jobs/CallHttpClientJob.cs
  4. +1
    -1
      simples/JT808.Gateway.SimpleClient/Program.cs
  5. +1
    -1
      simples/JT808.Gateway.SimpleClient/Services/Up2019Service.cs
  6. +3
    -3
      simples/JT808.Gateway.SimpleQueueNotification/JT808.Gateway.SimpleQueueNotification.csproj
  7. +7
    -7
      simples/JT808.Gateway.SimpleQueueServer/JT808.Gateway.SimpleQueueServer.csproj
  8. +2
    -3
      simples/JT808.Gateway.SimpleQueueServer/Program.cs
  9. +10
    -19
      simples/JT808.Gateway.SimpleQueueService/Impl/JT808QueueReplyMessageHandlerImpl.cs
  10. +13
    -13
      simples/JT808.Gateway.SimpleQueueService/JT808.Gateway.SimpleQueueService.csproj
  11. +9
    -11
      simples/JT808.Gateway.SimpleServer/Impl/JT808MessageHandlerImpl.cs
  12. +13
    -6
      simples/JT808.Gateway.SimpleServer/Impl/JT808MsgIdHandler.cs
  13. +10
    -12
      simples/JT808.Gateway.SimpleServer/JT808.Gateway.SimpleServer.csproj
  14. +0
    -48
      simples/JT808.Gateway.SimpleServer/Jobs/TrafficJob.cs
  15. +3
    -9
      simples/JT808.Gateway.SimpleServer/Program.cs
  16. +10
    -0
      src/JT808.Gateway.Benchmark/JT808.Gateway.CleintBenchmark/Configs/ClientBenchmarkOptions.cs
  17. +4
    -1
      src/JT808.Gateway.Benchmark/JT808.Gateway.CleintBenchmark/Services/CleintBenchmarkHostedService.cs
  18. +3
    -3
      src/JT808.Gateway.Benchmark/JT808.Gateway.CleintBenchmark/appsettings.json
  19. +1
    -6
      src/JT808.Gateway.Client/JT808ClientExtensions.cs
  20. +5
    -1
      src/JT808.Gateway.Client/JT808DeviceConfig.cs
  21. +7
    -1
      src/JT808.Gateway.Client/JT808TcpClient.cs
  22. +1
    -1
      src/JT808.Gateway.Client/JT808TcpClientFactory.cs
  23. +30
    -21
      src/JT808.Gateway.Client/Services/JT808RetryClientHostedService.cs

+ 8
- 8
simples/JT808.Gateway.SimpleClient/JT808.Gateway.SimpleClient.csproj Näytä tiedosto

@@ -2,16 +2,16 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JT808.Gateway.Abstractions" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.Client" Version="1.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.4" />
<PackageReference Include="WebApiClient.Extensions.DependencyInjection" Version="2.0.3" />
<PackageReference Include="JT808.Gateway.Abstractions" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.Client" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.WebApiClientTool" Version="1.1.0-preview1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
</ItemGroup>
</Project>

+ 0
- 67
simples/JT808.Gateway.SimpleClient/Jobs/CallGrpcClientJob.cs Näytä tiedosto

@@ -1,67 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
using JT808.Gateway.GrpcService;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Text.Json;

namespace JT808.Gateway.SimpleClient.Jobs
{
public class CallGrpcClientJob :IHostedService
{
private Channel channel;
private readonly ILogger Logger;
private Grpc.Core.Metadata AuthMetadata;
public CallGrpcClientJob(
ILoggerFactory loggerFactory)
{
Logger = loggerFactory.CreateLogger("CallGrpcClientJob");
channel = new Channel("localhost:828",
ChannelCredentials.Insecure);
AuthMetadata = new Grpc.Core.Metadata();
AuthMetadata.Add("token", "smallchi518");
}

public Task StartAsync(CancellationToken cancellationToken)
{
Task.Run(() =>
{
while (!cancellationToken.IsCancellationRequested)
{
JT808Gateway.JT808GatewayClient jT808GatewayClient = new JT808Gateway.JT808GatewayClient(channel);
try
{
var result1 = jT808GatewayClient.GetTcpAtomicCounter(new Empty(), AuthMetadata);
var result2 = jT808GatewayClient.GetTcpSessionAll(new Empty(), AuthMetadata);
Logger.LogInformation($"[GetTcpAtomicCounter]:{JsonSerializer.Serialize(result1)}");
Logger.LogInformation($"[GetTcpSessionAll]:{JsonSerializer.Serialize(result2)}");
}
catch (Exception ex)
{
Logger.LogError(ex, "Call Grpc Error");
}
try
{
var result1 = jT808GatewayClient.GetTcpAtomicCounter(new Empty());
}
catch (RpcException ex)
{
Logger.LogError($"{ex.StatusCode.ToString()}-{ex.Message}");
}
Thread.Sleep(3000);
}
}, cancellationToken);
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
channel.ShutdownAsync();
return Task.CompletedTask;
}
}
}

+ 54
- 0
simples/JT808.Gateway.SimpleClient/Jobs/CallHttpClientJob.cs Näytä tiedosto

@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using JT808.Protocol.Extensions;
using Microsoft.Extensions.Options;
using JT808.Gateway.Abstractions.Configurations;
using JT808.Gateway.WebApiClientTool;

namespace JT808.Gateway.SimpleClient.Jobs
{
public class CallHttpClientJob :IHostedService
{

private readonly ILogger Logger;
private JT808HttpClient jT808HttpClient;
public CallHttpClientJob(
ILoggerFactory loggerFactory,
JT808HttpClient jT808HttpClient)
{
Logger = loggerFactory.CreateLogger<CallHttpClientJob>();
this.jT808HttpClient = jT808HttpClient;
}

public Task StartAsync(CancellationToken cancellationToken)
{
Task.Run(async() =>
{
while (!cancellationToken.IsCancellationRequested)
{
var result2 = await jT808HttpClient.GetTcpSessionAll();
var result3 = await jT808HttpClient.UnificationSend(new Abstractions.Dtos.JT808UnificationSendRequestDto
{
TerminalPhoneNo= "123456789012",
HexData= "7E02000026123456789012007D02000000010000000200BA7F0E07E4F11C0028003C00001810151010100104000000640202007D01137E"
});
Logger.LogInformation($"[GetTcpAtomicCounter]:{JsonSerializer.Serialize(result2)}");
Logger.LogInformation($"[GetTcpSessionAll]:{JsonSerializer.Serialize(result3)}");
Thread.Sleep(3000);
}
}, cancellationToken);
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}

+ 1
- 1
simples/JT808.Gateway.SimpleClient/Program.cs Näytä tiedosto

@@ -31,7 +31,7 @@ namespace JT808.Gateway.SimpleClient
.AddClient();
services.AddHostedService<UpService>();
services.AddHostedService<Up2019Service>();
services.AddHostedService<CallGrpcClientJob>();
services.AddHostedService<CallHttpClientJob>();
});
await serverHostBuilder.RunConsoleAsync();
}


+ 1
- 1
simples/JT808.Gateway.SimpleClient/Services/Up2019Service.cs Näytä tiedosto

@@ -23,7 +23,7 @@ namespace JT808.Gateway.SimpleClient.Services
public async Task StartAsync(CancellationToken cancellationToken)
{
string sim = "22222222222";
JT808TcpClient client1 = await jT808TcpClientFactory.Create(new JT808DeviceConfig(sim, "127.0.0.1", 808, JT808Version.JTT2019), cancellationToken);
JT808TcpClient client1 = await jT808TcpClientFactory.Create(new JT808DeviceConfig(sim, "127.0.0.1", 808, version:JT808Version.JTT2019), cancellationToken);
await Task.Delay(2000);
//1.终端注册
await client1.SendAsync(JT808MsgId.终端注册.Create2019(sim, new JT808_0x0100()


+ 3
- 3
simples/JT808.Gateway.SimpleQueueNotification/JT808.Gateway.SimpleQueueNotification.csproj Näytä tiedosto

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
@@ -27,7 +27,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="JT808.Gateway.Kafka" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.MsgIdHandler" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.Kafka" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.MsgIdHandler" Version="1.0.2-preview1" />
</ItemGroup>
</Project>

+ 7
- 7
simples/JT808.Gateway.SimpleQueueServer/JT808.Gateway.SimpleQueueServer.csproj Näytä tiedosto

@@ -2,16 +2,16 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JT808.Gateway" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.Kafka" Version="1.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.4" />
<PackageReference Include="JT808.Gateway" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.Kafka" Version="1.1.0-preview1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
</ItemGroup>
<ItemGroup>


+ 2
- 3
simples/JT808.Gateway.SimpleQueueServer/Program.cs Näytä tiedosto

@@ -29,14 +29,13 @@ namespace JT808.Gateway.SimpleQueueServer
services.AddSingleton<ILoggerFactory, LoggerFactory>();
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
services.AddJT808Configure()
.AddQueueGateway(hostContext.Configuration)
.AddGateway(hostContext.Configuration)
.AddServerKafkaMsgProducer(hostContext.Configuration)
.AddServerKafkaMsgReplyConsumer(hostContext.Configuration)
.AddServerKafkaSessionProducer(hostContext.Configuration)
.AddTcp()
.AddUdp()
.AddGrpc()
.Builder();
.AddHttp();
});

await serverHostBuilder.RunConsoleAsync();


+ 10
- 19
simples/JT808.Gateway.SimpleQueueService/Impl/JT808QueueReplyMessageHandlerImpl.cs Näytä tiedosto

@@ -1,36 +1,27 @@
using JT808.Gateway.Abstractions;
using JT808.Gateway.Abstractions.Configurations;
using JT808.Protocol;
using JT808.Protocol.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Text;

namespace JT808.Gateway.SimpleQueueService.Impl
{
public class JT808QueueReplyMessageHandlerImpl : JT808QueueReplyMessageHandler
public class JT808QueueReplyMessageHandlerImpl : IJT808ReplyMessageHandler
{
public JT808QueueReplyMessageHandlerImpl(IJT808Config jT808Config, IJT808MsgReplyProducer jT808MsgReplyProducer) : base(jT808Config, jT808MsgReplyProducer)
{
//添加自定义消息
HandlerDict.Add(0x9999, Msg0x9999);
}
private ILogger logger;

/// <summary>
/// 重写消息
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public override byte[] Msg0x0001(JT808HeaderPackage request)
public JT808QueueReplyMessageHandlerImpl(ILoggerFactory loggerFactory)
{
return base.Msg0x0001(request);
logger = loggerFactory.CreateLogger<JT808QueueReplyMessageHandlerImpl>();
}

/// <summary>
/// 自定义消息
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public byte[] Msg0x9999(JT808HeaderPackage request)
public byte[] Processor(string TerminalNo, byte[] Data)
{
logger.LogDebug($"JT808QueueReplyMessageHandlerImpl=>{TerminalNo},{Data.ToHexString()}");
return default;
}
}


+ 13
- 13
simples/JT808.Gateway.SimpleQueueService/JT808.Gateway.SimpleQueueService.csproj Näytä tiedosto

@@ -2,22 +2,22 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JT808.Gateway" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.Kafka" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.MsgIdHandler" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.MsgLogging" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.ReplyMessage" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.SessionNotice" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.Traffic" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.Transmit" Version="1.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.4" />
<PackageReference Include="JT808.Gateway" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.Kafka" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.MsgIdHandler" Version="1.0.2-preview1" />
<PackageReference Include="JT808.Gateway.MsgLogging" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.ReplyMessage" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.SessionNotice" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.Traffic" Version="1.0.2-preview1" />
<PackageReference Include="JT808.Gateway.Transmit" Version="1.1.0-preview1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
</ItemGroup>

<ItemGroup>


simples/JT808.Gateway.SimpleServer/Impl/JT808NormalReplyMessageHandlerImpl.cs → simples/JT808.Gateway.SimpleServer/Impl/JT808MessageHandlerImpl.cs Näytä tiedosto

@@ -1,33 +1,33 @@
using JT808.Gateway.Abstractions;
using JT808.Gateway.Abstractions.Configurations;
using JT808.Gateway.MsgLogging;
using JT808.Gateway.Traffic;
using JT808.Gateway.Transmit;
using JT808.Protocol;
using JT808.Protocol.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Text;

namespace JT808.Gateway.SimpleServer.Impl
{
public class JT808NormalReplyMessageHandlerImpl : JT808NormalReplyMessageHandler
public class JT808MessageHandlerImpl : JT808MessageHandler
{
private readonly ILogger logger;
private readonly IJT808Traffic jT808Traffic;
private readonly IJT808MsgLogging jT808MsgLogging;
private readonly JT808TransmitService jT808TransmitService;
public JT808NormalReplyMessageHandlerImpl(

public JT808MessageHandlerImpl(
ILoggerFactory loggerFactory,
JT808TransmitService jT808TransmitService,
IJT808MsgLogging jT808MsgLogging,
IJT808Traffic jT808Traffic,
ILoggerFactory loggerFactory,
IJT808Config jT808Config) : base(jT808Config)
IOptionsMonitor<JT808Configuration> jT808ConfigurationOptionsMonitor, IJT808MsgProducer msgProducer, IJT808MsgReplyLoggingProducer msgReplyLoggingProducer, IJT808Config jT808Config)
: base(jT808ConfigurationOptionsMonitor, msgProducer, msgReplyLoggingProducer, jT808Config)
{
this.jT808TransmitService = jT808TransmitService;
this.jT808Traffic = jT808Traffic;
this.jT808MsgLogging = jT808MsgLogging;
logger =loggerFactory.CreateLogger("JT808NormalReplyMessageHandlerImpl");
logger = loggerFactory.CreateLogger<JT808MessageHandlerImpl>();
//添加自定义消息
HandlerDict.Add(0x9999, Msg0x9999);
}
@@ -41,8 +41,6 @@ namespace JT808.Gateway.SimpleServer.Impl
{
//AOP 可以自定义添加一些东西:上下行日志、数据转发
logger.LogDebug("可以自定义添加一些东西:上下行日志、数据转发");
//流量
jT808Traffic.Increment(request.Header.TerminalPhoneNo, DateTime.Now.ToString("yyyyMMdd"), request.OriginalData.Length);
var parameter = (request.Header.TerminalPhoneNo, request.OriginalData.ToArray());
//转发数据(可同步也可以使用队列进行异步)
try

+ 13
- 6
simples/JT808.Gateway.SimpleServer/Impl/JT808MsgIdHandler.cs Näytä tiedosto

@@ -1,22 +1,29 @@
using JT808.Gateway.MsgIdHandler;
using JT808.Protocol.Extensions;
using JT808.Protocol.Extensions;
using JT808.Gateway.Abstractions;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Options;
using JT808.Gateway.Abstractions.Configurations;
using JT808.Protocol;

namespace JT808.Gateway.SimpleServer.Impl
{
public class JT808MsgIdHandler : IJT808MsgIdHandler
public class JT808MsgIdHandler : JT808MessageHandler
{
private readonly ILogger Logger;
public JT808MsgIdHandler(ILoggerFactory loggerFactory)

public JT808MsgIdHandler(
ILoggerFactory loggerFactory,
IOptionsMonitor<JT808Configuration> jT808ConfigurationOptionsMonitor, IJT808MsgProducer msgProducer, IJT808MsgReplyLoggingProducer msgReplyLoggingProducer, IJT808Config jT808Config) : base(jT808ConfigurationOptionsMonitor, msgProducer, msgReplyLoggingProducer, jT808Config)
{
Logger = loggerFactory.CreateLogger("JT808MsgIdHandler");
Logger = loggerFactory.CreateLogger<JT808MsgIdHandler>();
}

public void Processor((string TerminalNo, byte[] Data) parameter)
{
Logger.LogDebug($"{parameter.TerminalNo}-{parameter.Data.ToHexString()}");
Logger.LogDebug($"JT808MsgIdHandler=>{parameter.TerminalNo}-{parameter.Data.ToHexString()}");
}
}
}

+ 10
- 12
simples/JT808.Gateway.SimpleServer/JT808.Gateway.SimpleServer.csproj Näytä tiedosto

@@ -2,20 +2,18 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JT808.Gateway" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.MsgIdHandler" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.MsgLogging" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.ReplyMessage" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.SessionNotice" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.Traffic" Version="1.0.1" />
<PackageReference Include="JT808.Gateway.Transmit" Version="1.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.4" />
<PackageReference Include="JT808.Gateway" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.MsgLogging" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.ReplyMessage" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.SessionNotice" Version="1.1.0-preview1" />
<PackageReference Include="JT808.Gateway.Transmit" Version="1.1.0-preview1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">


+ 0
- 48
simples/JT808.Gateway.SimpleServer/Jobs/TrafficJob.cs Näytä tiedosto

@@ -1,48 +0,0 @@
using JT808.Gateway.Traffic;
using JT808.Protocol.Enums;
using JT808.Protocol.Extensions;
using JT808.Protocol.MessageBody;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace JT808.Gateway.SimpleServer.Jobs
{
public class TrafficJob : IHostedService
{
private readonly IJT808Traffic jT808Traffic;
private readonly ILogger Logger;
public TrafficJob(
ILoggerFactory loggerFactory,
IJT808Traffic jT808Traffic)
{
Logger = loggerFactory.CreateLogger("TrafficJob");
this.jT808Traffic = jT808Traffic;
}

public Task StartAsync(CancellationToken cancellationToken)
{
Task.Run(async () =>
{
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(2 * 1000);
foreach (var item in jT808Traffic.GetAll())
{
Logger.LogDebug($"{item.Item1}-{item.Item2}");
}
}
}, cancellationToken);
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}

+ 3
- 9
simples/JT808.Gateway.SimpleServer/Program.cs Näytä tiedosto

@@ -1,8 +1,6 @@
using JT808.Gateway.Abstractions.Enums;
using JT808.Gateway.ReplyMessage;
using JT808.Gateway.MsgLogging;
using JT808.Gateway.Traffic;
using JT808.Gateway.MsgIdHandler;
using JT808.Gateway.SessionNotice;
using JT808.Protocol;
using Microsoft.Extensions.Configuration;
@@ -16,7 +14,6 @@ using JT808.Gateway.SimpleServer.Impl;
using JT808.Gateway.SimpleServer.Services;
using JT808.Gateway.Abstractions;
using JT808.Gateway.Transmit;
using JT808.Gateway.SimpleServer.Jobs;

namespace JT808.Gateway.SimpleServer
{
@@ -44,18 +41,15 @@ namespace JT808.Gateway.SimpleServer
services.AddSingleton<IJT808SessionProducer, JT808SessionProducer>();
services.AddSingleton<IJT808SessionConsumer, JT808SessionConsumer>();
services.AddJT808Configure()
.AddNormalGateway(hostContext.Configuration)
.ReplaceNormalReplyMessageHandler<JT808NormalReplyMessageHandlerImpl>()
.AddGateway(hostContext.Configuration)
.AddMessageHandler<JT808MessageHandlerImpl>()
.AddMsgLogging<JT808MsgLogging>()
.AddTraffic()
.AddSessionNotice()
.AddTransmit(hostContext.Configuration)
.AddTcp()
.AddUdp()
.AddGrpc()
.AddHttp()
.Builder();
//流量统计
services.AddHostedService<TrafficJob>();
});

await serverHostBuilder.RunConsoleAsync();


+ 10
- 0
src/JT808.Gateway.Benchmark/JT808.Gateway.CleintBenchmark/Configs/ClientBenchmarkOptions.cs Näytä tiedosto

@@ -19,6 +19,16 @@ namespace JT808.Gateway.CleintBenchmark.Configs
/// 100000-200000-300000
/// </summary>
public int DeviceTemplate { get; set; } = 0;
/// <summary>
/// 本地绑定IP地址
/// 适用多网卡绑定
/// </summary>
public string LocalIPAddress { get; set; }
/// <summary>
/// 本地绑定IP端口
/// 适用多网卡绑定
/// </summary>
public int LocalPort { get; set; }
public ClientBenchmarkOptions Value =>this;
}
}

+ 4
- 1
src/JT808.Gateway.Benchmark/JT808.Gateway.CleintBenchmark/Services/CleintBenchmarkHostedService.cs Näytä tiedosto

@@ -51,7 +51,9 @@ namespace JT808.Gateway.CleintBenchmark.Services
string deviceNo = ((int)state + 1 + clientBenchmarkOptions.DeviceTemplate).ToString();
var client = await jT808TcpClientFactory.Create(new JT808DeviceConfig(deviceNo,
clientBenchmarkOptions.IP,
clientBenchmarkOptions.Port), cancellationToken);
clientBenchmarkOptions.Port,
clientBenchmarkOptions.LocalIPAddress,
clientBenchmarkOptions.LocalPort + (int)state + 1), cancellationToken);
while (!cancellationToken.IsCancellationRequested)
{
try
@@ -80,6 +82,7 @@ namespace JT808.Gateway.CleintBenchmark.Services
await Task.Delay(clientBenchmarkOptions.Interval);
}
}, i);
Thread.Sleep(500);
}
return Task.CompletedTask;
}


+ 3
- 3
src/JT808.Gateway.Benchmark/JT808.Gateway.CleintBenchmark/appsettings.json Näytä tiedosto

@@ -13,11 +13,11 @@
}
},
"AllowedHosts": "*",
//"urls": "http://*:15004;",
"urls": "http://*:5000;",
"ClientBenchmarkOptions": {
"IP": "127.0.0.1",
"IP": "172.18.0.7",
"Port": 808,
"DeviceCount": 100,
"DeviceCount": 1,
"Interval": 1000,
"DeviceTemplate": 100000 //需要多台机器同时访问,那么可以根据这个避开重复终端号 100000-200000-300000
}


+ 1
- 6
src/JT808.Gateway.Client/JT808ClientExtensions.cs Näytä tiedosto

@@ -20,6 +20,7 @@ namespace JT808.Gateway.Client
jT808ClientBuilderDefault.JT808Builder.Services.AddSingleton<JT808ReceiveAtomicCounterService>();
jT808ClientBuilderDefault.JT808Builder.Services.AddSingleton<IJT808TcpClientFactory, JT808TcpClientFactory>();
jT808ClientBuilderDefault.JT808Builder.Services.AddSingleton<IJT808MessageProducer, JT808MessageProducerEmpty>();
jT808ClientBuilderDefault.JT808Builder.Services.AddHostedService<JT808RetryClientHostedService>();
return jT808ClientBuilderDefault;
}

@@ -29,12 +30,6 @@ namespace JT808.Gateway.Client
jT808ClientBuilder.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(IJT808MessageProducer), typeof(TJT808MessageProducer), ServiceLifetime.Singleton));
return jT808ClientBuilder;
}

public static IJT808ClientBuilder AddClientRetry(this IJT808ClientBuilder jT808ClientBuilder)
{
jT808ClientBuilder.JT808Builder.Services.AddHostedService<JT808RetryClientHostedService>();
return jT808ClientBuilder;
}
public static IJT808ClientBuilder AddClientReport(this IJT808ClientBuilder jT808ClientBuilder)
{


+ 5
- 1
src/JT808.Gateway.Client/JT808DeviceConfig.cs Näytä tiedosto

@@ -9,12 +9,14 @@ namespace JT808.Gateway.Client
{
public class JT808DeviceConfig
{
public JT808DeviceConfig(string terminalPhoneNo, string tcpHost,int tcpPort, JT808Version version= JT808Version.JTT2013)
public JT808DeviceConfig(string terminalPhoneNo, string tcpHost,int tcpPort, string localIPAddress=null,int localPort=0, JT808Version version= JT808Version.JTT2013)
{
TerminalPhoneNo = terminalPhoneNo;
TcpHost = tcpHost;
TcpPort = tcpPort;
Version = version;
LocalIPAddress = localIPAddress;
LocalPort = localPort;
}
public JT808Version Version { get; private set; }
public string TerminalPhoneNo { get; private set; }
@@ -29,5 +31,7 @@ namespace JT808.Gateway.Client
/// </summary>
public bool AutoReconnection { get; set; } = true;
public IJT808MsgSNDistributed MsgSNDistributed { get; }
public string LocalIPAddress { get; set; }
public int LocalPort { get; set; }
}
}

+ 7
- 1
src/JT808.Gateway.Client/JT808TcpClient.cs Näytä tiedosto

@@ -46,11 +46,17 @@ namespace JT808.Gateway.Client
producer = serviceProvider.GetRequiredService<IJT808MessageProducer>();
RetryBlockingCollection = serviceProvider.GetRequiredService<JT808RetryBlockingCollection>();
}
public async ValueTask<bool> ConnectAsync(EndPoint remoteEndPoint)
public async ValueTask<bool> ConnectAsync()
{
var remoteEndPoint = new IPEndPoint(IPAddress.Parse(DeviceConfig.TcpHost), DeviceConfig.TcpPort);
clientSocket = new Socket(remoteEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
if (!string.IsNullOrEmpty(DeviceConfig.LocalIPAddress))
{
IPAddress localIPAddress = IPAddress.Parse(DeviceConfig.LocalIPAddress);
clientSocket.Bind(new IPEndPoint(localIPAddress, DeviceConfig.LocalPort));
}
await clientSocket.ConnectAsync(remoteEndPoint);
await Task.Factory.StartNew(async()=> {
while (!heartbeatCTS.IsCancellationRequested)


+ 1
- 1
src/JT808.Gateway.Client/JT808TcpClientFactory.cs Näytä tiedosto

@@ -40,7 +40,7 @@ namespace JT808.Gateway.Client
else
{
JT808TcpClient jT808TcpClient = new JT808TcpClient(deviceConfig, serviceProvider);
var successed= await jT808TcpClient.ConnectAsync(new IPEndPoint(IPAddress.Parse(deviceConfig.TcpHost), deviceConfig.TcpPort));
var successed= await jT808TcpClient.ConnectAsync();
if (successed)
{
jT808TcpClient.StartAsync(cancellationToken);


+ 30
- 21
src/JT808.Gateway.Client/Services/JT808RetryClientHostedService.cs Näytä tiedosto

@@ -15,7 +15,7 @@ using System.Threading.Tasks;

namespace JT808.Gateway.Client.Services
{
internal class JT808RetryClientHostedService : BackgroundService
internal class JT808RetryClientHostedService : IHostedService
{
private readonly IJT808TcpClientFactory jT808TcpClientFactory;

@@ -33,37 +33,46 @@ namespace JT808.Gateway.Client.Services
RetryBlockingCollection = retryBlockingCollection;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
public Task StartAsync(CancellationToken cancellationToken)
{
foreach (var item in RetryBlockingCollection.RetryBlockingCollection.GetConsumingEnumerable(stoppingToken))
{
try
Task.Run(async()=> {
foreach (var item in RetryBlockingCollection.RetryBlockingCollection.GetConsumingEnumerable(cancellationToken))
{
jT808TcpClientFactory.Remove(item);
if (item.AutoReconnection)
try
{
var result = await jT808TcpClientFactory.Create(item, stoppingToken);
if (result != null)
jT808TcpClientFactory.Remove(item);
if (item.AutoReconnection)
{
if (logger.IsEnabled(LogLevel.Information))
var result = await jT808TcpClientFactory.Create(item, cancellationToken);
if (result != null)
{
logger.LogInformation($"Retry Success-{JsonSerializer.Serialize(item)}");
if (logger.IsEnabled(LogLevel.Information))
{
logger.LogInformation($"Retry Success-{JsonSerializer.Serialize(item)}");
}
}
}
else
{
if (logger.IsEnabled(LogLevel.Warning))
else
{
logger.LogWarning($"Retry Fail-{JsonSerializer.Serialize(item)}");
if (logger.IsEnabled(LogLevel.Warning))
{
logger.LogWarning($"Retry Fail-{JsonSerializer.Serialize(item)}");
}
}
}
}
catch (Exception ex)
{
logger.LogError(ex, $"Retry Error-{JsonSerializer.Serialize(item)}");
await Task.Delay(TimeSpan.FromSeconds(5));
}
}
catch (Exception ex)
{
logger.LogError(ex, $"Retry Error-{JsonSerializer.Serialize(item)}");
}
}
}, cancellationToken);
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}

Ladataan…
Peruuta
Tallenna