@@ -0,0 +1,12 @@ | |||||
using JT808.DotNetty.Internal; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
namespace JT808.DotNetty.Test.Internal | |||||
{ | |||||
public class JT808SessionServiceDefaultImplTest | |||||
{ | |||||
//JT808SessionServiceDefaultImpl jT808SessionServiceDefaultImpl = new JT808SessionServiceDefaultImpl(new JT808SessionManager()); | |||||
} | |||||
} |
@@ -0,0 +1,19 @@ | |||||
<Project Sdk="Microsoft.NET.Sdk"> | |||||
<PropertyGroup> | |||||
<TargetFramework>netcoreapp2.1</TargetFramework> | |||||
<IsPackable>false</IsPackable> | |||||
</PropertyGroup> | |||||
<ItemGroup> | |||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" /> | |||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" /> | |||||
<PackageReference Include="xunit" Version="2.3.1" /> | |||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> | |||||
</ItemGroup> | |||||
<ItemGroup> | |||||
<ProjectReference Include="..\JT808.DotNetty\JT808.DotNetty.csproj" /> | |||||
</ItemGroup> | |||||
</Project> |
@@ -0,0 +1,28 @@ | |||||
using DotNetty.Transport.Channels.Embedded; | |||||
using Microsoft.Extensions.Logging; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
using Xunit; | |||||
namespace JT808.DotNetty.Test | |||||
{ | |||||
public class JT808SessionManagerTest | |||||
{ | |||||
JT808SessionManager jT808SessionManager = new JT808SessionManager(new LoggerFactory()); | |||||
[Fact] | |||||
public void Test1() | |||||
{ | |||||
var channel = new EmbeddedChannel(); | |||||
jT808SessionManager.TryAddOrUpdateSession(new Metadata.JT808Session(channel, "123456789123")); | |||||
} | |||||
[Fact] | |||||
public void Test2() | |||||
{ | |||||
var channel = new EmbeddedChannel(); | |||||
jT808SessionManager.TryAddSession(new Metadata.JT808Session(channel, "123456789123")); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,19 @@ | |||||
using JT808.DotNetty.Configurations; | |||||
using JT808.DotNetty.Internal; | |||||
using Microsoft.Extensions.DependencyInjection; | |||||
using Microsoft.Extensions.DependencyInjection.Extensions; | |||||
using System; | |||||
using Xunit; | |||||
namespace JT808.DotNetty.Test | |||||
{ | |||||
public class TestBase | |||||
{ | |||||
private IServiceCollection serviceDescriptors; | |||||
public TestBase() | |||||
{ | |||||
serviceDescriptors = new ServiceCollection(); | |||||
} | |||||
} | |||||
} |
@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty", "JT808.Dot | |||||
EndProject | EndProject | ||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Hosting", "JT808.DotNetty.Hosting\JT808.DotNetty.Hosting.csproj", "{46772BD5-4132-48A7-856B-11D658F7ADDB}" | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Hosting", "JT808.DotNetty.Hosting\JT808.DotNetty.Hosting.csproj", "{46772BD5-4132-48A7-856B-11D658F7ADDB}" | ||||
EndProject | EndProject | ||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT808.DotNetty.Test", "JT808.DotNetty.Test\JT808.DotNetty.Test.csproj", "{7315D030-16CA-4AC8-B892-720F3D78C651}" | |||||
EndProject | |||||
Global | Global | ||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||||
Debug|Any CPU = Debug|Any CPU | Debug|Any CPU = Debug|Any CPU | ||||
@@ -21,6 +23,10 @@ Global | |||||
{46772BD5-4132-48A7-856B-11D658F7ADDB}.Debug|Any CPU.Build.0 = Debug|Any CPU | {46772BD5-4132-48A7-856B-11D658F7ADDB}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||||
{46772BD5-4132-48A7-856B-11D658F7ADDB}.Release|Any CPU.ActiveCfg = Release|Any CPU | {46772BD5-4132-48A7-856B-11D658F7ADDB}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||||
{46772BD5-4132-48A7-856B-11D658F7ADDB}.Release|Any CPU.Build.0 = Release|Any CPU | {46772BD5-4132-48A7-856B-11D658F7ADDB}.Release|Any CPU.Build.0 = Release|Any CPU | ||||
{7315D030-16CA-4AC8-B892-720F3D78C651}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||||
{7315D030-16CA-4AC8-B892-720F3D78C651}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||||
{7315D030-16CA-4AC8-B892-720F3D78C651}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||||
{7315D030-16CA-4AC8-B892-720F3D78C651}.Release|Any CPU.Build.0 = Release|Any CPU | |||||
EndGlobalSection | EndGlobalSection | ||||
GlobalSection(SolutionProperties) = preSolution | GlobalSection(SolutionProperties) = preSolution | ||||
HideSolutionNode = FALSE | HideSolutionNode = FALSE | ||||
@@ -20,18 +20,18 @@ namespace JT808.DotNetty.Codecs | |||||
private readonly IJT808SourcePackageDispatcher jT808SourcePackageDispatcher; | private readonly IJT808SourcePackageDispatcher jT808SourcePackageDispatcher; | ||||
private readonly JT808AtomicCounterService jT808AtomicCounterService; | |||||
public JT808Decoder( | public JT808Decoder( | ||||
JT808AtomicCounterService jT808AtomicCounterService, | |||||
IJT808SourcePackageDispatcher jT808SourcePackageDispatcher, | IJT808SourcePackageDispatcher jT808SourcePackageDispatcher, | ||||
ILoggerFactory loggerFactory) | ILoggerFactory loggerFactory) | ||||
{ | { | ||||
this.jT808AtomicCounterService = jT808AtomicCounterService; | |||||
this.logger = loggerFactory.CreateLogger<JT808Decoder>(); | this.logger = loggerFactory.CreateLogger<JT808Decoder>(); | ||||
this.jT808SourcePackageDispatcher = jT808SourcePackageDispatcher; | this.jT808SourcePackageDispatcher = jT808SourcePackageDispatcher; | ||||
} | } | ||||
private static readonly AtomicCounter MsgSuccessCounter = new AtomicCounter(); | |||||
private static readonly AtomicCounter MsgFailCounter = new AtomicCounter(); | |||||
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output) | protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output) | ||||
{ | { | ||||
byte[] buffer = new byte[input.Capacity + 2]; | byte[] buffer = new byte[input.Capacity + 2]; | ||||
@@ -43,25 +43,27 @@ namespace JT808.DotNetty.Codecs | |||||
jT808SourcePackageDispatcher?.SendAsync(buffer); | jT808SourcePackageDispatcher?.SendAsync(buffer); | ||||
JT808Package jT808Package = JT808Serializer.Deserialize<JT808Package>(buffer); | JT808Package jT808Package = JT808Serializer.Deserialize<JT808Package>(buffer); | ||||
output.Add(jT808Package); | output.Add(jT808Package); | ||||
MsgSuccessCounter.Increment(); | |||||
jT808AtomicCounterService.MsgSuccessIncrement(); | |||||
if (logger.IsEnabled(LogLevel.Debug)) | if (logger.IsEnabled(LogLevel.Debug)) | ||||
logger.LogDebug("accept package success count<<<" + MsgSuccessCounter.Count.ToString()); | |||||
{ | |||||
logger.LogDebug("accept package success count<<<" + jT808AtomicCounterService.MsgSuccessCount.ToString()); | |||||
} | |||||
} | } | ||||
catch (JT808.Protocol.Exceptions.JT808Exception ex) | catch (JT808.Protocol.Exceptions.JT808Exception ex) | ||||
{ | { | ||||
MsgFailCounter.Increment(); | |||||
jT808AtomicCounterService.MsgFailIncrement(); | |||||
if (logger.IsEnabled(LogLevel.Error)) | if (logger.IsEnabled(LogLevel.Error)) | ||||
{ | { | ||||
logger.LogError("accept package fail count<<<" + MsgFailCounter.Count.ToString()); | |||||
logger.LogError("accept package fail count<<<" + jT808AtomicCounterService.MsgFailCount.ToString()); | |||||
logger.LogError(ex, "accept msg<<<" + buffer); | logger.LogError(ex, "accept msg<<<" + buffer); | ||||
} | } | ||||
} | } | ||||
catch (Exception ex) | catch (Exception ex) | ||||
{ | { | ||||
MsgFailCounter.Increment(); | |||||
jT808AtomicCounterService.MsgFailIncrement(); | |||||
if (logger.IsEnabled(LogLevel.Error)) | if (logger.IsEnabled(LogLevel.Error)) | ||||
{ | { | ||||
logger.LogError("accept package fail count<<<" + MsgFailCounter.Count.ToString()); | |||||
logger.LogError("accept package fail count<<<" + jT808AtomicCounterService.MsgFailCount.ToString()); | |||||
logger.LogError(ex, "accept msg<<<" + buffer); | logger.LogError(ex, "accept msg<<<" + buffer); | ||||
} | } | ||||
} | } | ||||
@@ -32,11 +32,6 @@ namespace JT808.DotNetty.Configurations | |||||
/// </summary> | /// </summary> | ||||
public int WebAPIPort { get; set; } = 828; | public int WebAPIPort { get; set; } = 828; | ||||
/// <summary> | |||||
/// 会话报时 | |||||
/// 默认5分钟 | |||||
/// </summary> | |||||
public int SessionReportTime { get; set; } = 30000; | |||||
/// <summary> | /// <summary> | ||||
/// 源包分发器配置 | /// 源包分发器配置 | ||||
/// </summary> | /// </summary> | ||||
@@ -0,0 +1,43 @@ | |||||
using JT808.DotNetty.Metadata; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
namespace JT808.DotNetty.Internal | |||||
{ | |||||
/// <summary> | |||||
/// 计数包服务 | |||||
/// </summary> | |||||
internal class JT808AtomicCounterService | |||||
{ | |||||
private static readonly JT808AtomicCounter MsgSuccessCounter = new JT808AtomicCounter(); | |||||
private static readonly JT808AtomicCounter MsgFailCounter = new JT808AtomicCounter(); | |||||
public long MsgSuccessIncrement() | |||||
{ | |||||
return MsgSuccessCounter.Increment(); | |||||
} | |||||
public long MsgSuccessCount | |||||
{ | |||||
get | |||||
{ | |||||
return MsgSuccessCounter.Count; | |||||
} | |||||
} | |||||
public long MsgFailIncrement() | |||||
{ | |||||
return MsgFailCounter.Increment(); | |||||
} | |||||
public long MsgFailCount | |||||
{ | |||||
get | |||||
{ | |||||
return MsgFailCounter.Count; | |||||
} | |||||
} | |||||
} | |||||
} |
@@ -4,6 +4,7 @@ using System; | |||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Linq; | using System.Linq; | ||||
using System.Text; | using System.Text; | ||||
using System.Runtime.CompilerServices; | |||||
namespace JT808.DotNetty.Internal | namespace JT808.DotNetty.Internal | ||||
{ | { | ||||
@@ -8,7 +8,9 @@ using Microsoft.Extensions.DependencyInjection.Extensions; | |||||
using Microsoft.Extensions.Hosting; | using Microsoft.Extensions.Hosting; | ||||
using System; | using System; | ||||
using System.Reflection; | using System.Reflection; | ||||
using System.Runtime.CompilerServices; | |||||
[assembly: InternalsVisibleTo("JT808.DotNetty.Test")] | |||||
namespace JT808.DotNetty | namespace JT808.DotNetty | ||||
{ | { | ||||
public static class JT808DotnettyExtensions | public static class JT808DotnettyExtensions | ||||
@@ -19,6 +21,7 @@ namespace JT808.DotNetty | |||||
{ | { | ||||
services.Configure<JT808Configuration>(hostContext.Configuration.GetSection("JT808Configuration")); | services.Configure<JT808Configuration>(hostContext.Configuration.GetSection("JT808Configuration")); | ||||
services.TryAddSingleton<JT808SessionManager>(); | services.TryAddSingleton<JT808SessionManager>(); | ||||
services.TryAddSingleton<JT808AtomicCounterService>(); | |||||
services.TryAddSingleton<JT808MsgIdHandlerBase,JT808MsgIdDefaultHandler>(); | services.TryAddSingleton<JT808MsgIdHandlerBase,JT808MsgIdDefaultHandler>(); | ||||
services.TryAddSingleton<IJT808SourcePackageDispatcher, JT808SourcePackageDispatcherDefaultImpl>(); | services.TryAddSingleton<IJT808SourcePackageDispatcher, JT808SourcePackageDispatcherDefaultImpl>(); | ||||
services.TryAddScoped<JT808ConnectionHandler>(); | services.TryAddScoped<JT808ConnectionHandler>(); | ||||
@@ -1,6 +1,4 @@ | |||||
using JT808.DotNetty.Configurations; | |||||
using Microsoft.Extensions.Hosting; | |||||
using Microsoft.Extensions.Logging; | |||||
using Microsoft.Extensions.Logging; | |||||
using Microsoft.Extensions.Options; | using Microsoft.Extensions.Options; | ||||
using System; | using System; | ||||
using System.Collections.Concurrent; | using System.Collections.Concurrent; | ||||
@@ -17,14 +15,10 @@ namespace JT808.DotNetty | |||||
{ | { | ||||
private readonly ILogger<JT808SessionManager> logger; | private readonly ILogger<JT808SessionManager> logger; | ||||
private readonly JT808Configuration configuration; | |||||
public JT808SessionManager( | public JT808SessionManager( | ||||
IOptions<JT808Configuration> jT808ConfigurationAccessor, | |||||
ILoggerFactory loggerFactory) | ILoggerFactory loggerFactory) | ||||
{ | { | ||||
logger = loggerFactory.CreateLogger<JT808SessionManager>(); | logger = loggerFactory.CreateLogger<JT808SessionManager>(); | ||||
configuration = jT808ConfigurationAccessor.Value; | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -127,7 +121,7 @@ namespace JT808.DotNetty | |||||
public void TryAddOrUpdateSession(JT808Session appSession) | public void TryAddOrUpdateSession(JT808Session appSession) | ||||
{ | { | ||||
SessionIdDict.AddOrUpdate(appSession.SessionID, appSession, (x, y) => appSession); | |||||
//SessionIdDict.AddOrUpdate(appSession.SessionID, appSession, (x, y) => appSession); | |||||
TerminalPhoneNo_SessionId_Dict.AddOrUpdate(appSession.TerminalPhoneNo, appSession.SessionID, (x, y) => appSession.SessionID); | TerminalPhoneNo_SessionId_Dict.AddOrUpdate(appSession.TerminalPhoneNo, appSession.SessionID, (x, y) => appSession.SessionID); | ||||
} | } | ||||
@@ -3,26 +3,39 @@ using System.Collections.Generic; | |||||
using System.Text; | using System.Text; | ||||
using System.Threading; | using System.Threading; | ||||
namespace JT808.DotNetty.Internal | |||||
namespace JT808.DotNetty.Metadata | |||||
{ | { | ||||
/// <summary> | /// <summary> | ||||
/// | /// | ||||
/// <see cref="Grpc.Core.Internal"/> | /// <see cref="Grpc.Core.Internal"/> | ||||
/// </summary> | /// </summary> | ||||
internal class AtomicCounter | |||||
internal class JT808AtomicCounter | |||||
{ | { | ||||
long counter = 0; | long counter = 0; | ||||
public AtomicCounter(long initialCount = 0) | |||||
public JT808AtomicCounter(long initialCount = 0) | |||||
{ | { | ||||
this.counter = initialCount; | this.counter = initialCount; | ||||
} | } | ||||
public long Increment() | public long Increment() | ||||
{ | { | ||||
if (counter < 0) | |||||
{ | |||||
Interlocked.Exchange(ref counter,0); | |||||
} | |||||
return Interlocked.Increment(ref counter); | return Interlocked.Increment(ref counter); | ||||
} | } | ||||
public long Add(long len) | |||||
{ | |||||
if (counter < 0) | |||||
{ | |||||
Interlocked.Exchange(ref counter, 0); | |||||
} | |||||
return Interlocked.Add(ref counter,len); | |||||
} | |||||
public long Decrement() | public long Decrement() | ||||
{ | { | ||||
return Interlocked.Decrement(ref counter); | return Interlocked.Decrement(ref counter); |