@@ -82,7 +82,8 @@ namespace JT809.DotNetty.Core.Clients | |||
public async void ConnectAsync(string ip,int port,int delay=3000) | |||
{ | |||
var verifyCode = verifyCodeGenerator.Get(); | |||
logger.LogInformation($"ip:{ip},port:{port},verifycode:{verifyCode}"); | |||
if (logger.IsEnabled(LogLevel.Information)) | |||
logger.LogInformation($"ip:{ip},port:{port},verifycode:{verifyCode}"); | |||
await Task.Delay(delay); | |||
try | |||
{ | |||
@@ -94,7 +95,8 @@ namespace JT809.DotNetty.Core.Clients | |||
{ | |||
VerifyCode = verifyCode | |||
}); | |||
logger.LogInformation($"从链路连接请求消息>>>{JT809Serializer.Serialize(package, 100).ToHexString()}"); | |||
if (logger.IsEnabled(LogLevel.Information)) | |||
logger.LogInformation($"从链路连接请求消息>>>{JT809Serializer.Serialize(package, 100).ToHexString()}"); | |||
JT809Response jT809Response = new JT809Response(package, 100); | |||
SendAsync(jT809Response); | |||
} | |||
@@ -107,7 +109,8 @@ namespace JT809.DotNetty.Core.Clients | |||
{ | |||
VerifyCode = verifyCode | |||
}); | |||
logger.LogInformation($"从链路连接请求消息>>>{JT809Serializer.Serialize(package, 100).ToHexString()}"); | |||
if (logger.IsEnabled(LogLevel.Information)) | |||
logger.LogInformation($"从链路连接请求消息>>>{JT809Serializer.Serialize(package, 100).ToHexString()}"); | |||
JT809Response jT809Response = new JT809Response(package, 100); | |||
SendAsync(jT809Response); | |||
} | |||
@@ -129,14 +132,16 @@ namespace JT809.DotNetty.Core.Clients | |||
var verifyCode = verifyCodeGenerator.Get(); | |||
try | |||
{ | |||
logger.LogInformation($"IPAddress:{ipEndPoint.ToString()},verifycode:{verifyCode}"); | |||
if (logger.IsEnabled(LogLevel.Information)) | |||
logger.LogInformation($"IPAddress:{ipEndPoint.ToString()},verifycode:{verifyCode}"); | |||
channel = await bootstrap.ConnectAsync(ipEndPoint); | |||
//从链路连接请求消息 | |||
var package = JT809BusinessType.从链路连接请求消息.Create(new JT809_0x9001() | |||
{ | |||
VerifyCode = verifyCode | |||
}); | |||
logger.LogInformation($"从链路连接请求消息>>>{JT809Serializer.Serialize(package, 100).ToHexString()}"); | |||
if (logger.IsEnabled(LogLevel.Information)) | |||
logger.LogInformation($"从链路连接请求消息>>>{JT809Serializer.Serialize(package, 100).ToHexString()}"); | |||
JT809Response jT809Response = new JT809Response(package, 100); | |||
SendAsync(jT809Response); | |||
return channel.Open; | |||
@@ -188,8 +193,9 @@ namespace JT809.DotNetty.Core.Clients | |||
VerifyCode = verifyCodeGenerator.Get() | |||
}); | |||
JT809Response jT809Response = new JT809Response(package, 100); | |||
if (logger.IsEnabled(LogLevel.Information)) | |||
logger.LogInformation($"发送从链路注销请求>>>{JT809Serializer.Serialize(package, 100).ToHexString()}"); | |||
SendAsync(jT809Response); | |||
logger.LogInformation($"发送从链路注销请求>>>{JT809Serializer.Serialize(package, 100).ToHexString()}"); | |||
} | |||
catch (Exception ex) | |||
{ | |||
@@ -7,6 +7,7 @@ using JT809.Protocol.Exceptions; | |||
using JT809.DotNetty.Core.Services; | |||
using JT809.DotNetty.Core.Metadata; | |||
using JT809.DotNetty.Core.Enums; | |||
using JT809.DotNetty.Core.Session; | |||
namespace JT809.DotNetty.Core.Handlers | |||
{ | |||
@@ -20,16 +21,21 @@ namespace JT809.DotNetty.Core.Handlers | |||
private readonly JT809AtomicCounterService jT809AtomicCounterService; | |||
private readonly JT809SuperiorMainSessionManager SuperiorMainSessionManager; | |||
private readonly ILogger<JT809MainServerHandler> logger; | |||
public JT809MainServerHandler( | |||
ILoggerFactory loggerFactory, | |||
JT809SuperiorMsgIdReceiveHandlerBase handler, | |||
JT809SuperiorMainSessionManager superiorMainSessionManager, | |||
JT809AtomicCounterServiceFactory jT809AtomicCounterServiceFactorty | |||
) | |||
{ | |||
this.handler = handler; | |||
this.jT809AtomicCounterService = jT809AtomicCounterServiceFactorty.Create(JT809AtomicCounterType.ServerMain.ToString()); ; | |||
this.SuperiorMainSessionManager = superiorMainSessionManager; | |||
logger = loggerFactory.CreateLogger<JT809MainServerHandler>(); | |||
} | |||
@@ -44,6 +50,7 @@ namespace JT809.DotNetty.Core.Handlers | |||
{ | |||
logger.LogDebug("accept package success count<<<" + jT809AtomicCounterService.MsgSuccessCount.ToString()); | |||
} | |||
SuperiorMainSessionManager.TryAdd(ctx.Channel, jT809Package.Header.MsgGNSSCENTERID); | |||
Func<JT809Request, JT809Response> handlerFunc; | |||
if (handler.HandlerDict.TryGetValue(jT809Package.Header.BusinessType, out handlerFunc)) | |||
{ | |||
@@ -1,7 +1,9 @@ | |||
using DotNetty.Handlers.Timeout; | |||
using DotNetty.Transport.Channels; | |||
using JT809.DotNetty.Core.Clients; | |||
using JT809.DotNetty.Core.Interfaces; | |||
using JT809.DotNetty.Core.Metadata; | |||
using JT809.Protocol; | |||
using JT809.Protocol.Enums; | |||
using JT809.Protocol.Extensions; | |||
using Microsoft.Extensions.Logging; | |||
@@ -19,12 +21,15 @@ namespace JT809.DotNetty.Core.Handlers | |||
private readonly ILogger<JT809SubordinateClientConnectionHandler> logger; | |||
private readonly JT809SubordinateClient subordinateClient; | |||
private readonly IJT809SubordinateLinkNotifyService JT809SubordinateLinkNotifyService; | |||
public JT809SubordinateClientConnectionHandler( | |||
IJT809SubordinateLinkNotifyService jT809SubordinateLinkNotifyService, | |||
JT809SubordinateClient jT809SubordinateClient, | |||
ILoggerFactory loggerFactory) | |||
{ | |||
logger = loggerFactory.CreateLogger<JT809SubordinateClientConnectionHandler>(); | |||
JT809SubordinateLinkNotifyService = jT809SubordinateLinkNotifyService; | |||
subordinateClient = jT809SubordinateClient; | |||
} | |||
@@ -49,7 +54,17 @@ namespace JT809.DotNetty.Core.Handlers | |||
Policy.HandleResult(context.Channel.Open) | |||
.WaitAndRetryForeverAsync(retryAttempt => | |||
{ | |||
return retryAttempt > 3 ? TimeSpan.FromSeconds(Math.Pow(2, 50)) : TimeSpan.FromSeconds(Math.Pow(2, retryAttempt));//超过重试3次,之后重试都是接近12个小时重试一次 | |||
if(retryAttempt < 3) | |||
{ | |||
return TimeSpan.FromSeconds(10); | |||
} | |||
else | |||
{ | |||
JT809SubordinateLinkNotifyService.Notify(JT809_0x9007_ReasonCode.上级平台客户端与下级平台服务端断开); | |||
//超过重试3次,之后重试都是接近1个小时重试一次 | |||
return TimeSpan.FromSeconds(3600); | |||
} | |||
}, | |||
(exception, timespan, ctx) => | |||
{ | |||
@@ -90,10 +105,11 @@ namespace JT809.DotNetty.Core.Handlers | |||
if (idleStateEvent.State == IdleState.WriterIdle) | |||
{ | |||
string channelId = context.Channel.Id.AsShortText(); | |||
logger.LogInformation($"{idleStateEvent.State.ToString()}>>>Heartbeat-{channelId}"); | |||
//发送从链路保持请求数据包 | |||
var package = JT809BusinessType.从链路连接保持请求消息.Create(); | |||
JT809Response jT809Response = new JT809Response(package, 100); | |||
if (logger.IsEnabled(LogLevel.Information)) | |||
logger.LogInformation($"{idleStateEvent.State.ToString()}>>>Heartbeat-{channelId}-{JT809Serializer.Serialize(package, 100).ToHexString()}"); | |||
context.WriteAndFlushAsync(jT809Response); | |||
//context.WriteAndFlushAsync(Unpooled.WrappedBuffer(JT809Serializer.Serialize(package,100))); | |||
} | |||
@@ -59,20 +59,15 @@ namespace JT809.DotNetty.Core.Handlers | |||
catch (JT809Exception ex) | |||
{ | |||
jT809AtomicCounterService.MsgFailIncrement(); | |||
if (logger.IsEnabled(LogLevel.Error)) | |||
{ | |||
logger.LogError("accept package fail count<<<" + jT809AtomicCounterService.MsgFailCount.ToString()); | |||
logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg)); | |||
} | |||
} | |||
catch (Exception ex) | |||
{ | |||
jT809AtomicCounterService.MsgFailIncrement(); | |||
if (logger.IsEnabled(LogLevel.Error)) | |||
{ | |||
logger.LogError("accept package fail count<<<" + jT809AtomicCounterService.MsgFailCount.ToString()); | |||
logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg)); | |||
} | |||
logger.LogError("accept package fail count<<<" + jT809AtomicCounterService.MsgFailCount.ToString()); | |||
logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg)); | |||
} | |||
} | |||
} | |||
@@ -1,15 +1,13 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using JT809.DotNetty.Core.Configurations; | |||
using JT809.DotNetty.Core.Interfaces; | |||
using JT809.DotNetty.Core.Clients; | |||
using JT809.DotNetty.Core.Metadata; | |||
using JT809.Protocol.Enums; | |||
using JT809.Protocol.Extensions; | |||
using JT809.Protocol.MessageBody; | |||
using Microsoft.Extensions.Logging; | |||
using Microsoft.Extensions.Options; | |||
using Newtonsoft.Json; | |||
using JT809.Protocol; | |||
namespace JT809.DotNetty.Core.Handlers | |||
{ | |||
@@ -46,12 +44,10 @@ namespace JT809.DotNetty.Core.Handlers | |||
{JT809BusinessType.从链路连接应答消息, Msg0x9002}, | |||
{JT809BusinessType.从链路注销应答消息, Msg0x9004}, | |||
{JT809BusinessType.从链路连接保持应答消息, Msg0x9006}, | |||
}; | |||
SubHandlerDict = new Dictionary<JT809SubBusinessType, Func<JT809Request, JT809Response>> | |||
{ | |||
//{JT809SubBusinessType.实时上传车辆定位信息, Msg0x1200_0x1202}, | |||
{JT809SubBusinessType.实时上传车辆定位信息, Msg0x1200_0x1202}, | |||
}; | |||
} | |||
@@ -160,7 +156,6 @@ namespace JT809.DotNetty.Core.Handlers | |||
return null; | |||
} | |||
/// <summary> | |||
/// 主链路动态信息交换消息 | |||
/// </summary> | |||
@@ -168,8 +163,24 @@ namespace JT809.DotNetty.Core.Handlers | |||
/// <returns></returns> | |||
public virtual JT809Response Msg0x1200(JT809Request request) | |||
{ | |||
var exchangeMessageBodies = request.Package.Bodies as JT809ExchangeMessageBodies; | |||
if (Logger.IsEnabled(LogLevel.Debug)) | |||
Logger.LogDebug(JsonConvert.SerializeObject(request.Package)); | |||
if (SubHandlerDict.TryGetValue(exchangeMessageBodies.SubBusinessType,out var func)) | |||
{ | |||
return func(request); | |||
} | |||
return null; | |||
} | |||
/// <summary> | |||
/// 实时上传车辆定位信息 | |||
/// </summary> | |||
/// <param name="request"></param> | |||
/// <returns>本条消息无需被通知方应答</returns> | |||
public virtual JT809Response Msg0x1200_0x1202(JT809Request request) | |||
{ | |||
throw new NotImplementedException("实时上传车辆定位信息"); | |||
} | |||
} | |||
} |
@@ -49,7 +49,8 @@ namespace JT809.DotNetty.Core.Internal | |||
ReasonCode = reasonCode | |||
}); | |||
JT809Response jT809Response = new JT809Response(package, 100); | |||
logger.LogInformation($"从链路断开通知消息>>>{JT809Serializer.Serialize(package, 100).ToHexString()}"); | |||
if(logger.IsEnabled(LogLevel.Information)) | |||
logger.LogInformation($"从链路断开通知消息>>>{JT809Serializer.Serialize(package, 100).ToHexString()}"); | |||
session.Channel.WriteAndFlushAsync(jT809Response); | |||
} | |||
} | |||
@@ -114,6 +114,7 @@ namespace JT809.DotNetty.Core | |||
serviceDescriptors.TryAddScoped<JT809MainServerHandler>(); | |||
serviceDescriptors.TryAddScoped<JT809SubordinateClientHandler>(); | |||
serviceDescriptors.TryAddSingleton<IJT809SubordinateLoginService, JT809SubordinateLoginImplService>(); | |||
serviceDescriptors.TryAddSingleton<IJT809SubordinateLinkNotifyService, JT809SubordinateLinkNotifyImplService>(); | |||
//从链路客户端 | |||
serviceDescriptors.TryAddSingleton<JT809SubordinateClient>(); | |||
//主链路服务端 | |||
@@ -72,7 +72,8 @@ namespace JT809.DotNetty.Core.Session | |||
} | |||
if (SessionIdDict.TryRemove(msgGNSSCENTERID, out JT809Session jT808SessionRemove)) | |||
{ | |||
logger.LogInformation($">>>{msgGNSSCENTERID} Session Remove."); | |||
if(logger.IsEnabled( LogLevel.Information)) | |||
logger.LogInformation($">>>{msgGNSSCENTERID} Session Remove."); | |||
//jT809SessionPublishing.PublishAsync(JT809Constants.SessionOffline, msgGNSSCENTERID.ToString()); | |||
return jT808SessionRemove; | |||
} | |||
@@ -92,7 +93,8 @@ namespace JT809.DotNetty.Core.Session | |||
SessionIdDict.TryRemove(key, out JT809Session jT808SessionRemove); | |||
} | |||
string nos = string.Join(",", keys); | |||
logger.LogInformation($">>>{nos} Channel Remove."); | |||
if (logger.IsEnabled(LogLevel.Information)) | |||
logger.LogInformation($">>>{nos} Channel Remove."); | |||
//jT809SessionPublishing.PublishAsync(JT809Constants.SessionOffline, nos); | |||
} | |||
} | |||
@@ -17,7 +17,7 @@ namespace JT809.DotNetty.Host.Test | |||
JT809.Protocol.JT809GlobalConfig.Instance | |||
.SetHeaderOptions(new JT809HeaderOptions | |||
{ | |||
MsgGNSSCENTERID = 20190222, | |||
MsgGNSSCENTERID = 20141013, | |||
Version = new JT809.Protocol.JT809Header_Version(1, 0, 0), | |||
EncryptKey = 9595 | |||
}); | |||
@@ -13,6 +13,6 @@ | |||
} | |||
}, | |||
"JT809Configuration": { | |||
"SubordinateClientEnable": false | |||
} | |||
} |
@@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT809.DotNetty.Abstractions | |||
EndProject | |||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{DD4611CF-79A9-45C7-91EB-1E84D22B7D07}" | |||
EndProject | |||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT809.DotNetty.Tcp.Test", "JT809.DotNetty.Tests\JT809.DotNetty.Tcp.Test\JT809.DotNetty.Tcp.Test.csproj", "{560913C8-B618-46AD-B974-9D324F1ABBAC}" | |||
EndProject | |||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT809.DotNetty.Host.Test", "JT809.DotNetty.Tests\JT809.DotNetty.Host.Test\JT809.DotNetty.Host.Test.csproj", "{D4E18559-C429-416F-9399-42C0E604D27B}" | |||
EndProject | |||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT809.Protocol.Extensions.DependencyInjection", "JT809.Protocol\src\JT809.Protocol.Extensions.DependencyInjection\JT809.Protocol.Extensions.DependencyInjection.csproj", "{975D959C-7C0B-418E-838E-EB383E912F8C}" | |||
@@ -37,10 +35,6 @@ Global | |||
{EB8276CC-1848-4E7D-B77E-29B22AF767F0}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||
{EB8276CC-1848-4E7D-B77E-29B22AF767F0}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{EB8276CC-1848-4E7D-B77E-29B22AF767F0}.Release|Any CPU.Build.0 = Release|Any CPU | |||
{560913C8-B618-46AD-B974-9D324F1ABBAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||
{560913C8-B618-46AD-B974-9D324F1ABBAC}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||
{560913C8-B618-46AD-B974-9D324F1ABBAC}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{560913C8-B618-46AD-B974-9D324F1ABBAC}.Release|Any CPU.Build.0 = Release|Any CPU | |||
{D4E18559-C429-416F-9399-42C0E604D27B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||
{D4E18559-C429-416F-9399-42C0E604D27B}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||
{D4E18559-C429-416F-9399-42C0E604D27B}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
@@ -55,7 +49,6 @@ Global | |||
EndGlobalSection | |||
GlobalSection(NestedProjects) = preSolution | |||
{321EE8EE-10D7-4233-8B8A-279BE68FB18A} = {C712B2DE-34FE-4D9C-B574-A08B019246E4} | |||
{560913C8-B618-46AD-B974-9D324F1ABBAC} = {DD4611CF-79A9-45C7-91EB-1E84D22B7D07} | |||
{D4E18559-C429-416F-9399-42C0E604D27B} = {DD4611CF-79A9-45C7-91EB-1E84D22B7D07} | |||
{975D959C-7C0B-418E-838E-EB383E912F8C} = {C712B2DE-34FE-4D9C-B574-A08B019246E4} | |||
EndGlobalSection | |||