diff --git a/simples/JT808.DotNetty.SimpleServer/Handlers/JT808MsgIdTcpSimpleHandler.cs b/simples/JT808.DotNetty.SimpleServer/Handlers/JT808MsgIdTcpSimpleHandler.cs index 7489978..66260c9 100644 --- a/simples/JT808.DotNetty.SimpleServer/Handlers/JT808MsgIdTcpSimpleHandler.cs +++ b/simples/JT808.DotNetty.SimpleServer/Handlers/JT808MsgIdTcpSimpleHandler.cs @@ -2,6 +2,7 @@ using JT808.DotNetty.Core.Handlers; using JT808.DotNetty.Core.Interfaces; using JT808.DotNetty.Core.Metadata; +using JT808.DotNetty.Core.Session; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -13,7 +14,7 @@ namespace JT808.DotNetty.SimpleServer.Handlers { public JT808MsgIdTcpSimpleHandler( ILoggerFactory loggerFactory, - JT808TcpSessionManager sessionManager) : base(sessionManager) + JT808SessionManager sessionManager) : base(sessionManager) { logger = loggerFactory.CreateLogger(); } diff --git a/src/JT808.DotNetty.Abstractions/JT808Constants.cs b/src/JT808.DotNetty.Abstractions/JT808Constants.cs index db24035..616005a 100644 --- a/src/JT808.DotNetty.Abstractions/JT808Constants.cs +++ b/src/JT808.DotNetty.Abstractions/JT808Constants.cs @@ -25,29 +25,21 @@ /// public static string SessionTcpGetAll = $"{RouteTablePrefix}/{TcpPrefix}/{SessionPrefix}/GetAll"; /// - /// 基于Tcp的会话服务-通过设备终端号移除对应会话 + /// 会话服务-通过设备终端号移除对应会话 /// - public static string SessionTcpRemoveByTerminalPhoneNo = $"{RouteTablePrefix}/{TcpPrefix}/{SessionPrefix}/RemoveByTerminalPhoneNo"; + public static string SessionRemoveByTerminalPhoneNo = $"{RouteTablePrefix}/{SessionPrefix}/RemoveByTerminalPhoneNo"; /// - /// 基于Tcp的统一下发信息 + /// 统一下发信息 /// - public static string UnificationTcpSend = $"{RouteTablePrefix}/{TcpPrefix}/UnificationSend"; + public static string UnificationSend = $"{RouteTablePrefix}/UnificationSend"; /// /// 获取Udp包计数器 /// public static string GetUdpAtomicCounter = $"{RouteTablePrefix}/{UdpPrefix}/GetAtomicCounter"; /// - /// 基于Udp的统一下发信息 - /// - public static string UnificationUdpSend = $"{RouteTablePrefix}/{UdpPrefix}/UnificationSend"; - /// /// 基于Udp的会话服务集合 /// public static string SessionUdpGetAll = $"{RouteTablePrefix}/{UdpPrefix}/{SessionPrefix}/GetAll"; - /// - /// 基于Udp的会话服务-通过设备终端号移除对应会话 - /// - public static string SessionUdpRemoveByTerminalPhoneNo = $"{RouteTablePrefix}/{UdpPrefix}/{SessionPrefix}/RemoveByTerminalPhoneNo"; } } } diff --git a/src/JT808.DotNetty.Core/Handlers/JT808MsgIdTcpHandlerBase.cs b/src/JT808.DotNetty.Core/Handlers/JT808MsgIdTcpHandlerBase.cs index 8729013..b83aae4 100644 --- a/src/JT808.DotNetty.Core/Handlers/JT808MsgIdTcpHandlerBase.cs +++ b/src/JT808.DotNetty.Core/Handlers/JT808MsgIdTcpHandlerBase.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using JT808.DotNetty.Core.Interfaces; using JT808.DotNetty.Core.Metadata; +using JT808.DotNetty.Core.Session; using JT808.Protocol.Enums; using JT808.Protocol.Extensions; using JT808.Protocol.MessageBody; @@ -18,11 +19,11 @@ namespace JT808.DotNetty.Core.Handlers /// public abstract class JT808MsgIdTcpHandlerBase { - protected JT808TcpSessionManager sessionManager { get; } + protected JT808SessionManager sessionManager { get; } /// /// 初始化消息处理业务 /// - protected JT808MsgIdTcpHandlerBase(JT808TcpSessionManager sessionManager) + protected JT808MsgIdTcpHandlerBase(JT808SessionManager sessionManager) { this.sessionManager = sessionManager; HandlerDict = new Dictionary> diff --git a/src/JT808.DotNetty.Core/Handlers/JT808MsgIdUdpHandlerBase.cs b/src/JT808.DotNetty.Core/Handlers/JT808MsgIdUdpHandlerBase.cs index 19a072b..a8d798e 100644 --- a/src/JT808.DotNetty.Core/Handlers/JT808MsgIdUdpHandlerBase.cs +++ b/src/JT808.DotNetty.Core/Handlers/JT808MsgIdUdpHandlerBase.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using JT808.DotNetty.Core.Interfaces; using JT808.DotNetty.Core.Metadata; +using JT808.DotNetty.Core.Session; using JT808.Protocol.Enums; using JT808.Protocol.Extensions; using JT808.Protocol.MessageBody; @@ -18,11 +19,11 @@ namespace JT808.DotNetty.Core.Handlers /// public abstract class JT808MsgIdUdpHandlerBase { - protected JT808UdpSessionManager sessionManager { get; } + protected JT808SessionManager sessionManager { get; } /// /// 初始化消息处理业务 /// - protected JT808MsgIdUdpHandlerBase(JT808UdpSessionManager sessionManager) + protected JT808MsgIdUdpHandlerBase(JT808SessionManager sessionManager) { this.sessionManager = sessionManager; HandlerDict = new Dictionary> diff --git a/src/JT808.DotNetty.Core/Interfaces/IJT808DatagramPacket.cs b/src/JT808.DotNetty.Core/Interfaces/IJT808DatagramPacket.cs index 70e2a4b..196117f 100644 --- a/src/JT808.DotNetty.Core/Interfaces/IJT808DatagramPacket.cs +++ b/src/JT808.DotNetty.Core/Interfaces/IJT808DatagramPacket.cs @@ -6,7 +6,7 @@ namespace JT808.DotNetty.Core.Interfaces /// /// 基于udp的创建发送包 /// - interface IJT808DatagramPacket + public interface IJT808DatagramPacket { DatagramPacket Create(byte[] message, EndPoint recipient); } diff --git a/src/JT808.DotNetty.Core/Interfaces/IJT808TcpSessionService.cs b/src/JT808.DotNetty.Core/Interfaces/IJT808SessionService.cs similarity index 61% rename from src/JT808.DotNetty.Core/Interfaces/IJT808TcpSessionService.cs rename to src/JT808.DotNetty.Core/Interfaces/IJT808SessionService.cs index e18982b..c08c80b 100644 --- a/src/JT808.DotNetty.Core/Interfaces/IJT808TcpSessionService.cs +++ b/src/JT808.DotNetty.Core/Interfaces/IJT808SessionService.cs @@ -6,15 +6,20 @@ using System.Text; namespace JT808.DotNetty.Core.Interfaces { /// - /// JT808 Tcp会话服务 + /// JT808会话服务 /// - public interface IJT808TcpSessionService + public interface IJT808SessionService { /// - /// 获取会话集合 + /// 获取udp会话集合 /// /// - JT808ResultDto> GetAll(); + JT808ResultDto> GetUdpAll(); + /// + /// 获取tcp会话集合 + /// + /// + JT808ResultDto> GetTcpAll(); /// /// 通过设备终端号移除对应会话 /// diff --git a/src/JT808.DotNetty.Core/Interfaces/IJT808TcpNettyBuilder.cs b/src/JT808.DotNetty.Core/Interfaces/IJT808TcpNettyBuilder.cs index 9bd50c9..c7a5527 100644 --- a/src/JT808.DotNetty.Core/Interfaces/IJT808TcpNettyBuilder.cs +++ b/src/JT808.DotNetty.Core/Interfaces/IJT808TcpNettyBuilder.cs @@ -9,7 +9,5 @@ namespace JT808.DotNetty.Core.Interfaces { IJT808NettyBuilder Instance { get; } IJT808NettyBuilder Builder(); - IJT808TcpNettyBuilder ReplaceSessionService() where T : IJT808TcpSessionService; - IJT808TcpNettyBuilder ReplaceUnificationSendService() where T : IJT808UnificationTcpSendService; } } diff --git a/src/JT808.DotNetty.Core/Interfaces/IJT808UdpNettyBuilder.cs b/src/JT808.DotNetty.Core/Interfaces/IJT808UdpNettyBuilder.cs index 7a55635..870fcd5 100644 --- a/src/JT808.DotNetty.Core/Interfaces/IJT808UdpNettyBuilder.cs +++ b/src/JT808.DotNetty.Core/Interfaces/IJT808UdpNettyBuilder.cs @@ -9,7 +9,5 @@ namespace JT808.DotNetty.Core.Interfaces { IJT808NettyBuilder Instance { get; } IJT808NettyBuilder Builder(); - IJT808UdpNettyBuilder ReplaceSessionService() where T : IJT808UdpSessionService; - IJT808UdpNettyBuilder ReplaceUnificationSendService() where T : IJT808UnificationUdpSendService; } } diff --git a/src/JT808.DotNetty.Core/Interfaces/IJT808UdpSessionService.cs b/src/JT808.DotNetty.Core/Interfaces/IJT808UdpSessionService.cs deleted file mode 100644 index 8e9052c..0000000 --- a/src/JT808.DotNetty.Core/Interfaces/IJT808UdpSessionService.cs +++ /dev/null @@ -1,25 +0,0 @@ -using JT808.DotNetty.Abstractions.Dtos; -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Core.Interfaces -{ - /// - /// JT808 Udp会话服务 - /// - public interface IJT808UdpSessionService - { - /// - /// 获取会话集合 - /// - /// - JT808ResultDto> GetAll(); - /// - /// 通过设备终端号移除对应会话 - /// - /// - /// - JT808ResultDto RemoveByTerminalPhoneNo(string terminalPhoneNo); - } -} diff --git a/src/JT808.DotNetty.Core/Interfaces/IJT808UnificationTcpSendService.cs b/src/JT808.DotNetty.Core/Interfaces/IJT808UnificationSendService.cs similarity index 67% rename from src/JT808.DotNetty.Core/Interfaces/IJT808UnificationTcpSendService.cs rename to src/JT808.DotNetty.Core/Interfaces/IJT808UnificationSendService.cs index 528edcf..a4935d5 100644 --- a/src/JT808.DotNetty.Core/Interfaces/IJT808UnificationTcpSendService.cs +++ b/src/JT808.DotNetty.Core/Interfaces/IJT808UnificationSendService.cs @@ -3,9 +3,9 @@ namespace JT808.DotNetty.Core.Interfaces { /// - /// JT808基于tcp的统一下发命令服务 + /// JT808统一下发命令服务 /// - public interface IJT808UnificationTcpSendService + public interface IJT808UnificationSendService { JT808ResultDto Send(string terminalPhoneNo, byte[] data); } diff --git a/src/JT808.DotNetty.Core/Interfaces/IJT808UnificationUdpSendService.cs b/src/JT808.DotNetty.Core/Interfaces/IJT808UnificationUdpSendService.cs deleted file mode 100644 index fffbd94..0000000 --- a/src/JT808.DotNetty.Core/Interfaces/IJT808UnificationUdpSendService.cs +++ /dev/null @@ -1,12 +0,0 @@ -using JT808.DotNetty.Abstractions.Dtos; - -namespace JT808.DotNetty.Core.Interfaces -{ - /// - /// JT808基于udp的统一下发命令服务 - /// - public interface IJT808UnificationUdpSendService - { - JT808ResultDto Send(string terminalPhoneNo, byte[] data); - } -} diff --git a/src/JT808.DotNetty.Core/JT808CoreDotnettyExtensions.cs b/src/JT808.DotNetty.Core/JT808CoreDotnettyExtensions.cs index 1ac35bd..e859a83 100644 --- a/src/JT808.DotNetty.Core/JT808CoreDotnettyExtensions.cs +++ b/src/JT808.DotNetty.Core/JT808CoreDotnettyExtensions.cs @@ -4,6 +4,7 @@ using JT808.DotNetty.Core.Converters; using JT808.DotNetty.Core.Impls; using JT808.DotNetty.Core.Interfaces; using JT808.DotNetty.Core.Services; +using JT808.DotNetty.Core.Session; using JT808.DotNetty.Internal; using JT808.Protocol; using Microsoft.Extensions.Configuration; @@ -59,6 +60,9 @@ namespace JT808.DotNetty.Core IJT808NettyBuilder nettyBuilder = new JT808NettyBuilderDefault(jt808Builder); nettyBuilder.JT808Builder.Services.Configure(configuration.GetSection("JT808Configuration")); nettyBuilder.JT808Builder.Services.TryAddSingleton(); + nettyBuilder.JT808Builder.Services.TryAddSingleton(); + nettyBuilder.JT808Builder.Services.TryAddSingleton(); + nettyBuilder.JT808Builder.Services.TryAddSingleton(); return nettyBuilder; } @@ -78,6 +82,9 @@ namespace JT808.DotNetty.Core IJT808NettyBuilder nettyBuilder = new JT808NettyBuilderDefault(jt808Builder); nettyBuilder.JT808Builder.Services.Configure(jt808Options); nettyBuilder.JT808Builder.Services.TryAddSingleton(); + nettyBuilder.JT808Builder.Services.TryAddSingleton(); + nettyBuilder.JT808Builder.Services.TryAddSingleton(); + nettyBuilder.JT808Builder.Services.TryAddSingleton(); return nettyBuilder; } } diff --git a/src/JT808.DotNetty.Core/Services/JT808TcpSessionService.cs b/src/JT808.DotNetty.Core/Services/JT808SessionService.cs similarity index 64% rename from src/JT808.DotNetty.Core/Services/JT808TcpSessionService.cs rename to src/JT808.DotNetty.Core/Services/JT808SessionService.cs index 7ff872d..6762423 100644 --- a/src/JT808.DotNetty.Core/Services/JT808TcpSessionService.cs +++ b/src/JT808.DotNetty.Core/Services/JT808SessionService.cs @@ -3,20 +3,21 @@ using System.Collections.Generic; using System.Linq; using JT808.DotNetty.Abstractions.Dtos; using JT808.DotNetty.Core.Interfaces; +using JT808.DotNetty.Core.Session; namespace JT808.DotNetty.Core.Services { - internal class JT808TcpSessionService : IJT808TcpSessionService + internal class JT808SessionService : IJT808SessionService { - private readonly JT808TcpSessionManager jT808SessionManager; + private readonly JT808SessionManager jT808SessionManager; - public JT808TcpSessionService( - JT808TcpSessionManager jT808SessionManager) + public JT808SessionService( + JT808SessionManager jT808SessionManager) { this.jT808SessionManager = jT808SessionManager; } - public JT808ResultDto> GetAll() + public JT808ResultDto> GetTcpAll() { JT808ResultDto> resultDto = new JT808ResultDto>(); try @@ -39,6 +40,29 @@ namespace JT808.DotNetty.Core.Services return resultDto; } + public JT808ResultDto> GetUdpAll() + { + JT808ResultDto> resultDto = new JT808ResultDto>(); + try + { + resultDto.Data = jT808SessionManager.GetUdpAll().Select(s => new JT808UdpSessionInfoDto + { + LastActiveTime = s.LastActiveTime, + StartTime = s.StartTime, + TerminalPhoneNo = s.TerminalPhoneNo, + RemoteAddressIP = s.Sender.ToString() + }).ToList(); + resultDto.Code = JT808ResultCode.Ok; + } + catch (Exception ex) + { + resultDto.Data = null; + resultDto.Code = JT808ResultCode.Error; + resultDto.Message = Newtonsoft.Json.JsonConvert.SerializeObject(ex); + } + return resultDto; + } + public JT808ResultDto RemoveByTerminalPhoneNo(string terminalPhoneNo) { JT808ResultDto resultDto = new JT808ResultDto(); diff --git a/src/JT808.DotNetty.Core/Services/JT808UdpSessionService.cs b/src/JT808.DotNetty.Core/Services/JT808UdpSessionService.cs deleted file mode 100644 index 975108c..0000000 --- a/src/JT808.DotNetty.Core/Services/JT808UdpSessionService.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using JT808.DotNetty.Abstractions.Dtos; -using JT808.DotNetty.Core.Interfaces; - -namespace JT808.DotNetty.Core.Services -{ - internal class JT808UdpSessionService : IJT808UdpSessionService - { - private readonly JT808UdpSessionManager jT808SessionManager; - - public JT808UdpSessionService( - JT808UdpSessionManager jT808SessionManager) - { - this.jT808SessionManager = jT808SessionManager; - } - - public JT808ResultDto> GetAll() - { - JT808ResultDto> resultDto = new JT808ResultDto>(); - try - { - resultDto.Data = jT808SessionManager.GetAll().Select(s => new JT808UdpSessionInfoDto - { - LastActiveTime = s.LastActiveTime, - StartTime = s.StartTime, - TerminalPhoneNo = s.TerminalPhoneNo, - RemoteAddressIP = s.Sender.ToString() - }).ToList(); - resultDto.Code = JT808ResultCode.Ok; - } - catch (Exception ex) - { - resultDto.Data = null; - resultDto.Code = JT808ResultCode.Error; - resultDto.Message = Newtonsoft.Json.JsonConvert.SerializeObject(ex); - } - return resultDto; - } - - public JT808ResultDto RemoveByTerminalPhoneNo(string terminalPhoneNo) - { - JT808ResultDto resultDto = new JT808ResultDto(); - try - { - var session = jT808SessionManager.RemoveSession(terminalPhoneNo); - if (session != null) - { - if(session.Channel.Open) - { - session.Channel.CloseAsync(); - } - } - resultDto.Code = JT808ResultCode.Ok; - resultDto.Data = true; - } - catch (AggregateException ex) - { - resultDto.Data = false; - resultDto.Code = 500; - resultDto.Message = Newtonsoft.Json.JsonConvert.SerializeObject(ex); - } - catch (Exception ex) - { - resultDto.Data = false; - resultDto.Code = JT808ResultCode.Error; - resultDto.Message = Newtonsoft.Json.JsonConvert.SerializeObject(ex); - } - return resultDto; - } - } -} diff --git a/src/JT808.DotNetty.Core/Services/JT808UnificationTcpSendService.cs b/src/JT808.DotNetty.Core/Services/JT808UnificationSendService.cs similarity index 72% rename from src/JT808.DotNetty.Core/Services/JT808UnificationTcpSendService.cs rename to src/JT808.DotNetty.Core/Services/JT808UnificationSendService.cs index 0082c9f..4a75ec0 100644 --- a/src/JT808.DotNetty.Core/Services/JT808UnificationTcpSendService.cs +++ b/src/JT808.DotNetty.Core/Services/JT808UnificationSendService.cs @@ -1,20 +1,16 @@ -using DotNetty.Buffers; -using JT808.DotNetty.Abstractions.Dtos; -using JT808.DotNetty.Core; +using JT808.DotNetty.Abstractions.Dtos; using JT808.DotNetty.Core.Interfaces; -using JT808.DotNetty.Core.Metadata; -using JT808.DotNetty.Core.Services; +using JT808.DotNetty.Core.Session; using System; -using System.Linq; namespace JT808.DotNetty.Internal { - internal class JT808UnificationTcpSendService : IJT808UnificationTcpSendService + internal class JT808UnificationSendService : IJT808UnificationSendService { - private readonly JT808TcpSessionManager jT808SessionManager; + private readonly JT808SessionManager jT808SessionManager; - public JT808UnificationTcpSendService( - JT808TcpSessionManager jT808SessionManager) + public JT808UnificationSendService( + JT808SessionManager jT808SessionManager) { this.jT808SessionManager = jT808SessionManager; } diff --git a/src/JT808.DotNetty.Core/Services/JT808UnificationUdpSendService.cs b/src/JT808.DotNetty.Core/Services/JT808UnificationUdpSendService.cs deleted file mode 100644 index 89d6a99..0000000 --- a/src/JT808.DotNetty.Core/Services/JT808UnificationUdpSendService.cs +++ /dev/null @@ -1,54 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Transport.Channels.Sockets; -using JT808.DotNetty.Abstractions.Dtos; -using JT808.DotNetty.Core; -using JT808.DotNetty.Core.Interfaces; -using JT808.DotNetty.Core.Services; -using System; - -namespace JT808.DotNetty.Internal -{ - internal class JT808UnificationUdpSendService : IJT808UnificationUdpSendService - { - private readonly JT808UdpSessionManager jT808SessionManager; - - private readonly IJT808DatagramPacket jT808DatagramPacket; - - public JT808UnificationUdpSendService( - IJT808DatagramPacket jT808DatagramPacket, - JT808UdpSessionManager jT808SessionManager) - { - this.jT808DatagramPacket = jT808DatagramPacket; - this.jT808SessionManager = jT808SessionManager; - } - - public JT808ResultDto Send(string terminalPhoneNo, byte[] data) - { - JT808ResultDto resultDto = new JT808ResultDto(); - try - { - var session = jT808SessionManager.GetSession(terminalPhoneNo); - if (session != null) - { - session.Channel.WriteAndFlushAsync(jT808DatagramPacket.Create(data, session.Sender)); - resultDto.Code = JT808ResultCode.Ok; - resultDto.Data = true; - resultDto.Message = "Ok"; - } - else - { - resultDto.Code = JT808ResultCode.Ok; - resultDto.Data = false; - resultDto.Message = "offline"; - } - } - catch (Exception ex) - { - resultDto.Data = false; - resultDto.Code = JT808ResultCode.Error; - resultDto.Message = Newtonsoft.Json.JsonConvert.SerializeObject(ex); - } - return resultDto; - } - } -} diff --git a/src/JT808.DotNetty.Core/Session/JT808SessionManager.cs b/src/JT808.DotNetty.Core/Session/JT808SessionManager.cs new file mode 100644 index 0000000..887ae91 --- /dev/null +++ b/src/JT808.DotNetty.Core/Session/JT808SessionManager.cs @@ -0,0 +1,288 @@ +using DotNetty.Transport.Channels; +using JT808.DotNetty.Abstractions; +using JT808.DotNetty.Abstractions.Enums; +using JT808.DotNetty.Core.Interfaces; +using JT808.DotNetty.Core.Metadata; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; + +namespace JT808.DotNetty.Core.Session +{ + public class JT808SessionManager + { + private readonly ILogger logger; + + private readonly IJT808DatagramPacket jT808DatagramPacket; + public IJT808SessionPublishing JT808SessionPublishing { get; } + + public ConcurrentDictionary Sessions { get; } + + public JT808SessionManager( + IJT808SessionPublishing jT808SessionPublishing, + ILoggerFactory loggerFactory + ) + { + Sessions = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); + JT808SessionPublishing = jT808SessionPublishing; + logger = loggerFactory.CreateLogger(); + } + + public JT808SessionManager( + IJT808SessionPublishing jT808SessionPublishing, + ILoggerFactory loggerFactory, + IJT808DatagramPacket jT808DatagramPacket) + { + Sessions = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); + JT808SessionPublishing = jT808SessionPublishing; + logger = loggerFactory.CreateLogger(); + this.jT808DatagramPacket = jT808DatagramPacket; + } + + public int SessionCount + { + get + { + return Sessions.Count; + } + } + public IJT808Session GetSessionByTerminalPhoneNo(string terminalPhoneNo) + { + if (string.IsNullOrEmpty(terminalPhoneNo)) + return default; + if (Sessions.TryGetValue(terminalPhoneNo, out IJT808Session targetSession)) + { + return targetSession; + } + else + { + return default; + } + } + public JT808TcpSession GetTcpSessionByTerminalPhoneNo(string terminalPhoneNo) + { + return (JT808TcpSession)GetSessionByTerminalPhoneNo(terminalPhoneNo); + } + public JT808UdpSession GetUdpSessionByTerminalPhoneNo(string terminalPhoneNo) + { + return (JT808UdpSession)GetSessionByTerminalPhoneNo(terminalPhoneNo); + } + public void Heartbeat(string terminalPhoneNo) + { + if (string.IsNullOrEmpty(terminalPhoneNo)) return; + if (Sessions.TryGetValue(terminalPhoneNo, out IJT808Session oldjT808Session)) + { + oldjT808Session.LastActiveTime = DateTime.Now; + Sessions.TryUpdate(terminalPhoneNo, oldjT808Session, oldjT808Session); + } + } + public bool TrySend(string terminalPhoneNo, byte[] data, out string message) + { + bool isSuccessed; + var session = GetSessionByTerminalPhoneNo(terminalPhoneNo); + if (session != null) + { + //判断转发数据是下发不了消息的 + if (Sessions.Select(s => s.Value).Count(c => c.Channel.Id == session.Channel.Id) > 1) + { + isSuccessed = false; + message = "not support transmit data send."; + } + else + { + if(session.TransportProtocolType== JT808TransportProtocolType.tcp) + { + session.Channel.WriteAndFlushAsync(new JT808Response(data)); + isSuccessed = true; + message = "ok"; + } + else if (session.TransportProtocolType == JT808TransportProtocolType.udp) + { + isSuccessed = true; + message = "ok"; + session.Channel.WriteAndFlushAsync(jT808DatagramPacket.Create(data, ((JT808UdpSession)session).Sender)); + } + else + { + isSuccessed = false; + message = "unknow type"; + } + } + } + else + { + isSuccessed = false; + message = "offline"; + } + return isSuccessed; + } + public bool TrySend(string terminalPhoneNo, IJT808Reply reply, out string message) + { + bool isSuccessed; + var session = GetSessionByTerminalPhoneNo(terminalPhoneNo); + if (session != null) + { + //判断转发数据是下发不了消息的 + if (Sessions.Select(s => s.Value).Count(c => c.Channel.Id == session.Channel.Id) > 1) + { + isSuccessed = false; + message = "not support transmit data send."; + } + else + { + if (session.TransportProtocolType == JT808TransportProtocolType.tcp) + { + isSuccessed = true; + message = "ok"; + session.Channel.WriteAndFlushAsync(reply); + } + else if (session.TransportProtocolType == JT808TransportProtocolType.udp) + { + isSuccessed = true; + message = "ok"; + session.Channel.WriteAndFlushAsync(jT808DatagramPacket.Create(reply.HexData, ((JT808UdpSession)session).Sender)); + } + else + { + isSuccessed = false; + message = "unknow type"; + } + } + } + else + { + isSuccessed = false; + message = "offline"; + } + return isSuccessed; + } + public void TryAdd(string terminalPhoneNo, IChannel channel) + { + // 解决了设备号跟通道绑定到一起,不需要用到通道本身的SessionId + // 不管设备下发更改了设备终端号,只要是没有在内存中就当是新的 + // 存在的问题: + // 1.原先老的如何销毁 + // 2.这时候用的通道是相同的,设备终端是不同的 + // 当设备主动或者服务器断开以后,可以释放,这点内存忽略不计,况且更改设备号不是很频繁。 + + //修复第一次通过转发过来的数据,再次通过直连后通道没有改变导致下发不成功,所以每次进行通道的更新操作。 + if (Sessions.TryGetValue(terminalPhoneNo, out IJT808Session oldJT808Session)) + { + oldJT808Session.LastActiveTime = DateTime.Now; + oldJT808Session.Channel = channel; + Sessions.TryUpdate(terminalPhoneNo, oldJT808Session, oldJT808Session); + } + else + { + JT808TcpSession jT808TcpSession = new JT808TcpSession(channel, terminalPhoneNo); + if (Sessions.TryAdd(terminalPhoneNo, jT808TcpSession)) + { + //使用场景: + //部标的超长待机设备,不会像正常的设备一样一直连着,可能10几分钟连上了,然后发完就关闭连接, + //这时候想下发数据需要知道设备什么时候上线,在这边做通知最好不过了。 + //有设备关联上来可以进行通知 例如:使用Redis发布订阅 + JT808SessionPublishing.PublishAsync(JT808Constants.SessionOnline, jT808TcpSession.TerminalPhoneNo); + } + } + } + public void TryAdd(IChannel channel, EndPoint sender, string terminalPhoneNo) + { + //1.先判断是否在缓存里面 + if (Sessions.TryGetValue(terminalPhoneNo, out IJT808Session jT808UdpSession)) + { + if(jT808UdpSession is JT808UdpSession convertSession) + { + convertSession.LastActiveTime = DateTime.Now; + convertSession.Sender = sender; + convertSession.Channel = channel; + Sessions.TryUpdate(terminalPhoneNo, convertSession, convertSession); + } + } + else + { + //添加缓存 + //使用场景: + //部标的超长待机设备,不会像正常的设备一样一直连着,可能10几分钟连上了,然后发完就关闭连接, + //这时候想下发数据需要知道设备什么时候上线,在这边做通知最好不过了。 + //有设备关联上来可以进行通知 例如:使用Redis发布订阅 + Sessions.TryAdd(terminalPhoneNo, new JT808UdpSession(channel, sender, terminalPhoneNo)); + } + //移动是个大的内网,不跟随下发,根本就发不出来 + //移动很多卡,存储的那个socket地址端口,有效期非常短 + //不速度快点下发,那个socket地址端口就可能映射到别的对应卡去了 + //所以此处采用跟随设备消息下发指令 + JT808SessionPublishing.PublishAsync(JT808Constants.SessionOnline, terminalPhoneNo); + } + public IJT808Session RemoveSession(string terminalPhoneNo) + { + //设备离线可以进行通知 + //使用Redis 发布订阅 + if (string.IsNullOrEmpty(terminalPhoneNo)) return default; + if (!Sessions.TryGetValue(terminalPhoneNo, out IJT808Session jT808Session)) + { + return default; + } + // 处理转发过来的是数据 这时候通道对设备是1对多关系,需要清理垃圾数据 + //1.用当前会话的通道Id找出通过转发过来的其他设备的终端号 + var terminalPhoneNos = Sessions.Where(w => w.Value.Channel.Id == jT808Session.Channel.Id).Select(s => s.Key).ToList(); + //2.存在则一个个移除 + if (terminalPhoneNos.Count > 1) + { + //3.移除包括当前的设备号 + foreach (var key in terminalPhoneNos) + { + Sessions.TryRemove(key, out IJT808Session jT808SessionRemove); + } + string nos = string.Join(",", terminalPhoneNos); + logger.LogInformation($">>>{terminalPhoneNo}-{nos} 1-n Session Remove."); + JT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, nos); + return jT808Session; + } + else + { + if (Sessions.TryRemove(terminalPhoneNo, out IJT808Session jT808SessionRemove)) + { + logger.LogInformation($">>>{terminalPhoneNo} Session Remove."); + JT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, terminalPhoneNo); + return jT808SessionRemove; + } + else + { + return default; + } + } + } + public void RemoveSessionByChannel(IChannel channel) + { + //设备离线可以进行通知 + //使用Redis 发布订阅 + var terminalPhoneNos = Sessions.Where(w => w.Value.Channel.Id == channel.Id).Select(s => s.Key).ToList(); + if (terminalPhoneNos.Count > 0) + { + foreach (var key in terminalPhoneNos) + { + Sessions.TryRemove(key, out IJT808Session jT808SessionRemove); + } + string nos = string.Join(",", terminalPhoneNos); + logger.LogInformation($">>>{nos} Channel Remove."); + JT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, nos); + } + } + public IEnumerable GetAll() + { + return Sessions.Select(s => s.Value).ToList(); + } + public IEnumerable GetTcpAll() + { + return Sessions.Select(s => (JT808TcpSession)s.Value).Where(w => w.TransportProtocolType == JT808TransportProtocolType.tcp).ToList(); + } + public IEnumerable GetUdpAll() + { + return Sessions.Select(s => (JT808UdpSession)s.Value).Where(w => w.TransportProtocolType == JT808TransportProtocolType.udp).ToList(); + } + } +} diff --git a/src/JT808.DotNetty.Core/Session/JT808TcpSessionManager.cs b/src/JT808.DotNetty.Core/Session/JT808TcpSessionManager.cs deleted file mode 100644 index c122832..0000000 --- a/src/JT808.DotNetty.Core/Session/JT808TcpSessionManager.cs +++ /dev/null @@ -1,211 +0,0 @@ -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using DotNetty.Transport.Channels; -using JT808.DotNetty.Abstractions; -using JT808.DotNetty.Core.Metadata; -using JT808.DotNetty.Core.Interfaces; - -namespace JT808.DotNetty.Core -{ - /// - /// JT808 Tcp会话管理 - /// - public class JT808TcpSessionManager - { - private readonly ILogger logger; - - private readonly IJT808SessionPublishing jT808SessionPublishing; - - public JT808TcpSessionManager( - IJT808SessionPublishing jT808SessionPublishing, - ILoggerFactory loggerFactory) - { - this.jT808SessionPublishing = jT808SessionPublishing; - logger = loggerFactory.CreateLogger(); - } - - private ConcurrentDictionary SessionIdDict = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); - - public int SessionCount - { - get - { - return SessionIdDict.Count; - } - } - - public JT808TcpSession GetSession(string terminalPhoneNo) - { - if (string.IsNullOrEmpty(terminalPhoneNo)) - return default; - if (SessionIdDict.TryGetValue(terminalPhoneNo, out JT808TcpSession targetSession)) - { - return targetSession; - } - else - { - return default; - } - } - - public void Heartbeat(string terminalPhoneNo) - { - if (string.IsNullOrEmpty(terminalPhoneNo)) return; - if (SessionIdDict.TryGetValue(terminalPhoneNo, out JT808TcpSession oldjT808Session)) - { - oldjT808Session.LastActiveTime = DateTime.Now; - SessionIdDict.TryUpdate(terminalPhoneNo, oldjT808Session, oldjT808Session); - } - } - - public bool TrySend(string terminalPhoneNo, byte[] data, out string message) - { - bool isSuccessed; - var session = GetSession(terminalPhoneNo); - if (session != null) - { - //判断转发数据是下发不了消息的 - if (SessionIdDict.Select(s=>s.Value).Count(c => c.Channel.Id == session.Channel.Id) > 1) - { - isSuccessed = false; - message = "not support transmit data send."; - } - else - { - session.Channel.WriteAndFlushAsync(new JT808Response(data)); - isSuccessed = true; - message = "ok"; - } - } - else - { - isSuccessed = false; - message = "offline"; - } - return isSuccessed; - } - - public bool TrySend(string terminalPhoneNo, IJT808Reply reply, out string message) - { - bool isSuccessed; - var session = GetSession(terminalPhoneNo); - if (session != null) - { - //判断转发数据是下发不了消息的 - if (SessionIdDict.Select(s => s.Value).Count(c => c.Channel.Id == session.Channel.Id) > 1) - { - isSuccessed = false; - message = "not support transmit data send."; - } - else - { - session.Channel.WriteAndFlushAsync(reply); - isSuccessed = true; - message = "ok"; - } - } - else - { - isSuccessed = false; - message = "offline"; - } - return isSuccessed; - } - - public void TryAdd(string terminalPhoneNo,IChannel channel) - { - // 解决了设备号跟通道绑定到一起,不需要用到通道本身的SessionId - // 不管设备下发更改了设备终端号,只要是没有在内存中就当是新的 - // 存在的问题: - // 1.原先老的如何销毁 - // 2.这时候用的通道是相同的,设备终端是不同的 - // 当设备主动或者服务器断开以后,可以释放,这点内存忽略不计,况且更改设备号不是很频繁。 - - //修复第一次通过转发过来的数据,再次通过直连后通道没有改变导致下发不成功,所以每次进行通道的更新操作。 - if (SessionIdDict.TryGetValue(terminalPhoneNo, out JT808TcpSession oldJT808Session)) - { - oldJT808Session.LastActiveTime = DateTime.Now; - oldJT808Session.Channel = channel; - SessionIdDict.TryUpdate(terminalPhoneNo, oldJT808Session, oldJT808Session); - } - else - { - JT808TcpSession jT808TcpSession = new JT808TcpSession(channel, terminalPhoneNo); - if (SessionIdDict.TryAdd(terminalPhoneNo, jT808TcpSession)) - { - //使用场景: - //部标的超长待机设备,不会像正常的设备一样一直连着,可能10几分钟连上了,然后发完就关闭连接, - //这时候想下发数据需要知道设备什么时候上线,在这边做通知最好不过了。 - //有设备关联上来可以进行通知 例如:使用Redis发布订阅 - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOnline, jT808TcpSession.TerminalPhoneNo); - } - } - } - - public JT808TcpSession RemoveSession(string terminalPhoneNo) - { - //设备离线可以进行通知 - //使用Redis 发布订阅 - if (string.IsNullOrEmpty(terminalPhoneNo)) return default; - if (!SessionIdDict.TryGetValue(terminalPhoneNo, out JT808TcpSession jT808Session)) - { - return default; - } - // 处理转发过来的是数据 这时候通道对设备是1对多关系,需要清理垃圾数据 - //1.用当前会话的通道Id找出通过转发过来的其他设备的终端号 - var terminalPhoneNos = SessionIdDict.Where(w => w.Value.Channel.Id == jT808Session.Channel.Id).Select(s => s.Key).ToList(); - //2.存在则一个个移除 - if (terminalPhoneNos.Count > 1) - { - //3.移除包括当前的设备号 - foreach (var key in terminalPhoneNos) - { - SessionIdDict.TryRemove(key, out JT808TcpSession jT808SessionRemove); - } - string nos = string.Join(",", terminalPhoneNos); - logger.LogInformation($">>>{terminalPhoneNo}-{nos} 1-n Session Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, nos); - return jT808Session; - } - else - { - if (SessionIdDict.TryRemove(terminalPhoneNo, out JT808TcpSession jT808SessionRemove)) - { - logger.LogInformation($">>>{terminalPhoneNo} Session Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline,terminalPhoneNo); - return jT808SessionRemove; - } - else - { - return default; - } - } - } - - public void RemoveSessionByChannel(IChannel channel) - { - //设备离线可以进行通知 - //使用Redis 发布订阅 - var terminalPhoneNos = SessionIdDict.Where(w => w.Value.Channel.Id == channel.Id).Select(s => s.Key).ToList(); - if (terminalPhoneNos.Count > 0) - { - foreach (var key in terminalPhoneNos) - { - SessionIdDict.TryRemove(key, out JT808TcpSession jT808SessionRemove); - } - string nos = string.Join(",", terminalPhoneNos); - logger.LogInformation($">>>{nos} Channel Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, nos); - } - } - - public IEnumerable GetAll() - { - return SessionIdDict.Select(s => s.Value).ToList(); - } - } -} - diff --git a/src/JT808.DotNetty.Core/Session/JT808UdpSessionManager.cs b/src/JT808.DotNetty.Core/Session/JT808UdpSessionManager.cs deleted file mode 100644 index 4a202f6..0000000 --- a/src/JT808.DotNetty.Core/Session/JT808UdpSessionManager.cs +++ /dev/null @@ -1,139 +0,0 @@ -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using JT808.DotNetty.Abstractions; -using JT808.DotNetty.Core.Metadata; -using DotNetty.Transport.Channels; -using JT808.DotNetty.Core.Configurations; -using Microsoft.Extensions.Options; -using System.Net; - -namespace JT808.DotNetty.Core -{ - /// - /// JT808 udp会话管理 - /// 估计要轮询下 - /// - public class JT808UdpSessionManager - { - private readonly ILogger logger; - - private readonly IJT808SessionPublishing jT808SessionPublishing; - private readonly IOptionsMonitor jT808ConfigurationAccessor; - public JT808UdpSessionManager( - IJT808SessionPublishing jT808SessionPublishing, - IOptionsMonitor jT808ConfigurationAccessor, - ILoggerFactory loggerFactory) - { - this.jT808SessionPublishing = jT808SessionPublishing; - logger = loggerFactory.CreateLogger(); - this.jT808ConfigurationAccessor = jT808ConfigurationAccessor; - } - - private ConcurrentDictionary SessionIdDict = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); - - public int SessionCount - { - get - { - return SessionIdDict.Count; - } - } - - public JT808UdpSession GetSession(string terminalPhoneNo) - { - if (string.IsNullOrEmpty(terminalPhoneNo)) - return default; - if (SessionIdDict.TryGetValue(terminalPhoneNo, out JT808UdpSession targetSession)) - { - return targetSession; - } - else - { - return default; - } - } - - public void TryAdd(IChannel channel,EndPoint sender,string terminalPhoneNo) - { - //1.先判断是否在缓存里面 - if (SessionIdDict.TryGetValue(terminalPhoneNo, out JT808UdpSession jT808UdpSession)) - { - jT808UdpSession.LastActiveTime=DateTime.Now; - jT808UdpSession.Sender = sender; - jT808UdpSession.Channel = channel; - SessionIdDict.TryUpdate(terminalPhoneNo, jT808UdpSession, jT808UdpSession); - } - else - { - //添加缓存 - //使用场景: - //部标的超长待机设备,不会像正常的设备一样一直连着,可能10几分钟连上了,然后发完就关闭连接, - //这时候想下发数据需要知道设备什么时候上线,在这边做通知最好不过了。 - //有设备关联上来可以进行通知 例如:使用Redis发布订阅 - SessionIdDict.TryAdd(terminalPhoneNo, new JT808UdpSession(channel, sender, terminalPhoneNo)); - } - //移动是个大的内网,不跟随下发,根本就发不出来 - //移动很多卡,存储的那个socket地址端口,有效期非常短 - //不速度快点下发,那个socket地址端口就可能映射到别的对应卡去了 - //所以此处采用跟随设备消息下发指令 - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOnline, terminalPhoneNo); - } - - public void Heartbeat(string terminalPhoneNo) - { - if (string.IsNullOrEmpty(terminalPhoneNo)) return; - if (SessionIdDict.TryGetValue(terminalPhoneNo, out JT808UdpSession oldjT808Session)) - { - oldjT808Session.LastActiveTime = DateTime.Now; - SessionIdDict.TryUpdate(terminalPhoneNo, oldjT808Session, oldjT808Session); - } - } - - public JT808UdpSession RemoveSession(string terminalPhoneNo) - { - //设备离线可以进行通知 - //使用Redis 发布订阅 - if (string.IsNullOrEmpty(terminalPhoneNo)) return default; - if (!SessionIdDict.TryGetValue(terminalPhoneNo, out JT808UdpSession jT808Session)) - { - return default; - } - if (SessionIdDict.TryRemove(terminalPhoneNo, out JT808UdpSession jT808SessionRemove)) - { - logger.LogInformation($">>>{terminalPhoneNo} Session Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline,terminalPhoneNo); - return jT808SessionRemove; - } - else - { - return default; - } - } - - public void RemoveSessionByChannel(IChannel channel) - { - //设备离线可以进行通知 - //使用Redis 发布订阅 - var terminalPhoneNos = SessionIdDict.Where(w => w.Value.Channel.Id == channel.Id).Select(s => s.Key).ToList(); - if (terminalPhoneNos.Count > 0) - { - foreach (var key in terminalPhoneNos) - { - SessionIdDict.TryRemove(key, out JT808UdpSession jT808SessionRemove); - } - string nos = string.Join(",", terminalPhoneNos); - logger.LogInformation($">>>{nos} Channel Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, nos); - } - } - - public IEnumerable GetAll() - { - return SessionIdDict.Select(s => s.Value).ToList(); - } - } -} - diff --git a/src/JT808.DotNetty.Tcp/Handlers/JT808TcpConnectionHandler.cs b/src/JT808.DotNetty.Tcp/Handlers/JT808TcpConnectionHandler.cs index 33e4d13..62ad5d8 100644 --- a/src/JT808.DotNetty.Tcp/Handlers/JT808TcpConnectionHandler.cs +++ b/src/JT808.DotNetty.Tcp/Handlers/JT808TcpConnectionHandler.cs @@ -1,6 +1,7 @@ using DotNetty.Handlers.Timeout; using DotNetty.Transport.Channels; using JT808.DotNetty.Core; +using JT808.DotNetty.Core.Session; using Microsoft.Extensions.Logging; using System; using System.Threading.Tasks; @@ -14,10 +15,10 @@ namespace JT808.DotNetty.Tcp.Handlers { private readonly ILogger logger; - private readonly JT808TcpSessionManager jT808SessionManager; + private readonly JT808SessionManager jT808SessionManager; public JT808TcpConnectionHandler( - JT808TcpSessionManager jT808SessionManager, + JT808SessionManager jT808SessionManager, ILoggerFactory loggerFactory) { this.jT808SessionManager = jT808SessionManager; diff --git a/src/JT808.DotNetty.Tcp/Handlers/JT808TcpServerHandler.cs b/src/JT808.DotNetty.Tcp/Handlers/JT808TcpServerHandler.cs index a374868..b5534ed 100644 --- a/src/JT808.DotNetty.Tcp/Handlers/JT808TcpServerHandler.cs +++ b/src/JT808.DotNetty.Tcp/Handlers/JT808TcpServerHandler.cs @@ -8,6 +8,7 @@ using JT808.DotNetty.Core.Services; using JT808.DotNetty.Abstractions.Enums; using JT808.Protocol.Interfaces; using JT808.Protocol.Exceptions; +using JT808.DotNetty.Core.Session; namespace JT808.DotNetty.Tcp.Handlers { @@ -16,7 +17,7 @@ namespace JT808.DotNetty.Tcp.Handlers /// internal class JT808TcpServerHandler : SimpleChannelInboundHandler { - private readonly JT808TcpSessionManager jT808SessionManager; + private readonly JT808SessionManager jT808SessionManager; private readonly JT808AtomicCounterService jT808AtomicCounterService; @@ -28,7 +29,7 @@ namespace JT808.DotNetty.Tcp.Handlers IJT808Config jT808Config, ILoggerFactory loggerFactory, JT808AtomicCounterServiceFactory jT808AtomicCounterServiceFactory, - JT808TcpSessionManager jT808SessionManager) + JT808SessionManager jT808SessionManager) { this.jT808SessionManager = jT808SessionManager; this.jT808AtomicCounterService = jT808AtomicCounterServiceFactory.Create(JT808TransportProtocolType.tcp); diff --git a/src/JT808.DotNetty.Tcp/JT808TcpBuilderDefault.cs b/src/JT808.DotNetty.Tcp/JT808TcpBuilderDefault.cs index 7b247a1..6050716 100644 --- a/src/JT808.DotNetty.Tcp/JT808TcpBuilderDefault.cs +++ b/src/JT808.DotNetty.Tcp/JT808TcpBuilderDefault.cs @@ -21,18 +21,5 @@ namespace JT808.DotNetty.Tcp { return Instance; } - - - public IJT808TcpNettyBuilder ReplaceSessionService() where T : IJT808TcpSessionService - { - Instance.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(IJT808TcpSessionService), typeof(T), ServiceLifetime.Singleton)); - return this; - } - - public IJT808TcpNettyBuilder ReplaceUnificationSendService() where T : IJT808UnificationTcpSendService - { - Instance.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(IJT808UnificationTcpSendService), typeof(T), ServiceLifetime.Singleton)); - return this; - } } } diff --git a/src/JT808.DotNetty.Tcp/JT808TcpDotnettyExtensions.cs b/src/JT808.DotNetty.Tcp/JT808TcpDotnettyExtensions.cs index 0e8250a..b6e9b3a 100644 --- a/src/JT808.DotNetty.Tcp/JT808TcpDotnettyExtensions.cs +++ b/src/JT808.DotNetty.Tcp/JT808TcpDotnettyExtensions.cs @@ -21,9 +21,6 @@ namespace JT808.DotNetty.Tcp { public static IJT808TcpNettyBuilder AddJT808TcpNettyHost(this IJT808NettyBuilder jT808NettyBuilder) { - jT808NettyBuilder.JT808Builder.Services.TryAddSingleton(); - jT808NettyBuilder.JT808Builder.Services.TryAddSingleton(); - jT808NettyBuilder.JT808Builder.Services.TryAddSingleton(); jT808NettyBuilder.JT808Builder.Services.TryAddScoped(); jT808NettyBuilder.JT808Builder.Services.TryAddScoped(); jT808NettyBuilder.JT808Builder.Services.TryAddScoped(); diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Core.Test/SeedTcpSession.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.Core.Test/SeedTcpSession.cs index 352a1ca..edb4002 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Core.Test/SeedTcpSession.cs +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Core.Test/SeedTcpSession.cs @@ -1,4 +1,5 @@ using DotNetty.Transport.Channels.Embedded; +using JT808.DotNetty.Core.Session; using JT808.DotNetty.Internal; using Microsoft.Extensions.Logging; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -11,7 +12,7 @@ namespace JT808.DotNetty.Core.Test [TestClass] public class SeedTcpSession { - public JT808TcpSessionManager jT80TcpSessionManager = new JT808TcpSessionManager( + public JT808SessionManager jT80TcpSessionManager = new JT808SessionManager( new JT808SessionPublishingEmptyImpl(), new LoggerFactory()); diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Handlers/JT808MsgIdTcpCustomHandler.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Handlers/JT808MsgIdTcpCustomHandler.cs index 845b220..146eb99 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Handlers/JT808MsgIdTcpCustomHandler.cs +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Handlers/JT808MsgIdTcpCustomHandler.cs @@ -2,6 +2,7 @@ using JT808.DotNetty.Core.Handlers; using JT808.DotNetty.Core.Interfaces; using JT808.DotNetty.Core.Metadata; +using JT808.DotNetty.Core.Session; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -13,7 +14,7 @@ namespace JT808.DotNetty.Hosting.Handlers { public JT808MsgIdTcpCustomHandler( ILoggerFactory loggerFactory, - JT808TcpSessionManager sessionManager) : base(sessionManager) + JT808SessionManager sessionManager) : base(sessionManager) { logger = loggerFactory.CreateLogger(); } diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Handlers/JT808MsgIdUdpCustomHandler.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Handlers/JT808MsgIdUdpCustomHandler.cs index 7280a9d..e153b44 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Handlers/JT808MsgIdUdpCustomHandler.cs +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Handlers/JT808MsgIdUdpCustomHandler.cs @@ -2,6 +2,7 @@ using JT808.DotNetty.Core.Handlers; using JT808.DotNetty.Core.Interfaces; using JT808.DotNetty.Core.Metadata; +using JT808.DotNetty.Core.Session; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -13,7 +14,7 @@ namespace JT808.DotNetty.Hosting.Handlers { public JT808MsgIdUdpCustomHandler( ILoggerFactory loggerFactory, - JT808UdpSessionManager sessionManager) : base(sessionManager) + JT808SessionManager sessionManager) : base(sessionManager) { logger = loggerFactory.CreateLogger(); } diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Tcp.Test/JT808SessionServiceTest.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.Tcp.Test/JT808SessionServiceTest.cs index 3a483d8..48df2ea 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Tcp.Test/JT808SessionServiceTest.cs +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Tcp.Test/JT808SessionServiceTest.cs @@ -9,6 +9,7 @@ using System.Threading; using Microsoft.Extensions.DependencyInjection; using JT808.Protocol.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; +using JT808.DotNetty.Core.Session; namespace JT808.DotNetty.Tcp.Test { @@ -64,30 +65,30 @@ namespace JT808.DotNetty.Tcp.Test [TestMethod] public void Test1() { - IJT808TcpSessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); - var result = jT808SessionServiceDefaultImpl.GetAll(); + IJT808SessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); + var result = jT808SessionServiceDefaultImpl.GetTcpAll(); Thread.Sleep(5000); } [TestMethod] public void Test2() { - IJT808TcpSessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); - var result1 = jT808SessionServiceDefaultImpl.GetAll(); + IJT808SessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); + var result1 = jT808SessionServiceDefaultImpl.GetTcpAll(); var result2 = jT808SessionServiceDefaultImpl.RemoveByTerminalPhoneNo("123456789001"); - var result3 = jT808SessionServiceDefaultImpl.GetAll(); + var result3 = jT808SessionServiceDefaultImpl.GetTcpAll(); } [TestMethod] public void Test3() { // 判断通道是否关闭 - IJT808TcpSessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); - JT808TcpSessionManager jT808TcpSessionManager = ServiceProvider.GetService(); - var result1 = jT808SessionServiceDefaultImpl.GetAll(); + IJT808SessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); + JT808SessionManager jT808TcpSessionManager = ServiceProvider.GetService(); + var result1 = jT808SessionServiceDefaultImpl.GetTcpAll(); SimpleTcpClient1.Down(); Thread.Sleep(5000); - var session = jT808TcpSessionManager.GetSession("123456789001"); + var session = jT808TcpSessionManager.GetSessionByTerminalPhoneNo("123456789001"); Thread.Sleep(100000); } } diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Tcp.Test/JT808UnificationTcpSendServiceTest.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.Tcp.Test/JT808UnificationTcpSendServiceTest.cs index d2070e7..33df6d3 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Tcp.Test/JT808UnificationTcpSendServiceTest.cs +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Tcp.Test/JT808UnificationTcpSendServiceTest.cs @@ -19,8 +19,8 @@ namespace JT808.DotNetty.Tcp.Test { static IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6565); - private IJT808UnificationTcpSendService jT808UnificationTcpSendService; - private IJT808TcpSessionService jT808SessionServiceDefaultImpl; + private IJT808UnificationSendService jT808UnificationSendService; + private IJT808SessionService jT808SessionServiceDefaultImpl; public JT808UnificationTcpSendServiceTest() { @@ -56,9 +56,9 @@ namespace JT808.DotNetty.Tcp.Test [TestMethod] public void Test1() { - jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); - jT808UnificationTcpSendService = ServiceProvider.GetService(); - jT808SessionServiceDefaultImpl.GetAll(); + jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); + jT808UnificationSendService = ServiceProvider.GetService(); + jT808SessionServiceDefaultImpl.GetTcpAll(); string no = "123456789001"; // 文本信息包 JT808Package jT808Package2 = JT808.Protocol.Enums.JT808MsgId.文本信息下发.Create(no, new JT808_0x8300 @@ -67,7 +67,7 @@ namespace JT808.DotNetty.Tcp.Test TextInfo = "smallchi 518" }); var data = JT808Serializer.Serialize(jT808Package2); - JT808ResultDto jt808Result = jT808UnificationTcpSendService.Send(no, data); + JT808ResultDto jt808Result = jT808UnificationSendService.Send(no, data); Thread.Sleep(1000); Assert.AreEqual(200, jt808Result.Code); Assert.IsTrue(jt808Result.Data); diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Udp.Test/JT808SessionServiceTest.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.Udp.Test/JT808SessionServiceTest.cs index d9ab3fd..aec021b 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Udp.Test/JT808SessionServiceTest.cs +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Udp.Test/JT808SessionServiceTest.cs @@ -9,6 +9,7 @@ using System.Threading; using Microsoft.Extensions.DependencyInjection; using JT808.Protocol.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; +using JT808.DotNetty.Core.Session; namespace JT808.DotNetty.Udp.Test { @@ -63,29 +64,29 @@ namespace JT808.DotNetty.Udp.Test [TestMethod] public void Test1() { - IJT808UdpSessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); - var result = jT808SessionServiceDefaultImpl.GetAll(); + IJT808SessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); + var result = jT808SessionServiceDefaultImpl.GetUdpAll(); } [TestMethod] public void Test2() { - IJT808UdpSessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); - var result1 = jT808SessionServiceDefaultImpl.GetAll(); + IJT808SessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); + var result1 = jT808SessionServiceDefaultImpl.GetUdpAll(); var result2 = jT808SessionServiceDefaultImpl.RemoveByTerminalPhoneNo("123456789001"); - var result3 = jT808SessionServiceDefaultImpl.GetAll(); + var result3 = jT808SessionServiceDefaultImpl.GetUdpAll(); } [TestMethod] public void Test3() { // 判断通道是否关闭 - IJT808UdpSessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); - JT808UdpSessionManager jT808UdpSessionManager = ServiceProvider.GetService(); - var result1 = jT808SessionServiceDefaultImpl.GetAll(); + IJT808SessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); + JT808SessionManager jT808UdpSessionManager = ServiceProvider.GetService(); + var result1 = jT808SessionServiceDefaultImpl.GetUdpAll(); SimpleUdpClient1.Down(); - var session = jT808UdpSessionManager.GetSession("123456789001"); - var result3 = jT808UdpSessionManager.GetAll(); + var session = jT808UdpSessionManager.GetSessionByTerminalPhoneNo("123456789001"); + var result3 = jT808UdpSessionManager.GetUdpAll(); Thread.Sleep(100000); } } diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Udp.Test/JT808UnificationUdpSendServiceTest.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.Udp.Test/JT808UnificationUdpSendServiceTest.cs index e64fd9a..2b73b5d 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Udp.Test/JT808UnificationUdpSendServiceTest.cs +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Udp.Test/JT808UnificationUdpSendServiceTest.cs @@ -19,8 +19,8 @@ namespace JT808.DotNetty.Udp.Test { static IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 818); - private IJT808UnificationUdpSendService jT808UnificationUdpSendService; - private IJT808UdpSessionService jT808SessionServiceDefaultImpl; + private IJT808UnificationSendService jT808UnificationSendService; + private IJT808SessionService jT808SessionServiceDefaultImpl; public JT808UnificationUdpSendServiceTest() { @@ -56,9 +56,9 @@ namespace JT808.DotNetty.Udp.Test public void Test1() { //"126 131 0 0 13 18 52 86 120 144 1 0 11 5 115 109 97 108 108 99 104 105 32 53 49 56 24 126" - jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); - jT808UnificationUdpSendService = ServiceProvider.GetService(); - jT808SessionServiceDefaultImpl.GetAll(); + jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); + jT808UnificationSendService = ServiceProvider.GetService(); + jT808SessionServiceDefaultImpl.GetUdpAll(); string no = "123456789001"; // 文本信息包 JT808Package jT808Package2 = JT808.Protocol.Enums.JT808MsgId.文本信息下发.Create(no, new JT808_0x8300 @@ -67,7 +67,7 @@ namespace JT808.DotNetty.Udp.Test TextInfo = "smallchi 518" }); var data = JT808Serializer.Serialize(jT808Package2); - JT808ResultDto jt808Result = jT808UnificationUdpSendService.Send(no, data); + JT808ResultDto jt808Result = jT808UnificationSendService.Send(no, data); Thread.Sleep(1000); Assert.AreEqual(200, jt808Result.Code); Assert.IsTrue(jt808Result.Data); diff --git a/src/JT808.DotNetty.Udp/Handlers/JT808UdpServerHandler.cs b/src/JT808.DotNetty.Udp/Handlers/JT808UdpServerHandler.cs index 950c07d..ccf589d 100644 --- a/src/JT808.DotNetty.Udp/Handlers/JT808UdpServerHandler.cs +++ b/src/JT808.DotNetty.Udp/Handlers/JT808UdpServerHandler.cs @@ -8,6 +8,7 @@ using JT808.DotNetty.Core.Services; using JT808.DotNetty.Core; using JT808.DotNetty.Abstractions.Enums; using JT808.Protocol.Interfaces; +using JT808.DotNetty.Core.Session; namespace JT808.DotNetty.Udp.Handlers { @@ -20,7 +21,7 @@ namespace JT808.DotNetty.Udp.Handlers private readonly ILogger logger; - private readonly JT808UdpSessionManager jT808UdpSessionManager; + private readonly JT808SessionManager jT808UdpSessionManager; private readonly JT808Serializer JT808Serializer; @@ -28,7 +29,7 @@ namespace JT808.DotNetty.Udp.Handlers IJT808Config jT808Config, ILoggerFactory loggerFactory, JT808AtomicCounterServiceFactory jT808AtomicCounterServiceFactory, - JT808UdpSessionManager jT808UdpSessionManager) + JT808SessionManager jT808UdpSessionManager) { this.jT808AtomicCounterService = jT808AtomicCounterServiceFactory.Create(JT808TransportProtocolType.udp); this.jT808UdpSessionManager = jT808UdpSessionManager; diff --git a/src/JT808.DotNetty.Udp/JT1078UdpBuilderDefault.cs b/src/JT808.DotNetty.Udp/JT1078UdpBuilderDefault.cs index f894297..ddec822 100644 --- a/src/JT808.DotNetty.Udp/JT1078UdpBuilderDefault.cs +++ b/src/JT808.DotNetty.Udp/JT1078UdpBuilderDefault.cs @@ -17,17 +17,5 @@ namespace JT808.DotNetty.Udp { return Instance; } - - public IJT808UdpNettyBuilder ReplaceSessionService() where T : IJT808UdpSessionService - { - Instance.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(IJT808UdpSessionService), typeof(T), ServiceLifetime.Singleton)); - return this; - } - - public IJT808UdpNettyBuilder ReplaceUnificationSendService() where T : IJT808UnificationUdpSendService - { - Instance.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(IJT808UnificationUdpSendService), typeof(T), ServiceLifetime.Singleton)); - return this; - } } } \ No newline at end of file diff --git a/src/JT808.DotNetty.Udp/JT808UdpDotnettyExtensions.cs b/src/JT808.DotNetty.Udp/JT808UdpDotnettyExtensions.cs index 9e74f3d..9d405bc 100644 --- a/src/JT808.DotNetty.Udp/JT808UdpDotnettyExtensions.cs +++ b/src/JT808.DotNetty.Udp/JT808UdpDotnettyExtensions.cs @@ -19,10 +19,7 @@ namespace JT808.DotNetty.Udp { public static IJT808UdpNettyBuilder AddJT808UdpNettyHost(this IJT808NettyBuilder jT808NettyBuilder) { - jT808NettyBuilder.JT808Builder.Services.TryAddSingleton(); jT808NettyBuilder.JT808Builder.Services.TryAddSingleton(); - jT808NettyBuilder.JT808Builder.Services.TryAddSingleton(); - jT808NettyBuilder.JT808Builder.Services.TryAddSingleton(); jT808NettyBuilder.JT808Builder.Services.TryAddScoped(); jT808NettyBuilder.JT808Builder.Services.TryAddScoped(); jT808NettyBuilder.JT808Builder.Services.AddHostedService(); diff --git a/src/JT808.DotNetty.WebApi/Handlers/JT808MsgIdDefaultWebApiHandler.cs b/src/JT808.DotNetty.WebApi/Handlers/JT808MsgIdDefaultWebApiHandler.cs index 83ab43d..927fe67 100644 --- a/src/JT808.DotNetty.WebApi/Handlers/JT808MsgIdDefaultWebApiHandler.cs +++ b/src/JT808.DotNetty.WebApi/Handlers/JT808MsgIdDefaultWebApiHandler.cs @@ -5,6 +5,7 @@ using JT808.DotNetty.Core.Handlers; using JT808.DotNetty.Core.Interfaces; using JT808.DotNetty.Core.Metadata; using JT808.DotNetty.Core.Services; +using JT808.DotNetty.Internal; using Newtonsoft.Json; namespace JT808.DotNetty.WebApi.Handlers @@ -18,67 +19,23 @@ namespace JT808.DotNetty.WebApi.Handlers private readonly JT808AtomicCounterService jT808UdpAtomicCounterService; - private readonly IJT808TcpSessionService jT808TcpSessionService; + private readonly IJT808SessionService jT808SessionService; - private readonly IJT808UdpSessionService jT808UdpSessionService; + private readonly IJT808UnificationSendService jT808UnificationSendService; - private readonly IJT808UnificationTcpSendService jT808UnificationTcpSendService; - - private readonly IJT808UnificationUdpSendService jT808UnificationUdpSendService; - - /// - /// TCP一套注入 - /// - /// public JT808MsgIdDefaultWebApiHandler( - IJT808UnificationTcpSendService jT808UnificationTcpSendService, - IJT808TcpSessionService jT808TcpSessionService, + IJT808UnificationSendService jT808UnificationSendService, + IJT808SessionService jT808SessionService, JT808AtomicCounterServiceFactory jT808AtomicCounterServiceFactory ) { - this.jT808UnificationTcpSendService = jT808UnificationTcpSendService; - this.jT808TcpSessionService = jT808TcpSessionService; - this.jT808TcpAtomicCounterService = jT808AtomicCounterServiceFactory.Create(JT808TransportProtocolType.tcp); - InitTcpRoute(); - } - - /// - /// UDP一套注入 - /// - /// - public JT808MsgIdDefaultWebApiHandler( - IJT808UdpSessionService jT808UdpSessionService, - IJT808UnificationUdpSendService jT808UnificationUdpSendService, - JT808AtomicCounterServiceFactory jT808AtomicCounterServiceFactory - ) - { - this.jT808UdpSessionService = jT808UdpSessionService; - this.jT808UnificationUdpSendService = jT808UnificationUdpSendService; - this.jT808UdpAtomicCounterService = jT808AtomicCounterServiceFactory.Create(JT808TransportProtocolType.udp); - InitUdpRoute(); - } - - /// - /// 统一的一套注入 - /// - /// - /// - public JT808MsgIdDefaultWebApiHandler( - IJT808UnificationTcpSendService jT808UnificationTcpSendService, - IJT808UnificationUdpSendService jT808UnificationUdpSendService, - IJT808TcpSessionService jT808TcpSessionService, - IJT808UdpSessionService jT808UdpSessionService, - JT808AtomicCounterServiceFactory jT808AtomicCounterServiceFactory - ) - { - this.jT808UdpSessionService = jT808UdpSessionService; - this.jT808UnificationTcpSendService = jT808UnificationTcpSendService; - this.jT808UnificationUdpSendService = jT808UnificationUdpSendService; - this.jT808TcpSessionService = jT808TcpSessionService; + this.jT808UnificationSendService = jT808UnificationSendService; + this.jT808SessionService = jT808SessionService; this.jT808TcpAtomicCounterService = jT808AtomicCounterServiceFactory.Create(JT808TransportProtocolType.tcp); this.jT808UdpAtomicCounterService = jT808AtomicCounterServiceFactory.Create(JT808TransportProtocolType.udp); InitTcpRoute(); InitUdpRoute(); + InitCommontRoute(); } /// @@ -88,7 +45,7 @@ namespace JT808.DotNetty.WebApi.Handlers /// public JT808HttpResponse GetTcpSessionAll(JT808HttpRequest request) { - var result = jT808TcpSessionService.GetAll(); + var result = jT808SessionService.GetTcpAll(); return CreateJT808HttpResponse(result); } @@ -97,13 +54,13 @@ namespace JT808.DotNetty.WebApi.Handlers /// /// /// - public JT808HttpResponse RemoveTcpSessionByTerminalPhoneNo(JT808HttpRequest request) + public JT808HttpResponse RemoveSessionByTerminalPhoneNo(JT808HttpRequest request) { if (string.IsNullOrEmpty(request.Json)) { return EmptyHttpResponse(); } - var result = jT808TcpSessionService.RemoveByTerminalPhoneNo(request.Json); + var result = jT808SessionService.RemoveByTerminalPhoneNo(request.Json); return CreateJT808HttpResponse(result); } @@ -114,22 +71,7 @@ namespace JT808.DotNetty.WebApi.Handlers /// public JT808HttpResponse GetUdpSessionAll(JT808HttpRequest request) { - var result = jT808UdpSessionService.GetAll(); - return CreateJT808HttpResponse(result); - } - - /// - /// 会话服务-通过设备终端号移除对应会话 - /// - /// - /// - public JT808HttpResponse RemoveUdpSessionByTerminalPhoneNo(JT808HttpRequest request) - { - if (string.IsNullOrEmpty(request.Json)) - { - return EmptyHttpResponse(); - } - var result = jT808UdpSessionService.RemoveByTerminalPhoneNo(request.Json); + var result = jT808SessionService.GetUdpAll(); return CreateJT808HttpResponse(result); } @@ -168,51 +110,39 @@ namespace JT808.DotNetty.WebApi.Handlers } /// - /// 基于Tcp的统一下发信息 + /// 统一下发信息 /// /// /// - public JT808HttpResponse UnificationTcpSend(JT808HttpRequest request) + public JT808HttpResponse UnificationSend(JT808HttpRequest request) { if (string.IsNullOrEmpty(request.Json)) { return EmptyHttpResponse(); } JT808UnificationSendRequestDto jT808UnificationSendRequestDto = JsonConvert.DeserializeObject(request.Json); - var result = jT808UnificationTcpSendService.Send(jT808UnificationSendRequestDto.TerminalPhoneNo, jT808UnificationSendRequestDto.Data); + var result = jT808UnificationSendService.Send(jT808UnificationSendRequestDto.TerminalPhoneNo, jT808UnificationSendRequestDto.Data); return CreateJT808HttpResponse(result); } - /// - /// 基于Udp的统一下发信息 - /// - /// - /// - public JT808HttpResponse UnificationUdpSend(JT808HttpRequest request) + protected virtual void InitCommontRoute() { - if (string.IsNullOrEmpty(request.Json)) - { - return EmptyHttpResponse(); - } - JT808UnificationSendRequestDto jT808UnificationSendRequestDto = JsonConvert.DeserializeObject(request.Json); - var result = jT808UnificationUdpSendService.Send(jT808UnificationSendRequestDto.TerminalPhoneNo, jT808UnificationSendRequestDto.Data); - return CreateJT808HttpResponse(result); + CreateRoute(JT808Constants.JT808WebApiRouteTable.UnificationSend, UnificationSend); + CreateRoute(JT808Constants.JT808WebApiRouteTable.SessionRemoveByTerminalPhoneNo, RemoveSessionByTerminalPhoneNo); } protected virtual void InitTcpRoute() { CreateRoute(JT808Constants.JT808WebApiRouteTable.GetTcpAtomicCounter, GetTcpAtomicCounter); CreateRoute(JT808Constants.JT808WebApiRouteTable.SessionTcpGetAll, GetTcpSessionAll); - CreateRoute(JT808Constants.JT808WebApiRouteTable.SessionTcpRemoveByTerminalPhoneNo, RemoveTcpSessionByTerminalPhoneNo); - CreateRoute(JT808Constants.JT808WebApiRouteTable.UnificationTcpSend, UnificationTcpSend); + + } protected virtual void InitUdpRoute() { CreateRoute(JT808Constants.JT808WebApiRouteTable.GetUdpAtomicCounter, GetUdpAtomicCounter); - CreateRoute(JT808Constants.JT808WebApiRouteTable.UnificationUdpSend, UnificationUdpSend); CreateRoute(JT808Constants.JT808WebApiRouteTable.SessionUdpGetAll, GetUdpSessionAll); - CreateRoute(JT808Constants.JT808WebApiRouteTable.SessionUdpRemoveByTerminalPhoneNo, RemoveUdpSessionByTerminalPhoneNo); } } }