diff --git a/src/JT808.DotNetty.Abstractions/JT808.DotNetty.Abstractions.csproj b/src/JT808.DotNetty.Abstractions/JT808.DotNetty.Abstractions.csproj
index 0ee2fad..559fd40 100644
--- a/src/JT808.DotNetty.Abstractions/JT808.DotNetty.Abstractions.csproj
+++ b/src/JT808.DotNetty.Abstractions/JT808.DotNetty.Abstractions.csproj
@@ -14,7 +14,7 @@
https://github.com/SmallChi/JT808DotNetty
https://github.com/SmallChi/JT808DotNetty/blob/master/LICENSE
true
- 1.0.1
+ 1.0.2
diff --git a/src/JT808.DotNetty.Core/Configurations/JT808Configuration.cs b/src/JT808.DotNetty.Core/Configurations/JT808Configuration.cs
index 48f1975..ff15407 100644
--- a/src/JT808.DotNetty.Core/Configurations/JT808Configuration.cs
+++ b/src/JT808.DotNetty.Core/Configurations/JT808Configuration.cs
@@ -28,6 +28,8 @@ namespace JT808.DotNetty.Core.Configurations
public int AllIdleTimeSeconds { get; set; } = 3600;
+ public int UdpSlidingExpirationTimeSeconds { get; set; } = 5*60;
+
///
/// WebApi服务
/// 默认828端口
diff --git a/src/JT808.DotNetty.Core/JT808.DotNetty.Core.csproj b/src/JT808.DotNetty.Core/JT808.DotNetty.Core.csproj
index 737625d..fb50efa 100644
--- a/src/JT808.DotNetty.Core/JT808.DotNetty.Core.csproj
+++ b/src/JT808.DotNetty.Core/JT808.DotNetty.Core.csproj
@@ -14,13 +14,14 @@
https://github.com/SmallChi/JT808DotNetty
https://github.com/SmallChi/JT808DotNetty/blob/master/LICENSE
true
- 1.0.1
+ 1.0.2
+
diff --git a/src/JT808.DotNetty.Core/Jobs/JT808UdpMaintainSessionJob.cs b/src/JT808.DotNetty.Core/Jobs/JT808UdpMaintainSessionJob.cs
new file mode 100644
index 0000000..f513e2b
--- /dev/null
+++ b/src/JT808.DotNetty.Core/Jobs/JT808UdpMaintainSessionJob.cs
@@ -0,0 +1,44 @@
+using JT808.DotNetty.Core.Configurations;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace JT808.DotNetty.Core.Jobs
+{
+ public class JT808UdpMaintainSessionJob : JT808BackgroundService
+ {
+ private readonly ILogger _logger;
+
+ private readonly JT808UdpSessionManager jT808UdpSessionManager;
+ private readonly IOptionsMonitor jT808ConfigurationAccessor;
+
+ public JT808UdpMaintainSessionJob(
+ JT808UdpSessionManager jT808UdpSessionManager,
+ IOptionsMonitor jT808ConfigurationAccessor,
+ ILoggerFactory loggerFactory)
+ {
+ this.jT808UdpSessionManager = jT808UdpSessionManager;
+ this.jT808ConfigurationAccessor = jT808ConfigurationAccessor;
+ _logger = loggerFactory.CreateLogger();
+ }
+
+ public override string ServiceName => nameof(JT808UdpMaintainSessionJob);
+
+ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
+ {
+ _logger.LogInformation($"{ServiceName} is starting.");
+ stoppingToken.Register(() => _logger.LogInformation($"{ServiceName} background task is stopping."));
+ while (!stoppingToken.IsCancellationRequested)
+ {
+ _logger.LogInformation($"{ServiceName} task doing background work.");
+ jT808UdpSessionManager.TimerToRemoveExpiredData();
+ await Task.Delay(TimeSpan.FromSeconds(jT808ConfigurationAccessor.CurrentValue.UdpSlidingExpirationTimeSeconds), stoppingToken);
+ }
+ _logger.LogInformation($"{ServiceName} background task is stopping.");
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/JT808.DotNetty.Core/Session/JT808UdpSessionManager.cs b/src/JT808.DotNetty.Core/Session/JT808UdpSessionManager.cs
index 4e43f38..547ea08 100644
--- a/src/JT808.DotNetty.Core/Session/JT808UdpSessionManager.cs
+++ b/src/JT808.DotNetty.Core/Session/JT808UdpSessionManager.cs
@@ -6,6 +6,9 @@ using System.Linq;
using JT808.DotNetty.Abstractions;
using JT808.DotNetty.Core.Metadata;
using DotNetty.Transport.Channels;
+using Microsoft.Extensions.Caching.Memory;
+using JT808.DotNetty.Core.Configurations;
+using Microsoft.Extensions.Options;
namespace JT808.DotNetty.Core
{
@@ -18,13 +21,18 @@ namespace JT808.DotNetty.Core
private readonly ILogger logger;
private readonly IJT808SessionPublishing jT808SessionPublishing;
-
+ private readonly IMemoryCache memoryCache;
+ private readonly IOptionsMonitor jT808ConfigurationAccessor;
public JT808UdpSessionManager(
IJT808SessionPublishing jT808SessionPublishing,
+ IMemoryCache memoryCache,
+ IOptionsMonitor jT808ConfigurationAccessor,
ILoggerFactory loggerFactory)
{
this.jT808SessionPublishing = jT808SessionPublishing;
logger = loggerFactory.CreateLogger();
+ this.memoryCache = memoryCache;
+ this.jT808ConfigurationAccessor = jT808ConfigurationAccessor;
}
private ConcurrentDictionary SessionIdDict = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase);
@@ -50,11 +58,29 @@ namespace JT808.DotNetty.Core
return default;
}
}
+ ///
+ /// 定期去删除过期数据
+ ///
+ public void TimerToRemoveExpiredData() {
+ foreach (var item in SessionIdDict)
+ {
+ memoryCache.Get(item.Key);
+ }
+ }
public void TryAdd(JT808UdpSession appSession)
- {
+ {
+ memoryCache.GetOrCreate(appSession.TerminalPhoneNo, (cacheEntry) =>
+ {
+ cacheEntry.SetSlidingExpiration(TimeSpan.FromSeconds(jT808ConfigurationAccessor.CurrentValue.UdpSlidingExpirationTimeSeconds));
+ cacheEntry.RegisterPostEvictionCallback((key, value, reason, state) =>
+ {
+ RemoveSession(key.ToString());
+ });
+ return true;
+ });
//1.先判断是否在缓存里面
- if(SessionIdDict.TryGetValue(appSession.TerminalPhoneNo,out JT808UdpSession jT808UdpSession))
+ if (SessionIdDict.TryGetValue(appSession.TerminalPhoneNo,out JT808UdpSession jT808UdpSession))
{
//处理缓存
//判断设备的终结点是否相同
diff --git a/src/JT808.DotNetty.Tcp/JT808.DotNetty.Tcp.csproj b/src/JT808.DotNetty.Tcp/JT808.DotNetty.Tcp.csproj
index 78135cb..9ea3f13 100644
--- a/src/JT808.DotNetty.Tcp/JT808.DotNetty.Tcp.csproj
+++ b/src/JT808.DotNetty.Tcp/JT808.DotNetty.Tcp.csproj
@@ -14,7 +14,7 @@
https://github.com/SmallChi/JT808DotNetty
https://github.com/SmallChi/JT808DotNetty/blob/master/LICENSE
true
- 1.0.1
+ 1.0.2
diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs
index 2008b38..1831706 100644
--- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs
+++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs
@@ -59,8 +59,8 @@ namespace JT808.DotNetty.Hosting
c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
c.LoggerFactory = p.GetRequiredService();
});
- var client = services.BuildServiceProvider().GetRequiredService();
- var result = client.GetTcpAtomicCounter().InvokeAsync().Result;
+ //var client = services.BuildServiceProvider().GetRequiredService();
+ //var result = client.GetTcpAtomicCounter().InvokeAsync().Result;
});
await serverHostBuilder.RunConsoleAsync();
diff --git a/src/JT808.DotNetty.Udp/JT808.DotNetty.Udp.csproj b/src/JT808.DotNetty.Udp/JT808.DotNetty.Udp.csproj
index 703114e..d0fee8b 100644
--- a/src/JT808.DotNetty.Udp/JT808.DotNetty.Udp.csproj
+++ b/src/JT808.DotNetty.Udp/JT808.DotNetty.Udp.csproj
@@ -14,7 +14,7 @@
https://github.com/SmallChi/JT808DotNetty
https://github.com/SmallChi/JT808DotNetty/blob/master/LICENSE
true
- 1.0.1
+ 1.0.2
@@ -22,6 +22,7 @@
+
diff --git a/src/JT808.DotNetty.Udp/JT808UdpDotnettyExtensions.cs b/src/JT808.DotNetty.Udp/JT808UdpDotnettyExtensions.cs
index 6619f04..5a747dd 100644
--- a/src/JT808.DotNetty.Udp/JT808UdpDotnettyExtensions.cs
+++ b/src/JT808.DotNetty.Udp/JT808UdpDotnettyExtensions.cs
@@ -4,8 +4,11 @@ using JT808.DotNetty.Core.Handlers;
using JT808.DotNetty.Core.Jobs;
using JT808.DotNetty.Core.Services;
using JT808.DotNetty.Udp.Handlers;
+using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
+using Microsoft.Extensions.Internal;
+using System;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("JT808.DotNetty.Udp.Test")]
@@ -16,7 +19,8 @@ namespace JT808.DotNetty.Udp
{
public static IServiceCollection AddJT808UdpHost(this IServiceCollection serviceDescriptors)
{
- serviceDescriptors.TryAddSingleton();
+ serviceDescriptors.TryAddSingleton< IMemoryCache, MemoryCache>();
+ serviceDescriptors.TryAddSingleton();
serviceDescriptors.TryAddSingleton();
serviceDescriptors.TryAddSingleton();
serviceDescriptors.TryAddSingleton();
@@ -24,6 +28,7 @@ namespace JT808.DotNetty.Udp
serviceDescriptors.TryAddScoped();
serviceDescriptors.AddHostedService();
serviceDescriptors.AddHostedService();
+ serviceDescriptors.AddHostedService();
serviceDescriptors.AddHostedService();
return serviceDescriptors;
}
diff --git a/src/JT808.DotNetty.WebApi/JT808.DotNetty.WebApi.csproj b/src/JT808.DotNetty.WebApi/JT808.DotNetty.WebApi.csproj
index 9ba1210..f354cdf 100644
--- a/src/JT808.DotNetty.WebApi/JT808.DotNetty.WebApi.csproj
+++ b/src/JT808.DotNetty.WebApi/JT808.DotNetty.WebApi.csproj
@@ -14,7 +14,7 @@
https://github.com/SmallChi/JT808DotNetty
https://github.com/SmallChi/JT808DotNetty/blob/master/LICENSE
true
- 1.0.1
+ 1.0.2
diff --git a/src/JT808.DotNetty.WebApiClientTool/JT808.DotNetty.WebApiClientTool.csproj b/src/JT808.DotNetty.WebApiClientTool/JT808.DotNetty.WebApiClientTool.csproj
index 33e5c74..f5f9e7c 100644
--- a/src/JT808.DotNetty.WebApiClientTool/JT808.DotNetty.WebApiClientTool.csproj
+++ b/src/JT808.DotNetty.WebApiClientTool/JT808.DotNetty.WebApiClientTool.csproj
@@ -14,7 +14,7 @@
https://github.com/SmallChi/JT808DotNetty
https://github.com/SmallChi/JT808DotNetty/blob/master/LICENSE
true
- 1.0.1
+ 1.0.2
diff --git a/src/JT808.Protocol b/src/JT808.Protocol
index 16b8bfc..641907a 160000
--- a/src/JT808.Protocol
+++ b/src/JT808.Protocol
@@ -1 +1 @@
-Subproject commit 16b8bfcd87686c809912b3ed722c5c035e1121cf
+Subproject commit 641907ad8373cf62d8973dedf76b84aaa894e52f