diff --git a/src/JT808.DotNetty.Test/Internal/JT808SessionServiceDefaultImplTest.cs b/src/JT808.DotNetty.Test/Internal/JT808SessionServiceDefaultImplTest.cs new file mode 100644 index 0000000..866247f --- /dev/null +++ b/src/JT808.DotNetty.Test/Internal/JT808SessionServiceDefaultImplTest.cs @@ -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()); + } +} diff --git a/src/JT808.DotNetty.Test/JT808.DotNetty.Test.csproj b/src/JT808.DotNetty.Test/JT808.DotNetty.Test.csproj new file mode 100644 index 0000000..bb87711 --- /dev/null +++ b/src/JT808.DotNetty.Test/JT808.DotNetty.Test.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp2.1 + + false + + + + + + + + + + + + + diff --git a/src/JT808.DotNetty.Test/JT808SessionManagerTest.cs b/src/JT808.DotNetty.Test/JT808SessionManagerTest.cs new file mode 100644 index 0000000..04a1e66 --- /dev/null +++ b/src/JT808.DotNetty.Test/JT808SessionManagerTest.cs @@ -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")); + } + } +} diff --git a/src/JT808.DotNetty.Test/TestBase.cs b/src/JT808.DotNetty.Test/TestBase.cs new file mode 100644 index 0000000..abf185f --- /dev/null +++ b/src/JT808.DotNetty.Test/TestBase.cs @@ -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(); + } + } +} diff --git a/src/JT808.DotNetty.sln b/src/JT808.DotNetty.sln index f173c44..df4933a 100644 --- a/src/JT808.DotNetty.sln +++ b/src/JT808.DotNetty.sln @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty", "JT808.Dot EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Hosting", "JT808.DotNetty.Hosting\JT808.DotNetty.Hosting.csproj", "{46772BD5-4132-48A7-856B-11D658F7ADDB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT808.DotNetty.Test", "JT808.DotNetty.Test\JT808.DotNetty.Test.csproj", "{7315D030-16CA-4AC8-B892-720F3D78C651}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/JT808.DotNetty/Codecs/JT808Decoder.cs b/src/JT808.DotNetty/Codecs/JT808Decoder.cs index 6ce54b5..d082643 100644 --- a/src/JT808.DotNetty/Codecs/JT808Decoder.cs +++ b/src/JT808.DotNetty/Codecs/JT808Decoder.cs @@ -20,18 +20,18 @@ namespace JT808.DotNetty.Codecs private readonly IJT808SourcePackageDispatcher jT808SourcePackageDispatcher; + private readonly JT808AtomicCounterService jT808AtomicCounterService; + public JT808Decoder( + JT808AtomicCounterService jT808AtomicCounterService, IJT808SourcePackageDispatcher jT808SourcePackageDispatcher, ILoggerFactory loggerFactory) { + this.jT808AtomicCounterService = jT808AtomicCounterService; this.logger = loggerFactory.CreateLogger(); 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 output) { byte[] buffer = new byte[input.Capacity + 2]; @@ -43,25 +43,27 @@ namespace JT808.DotNetty.Codecs jT808SourcePackageDispatcher?.SendAsync(buffer); JT808Package jT808Package = JT808Serializer.Deserialize(buffer); output.Add(jT808Package); - MsgSuccessCounter.Increment(); + jT808AtomicCounterService.MsgSuccessIncrement(); 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) { - MsgFailCounter.Increment(); + jT808AtomicCounterService.MsgFailIncrement(); 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); } } catch (Exception ex) { - MsgFailCounter.Increment(); + jT808AtomicCounterService.MsgFailIncrement(); 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); } } diff --git a/src/JT808.DotNetty/Configurations/JT808Configuration.cs b/src/JT808.DotNetty/Configurations/JT808Configuration.cs index 4c73388..7f8248f 100644 --- a/src/JT808.DotNetty/Configurations/JT808Configuration.cs +++ b/src/JT808.DotNetty/Configurations/JT808Configuration.cs @@ -32,11 +32,6 @@ namespace JT808.DotNetty.Configurations /// public int WebAPIPort { get; set; } = 828; - /// - /// 会话报时 - /// 默认5分钟 - /// - public int SessionReportTime { get; set; } = 30000; /// /// 源包分发器配置 /// diff --git a/src/JT808.DotNetty/Internal/JT808AtomicCounterService.cs b/src/JT808.DotNetty/Internal/JT808AtomicCounterService.cs new file mode 100644 index 0000000..f30de89 --- /dev/null +++ b/src/JT808.DotNetty/Internal/JT808AtomicCounterService.cs @@ -0,0 +1,43 @@ +using JT808.DotNetty.Metadata; +using System; +using System.Collections.Generic; +using System.Text; + +namespace JT808.DotNetty.Internal +{ + /// + /// 计数包服务 + /// + 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; + } + } + } +} diff --git a/src/JT808.DotNetty/Internal/JT808SessionServiceDefaultImpl.cs b/src/JT808.DotNetty/Internal/JT808SessionServiceDefaultImpl.cs index 64e976e..7929453 100644 --- a/src/JT808.DotNetty/Internal/JT808SessionServiceDefaultImpl.cs +++ b/src/JT808.DotNetty/Internal/JT808SessionServiceDefaultImpl.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Runtime.CompilerServices; namespace JT808.DotNetty.Internal { diff --git a/src/JT808.DotNetty/JT808DotnettyExtensions.cs b/src/JT808.DotNetty/JT808DotnettyExtensions.cs index b151ec8..091d6a5 100644 --- a/src/JT808.DotNetty/JT808DotnettyExtensions.cs +++ b/src/JT808.DotNetty/JT808DotnettyExtensions.cs @@ -8,7 +8,9 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using System; using System.Reflection; +using System.Runtime.CompilerServices; +[assembly: InternalsVisibleTo("JT808.DotNetty.Test")] namespace JT808.DotNetty { public static class JT808DotnettyExtensions @@ -19,6 +21,7 @@ namespace JT808.DotNetty { services.Configure(hostContext.Configuration.GetSection("JT808Configuration")); services.TryAddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddScoped(); diff --git a/src/JT808.DotNetty/JT808SessionManager.cs b/src/JT808.DotNetty/JT808SessionManager.cs index 178971e..eb829d9 100644 --- a/src/JT808.DotNetty/JT808SessionManager.cs +++ b/src/JT808.DotNetty/JT808SessionManager.cs @@ -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 System; using System.Collections.Concurrent; @@ -17,14 +15,10 @@ namespace JT808.DotNetty { private readonly ILogger logger; - private readonly JT808Configuration configuration; - public JT808SessionManager( - IOptions jT808ConfigurationAccessor, ILoggerFactory loggerFactory) { logger = loggerFactory.CreateLogger(); - configuration = jT808ConfigurationAccessor.Value; } /// @@ -127,7 +121,7 @@ namespace JT808.DotNetty 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); } diff --git a/src/JT808.DotNetty/Internal/AtomicCounter.cs b/src/JT808.DotNetty/Metadata/JT808AtomicCounter.cs similarity index 58% rename from src/JT808.DotNetty/Internal/AtomicCounter.cs rename to src/JT808.DotNetty/Metadata/JT808AtomicCounter.cs index e0f3b0c..de9cc5d 100644 --- a/src/JT808.DotNetty/Internal/AtomicCounter.cs +++ b/src/JT808.DotNetty/Metadata/JT808AtomicCounter.cs @@ -3,26 +3,39 @@ using System.Collections.Generic; using System.Text; using System.Threading; -namespace JT808.DotNetty.Internal +namespace JT808.DotNetty.Metadata { /// /// /// /// - internal class AtomicCounter + internal class JT808AtomicCounter { long counter = 0; - public AtomicCounter(long initialCount = 0) + public JT808AtomicCounter(long initialCount = 0) { this.counter = initialCount; } public long Increment() { + if (counter < 0) + { + Interlocked.Exchange(ref counter,0); + } 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() { return Interlocked.Decrement(ref counter);