From bb53b7c3319c84711992a618222edb0aa6b06539 Mon Sep 17 00:00:00 2001 From: SmallChi <564952747@qq.com> Date: Fri, 28 Dec 2018 17:42:19 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=85=83=E6=97=A6=E5=BF=AB=E4=B9=90,?= =?UTF-8?q?=E6=9C=89=E4=BA=8B=E5=81=9C=E5=86=994=E5=A4=A9=202.=E5=8E=BB?= =?UTF-8?q?=E9=99=A4=E6=97=A0=E7=94=A8=E9=A1=B9=E7=9B=AE=203.=E8=B0=83?= =?UTF-8?q?=E6=95=B4UDP=E7=9A=84=E4=BC=9A=E8=AF=9D=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E3=80=81=E8=A7=A3=E7=A0=81=E5=99=A8=204.=E8=B0=83=E6=95=B4TCP/?= =?UTF-8?q?UDP=E7=9A=84=E7=BB=9F=E4=B8=80=E4=B8=8B=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Internal/JT808AtomicCounterServiceTest.cs | 90 ---- .../JT808SessionPublishingRedisImplTest.cs | 78 ---- .../JT808SessionServiceDefaultImplTest.cs | 62 --- .../JT808SourcePackageChannelServiceTest.cs | 146 ------ ...8SourcePackageDispatcherDefaultImplTest.cs | 72 --- .../JT808TransmitAddressFilterServiceTest.cs | 104 ----- ...08UnificationSendServiceDefaultImplTest.cs | 87 ---- .../JT808.DotNetty.Test.csproj | 32 -- .../JT808DefaultChannelId.cs | 39 -- .../JT808SessionManagerTest.cs | 85 ---- .../JT808WebAPIServiceTest.cs | 90 ---- src/JT808.DotNetty.Test/SeedSession.cs | 36 -- src/JT808.DotNetty.Test/TestBase.cs | 46 -- src/JT808.DotNetty.Test/appsettings.json | 39 -- .../Codecs/JT808ClientDecoder.cs | 59 --- src/JT808.DotNetty/Codecs/JT808Decoder.cs | 28 -- src/JT808.DotNetty/Codecs/JT808UDPDecoder.cs | 31 -- .../JT808ClientConfiguration.cs | 35 -- .../Configurations/JT808Configuration.cs | 54 --- .../Dtos/JT808AtomicCounterDto.cs | 16 - .../Dtos/JT808DefaultResultDto.cs | 15 - src/JT808.DotNetty/Dtos/JT808IPAddressDto.cs | 35 -- src/JT808.DotNetty/Dtos/JT808ResultDto.cs | 24 - .../Dtos/JT808SessionInfoDto.cs | 30 -- .../Dtos/JT808SourcePackageChannelInfoDto.cs | 33 -- .../Dtos/JT808UnificationSendRequestDto.cs | 15 - .../Handlers/JT808ConnectionHandler.cs | 107 ----- .../Handlers/JT808ServerHandler.cs | 96 ---- .../JT808SourcePackageDispatcherHandler.cs | 70 --- .../Handlers/JT808UDPServerHandler.cs | 88 ---- .../Handlers/JT808WebAPIServerHandler.cs | 82 ---- .../Interfaces/IJT808SessionPublishing.cs | 12 - .../Interfaces/IJT808SessionService.cs | 25 -- .../IJT808SourcePackageDispatcher.cs | 18 - .../IJT808UnificationSendService.cs | 15 - .../Internal/JT808AtomicCounterService.cs | 48 -- .../Internal/JT808MsgIdDefaultHandler.cs | 16 - .../JT808SessionPublishingEmptyImpl.cs | 16 - .../JT808SessionPublishingRedisImpl.cs | 75 ---- .../JT808SessionServiceDefaultImpl.cs | 77 ---- .../JT808SourcePackageChannelService.cs | 290 ------------ ...JT808SourcePackageDispatcherDefaultImpl.cs | 39 -- .../JT808TransmitAddressFilterService.cs | 88 ---- .../JT808UnificationSendServiceDefaultImpl.cs | 56 --- .../Internal/JT808WebAPIService.cs | 253 ----------- src/JT808.DotNetty/JT808.DotNetty.csproj | 46 -- src/JT808.DotNetty/JT808.DotNetty.xml | 418 ------------------ src/JT808.DotNetty/JT808Constants.cs | 13 - src/JT808.DotNetty/JT808DotnettyExtensions.cs | 58 --- src/JT808.DotNetty/JT808MsgIdHandlerBase.cs | 160 ------- src/JT808.DotNetty/JT808ServerHost.cs | 96 ---- src/JT808.DotNetty/JT808SessionManager.cs | 141 ------ src/JT808.DotNetty/JT808SimpleTcpClient.cs | 77 ---- src/JT808.DotNetty/JT808UDPServerHost.cs | 82 ---- src/JT808.DotNetty/JT808WebAPIServerHost.cs | 82 ---- .../Metadata/JT808AtomicCounter.cs | 44 -- .../Metadata/JT808HttpRequest.cs | 22 - .../Metadata/JT808HttpResponse.cs | 22 - src/JT808.DotNetty/Metadata/JT808Request.cs | 23 - src/JT808.DotNetty/Metadata/JT808Response.cs | 27 -- src/JT808.DotNetty/Metadata/JT808Session.cs | 31 -- .../Metadata/JT808UDPPackage.cs | 20 - 62 files changed, 4214 deletions(-) delete mode 100644 src/JT808.DotNetty.Test/Internal/JT808AtomicCounterServiceTest.cs delete mode 100644 src/JT808.DotNetty.Test/Internal/JT808SessionPublishingRedisImplTest.cs delete mode 100644 src/JT808.DotNetty.Test/Internal/JT808SessionServiceDefaultImplTest.cs delete mode 100644 src/JT808.DotNetty.Test/Internal/JT808SourcePackageChannelServiceTest.cs delete mode 100644 src/JT808.DotNetty.Test/Internal/JT808SourcePackageDispatcherDefaultImplTest.cs delete mode 100644 src/JT808.DotNetty.Test/Internal/JT808TransmitAddressFilterServiceTest.cs delete mode 100644 src/JT808.DotNetty.Test/Internal/JT808UnificationSendServiceDefaultImplTest.cs delete mode 100644 src/JT808.DotNetty.Test/JT808.DotNetty.Test.csproj delete mode 100644 src/JT808.DotNetty.Test/JT808DefaultChannelId.cs delete mode 100644 src/JT808.DotNetty.Test/JT808SessionManagerTest.cs delete mode 100644 src/JT808.DotNetty.Test/JT808WebAPIServiceTest.cs delete mode 100644 src/JT808.DotNetty.Test/SeedSession.cs delete mode 100644 src/JT808.DotNetty.Test/TestBase.cs delete mode 100644 src/JT808.DotNetty.Test/appsettings.json delete mode 100644 src/JT808.DotNetty/Codecs/JT808ClientDecoder.cs delete mode 100644 src/JT808.DotNetty/Codecs/JT808Decoder.cs delete mode 100644 src/JT808.DotNetty/Codecs/JT808UDPDecoder.cs delete mode 100644 src/JT808.DotNetty/Configurations/JT808ClientConfiguration.cs delete mode 100644 src/JT808.DotNetty/Configurations/JT808Configuration.cs delete mode 100644 src/JT808.DotNetty/Dtos/JT808AtomicCounterDto.cs delete mode 100644 src/JT808.DotNetty/Dtos/JT808DefaultResultDto.cs delete mode 100644 src/JT808.DotNetty/Dtos/JT808IPAddressDto.cs delete mode 100644 src/JT808.DotNetty/Dtos/JT808ResultDto.cs delete mode 100644 src/JT808.DotNetty/Dtos/JT808SessionInfoDto.cs delete mode 100644 src/JT808.DotNetty/Dtos/JT808SourcePackageChannelInfoDto.cs delete mode 100644 src/JT808.DotNetty/Dtos/JT808UnificationSendRequestDto.cs delete mode 100644 src/JT808.DotNetty/Handlers/JT808ConnectionHandler.cs delete mode 100644 src/JT808.DotNetty/Handlers/JT808ServerHandler.cs delete mode 100644 src/JT808.DotNetty/Handlers/JT808SourcePackageDispatcherHandler.cs delete mode 100644 src/JT808.DotNetty/Handlers/JT808UDPServerHandler.cs delete mode 100644 src/JT808.DotNetty/Handlers/JT808WebAPIServerHandler.cs delete mode 100644 src/JT808.DotNetty/Interfaces/IJT808SessionPublishing.cs delete mode 100644 src/JT808.DotNetty/Interfaces/IJT808SessionService.cs delete mode 100644 src/JT808.DotNetty/Interfaces/IJT808SourcePackageDispatcher.cs delete mode 100644 src/JT808.DotNetty/Interfaces/IJT808UnificationSendService.cs delete mode 100644 src/JT808.DotNetty/Internal/JT808AtomicCounterService.cs delete mode 100644 src/JT808.DotNetty/Internal/JT808MsgIdDefaultHandler.cs delete mode 100644 src/JT808.DotNetty/Internal/JT808SessionPublishingEmptyImpl.cs delete mode 100644 src/JT808.DotNetty/Internal/JT808SessionPublishingRedisImpl.cs delete mode 100644 src/JT808.DotNetty/Internal/JT808SessionServiceDefaultImpl.cs delete mode 100644 src/JT808.DotNetty/Internal/JT808SourcePackageChannelService.cs delete mode 100644 src/JT808.DotNetty/Internal/JT808SourcePackageDispatcherDefaultImpl.cs delete mode 100644 src/JT808.DotNetty/Internal/JT808TransmitAddressFilterService.cs delete mode 100644 src/JT808.DotNetty/Internal/JT808UnificationSendServiceDefaultImpl.cs delete mode 100644 src/JT808.DotNetty/Internal/JT808WebAPIService.cs delete mode 100644 src/JT808.DotNetty/JT808.DotNetty.csproj delete mode 100644 src/JT808.DotNetty/JT808.DotNetty.xml delete mode 100644 src/JT808.DotNetty/JT808Constants.cs delete mode 100644 src/JT808.DotNetty/JT808DotnettyExtensions.cs delete mode 100644 src/JT808.DotNetty/JT808MsgIdHandlerBase.cs delete mode 100644 src/JT808.DotNetty/JT808ServerHost.cs delete mode 100644 src/JT808.DotNetty/JT808SessionManager.cs delete mode 100644 src/JT808.DotNetty/JT808SimpleTcpClient.cs delete mode 100644 src/JT808.DotNetty/JT808UDPServerHost.cs delete mode 100644 src/JT808.DotNetty/JT808WebAPIServerHost.cs delete mode 100644 src/JT808.DotNetty/Metadata/JT808AtomicCounter.cs delete mode 100644 src/JT808.DotNetty/Metadata/JT808HttpRequest.cs delete mode 100644 src/JT808.DotNetty/Metadata/JT808HttpResponse.cs delete mode 100644 src/JT808.DotNetty/Metadata/JT808Request.cs delete mode 100644 src/JT808.DotNetty/Metadata/JT808Response.cs delete mode 100644 src/JT808.DotNetty/Metadata/JT808Session.cs delete mode 100644 src/JT808.DotNetty/Metadata/JT808UDPPackage.cs diff --git a/src/JT808.DotNetty.Test/Internal/JT808AtomicCounterServiceTest.cs b/src/JT808.DotNetty.Test/Internal/JT808AtomicCounterServiceTest.cs deleted file mode 100644 index fce3f8a..0000000 --- a/src/JT808.DotNetty.Test/Internal/JT808AtomicCounterServiceTest.cs +++ /dev/null @@ -1,90 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Codecs; -using DotNetty.Transport.Bootstrapping; -using DotNetty.Transport.Channels; -using DotNetty.Transport.Libuv; -using JT808.DotNetty.Codecs; -using JT808.DotNetty.Dtos; -using JT808.DotNetty.Metadata; -using JT808.Protocol; -using JT808.Protocol.Extensions; -using JT808.Protocol.MessageBody; -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Net; -using System.Net.Http; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using Xunit; - -namespace JT808.DotNetty.Test.Internal -{ - public class JT808AtomicCounterServiceTest: TestBase - { - private IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6565); - - public JT808SimpleTcpClient SimpleTcpClient; - - public JT808AtomicCounterServiceTest() - { - SimpleTcpClient = new JT808SimpleTcpClient(endPoint); - } - - [Fact] - public void Test1() - { - JT808AtomicCounter jT808AtomicCounter = new JT808AtomicCounter(); - JT808AtomicCounter jT808AtomicCounter1 = new JT808AtomicCounter(); - Parallel.For(0, 1000, (i) => - { - jT808AtomicCounter.Increment(); - }); - Assert.Equal(1000, jT808AtomicCounter.Count); - Parallel.For(0, 1000, (i) => - { - jT808AtomicCounter1.Increment(); - }); - Assert.Equal(1000, jT808AtomicCounter1.Count); - } - - [Fact] - public void Test2() - { - // 心跳会话包 - JT808Package jT808Package1 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789001"); - SimpleTcpClient.WriteAsync(JT808Serializer.Serialize(jT808Package1)); - - // 心跳会话包 - JT808Package jT808Package2 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789002"); - SimpleTcpClient.WriteAsync(JT808Serializer.Serialize(jT808Package2)); - - // 心跳会话包 - JT808Package jT808Package3 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789003"); - SimpleTcpClient.WriteAsync(JT808Serializer.Serialize(jT808Package3)); - - // 心跳会话包 - JT808Package jT808Package4 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789004"); - SimpleTcpClient.WriteAsync(JT808Serializer.Serialize(jT808Package4)); - - // 心跳会话包 - JT808Package jT808Package5 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789005"); - SimpleTcpClient.WriteAsync(JT808Serializer.Serialize(jT808Package5)); - - // 异步的需要延时下 - Thread.Sleep(1000); - - HttpClient httpClient = new HttpClient(); - // 调用内置的http服务接收文本信息下发 - var result = httpClient.GetAsync("http://127.0.0.1:828/jt808api/GetAtomicCounter").Result; - string content = result.Content.ReadAsStringAsync().Result; - JT808ResultDto jt808Result = JsonConvert.DeserializeObject>(content); - Assert.Equal(200, jt808Result.Code); - Assert.Equal(5,jt808Result.Data.MsgSuccessCount); - Assert.Equal(0, jt808Result.Data.MsgFailCount); - SimpleTcpClient.Down(); - } - } -} diff --git a/src/JT808.DotNetty.Test/Internal/JT808SessionPublishingRedisImplTest.cs b/src/JT808.DotNetty.Test/Internal/JT808SessionPublishingRedisImplTest.cs deleted file mode 100644 index 5d0d737..0000000 --- a/src/JT808.DotNetty.Test/Internal/JT808SessionPublishingRedisImplTest.cs +++ /dev/null @@ -1,78 +0,0 @@ -using JT808.DotNetty.Internal; -using System; -using System.Collections.Generic; -using System.Text; -using Microsoft.Extensions.DependencyInjection; -using JT808.DotNetty.Configurations; -using Microsoft.Extensions.Options; -using Xunit; -using System.Threading.Tasks; -using System.Threading; -using StackExchange.Redis; - -namespace JT808.DotNetty.Test.Internal -{ - public class JT808SessionPublishingRedisImplTest: TestBase - { - JT808SessionPublishingRedisImpl jT808SessionPublishingRedisImpl; - - public JT808SessionPublishingRedisImplTest() - { - jT808SessionPublishingRedisImpl = new JT808SessionPublishingRedisImpl( - ServiceProvider.GetRequiredService>()); - } - - [Fact] - public void Test1() - { - int i = 10000; - Task.Run(() => { - while (i > 0) - { - jT808SessionPublishingRedisImpl.PublishAsync(JT808Constants.SessionOnline, null, Guid.NewGuid().ToString("N")); - jT808SessionPublishingRedisImpl.PublishAsync(JT808Constants.SessionOffline, null, Guid.NewGuid().ToString("N")); - i--; - Thread.Sleep(1000); - } - }); - Thread.Sleep(1000); - List SessionOnlines = new List(); - ChannelMessageQueue channelMessageQueue= jT808SessionPublishingRedisImpl.Subscriber.Subscribe(JT808Constants.SessionOnline); - channelMessageQueue.OnMessage((msg) => { - SessionOnlines.Add(msg.Message); - }); - List SessionOfflines = new List(); - ChannelMessageQueue channelMessageQueue1 = jT808SessionPublishingRedisImpl.Subscriber.Subscribe(JT808Constants.SessionOffline); - channelMessageQueue1.OnMessage((msg) => { - SessionOfflines.Add(msg.Message); - }); - Thread.Sleep(3000); - } - - [Fact] - public void Test2() - { - int i = 100000; - Task.Run(() => { - while (i > 0) - { - jT808SessionPublishingRedisImpl.PublishAsync(JT808Constants.SessionOnline, null, Guid.NewGuid().ToString("N")); - jT808SessionPublishingRedisImpl.PublishAsync(JT808Constants.SessionOffline, null, Guid.NewGuid().ToString("N")); - i--; - Thread.Sleep(1000); - } - }); - Thread.Sleep(1000); - List SessionOnlines = new List(); - List SessionOfflines = new List(); - ChannelMessageQueue channelMessageQueue = jT808SessionPublishingRedisImpl.Subscriber.Subscribe(JT808Constants.SessionOnline); - channelMessageQueue.OnMessage((msg) => { - SessionOnlines.Add(msg.Message); - }); - ChannelMessageQueue channelMessageQueue1 = jT808SessionPublishingRedisImpl.Subscriber.Subscribe(JT808Constants.SessionOffline); - channelMessageQueue1.OnMessage((msg) => { - SessionOfflines.Add(msg.Message); - }); - } - } -} diff --git a/src/JT808.DotNetty.Test/Internal/JT808SessionServiceDefaultImplTest.cs b/src/JT808.DotNetty.Test/Internal/JT808SessionServiceDefaultImplTest.cs deleted file mode 100644 index 855fbd5..0000000 --- a/src/JT808.DotNetty.Test/Internal/JT808SessionServiceDefaultImplTest.cs +++ /dev/null @@ -1,62 +0,0 @@ -using JT808.DotNetty.Internal; -using System; -using System.Linq; -using System.Collections.Generic; -using System.Text; -using Microsoft.Extensions.DependencyInjection; -using System.Net; -using JT808.Protocol; -using JT808.Protocol.Extensions; -using System.Threading; -using Xunit; -using JT808.DotNetty.Interfaces; - -namespace JT808.DotNetty.Test.Internal -{ - public class JT808SessionServiceDefaultImplTest : TestBase - { - static IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6565); - - [Fact] - public void Test1() - { - IJT808SessionService jT808SessionServiceDefaultImpl = ServiceProvider.GetService(); - JT808SimpleTcpClient SimpleTcpClient1 = new JT808SimpleTcpClient(endPoint); - JT808SimpleTcpClient SimpleTcpClient2 = new JT808SimpleTcpClient(endPoint); - JT808SimpleTcpClient SimpleTcpClient3 = new JT808SimpleTcpClient(endPoint); - JT808SimpleTcpClient SimpleTcpClient4 = new JT808SimpleTcpClient(endPoint); - JT808SimpleTcpClient SimpleTcpClient5 = new JT808SimpleTcpClient(endPoint); - // 心跳会话包 - JT808Package jT808Package1 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789001"); - SimpleTcpClient1.WriteAsync(JT808Serializer.Serialize(jT808Package1)); - - // 心跳会话包 - JT808Package jT808Package2 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789002"); - SimpleTcpClient2.WriteAsync(JT808Serializer.Serialize(jT808Package2)); - - // 心跳会话包 - JT808Package jT808Package3 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789003"); - SimpleTcpClient3.WriteAsync(JT808Serializer.Serialize(jT808Package3)); - - // 心跳会话包 - JT808Package jT808Package4 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789004"); - SimpleTcpClient4.WriteAsync(JT808Serializer.Serialize(jT808Package4)); - - // 心跳会话包 - JT808Package jT808Package5 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789005"); - SimpleTcpClient5.WriteAsync(JT808Serializer.Serialize(jT808Package5)); - Thread.Sleep(1000); - var result = jT808SessionServiceDefaultImpl.GetAll(); - - var result1 = jT808SessionServiceDefaultImpl.GetAll(); - - Thread.Sleep(10000); - - SimpleTcpClient1.Down(); - SimpleTcpClient2.Down(); - SimpleTcpClient3.Down(); - SimpleTcpClient4.Down(); - SimpleTcpClient5.Down(); - } - } -} diff --git a/src/JT808.DotNetty.Test/Internal/JT808SourcePackageChannelServiceTest.cs b/src/JT808.DotNetty.Test/Internal/JT808SourcePackageChannelServiceTest.cs deleted file mode 100644 index 507d170..0000000 --- a/src/JT808.DotNetty.Test/Internal/JT808SourcePackageChannelServiceTest.cs +++ /dev/null @@ -1,146 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Codecs; -using DotNetty.Transport.Bootstrapping; -using DotNetty.Transport.Channels; -using DotNetty.Transport.Libuv; -using JT808.DotNetty.Codecs; -using JT808.DotNetty.Configurations; -using JT808.DotNetty.Internal; -using JT808.Protocol; -using JT808.Protocol.Extensions; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Net; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using Xunit; - -namespace JT808.DotNetty.Test.Internal -{ - public class JT808SourcePackageChannelServiceTest:TestBase - { - private JT808SourcePackageChannelService jT808SourcePackageChannelService; - - private IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6565); - - public JT808SimpleTcpClient SimpleTcpClient; - - /// - /// 需要使用 SocketTool 创建tcp服务器 - /// - public JT808SourcePackageChannelServiceTest() - { - SimpleTcpClient = new JT808SimpleTcpClient(endPoint, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6555)); - //作为设备5秒上传 - Task.Run(() => - { - Random random = new Random(); - while (true) - { - - JT808Package jT808Package = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("12345678900"+ random.Next(0,2).ToString()); - SimpleTcpClient.WriteAsync(JT808Serializer.Serialize(jT808Package)); - Thread.Sleep(1000); - } - }); - - // 作为源包转发服务端 - DispatcherEventLoopGroup bossGroup = new DispatcherEventLoopGroup(); - WorkerEventLoopGroup workerGroup = new WorkerEventLoopGroup(bossGroup, 1); - ServerBootstrap bootstrap = new ServerBootstrap(); - bootstrap.Group(bossGroup, workerGroup); - bootstrap.Channel(); - bootstrap - .ChildHandler(new ActionChannelInitializer(channel => - { - IChannelPipeline pipeline = channel.Pipeline; - channel.Pipeline.AddLast("jt808Buffer", new DelimiterBasedFrameDecoder(int.MaxValue, - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.BeginFlag }), - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.EndFlag }))); - channel.Pipeline.AddLast("jt808Decode", new JT808ClientDecoder()); - })); - bootstrap.BindAsync(6655); - - DispatcherEventLoopGroup bossGroup1 = new DispatcherEventLoopGroup(); - WorkerEventLoopGroup workerGroup1 = new WorkerEventLoopGroup(bossGroup1, 1); - ServerBootstrap bootstrap1 = new ServerBootstrap(); - bootstrap1.Group(bossGroup1, workerGroup1); - bootstrap1.Channel(); - bootstrap1 - .ChildHandler(new ActionChannelInitializer(channel => - { - IChannelPipeline pipeline = channel.Pipeline; - channel.Pipeline.AddLast("jt808Buffer", new DelimiterBasedFrameDecoder(int.MaxValue, - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.BeginFlag }), - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.EndFlag }))); - channel.Pipeline.AddLast("jt808Decode", new JT808ClientDecoder()); - })); - bootstrap1.BindAsync(6656); - } - - [Fact] - public void Test1() - { - //预热 - Thread.Sleep(3000); - - jT808SourcePackageChannelService = ServiceProvider.GetService(); - var result = jT808SourcePackageChannelService.GetAll(); - - //创建服务 - DispatcherEventLoopGroup bossGroup2 = new DispatcherEventLoopGroup(); - WorkerEventLoopGroup workerGroup2 = new WorkerEventLoopGroup(bossGroup2, 1); - ServerBootstrap bootstrap2 = new ServerBootstrap(); - bootstrap2.Group(bossGroup2, workerGroup2); - bootstrap2.Channel(); - bootstrap2 - .ChildHandler(new ActionChannelInitializer(channel => - { - IChannelPipeline pipeline = channel.Pipeline; - channel.Pipeline.AddLast("jt808Buffer", new DelimiterBasedFrameDecoder(int.MaxValue, - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.BeginFlag }), - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.EndFlag }))); - channel.Pipeline.AddLast("jt808Decode", new JT808ClientDecoder()); - })); - bootstrap2.BindAsync(6522); - - //添加服务 - var addResult = jT808SourcePackageChannelService.Add(new Dtos.JT808IPAddressDto - { - Host = "127.0.0.1", - Port = 6522 - }).Result; - - Thread.Sleep(1000); - - var result1 = jT808SourcePackageChannelService.GetAll(); - - //删除 - var result2 = jT808SourcePackageChannelService.Remove(new Dtos.JT808IPAddressDto - { - Host = "127.0.0.1", - Port = 6522 - }).Result; - //[::ffff:127.0.0.1]:13196 - var result3 = jT808SourcePackageChannelService.GetAll(); - } - - [Fact] - public void Test2() - { - //[::ffff:127.0.0.1]:13196 - var str = "[::ffff:127.0.0.1]:13196"; - IPAddress.TryParse(str, out IPAddress ip); - var a = ip.MapToIPv4().ToString(); - - var a1 = str.Replace("[::ffff:", "").Replace("]", ""); - - } - } -} diff --git a/src/JT808.DotNetty.Test/Internal/JT808SourcePackageDispatcherDefaultImplTest.cs b/src/JT808.DotNetty.Test/Internal/JT808SourcePackageDispatcherDefaultImplTest.cs deleted file mode 100644 index 55ef405..0000000 --- a/src/JT808.DotNetty.Test/Internal/JT808SourcePackageDispatcherDefaultImplTest.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net; -using System.Text; -using Xunit; -using JT808.Protocol.Extensions; -using System.Threading; -using DotNetty.Transport.Channels; -using System.Runtime.InteropServices; -using DotNetty.Transport.Libuv; -using DotNetty.Transport.Bootstrapping; -using DotNetty.Buffers; -using DotNetty.Codecs; -using JT808.DotNetty.Codecs; - -namespace JT808.DotNetty.Test.Internal -{ - public class JT808SourcePackageDispatcherDefaultImplTest: TestBase - { - private IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6565); - - public JT808SimpleTcpClient SimpleTcpClient; - - public JT808SourcePackageDispatcherDefaultImplTest() - { - SimpleTcpClient = new JT808SimpleTcpClient(endPoint, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6561)); - } - - [Fact] - public void Test1() - { - // 作为源包转发服务端 - DispatcherEventLoopGroup bossGroup = new DispatcherEventLoopGroup(); - WorkerEventLoopGroup workerGroup = new WorkerEventLoopGroup(bossGroup, 1); - ServerBootstrap bootstrap = new ServerBootstrap(); - bootstrap.Group(bossGroup, workerGroup); - bootstrap.Channel(); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) - || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - bootstrap - .Option(ChannelOption.SoReuseport, true) - .ChildOption(ChannelOption.SoReuseaddr, true); - } - bootstrap - .ChildHandler(new ActionChannelInitializer(channel => - { - IChannelPipeline pipeline = channel.Pipeline; - channel.Pipeline.AddLast("jt808Buffer", new DelimiterBasedFrameDecoder(int.MaxValue, - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.BeginFlag }), - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.EndFlag }))); - channel.Pipeline.AddLast("jt808Decode", new JT808ClientDecoder()); - })); - bootstrap.BindAsync(6655); - //作为设备上传 - byte[] bytes = "7E 02 00 00 26 12 34 56 78 90 12 00 7D 02 00 00 00 01 00 00 00 02 00 BA 7F 0E 07 E4 F1 1C 00 28 00 3C 00 00 18 10 15 10 10 10 01 04 00 00 00 64 02 02 00 7D 01 13 7E".ToHexBytes(); - SimpleTcpClient.WriteAsync(bytes); - Thread.Sleep(10000); - SimpleTcpClient.Down(); - } - - [Fact] - public void Test2() - { - //原包转发 不下发 - 貔貅 - byte[] bytes = "7E 02 00 00 26 12 34 56 78 90 12 00 7D 02 00 00 00 01 00 00 00 02 00 BA 7F 0E 07 E4 F1 1C 00 28 00 3C 00 00 18 10 15 10 10 10 01 04 00 00 00 64 02 02 00 7D 01 13 7E".ToHexBytes(); - SimpleTcpClient.WriteAsync(bytes); - Thread.Sleep(3000); - SimpleTcpClient.Down(); - } - } -} diff --git a/src/JT808.DotNetty.Test/Internal/JT808TransmitAddressFilterServiceTest.cs b/src/JT808.DotNetty.Test/Internal/JT808TransmitAddressFilterServiceTest.cs deleted file mode 100644 index 190bf58..0000000 --- a/src/JT808.DotNetty.Test/Internal/JT808TransmitAddressFilterServiceTest.cs +++ /dev/null @@ -1,104 +0,0 @@ -using JT808.DotNetty.Configurations; -using JT808.DotNetty.Dtos; -using JT808.DotNetty.Internal; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Text; -using Xunit; - -namespace JT808.DotNetty.Test.Internal -{ - public class JT808TransmitAddressFilterServiceTest - { - private JT808TransmitAddressFilterService jT808TransmitAddressFilterService; - - public JT808TransmitAddressFilterServiceTest() - { - var serverHostBuilder = new HostBuilder() - .ConfigureAppConfiguration((hostingContext, config) => - { - config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory); - config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); - }) - .ConfigureServices((hostContext, services) => - { - services.AddSingleton(); - services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); - services.Configure(hostContext.Configuration.GetSection("JT808Configuration")); - services.AddSingleton(); - }); - var serviceProvider = serverHostBuilder.Build().Services; - jT808TransmitAddressFilterService = serviceProvider.GetService(); - jT808TransmitAddressFilterService.Add(new Dtos.JT808IPAddressDto - { - Host = "127.0.0.1", - Port = 12345 - }); - jT808TransmitAddressFilterService.Add(new Dtos.JT808IPAddressDto - { - Host = "127.0.0.1", - Port = 12346 - }); - jT808TransmitAddressFilterService.Add(new Dtos.JT808IPAddressDto - { - Host = "127.0.0.1", - Port = 12347 - }); - jT808TransmitAddressFilterService.Add(new Dtos.JT808IPAddressDto - { - Host = "127.0.0.1", - Port = 12348 - }); - } - - [Fact] - public void Test1() - { - Assert.True(jT808TransmitAddressFilterService.ContainsKey(new Dtos.JT808IPAddressDto - { - Host = "127.0.0.1", - Port = 12348 - }.EndPoint)); - } - - [Fact] - public void Test2() - { - var result = jT808TransmitAddressFilterService.GetAll(); - } - - [Fact] - public void Test3() - { - var ip1 = new Dtos.JT808IPAddressDto - { - Host = "127.0.0.1", - Port = 12349 - }; - var result1= jT808TransmitAddressFilterService.Add(ip1); - Assert.Equal(JT808ResultCode.Ok, result1.Code); - Assert.True(result1.Data); - var result2 = jT808TransmitAddressFilterService.Remove(ip1); - Assert.Equal(JT808ResultCode.Ok, result2.Code); - Assert.True(result2.Data); - } - - [Fact] - public void Test4() - { - var configIp = new Dtos.JT808IPAddressDto - { - Host = "127.0.0.1", - Port = 6561 - }; - var result2 = jT808TransmitAddressFilterService.Remove(configIp); - Assert.Equal(JT808ResultCode.Ok, result2.Code); - Assert.False(result2.Data); - Assert.Equal("不能删除服务器配置的地址", result2.Message); - } - } -} diff --git a/src/JT808.DotNetty.Test/Internal/JT808UnificationSendServiceDefaultImplTest.cs b/src/JT808.DotNetty.Test/Internal/JT808UnificationSendServiceDefaultImplTest.cs deleted file mode 100644 index 87b64ca..0000000 --- a/src/JT808.DotNetty.Test/Internal/JT808UnificationSendServiceDefaultImplTest.cs +++ /dev/null @@ -1,87 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Codecs; -using DotNetty.Transport.Bootstrapping; -using DotNetty.Transport.Channels; -using DotNetty.Transport.Libuv; -using JT808.DotNetty.Codecs; -using JT808.DotNetty.Dtos; -using JT808.Protocol; -using JT808.Protocol.Extensions; -using JT808.Protocol.MessageBody; -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Net; -using System.Net.Http; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading; -using Xunit; - -namespace JT808.DotNetty.Test.Internal -{ - public class JT808UnificationSendServiceDefaultImplTest : TestBase - { - private IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6565); - - public JT808SimpleTcpClient SimpleTcpClient; - - public JT808UnificationSendServiceDefaultImplTest() - { - SimpleTcpClient = new JT808SimpleTcpClient(endPoint); - } - - [Fact] - public void Test1() - { - // 文本信息包 - JT808Package jT808Package2 = JT808.Protocol.Enums.JT808MsgId.文本信息下发.Create("123456789012", new JT808_0x8300 - { - TextFlag = 5, - TextInfo = "smallchi 518" - }); - var data = JT808Serializer.Serialize(jT808Package2); - - JT808UnificationSendRequestDto jT808UnificationSendRequestDto = new JT808UnificationSendRequestDto(); - jT808UnificationSendRequestDto.TerminalPhoneNo = "123456789012"; - jT808UnificationSendRequestDto.Data = data; - - HttpClient httpClient = new HttpClient(); - // 调用内置的http服务接收文本信息下发 - var result = httpClient.PostAsync("http://127.0.0.1:828/jt808api/UnificationSend", new StringContent(JsonConvert.SerializeObject(jT808UnificationSendRequestDto))).Result; - string content = result.Content.ReadAsStringAsync().Result; - JT808ResultDto jt808Result = JsonConvert.DeserializeObject>(content); - Assert.Equal(200, jt808Result.Code); - Assert.False(jt808Result.Data); - } - - [Fact] - public void Test2() - { - // 心跳会话包 - JT808Package jT808Package1 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789012"); - SimpleTcpClient.WriteAsync(JT808Serializer.Serialize(jT808Package1)); - - // 文本信息包 - JT808Package jT808Package2 = JT808.Protocol.Enums.JT808MsgId.文本信息下发.Create("123456789012", new JT808_0x8300 - { - TextFlag=5, - TextInfo= "smallchi 518" - }); - var data = JT808Serializer.Serialize(jT808Package2); - - JT808UnificationSendRequestDto jT808UnificationSendRequestDto = new JT808UnificationSendRequestDto(); - jT808UnificationSendRequestDto.TerminalPhoneNo = "123456789012"; - jT808UnificationSendRequestDto.Data = data; - - HttpClient httpClient = new HttpClient(); - // 调用内置的http服务接收文本信息下发 - var result =httpClient.PostAsync("http://127.0.0.1:828/jt808api/UnificationSend", new StringContent(JsonConvert.SerializeObject(jT808UnificationSendRequestDto))).Result; - string content = result.Content.ReadAsStringAsync().Result; - JT808ResultDto jt808Result = JsonConvert.DeserializeObject>(content); - Assert.Equal(200, jt808Result.Code); - Assert.True(jt808Result.Data); - SimpleTcpClient.Down(); - } - } -} diff --git a/src/JT808.DotNetty.Test/JT808.DotNetty.Test.csproj b/src/JT808.DotNetty.Test/JT808.DotNetty.Test.csproj deleted file mode 100644 index 993c60c..0000000 --- a/src/JT808.DotNetty.Test/JT808.DotNetty.Test.csproj +++ /dev/null @@ -1,32 +0,0 @@ - - - - netcoreapp2.2 - - false - - 7.1 - - - - - - - - - - all - runtime; build; native; contentfiles; analyzers - - - - - - - - - - Always - - - diff --git a/src/JT808.DotNetty.Test/JT808DefaultChannelId.cs b/src/JT808.DotNetty.Test/JT808DefaultChannelId.cs deleted file mode 100644 index 81114f0..0000000 --- a/src/JT808.DotNetty.Test/JT808DefaultChannelId.cs +++ /dev/null @@ -1,39 +0,0 @@ -using DotNetty.Transport.Channels; -using JT808.DotNetty.Metadata; -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Test -{ - public class JT808DefaultChannelId : IChannelId - { - private static readonly JT808AtomicCounter jT808AtomicCounter = new JT808AtomicCounter(); - - private string Id { - get - { - return Guid.NewGuid().ToString("N"); - } - } - - public string AsLongText() - { - return Id; - } - - public string AsShortText() - { - return Id; - } - - public int CompareTo(IChannelId other) - { - if(other.AsShortText()== Id) - { - return 1; - } - return 0; - } - } -} diff --git a/src/JT808.DotNetty.Test/JT808SessionManagerTest.cs b/src/JT808.DotNetty.Test/JT808SessionManagerTest.cs deleted file mode 100644 index 7e7eabb..0000000 --- a/src/JT808.DotNetty.Test/JT808SessionManagerTest.cs +++ /dev/null @@ -1,85 +0,0 @@ -using DotNetty.Transport.Channels.Embedded; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading; -using Xunit; - -namespace JT808.DotNetty.Test -{ - public class JT808SessionManagerTest: SeedSession - { - [Fact] - public void Test1() - { - var no = "test150"; - var channel = new EmbeddedChannel(new JT808DefaultChannelId()); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel, no)); - Thread.Sleep(1000); - jT808SessionManager.Heartbeat(no); - } - - [Fact] - public void Test2() - { - var no = "test151"; - var channel = new EmbeddedChannel(new JT808DefaultChannelId()); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel, no)); - var sessionInfo = jT808SessionManager.RemoveSession(no); - Assert.Equal(no, sessionInfo.TerminalPhoneNo); - } - - [Fact] - public void Test3() - { - var realSessionInfos = jT808SessionManager.GetAll(); - } - - [Fact] - public void Test4() - { - var realSessionCount = jT808SessionManager.SessionCount; - } - - [Fact] - public void Test5() - { - //转发过来的数据 1:n 一个通道对应多个设备 - var no = "test1"; - var no1 = "test2"; - var no2 = "test3"; - var no3 = "test4"; - var no4 = "test5"; - var channel = new EmbeddedChannel(new JT808DefaultChannelId()); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel, no)); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel, no1)); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel, no2)); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel, no3)); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel, no4)); - var removeSession = jT808SessionManager.RemoveSession(no); - Assert.Equal(no, removeSession.TerminalPhoneNo); - Assert.Equal(channel, removeSession.Channel); - Assert.Equal(channel.Id, removeSession.Channel.Id); - } - - [Fact] - public void Test6() - { - //转发过来的数据 1:n 一个通道对应多个设备 - var no = "test61"; - var no1 = "test62"; - var no2 = "test63"; - var no3 = "test64"; - var no4 = "test65"; - var channel1 = new EmbeddedChannel(new JT808DefaultChannelId()); - var channel2 = new EmbeddedChannel(new JT808DefaultChannelId()); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel1, no)); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel1, no1)); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel1, no2)); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel2, no3)); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel2, no4)); - jT808SessionManager.RemoveSessionByChannel(channel1); - } - } -} diff --git a/src/JT808.DotNetty.Test/JT808WebAPIServiceTest.cs b/src/JT808.DotNetty.Test/JT808WebAPIServiceTest.cs deleted file mode 100644 index 4bc2c1a..0000000 --- a/src/JT808.DotNetty.Test/JT808WebAPIServiceTest.cs +++ /dev/null @@ -1,90 +0,0 @@ -using JT808.DotNetty.Dtos; -using JT808.Protocol; -using JT808.Protocol.Extensions; -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Text; -using System.Threading; -using Xunit; - -namespace JT808.DotNetty.Test -{ - public class JT808WebAPIServiceTest: TestBase - { - private IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6565); - - public JT808SimpleTcpClient SimpleTcpClient; - - private HttpClient httpClient; - - private const string Url = "http://127.0.0.1:828/jt808api"; - - private const string sessionRoutePrefix = "Session"; - - private const int length = 10; - - public JT808WebAPIServiceTest() - { - SimpleTcpClient = new JT808SimpleTcpClient(endPoint); - httpClient = new HttpClient(); - for (var i = 1; i <= length; i++) - { - // 心跳会话包 - JT808Package jT808Package1 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create($"{i.ToString()}"); - SimpleTcpClient.WriteAsync(JT808Serializer.Serialize(jT808Package1)); - } - Thread.Sleep(300); - } - - [Fact] - public void GetAllTest() - { - var result = httpClient.GetAsync($"{Url}/{sessionRoutePrefix}/GetAll").Result; - string content = result.Content.ReadAsStringAsync().Result; - JT808ResultDto> jt808Result = JsonConvert.DeserializeObject>>(content); - Assert.Equal(200, jt808Result.Code); - Assert.Equal(10,jt808Result.Data.Count()); - } - - [Fact] - public void RemoveByChannelIdTest() - { - // 心跳会话包 - JT808Package jT808Package1 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("99"); - SimpleTcpClient.WriteAsync(JT808Serializer.Serialize(jT808Package1)); - - var result4 = httpClient.PostAsync($"{Url}/{sessionRoutePrefix}/RemoveByChannelId", new StringContent("99")).Result; - string content4 = result4.Content.ReadAsStringAsync().Result; - JT808ResultDto jt808Result4= JsonConvert.DeserializeObject>(content4); - Assert.Equal(200, jt808Result4.Code); - Assert.True(jt808Result4.Data); - } - - - [Fact] - public void RemoveByTerminalPhoneNoTest() - { - // 心跳会话包 - JT808Package jT808Package1 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("999"); - SimpleTcpClient.WriteAsync(JT808Serializer.Serialize(jT808Package1)); - - - var result4 = httpClient.PostAsync($"{Url}/{sessionRoutePrefix}/RemoveByTerminalPhoneNo", new StringContent("999")).Result; - string content4 = result4.Content.ReadAsStringAsync().Result; - JT808ResultDto jt808Result4 = JsonConvert.DeserializeObject>(content4); - Assert.Equal(200, jt808Result4.Code); - Assert.True(jt808Result4.Data); - } - - - public override void Dispose() - { - base.Dispose(); - SimpleTcpClient.Down(); - } - } -} diff --git a/src/JT808.DotNetty.Test/SeedSession.cs b/src/JT808.DotNetty.Test/SeedSession.cs deleted file mode 100644 index 4a9124d..0000000 --- a/src/JT808.DotNetty.Test/SeedSession.cs +++ /dev/null @@ -1,36 +0,0 @@ -using DotNetty.Transport.Channels.Embedded; -using JT808.DotNetty.Internal; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Text; -using Xunit; - -namespace JT808.DotNetty.Test -{ - public class SeedSession - { - public JT808SessionManager jT808SessionManager = new JT808SessionManager( - new JT808SessionPublishingEmptyImpl(), - new LoggerFactory()); - - public SeedSession() - { - for (var i = 0; i < 10; i++) - { - var channel = new EmbeddedChannel(new JT808DefaultChannelId()); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel, i.ToString())); - } - } - - [Fact] - public void Init() - { - for (var i = 0; i < 10; i++) - { - var channel = new EmbeddedChannel(new JT808DefaultChannelId()); - jT808SessionManager.TryAdd(new Metadata.JT808Session(channel, i.ToString())); - } - } - } -} diff --git a/src/JT808.DotNetty.Test/TestBase.cs b/src/JT808.DotNetty.Test/TestBase.cs deleted file mode 100644 index 55bad9d..0000000 --- a/src/JT808.DotNetty.Test/TestBase.cs +++ /dev/null @@ -1,46 +0,0 @@ -using JT808.DotNetty.Codecs; -using JT808.DotNetty.Configurations; -using JT808.DotNetty.Handlers; -using JT808.DotNetty.Interfaces; -using JT808.DotNetty.Internal; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using System; -using System.Net; -using Xunit; - -namespace JT808.DotNetty.Test -{ - public class TestBase:IDisposable - { - public static IServiceProvider ServiceProvider; - - static TestBase() - { - var serverHostBuilder = new HostBuilder() - .ConfigureAppConfiguration((hostingContext, config) => - { - config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory); - config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); - }) - .ConfigureServices((hostContext, services) => - { - services.AddSingleton(); - services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); - }) - .UseJT808Host(); - var build = serverHostBuilder.Build(); - build.Start(); - ServiceProvider = build.Services; - - } - - public virtual void Dispose() - { - - } - } -} diff --git a/src/JT808.DotNetty.Test/appsettings.json b/src/JT808.DotNetty.Test/appsettings.json deleted file mode 100644 index 28e73d8..0000000 --- a/src/JT808.DotNetty.Test/appsettings.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "Logging": { - "IncludeScopes": false, - "Debug": { - "LogLevel": { - "Default": "Trace" - } - }, - "Console": { - "LogLevel": { - "Default": "Trace" - } - } - }, - "JT808Configuration": { - "Port": 6565, - "SourcePackageDispatcherClientConfigurations": [ - { - "Host": "127.0.0.1", - "Port": 6655 - }, - { - "Host": "127.0.0.1", - "Port": 6656 - } - ], - "ForwardingRemoteAddress": [ - { - "Host": "127.0.0.1", - "Port": 6561 - }, - { - "Host": "127.0.0.1", - "Port": 6562 - } - ], - "RedisHost": "127.0.0.1:6379" - } -} diff --git a/src/JT808.DotNetty/Codecs/JT808ClientDecoder.cs b/src/JT808.DotNetty/Codecs/JT808ClientDecoder.cs deleted file mode 100644 index 4725517..0000000 --- a/src/JT808.DotNetty/Codecs/JT808ClientDecoder.cs +++ /dev/null @@ -1,59 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Codecs; -using DotNetty.Transport.Channels; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using JT808.Protocol; -using JT808.DotNetty.Metadata; - -namespace JT808.DotNetty.Codecs -{ - /// - /// JT808客户端解码(测试客户端) - /// - internal class JT808ClientDecoder : ByteToMessageDecoder - { - private static readonly ILogger logger=new LoggerFactory().CreateLogger(); - - private static readonly JT808AtomicCounter MsgSuccessCounter = new JT808AtomicCounter(); - - private static readonly JT808AtomicCounter MsgFailCounter = new JT808AtomicCounter(); - - protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List output) - { - byte[] buffer = new byte[input.Capacity + 2]; - try - { - input.ReadBytes(buffer, 1, input.Capacity); - buffer[0] = JT808Package.BeginFlag; - buffer[input.Capacity + 1] = JT808Package.EndFlag; - JT808Package jT808Package = JT808Serializer.Deserialize(buffer); - MsgSuccessCounter.Increment(); - output.Add(jT808Package); - if (logger.IsEnabled(LogLevel.Debug)) - { - logger.LogDebug("accept package success count<<<" + MsgSuccessCounter.Count.ToString()); - } - } - catch (JT808.Protocol.Exceptions.JT808Exception ex) - { - MsgFailCounter.Increment(); - if (logger.IsEnabled(LogLevel.Error)) - { - logger.LogError("accept package fail count<<<" + MsgFailCounter.Count.ToString()); - logger.LogError(ex, "accept msg<<<" + buffer); - } - } - catch (Exception ex) - { - MsgFailCounter.Increment(); - if (logger.IsEnabled(LogLevel.Error)) - { - logger.LogError("accept package fail count<<<" + MsgFailCounter.Count.ToString()); - logger.LogError(ex, "accept msg<<<" + buffer); - } - } - } - } -} diff --git a/src/JT808.DotNetty/Codecs/JT808Decoder.cs b/src/JT808.DotNetty/Codecs/JT808Decoder.cs deleted file mode 100644 index fda4fbe..0000000 --- a/src/JT808.DotNetty/Codecs/JT808Decoder.cs +++ /dev/null @@ -1,28 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Codecs; -using DotNetty.Transport.Channels; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Text; -using JT808.Protocol; -using JT808.DotNetty.Internal; -using JT808.DotNetty.Interfaces; - -namespace JT808.DotNetty.Codecs -{ - /// - /// JT808解码 - /// - internal class JT808Decoder : ByteToMessageDecoder - { - protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List output) - { - byte[] buffer = new byte[input.Capacity + 2]; - input.ReadBytes(buffer, 1, input.Capacity); - buffer[0] = JT808Package.BeginFlag; - buffer[input.Capacity + 1] = JT808Package.EndFlag; - output.Add(buffer); - } - } -} diff --git a/src/JT808.DotNetty/Codecs/JT808UDPDecoder.cs b/src/JT808.DotNetty/Codecs/JT808UDPDecoder.cs deleted file mode 100644 index 013f11d..0000000 --- a/src/JT808.DotNetty/Codecs/JT808UDPDecoder.cs +++ /dev/null @@ -1,31 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Codecs; -using DotNetty.Transport.Channels; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Text; -using JT808.Protocol; -using JT808.DotNetty.Internal; -using JT808.DotNetty.Interfaces; -using DotNetty.Transport.Channels.Sockets; -using JT808.DotNetty.Metadata; - -namespace JT808.DotNetty.Codecs -{ - /// - /// JT808 UDP解码 - /// - internal class JT808UDPDecoder : MessageToMessageDecoder - { - - - protected override void Decode(IChannelHandlerContext context, DatagramPacket message, List output) - { - IByteBuffer byteBuffer = message.Content; - byte[] buffer = new byte[byteBuffer.ReadableBytes]; - byteBuffer.ReadBytes(buffer); - output.Add(new JT808UDPPackage(buffer, message.Sender)); - } - } -} diff --git a/src/JT808.DotNetty/Configurations/JT808ClientConfiguration.cs b/src/JT808.DotNetty/Configurations/JT808ClientConfiguration.cs deleted file mode 100644 index 3d8385d..0000000 --- a/src/JT808.DotNetty/Configurations/JT808ClientConfiguration.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net; -using System.Text; - -namespace JT808.DotNetty.Configurations -{ - public class JT808ClientConfiguration - { - public string Host { get; set; } - - public int Port { get; set; } - - private EndPoint endPoint; - - public EndPoint EndPoint - { - get - { - if (endPoint == null) - { - if (IPAddress.TryParse(Host, out IPAddress ip)) - { - endPoint = new IPEndPoint(ip, Port); - } - else - { - endPoint = new DnsEndPoint(Host, Port); - } - } - return endPoint; - } - } - } -} diff --git a/src/JT808.DotNetty/Configurations/JT808Configuration.cs b/src/JT808.DotNetty/Configurations/JT808Configuration.cs deleted file mode 100644 index 3565e4b..0000000 --- a/src/JT808.DotNetty/Configurations/JT808Configuration.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Configurations -{ - public class JT808Configuration - { - public int Port { get; set; } = 808; - - public int UDPPort { get; set; } = 809; - - public int QuietPeriodSeconds { get; set; } = 1; - - public TimeSpan QuietPeriodTimeSpan => TimeSpan.FromSeconds(QuietPeriodSeconds); - - public int ShutdownTimeoutSeconds { get; set; } = 3; - - public TimeSpan ShutdownTimeoutTimeSpan => TimeSpan.FromSeconds(ShutdownTimeoutSeconds); - - public int SoBacklog { get; set; } = 8192; - - public int EventLoopCount { get; set; } = Environment.ProcessorCount; - - public int ReaderIdleTimeSeconds { get; set; } = 3600; - - public int WriterIdleTimeSeconds { get; set; } = 3600; - - public int AllIdleTimeSeconds { get; set; } = 3600; - - /// - /// WebApi服务 - /// 默认828端口 - /// - public int WebApiPort { get; set; } = 828; - - /// - /// 源包分发器配置 - /// - public List SourcePackageDispatcherClientConfigurations { get; set; } - - /// - /// 转发远程地址 (可选项)知道转发的地址有利于提升性能 - /// 按照808的消息,有些请求必须要应答,但是转发可以不需要有应答可以节省部分资源包括: - // 1.消息的序列化 - // 2.消息的下发 - // 都有一定的性能损耗,那么不需要判断写超时 IdleState.WriterIdle - // 就跟神兽貔貅一样。。。 - /// - public List ForwardingRemoteAddress { get; set; } - - public string RedisHost { get; set; } - } -} diff --git a/src/JT808.DotNetty/Dtos/JT808AtomicCounterDto.cs b/src/JT808.DotNetty/Dtos/JT808AtomicCounterDto.cs deleted file mode 100644 index e1b1627..0000000 --- a/src/JT808.DotNetty/Dtos/JT808AtomicCounterDto.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Dtos -{ - /// - /// 包计数器服务 - /// - public class JT808AtomicCounterDto - { - public long MsgSuccessCount { get; set; } - - public long MsgFailCount { get; set; } - } -} diff --git a/src/JT808.DotNetty/Dtos/JT808DefaultResultDto.cs b/src/JT808.DotNetty/Dtos/JT808DefaultResultDto.cs deleted file mode 100644 index 7b94584..0000000 --- a/src/JT808.DotNetty/Dtos/JT808DefaultResultDto.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Dtos -{ - public class JT808DefaultResultDto: JT808ResultDto - { - public JT808DefaultResultDto() - { - Data = "Hello,JT808 WebAPI"; - Code = JT808ResultCode.Ok; - } - } -} diff --git a/src/JT808.DotNetty/Dtos/JT808IPAddressDto.cs b/src/JT808.DotNetty/Dtos/JT808IPAddressDto.cs deleted file mode 100644 index 3ce22dd..0000000 --- a/src/JT808.DotNetty/Dtos/JT808IPAddressDto.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net; -using System.Text; - -namespace JT808.DotNetty.Dtos -{ - public class JT808IPAddressDto - { - public string Host { get; set; } - - public int Port { get; set; } - - private EndPoint endPoint; - - public EndPoint EndPoint - { - get - { - if (endPoint == null) - { - if (IPAddress.TryParse(Host, out IPAddress ip)) - { - endPoint = new IPEndPoint(ip, Port); - } - else - { - endPoint = new DnsEndPoint(Host, Port); - } - } - return endPoint; - } - } - } -} diff --git a/src/JT808.DotNetty/Dtos/JT808ResultDto.cs b/src/JT808.DotNetty/Dtos/JT808ResultDto.cs deleted file mode 100644 index dabc8f2..0000000 --- a/src/JT808.DotNetty/Dtos/JT808ResultDto.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Dtos -{ - public class JT808ResultDto - { - public string Message { get; set; } - - public int Code { get; set; } - - public T Data { get; set; } - } - - internal class JT808ResultCode - { - public const int Ok = 200; - public const int Empty = 201; - public const int NotFound = 404; - public const int Fail = 400; - public const int Error = 500; - } -} diff --git a/src/JT808.DotNetty/Dtos/JT808SessionInfoDto.cs b/src/JT808.DotNetty/Dtos/JT808SessionInfoDto.cs deleted file mode 100644 index 306e892..0000000 --- a/src/JT808.DotNetty/Dtos/JT808SessionInfoDto.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Dtos -{ - public class JT808SessionInfoDto - { - /// - /// 通道Id - /// - public string ChannelId { get; set; } - /// - /// 最后上线时间 - /// - public DateTime LastActiveTime { get; set; } - /// - /// 上线时间 - /// - public DateTime StartTime { get; set; } - /// - /// 终端手机号 - /// - public string TerminalPhoneNo { get; set; } - /// - /// 远程ip地址 - /// - public string RemoteAddressIP { get; set; } - } -} diff --git a/src/JT808.DotNetty/Dtos/JT808SourcePackageChannelInfoDto.cs b/src/JT808.DotNetty/Dtos/JT808SourcePackageChannelInfoDto.cs deleted file mode 100644 index 82b9c5a..0000000 --- a/src/JT808.DotNetty/Dtos/JT808SourcePackageChannelInfoDto.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Dtos -{ - /// - /// 原包通道信息 - /// - public class JT808SourcePackageChannelInfoDto - { - /// - /// 远程地址 - /// - public string RemoteAddress { get; set; } - /// - /// 本地地址 - /// - public string LocalAddress { get; set; } - /// - /// 是否注册 - /// - public bool Registered { get; set; } - /// - /// 是否活动 - /// - public bool Active { get; set; } - /// - /// 是否打开 - /// - public bool Open { get; set; } - } -} diff --git a/src/JT808.DotNetty/Dtos/JT808UnificationSendRequestDto.cs b/src/JT808.DotNetty/Dtos/JT808UnificationSendRequestDto.cs deleted file mode 100644 index 5b565f1..0000000 --- a/src/JT808.DotNetty/Dtos/JT808UnificationSendRequestDto.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Dtos -{ - /// - /// 统一下发请求参数 - /// - public class JT808UnificationSendRequestDto - { - public string TerminalPhoneNo { get; set; } - public byte[] Data { get; set; } - } -} diff --git a/src/JT808.DotNetty/Handlers/JT808ConnectionHandler.cs b/src/JT808.DotNetty/Handlers/JT808ConnectionHandler.cs deleted file mode 100644 index 129b281..0000000 --- a/src/JT808.DotNetty/Handlers/JT808ConnectionHandler.cs +++ /dev/null @@ -1,107 +0,0 @@ -using DotNetty.Handlers.Timeout; -using DotNetty.Transport.Channels; -using JT808.DotNetty.Metadata; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Net; -using System.Text; -using System.Threading.Tasks; - -namespace JT808.DotNetty.Handlers -{ - /// - /// JT808服务通道处理程序 - /// - internal class JT808ConnectionHandler : ChannelHandlerAdapter - { - private readonly ILogger logger; - - private readonly JT808SessionManager jT808SessionManager; - - public JT808ConnectionHandler( - JT808SessionManager jT808SessionManager, - ILoggerFactory loggerFactory) - { - this.jT808SessionManager = jT808SessionManager; - logger = loggerFactory.CreateLogger(); - } - - /// - /// 通道激活 - /// - /// - public override void ChannelActive(IChannelHandlerContext context) - { - string channelId = context.Channel.Id.AsShortText(); - if (logger.IsEnabled(LogLevel.Debug)) - logger.LogDebug($"<<<{ channelId } Successful client connection to server."); - base.ChannelActive(context); - } - - /// - /// 设备主动断开 - /// - /// - public override void ChannelInactive(IChannelHandlerContext context) - { - string channelId = context.Channel.Id.AsShortText(); - if (logger.IsEnabled(LogLevel.Debug)) - logger.LogDebug($">>>{ channelId } The client disconnects from the server."); - jT808SessionManager.RemoveSessionByChannel(context.Channel); - base.ChannelInactive(context); - } - - /// - /// 服务器主动断开 - /// - /// - /// - public override Task CloseAsync(IChannelHandlerContext context) - { - string channelId = context.Channel.Id.AsShortText(); - if (logger.IsEnabled(LogLevel.Debug)) - logger.LogDebug($"<<<{ channelId } The server disconnects from the client."); - jT808SessionManager.RemoveSessionByChannel(context.Channel); - return base.CloseAsync(context); - } - - public override void ChannelReadComplete(IChannelHandlerContext context)=> context.Flush(); - - /// - /// 超时策略 - /// - /// - /// - public override void UserEventTriggered(IChannelHandlerContext context, object evt) - { - IdleStateEvent idleStateEvent = evt as IdleStateEvent; - if (idleStateEvent != null) - { - if(idleStateEvent.State== IdleState.ReaderIdle) - { - string channelId = context.Channel.Id.AsShortText(); - logger.LogInformation($"{idleStateEvent.State.ToString()}>>>{channelId}"); - // 由于808是设备发心跳,如果很久没有上报数据,那么就由服务器主动关闭连接。 - jT808SessionManager.RemoveSessionByChannel(context.Channel); - context.CloseAsync(); - } - // 按照808的消息,有些请求必须要应答,但是转发可以不需要有应答可以节省部分资源包括: - // 1.消息的序列化 - // 2.消息的下发 - // 都有一定的性能损耗,那么不需要判断写超时 IdleState.WriterIdle - // 就跟神兽貔貅一样。。。 - } - base.UserEventTriggered(context, evt); - } - - public override void ExceptionCaught(IChannelHandlerContext context, Exception exception) - { - string channelId = context.Channel.Id.AsShortText(); - logger.LogError(exception,$"{channelId} {exception.Message}" ); - jT808SessionManager.RemoveSessionByChannel(context.Channel); - context.CloseAsync(); - } - } -} - diff --git a/src/JT808.DotNetty/Handlers/JT808ServerHandler.cs b/src/JT808.DotNetty/Handlers/JT808ServerHandler.cs deleted file mode 100644 index 6cca2a4..0000000 --- a/src/JT808.DotNetty/Handlers/JT808ServerHandler.cs +++ /dev/null @@ -1,96 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Transport.Channels; -using JT808.Protocol; -using System; -using System.Collections.Generic; -using System.Text; -using JT808.DotNetty.Metadata; -using JT808.DotNetty.Internal; -using JT808.DotNetty.Interfaces; -using Microsoft.Extensions.Logging; - -namespace JT808.DotNetty.Handlers -{ - /// - /// JT808服务端处理程序 - /// - internal class JT808ServerHandler : SimpleChannelInboundHandler - { - private readonly JT808MsgIdHandlerBase handler; - - private readonly JT808SessionManager jT808SessionManager; - - private readonly JT808TransmitAddressFilterService jT808TransmitAddressFilterService; - - private readonly IJT808SourcePackageDispatcher jT808SourcePackageDispatcher; - - private readonly JT808AtomicCounterService jT808AtomicCounterService; - - private readonly ILogger logger; - - public JT808ServerHandler( - ILoggerFactory loggerFactory, - JT808TransmitAddressFilterService jT808TransmitAddressFilterService, - IJT808SourcePackageDispatcher jT808SourcePackageDispatcher, - JT808MsgIdHandlerBase handler, - JT808AtomicCounterService jT808AtomicCounterService, - JT808SessionManager jT808SessionManager) - { - this.jT808TransmitAddressFilterService = jT808TransmitAddressFilterService; - this.handler = handler; - this.jT808SessionManager = jT808SessionManager; - this.jT808SourcePackageDispatcher = jT808SourcePackageDispatcher; - this.jT808AtomicCounterService = jT808AtomicCounterService; - logger = loggerFactory.CreateLogger(); - } - - - protected override void ChannelRead0(IChannelHandlerContext ctx, byte[] msg) - { - try - { - jT808SourcePackageDispatcher?.SendAsync(msg); - //解析到头部,然后根据具体的消息Id通过队列去进行消费 - //要是一定要解析到数据体可以在JT808MsgIdHandlerBase类中根据具体的消息, - //解析具体的消息体,具体调用JT808Serializer.Deserialize - JT808HeaderPackage jT808HeaderPackage = JT808Serializer.Deserialize(msg); - jT808AtomicCounterService.MsgSuccessIncrement(); - if (logger.IsEnabled(LogLevel.Debug)) - { - logger.LogDebug("accept package success count<<<" + jT808AtomicCounterService.MsgSuccessCount.ToString()); - } - jT808SessionManager.TryAdd(new JT808Session(ctx.Channel, jT808HeaderPackage.Header.TerminalPhoneNo)); - Func handlerFunc; - if (handler.HandlerDict.TryGetValue(jT808HeaderPackage.Header.MsgId, out handlerFunc)) - { - JT808Response jT808Response = handlerFunc(new JT808Request(jT808HeaderPackage, msg)); - if (jT808Response != null) - { - if (!jT808TransmitAddressFilterService.ContainsKey(ctx.Channel.RemoteAddress)) - { - ctx.WriteAndFlushAsync(Unpooled.WrappedBuffer(JT808Serializer.Serialize(jT808Response.Package, jT808Response.MinBufferSize))); - } - } - } - } - catch (JT808.Protocol.Exceptions.JT808Exception ex) - { - jT808AtomicCounterService.MsgFailIncrement(); - if (logger.IsEnabled(LogLevel.Error)) - { - logger.LogError("accept package fail count<<<" + jT808AtomicCounterService.MsgFailCount.ToString()); - logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg)); - } - } - catch (Exception ex) - { - jT808AtomicCounterService.MsgFailIncrement(); - if (logger.IsEnabled(LogLevel.Error)) - { - logger.LogError("accept package fail count<<<" + jT808AtomicCounterService.MsgFailCount.ToString()); - logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg)); - } - } - } - } -} diff --git a/src/JT808.DotNetty/Handlers/JT808SourcePackageDispatcherHandler.cs b/src/JT808.DotNetty/Handlers/JT808SourcePackageDispatcherHandler.cs deleted file mode 100644 index 5dcdb89..0000000 --- a/src/JT808.DotNetty/Handlers/JT808SourcePackageDispatcherHandler.cs +++ /dev/null @@ -1,70 +0,0 @@ -using DotNetty.Transport.Channels; -using JT808.DotNetty.Internal; -using Microsoft.Extensions.Logging; -using Polly; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; - -namespace JT808.DotNetty.Handlers -{ - internal class JT808SourcePackageDispatcherHandler: ChannelHandlerAdapter - { - private readonly ILogger logger; - - private readonly JT808SourcePackageChannelService jT808SourcePackageChannelService; - - public JT808SourcePackageDispatcherHandler(JT808SourcePackageChannelService jT808SourcePackageChannelService) - { - logger= jT808SourcePackageChannelService.LoggerFactory.CreateLogger(); - this.jT808SourcePackageChannelService = jT808SourcePackageChannelService; - } - - public override void ChannelInactive(IChannelHandlerContext context) - { - Policy.HandleResult(context.Channel.Open) - .WaitAndRetryForeverAsync(retryAttempt => - { - return retryAttempt > 20 ? TimeSpan.FromSeconds(Math.Pow(2, 50)) : TimeSpan.FromSeconds(Math.Pow(2, retryAttempt));//超过重试20次,接近12个小时链接一次 - },(exception, timespan, ctx) => - { - logger.LogError($"Server Disconnection {context.Channel.RemoteAddress.ToString()},Retry Results{exception.Result},Retry Number{timespan},Next Retry Interval(s){ctx.TotalSeconds}"); - }) - .ExecuteAsync(async () => - { - try - { - var newChannel = jT808SourcePackageChannelService.channels.FirstOrDefault(m => m.Value == context.Channel); - if (default(KeyValuePair).Equals(newChannel)) - { - if(logger.IsEnabled(LogLevel.Debug)) - logger.LogDebug($"Server already deleted {context.Channel.RemoteAddress.ToString()} remote server configuration"); - return true; - } - var channel = await jT808SourcePackageChannelService.bootstrap.ConnectAsync(context.Channel.RemoteAddress); - jT808SourcePackageChannelService.channels.AddOrUpdate(newChannel.Key, channel, (x, y) => channel); - return channel.Open; - } - catch (Exception ex) - { - logger.LogError(ex,$"Reconnection abnormal:After the server is disconnected {context.Channel.RemoteAddress.ToString()}"); - return false; - } - }); - } - - public override void ChannelRead(IChannelHandlerContext context, object message) - { - if(logger.IsEnabled(LogLevel.Debug)) - logger.LogDebug($"The server returns a message {message.ToString()}"); - } - - public override void ExceptionCaught(IChannelHandlerContext context, Exception exception) - { - logger.LogError(exception, context.Channel.RemoteAddress.ToString()); - context.CloseAsync(); - } - } -} diff --git a/src/JT808.DotNetty/Handlers/JT808UDPServerHandler.cs b/src/JT808.DotNetty/Handlers/JT808UDPServerHandler.cs deleted file mode 100644 index 8cfcd38..0000000 --- a/src/JT808.DotNetty/Handlers/JT808UDPServerHandler.cs +++ /dev/null @@ -1,88 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Transport.Channels; -using JT808.Protocol; -using System; -using System.Collections.Generic; -using System.Text; -using JT808.DotNetty.Metadata; -using JT808.DotNetty.Internal; -using JT808.DotNetty.Interfaces; -using Microsoft.Extensions.Logging; -using DotNetty.Transport.Channels.Sockets; - -namespace JT808.DotNetty.Handlers -{ - /// - /// JT808 UDP服务端处理程序 - /// - internal class JT808UDPServerHandler : SimpleChannelInboundHandler - { - private readonly JT808MsgIdHandlerBase handler; - - private readonly JT808SessionManager jT808SessionManager; - - private readonly IJT808SourcePackageDispatcher jT808SourcePackageDispatcher; - - private readonly JT808AtomicCounterService jT808AtomicCounterService; - - private readonly ILogger logger; - - public JT808UDPServerHandler( - ILoggerFactory loggerFactory, - IJT808SourcePackageDispatcher jT808SourcePackageDispatcher, - JT808MsgIdHandlerBase handler, - JT808AtomicCounterService jT808AtomicCounterService, - JT808SessionManager jT808SessionManager) - { - this.handler = handler; - this.jT808SessionManager = jT808SessionManager; - this.jT808SourcePackageDispatcher = jT808SourcePackageDispatcher; - this.jT808AtomicCounterService = jT808AtomicCounterService; - logger = loggerFactory.CreateLogger(); - } - - protected override void ChannelRead0(IChannelHandlerContext ctx, JT808UDPPackage msg) - { - try - { - jT808SourcePackageDispatcher?.SendAsync(msg.Buffer); - //解析到头部,然后根据具体的消息Id通过队列去进行消费 - //要是一定要解析到数据体可以在JT808MsgIdHandlerBase类中根据具体的消息, - //解析具体的消息体,具体调用JT808Serializer.Deserialize - JT808HeaderPackage jT808HeaderPackage = JT808Serializer.Deserialize(msg.Buffer); - jT808AtomicCounterService.MsgSuccessIncrement(); - if (logger.IsEnabled(LogLevel.Debug)) - { - logger.LogDebug("accept package success count<<<" + jT808AtomicCounterService.MsgSuccessCount.ToString()); - } - Func handlerFunc; - if (handler.HandlerDict.TryGetValue(jT808HeaderPackage.Header.MsgId, out handlerFunc)) - { - JT808Response jT808Response = handlerFunc(new JT808Request(jT808HeaderPackage, msg.Buffer)); - if (jT808Response != null) - { - ctx.WriteAndFlushAsync(new DatagramPacket(Unpooled.WrappedBuffer(JT808Serializer.Serialize(jT808Response.Package, jT808Response.MinBufferSize)), msg.Sender)); - } - } - } - catch (JT808.Protocol.Exceptions.JT808Exception ex) - { - jT808AtomicCounterService.MsgFailIncrement(); - if (logger.IsEnabled(LogLevel.Error)) - { - logger.LogError("accept package fail count<<<" + jT808AtomicCounterService.MsgFailCount.ToString()); - logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg.Buffer)); - } - } - catch (Exception ex) - { - jT808AtomicCounterService.MsgFailIncrement(); - if (logger.IsEnabled(LogLevel.Error)) - { - logger.LogError("accept package fail count<<<" + jT808AtomicCounterService.MsgFailCount.ToString()); - logger.LogError(ex, "accept msg<<<" + ByteBufferUtil.HexDump(msg.Buffer)); - } - } - } - } -} diff --git a/src/JT808.DotNetty/Handlers/JT808WebAPIServerHandler.cs b/src/JT808.DotNetty/Handlers/JT808WebAPIServerHandler.cs deleted file mode 100644 index 79e0de2..0000000 --- a/src/JT808.DotNetty/Handlers/JT808WebAPIServerHandler.cs +++ /dev/null @@ -1,82 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Codecs.Http; -using DotNetty.Common.Utilities; -using DotNetty.Transport.Channels; -using JT808.DotNetty.Internal; -using JT808.DotNetty.Metadata; -using Microsoft.Extensions.Logging; -using System; -using System.Text; - -namespace JT808.DotNetty.Handlers -{ - /// - /// jt808 webapi服务 - /// 请求量不大,只支持JSON格式并且只支持post发数据 - /// ref: dotnetty HttpServer - /// - internal class JT808WebAPIServerHandler : SimpleChannelInboundHandler - { - private static readonly AsciiString TypeJson = AsciiString.Cached("application/json"); - private static readonly AsciiString ServerName = AsciiString.Cached("JT808WebAPINetty"); - private static readonly AsciiString ContentTypeEntity = HttpHeaderNames.ContentType; - private static readonly AsciiString DateEntity = HttpHeaderNames.Date; - private static readonly AsciiString ContentLengthEntity = HttpHeaderNames.ContentLength; - private static readonly AsciiString ServerEntity = HttpHeaderNames.Server; - private readonly JT808WebAPIService jT808WebAPIService; - private readonly ILogger logger; - - public JT808WebAPIServerHandler( - JT808WebAPIService jT808WebAPIService, - ILoggerFactory loggerFactory) - { - this.jT808WebAPIService = jT808WebAPIService; - logger = loggerFactory.CreateLogger(); - } - - protected override void ChannelRead0(IChannelHandlerContext ctx, IFullHttpRequest msg) - { - if (logger.IsEnabled(LogLevel.Debug)) - { - logger.LogDebug($"Uri:{msg.Uri}"); - logger.LogDebug($"Content:{msg.Content.ToString(Encoding.UTF8)}"); - } - JT808HttpResponse jT808HttpResponse = null; - if (jT808WebAPIService.HandlerDict.TryGetValue(msg.Uri,out var funcHandler)) - { - jT808HttpResponse = funcHandler( new JT808HttpRequest() { Json = msg.Content.ToString(Encoding.UTF8)}); - } - else - { - jT808HttpResponse = jT808WebAPIService.NotFoundHttpResponse(); - } - if (jT808HttpResponse != null) - { - WriteResponse(ctx, Unpooled.WrappedBuffer(jT808HttpResponse.Data), TypeJson, jT808HttpResponse.Data.Length); - } - } - - private void WriteResponse(IChannelHandlerContext ctx, IByteBuffer buf, ICharSequence contentType, int contentLength) - { - // Build the response object. - var response = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.OK, buf, false); - HttpHeaders headers = response.Headers; - headers.Set(ContentTypeEntity, contentType); - headers.Set(ServerEntity, ServerName); - headers.Set(DateEntity, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); - headers.Set(ContentLengthEntity, contentLength); - // Close the non-keep-alive connection after the write operation is done. - ctx.WriteAndFlushAsync(response); - ctx.CloseAsync(); - } - - public override void ExceptionCaught(IChannelHandlerContext context, Exception exception) - { - WriteResponse(context, Unpooled.WrappedBuffer(jT808WebAPIService.ErrorHttpResponse(exception).Data), TypeJson, jT808WebAPIService.ErrorHttpResponse(exception).Data.Length); - logger.LogError(exception, exception.Message); - context.CloseAsync(); - } - - public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush(); - } -} diff --git a/src/JT808.DotNetty/Interfaces/IJT808SessionPublishing.cs b/src/JT808.DotNetty/Interfaces/IJT808SessionPublishing.cs deleted file mode 100644 index 62cde9f..0000000 --- a/src/JT808.DotNetty/Interfaces/IJT808SessionPublishing.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; - -namespace JT808.DotNetty.Interfaces -{ - public interface IJT808SessionPublishing - { - Task PublishAsync(string topicName, string key, string value); - } -} diff --git a/src/JT808.DotNetty/Interfaces/IJT808SessionService.cs b/src/JT808.DotNetty/Interfaces/IJT808SessionService.cs deleted file mode 100644 index 2f86f74..0000000 --- a/src/JT808.DotNetty/Interfaces/IJT808SessionService.cs +++ /dev/null @@ -1,25 +0,0 @@ -using JT808.DotNetty.Dtos; -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Interfaces -{ - /// - /// JT808会话服务 - /// - internal interface IJT808SessionService - { - /// - /// 获取会话集合 - /// - /// - JT808ResultDto> GetAll(); - /// - /// 通过设备终端号移除对应会话 - /// - /// - /// - JT808ResultDto RemoveByTerminalPhoneNo(string terminalPhoneNo); - } -} diff --git a/src/JT808.DotNetty/Interfaces/IJT808SourcePackageDispatcher.cs b/src/JT808.DotNetty/Interfaces/IJT808SourcePackageDispatcher.cs deleted file mode 100644 index c183f2d..0000000 --- a/src/JT808.DotNetty/Interfaces/IJT808SourcePackageDispatcher.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; - -namespace JT808.DotNetty.Interfaces -{ - /// - /// 源包分发器 - /// 自定义源包分发器业务 - /// ConfigureServices: - /// services.Replace(new ServiceDescriptor(typeof(IJT808SourcePackageDispatcher),typeof(JT808SourcePackageDispatcherDefaultImpl),ServiceLifetime.Singleton)); - /// - public interface IJT808SourcePackageDispatcher - { - Task SendAsync(byte[] data); - } -} diff --git a/src/JT808.DotNetty/Interfaces/IJT808UnificationSendService.cs b/src/JT808.DotNetty/Interfaces/IJT808UnificationSendService.cs deleted file mode 100644 index b7434c3..0000000 --- a/src/JT808.DotNetty/Interfaces/IJT808UnificationSendService.cs +++ /dev/null @@ -1,15 +0,0 @@ -using JT808.DotNetty.Dtos; -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Interfaces -{ - /// - /// JT808统一下发命令 - /// - internal interface IJT808UnificationSendService - { - JT808ResultDto Send(string terminalPhoneNo, byte[] data); - } -} diff --git a/src/JT808.DotNetty/Internal/JT808AtomicCounterService.cs b/src/JT808.DotNetty/Internal/JT808AtomicCounterService.cs deleted file mode 100644 index 6cd3b62..0000000 --- a/src/JT808.DotNetty/Internal/JT808AtomicCounterService.cs +++ /dev/null @@ -1,48 +0,0 @@ -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 JT808AtomicCounterService() - { - - } - - 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/JT808MsgIdDefaultHandler.cs b/src/JT808.DotNetty/Internal/JT808MsgIdDefaultHandler.cs deleted file mode 100644 index 4c6d8fe..0000000 --- a/src/JT808.DotNetty/Internal/JT808MsgIdDefaultHandler.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Internal -{ - /// - /// 默认消息处理业务实现 - /// - internal class JT808MsgIdDefaultHandler : JT808MsgIdHandlerBase - { - public JT808MsgIdDefaultHandler(JT808SessionManager sessionManager) : base(sessionManager) - { - } - } -} diff --git a/src/JT808.DotNetty/Internal/JT808SessionPublishingEmptyImpl.cs b/src/JT808.DotNetty/Internal/JT808SessionPublishingEmptyImpl.cs deleted file mode 100644 index 75066e9..0000000 --- a/src/JT808.DotNetty/Internal/JT808SessionPublishingEmptyImpl.cs +++ /dev/null @@ -1,16 +0,0 @@ -using JT808.DotNetty.Interfaces; -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; - -namespace JT808.DotNetty.Internal -{ - internal class JT808SessionPublishingEmptyImpl : IJT808SessionPublishing - { - public Task PublishAsync(string topicName, string key, string value) - { - return Task.CompletedTask; - } - } -} diff --git a/src/JT808.DotNetty/Internal/JT808SessionPublishingRedisImpl.cs b/src/JT808.DotNetty/Internal/JT808SessionPublishingRedisImpl.cs deleted file mode 100644 index 8f492a5..0000000 --- a/src/JT808.DotNetty/Internal/JT808SessionPublishingRedisImpl.cs +++ /dev/null @@ -1,75 +0,0 @@ -using JT808.DotNetty.Interfaces; -using System; -using System.Threading.Tasks; -using StackExchange.Redis; -using Microsoft.Extensions.Options; -using JT808.DotNetty.Configurations; - -namespace JT808.DotNetty.Internal -{ - internal class JT808SessionPublishingRedisImpl : IJT808SessionPublishing,IDisposable - { - private IConnectionMultiplexer connectionMultiplexer; - - private IOptionsMonitor optionsMonitor; - - private string redisHost; - - private IDisposable optionsMonitorDisposable; - - public JT808SessionPublishingRedisImpl( - IOptionsMonitor optionsMonitor - ) - { - this.optionsMonitor = optionsMonitor; - redisHost = optionsMonitor.CurrentValue.RedisHost; - try - { - connectionMultiplexer = ConnectionMultiplexer.Connect(redisHost); - } - catch - { - - } - optionsMonitorDisposable= this.optionsMonitor.OnChange((config,str) => - { - if(config.RedisHost!= redisHost) - { - redisHost = config.RedisHost; - connectionMultiplexer.Close(); - try - { - connectionMultiplexer = ConnectionMultiplexer.Connect(redisHost); - } - catch - { - - } - } - }); - } - - public Task PublishAsync(string topicName, string key, string value) - { - if (connectionMultiplexer.IsConnected) - { - Subscriber?.PublishAsync(topicName, value); - } - return Task.CompletedTask; - } - - internal ISubscriber Subscriber - { - get - { - return connectionMultiplexer.GetSubscriber(); - } - } - - public void Dispose() - { - connectionMultiplexer.Close(); - optionsMonitorDisposable.Dispose(); - } - } -} diff --git a/src/JT808.DotNetty/Internal/JT808SessionServiceDefaultImpl.cs b/src/JT808.DotNetty/Internal/JT808SessionServiceDefaultImpl.cs deleted file mode 100644 index ee39163..0000000 --- a/src/JT808.DotNetty/Internal/JT808SessionServiceDefaultImpl.cs +++ /dev/null @@ -1,77 +0,0 @@ -using JT808.DotNetty.Dtos; -using JT808.DotNetty.Interfaces; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Runtime.CompilerServices; -using Microsoft.Extensions.Options; -using JT808.DotNetty.Configurations; - -namespace JT808.DotNetty.Internal -{ - internal class JT808SessionServiceDefaultImpl : IJT808SessionService - { - private readonly JT808SessionManager jT808SessionManager; - - public JT808SessionServiceDefaultImpl( - JT808SessionManager jT808SessionManager) - { - this.jT808SessionManager = jT808SessionManager; - } - - public JT808ResultDto> GetAll() - { - JT808ResultDto> resultDto = new JT808ResultDto>(); - try - { - resultDto.Data = jT808SessionManager.GetAll().Select(s => new JT808SessionInfoDto - { - LastActiveTime = s.LastActiveTime, - StartTime = s.StartTime, - TerminalPhoneNo = s.TerminalPhoneNo, - RemoteAddressIP = s.Channel.RemoteAddress.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/Internal/JT808SourcePackageChannelService.cs b/src/JT808.DotNetty/Internal/JT808SourcePackageChannelService.cs deleted file mode 100644 index 8dcd860..0000000 --- a/src/JT808.DotNetty/Internal/JT808SourcePackageChannelService.cs +++ /dev/null @@ -1,290 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Transport.Bootstrapping; -using DotNetty.Transport.Channels; -using DotNetty.Transport.Channels.Sockets; -using JT808.DotNetty.Configurations; -using JT808.DotNetty.Dtos; -using JT808.DotNetty.Handlers; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using Newtonsoft.Json; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; - -namespace JT808.DotNetty.Internal -{ - /// - /// 原包分发器通道服务 - /// - internal class JT808SourcePackageChannelService:IDisposable - { - private readonly IOptionsMonitor jT808ConfigurationOptionsMonitor; - - internal readonly ConcurrentDictionary channels; - - private readonly ILogger logger; - - private readonly MultithreadEventLoopGroup group; - - internal readonly ILoggerFactory LoggerFactory; - - internal readonly Bootstrap bootstrap; - - private IDisposable jT808ConfigurationOptionsMonitorDisposable; - - public JT808SourcePackageChannelService( - ILoggerFactory loggerFactory, - IOptionsMonitor jT808ConfigurationOptionsMonitor) - { - this.LoggerFactory = loggerFactory; - this.logger = loggerFactory.CreateLogger(); - this.channels = new ConcurrentDictionary(); - this.jT808ConfigurationOptionsMonitor = jT808ConfigurationOptionsMonitor; - this.group = new MultithreadEventLoopGroup(); - this.bootstrap = new Bootstrap(); - jT808ConfigurationOptionsMonitorDisposable = jT808ConfigurationOptionsMonitor.OnChange(options => - { - List chgRemoteServers = new List(); - if (jT808ConfigurationOptionsMonitor.CurrentValue.SourcePackageDispatcherClientConfigurations != null && jT808ConfigurationOptionsMonitor.CurrentValue.SourcePackageDispatcherClientConfigurations.Count > 0) - { - chgRemoteServers = options.SourcePackageDispatcherClientConfigurations; - } - DelRemoteServsers(chgRemoteServers); - AddRemoteServsers(chgRemoteServers); - }); - StartAsync(); - } - - /// - /// 下发数据 - /// - /// - /// - public Task SendAsync(byte[] data) - { - foreach (var item in channels) - { - try - { - if (item.Value.Open && item.Value.Active) - { - item.Value.WriteAndFlushAsync(Unpooled.WrappedBuffer(data)); - } - else - { - logger.LogInformation($"{item} link closed."); - } - } - catch (AggregateException ex) - { - logger.LogError(ex, $"{item} Send Data Error."); - } - catch (Exception ex) - { - logger.LogError(ex, $"{item} Send Data Error."); - } - } - return Task.CompletedTask; - } - - /// - /// 获取通道信息集合 - /// - /// - public JT808ResultDto> GetAll() - { - JT808ResultDto> jT808ResultDto = new JT808ResultDto>(); - jT808ResultDto.Data = new List(); - jT808ResultDto.Code = JT808ResultCode.Ok; - foreach (var item in channels) - { - JT808SourcePackageChannelInfoDto jT808SourcePackageChannelInfoDto = new JT808SourcePackageChannelInfoDto(); - jT808SourcePackageChannelInfoDto.Active = item.Value.Active; - jT808SourcePackageChannelInfoDto.Open = item.Value.Open; - jT808SourcePackageChannelInfoDto.Registered = item.Value.Registered; - jT808SourcePackageChannelInfoDto.LocalAddress = item.Value.LocalAddress.ToString().Replace("[::ffff:", "").Replace("]", ""); - jT808SourcePackageChannelInfoDto.RemoteAddress = item.Value.RemoteAddress.ToString().Replace("[::ffff:", "").Replace("]", ""); - jT808ResultDto.Data.Add(jT808SourcePackageChannelInfoDto); - } - return jT808ResultDto; - } - - /// - /// 添加地址 - /// - /// - public async Task> Add(JT808IPAddressDto jT808IPAddressDto) - { - JT808ResultDto jT808ResultDto = new JT808ResultDto(); - jT808ResultDto.Code= JT808ResultCode.Ok; - jT808ResultDto.Data = true; - if (!channels.ContainsKey(jT808IPAddressDto.EndPoint)) - { - try - { - IChannel clientChannel = await bootstrap.ConnectAsync(jT808IPAddressDto.EndPoint); - channels.TryAdd(jT808IPAddressDto.EndPoint, clientChannel); - } - catch (AggregateException ex) - { - jT808ResultDto.Data = false; - jT808ResultDto.Code = JT808ResultCode.Error; - jT808ResultDto.Message = JsonConvert.SerializeObject(ex); - } - catch (Exception ex) - { - jT808ResultDto.Data = false; - jT808ResultDto.Code= JT808ResultCode.Error; - jT808ResultDto.Message = JsonConvert.SerializeObject(ex); - } - } - return jT808ResultDto; - } - - /// - /// 删除地址 - /// - /// - public async Task> Remove(JT808IPAddressDto jT808IPAddressDto) - { - JT808ResultDto jT808ResultDto = new JT808ResultDto(); - jT808ResultDto.Code = JT808ResultCode.Ok; - jT808ResultDto.Data = true; - - if(jT808ConfigurationOptionsMonitor.CurrentValue.SourcePackageDispatcherClientConfigurations!=null && - jT808ConfigurationOptionsMonitor.CurrentValue.SourcePackageDispatcherClientConfigurations.Any(a=>a.EndPoint.ToString()== jT808IPAddressDto.EndPoint.ToString()) - ) - { - jT808ResultDto.Data = false; - jT808ResultDto.Message = "不能删除服务器配置的地址"; - } - else - { - if (channels.TryRemove(jT808IPAddressDto.EndPoint, out var channel)) - { - try - { - await channel.CloseAsync(); - } - catch (AggregateException ex) - { - jT808ResultDto.Data = false; - jT808ResultDto.Code = JT808ResultCode.Error; - jT808ResultDto.Message = JsonConvert.SerializeObject(ex); - } - catch (Exception ex) - { - jT808ResultDto.Data = false; - jT808ResultDto.Code = JT808ResultCode.Error; - jT808ResultDto.Message = JsonConvert.SerializeObject(ex); - } - } - } - return jT808ResultDto; - } - - private void StartAsync() - { - bootstrap - .Group(group) - .Channel() - .Option(ChannelOption.TcpNodelay, true) - .Handler(new ActionChannelInitializer(channel => - { - channel.Pipeline.AddLast(new JT808SourcePackageDispatcherHandler(this)); - })); - if (jT808ConfigurationOptionsMonitor.CurrentValue.SourcePackageDispatcherClientConfigurations != null && - jT808ConfigurationOptionsMonitor.CurrentValue.SourcePackageDispatcherClientConfigurations.Count > 0) - { - foreach (var item in jT808ConfigurationOptionsMonitor.CurrentValue.SourcePackageDispatcherClientConfigurations) - { - try - { - Task.Run(async () => - { - IChannel clientChannel = await bootstrap.ConnectAsync(item.EndPoint); - channels.TryAdd(item.EndPoint, clientChannel); - logger.LogInformation($"init remote link {item.EndPoint.ToString()}."); - }); - } - catch (Exception ex) - { - logger.LogError(ex, $"there is an exception in sending data {item}."); - } - } - } - } - - private Task StopAsync() - { - foreach (var channel in channels) - { - try - { - channel.Value.CloseAsync(); - } - catch - { - - } - } - group.ShutdownGracefullyAsync(jT808ConfigurationOptionsMonitor.CurrentValue.QuietPeriodTimeSpan, jT808ConfigurationOptionsMonitor.CurrentValue.ShutdownTimeoutTimeSpan); - return Task.CompletedTask; - } - - /// - /// 动态删除远程服务器 - /// - /// - private void DelRemoteServsers(List chgRemoteServers) - { - var delChannels = channels.Keys.Except(chgRemoteServers.Select(s => s.EndPoint)).ToList(); - foreach (var item in delChannels) - { - try - { - channels.TryRemove(item, out var channel); - channel.CloseAsync(); - } - catch - { - - } - } - } - - /// - /// 动态添加远程服务器 - /// - /// - /// - private async void AddRemoteServsers(List chgRemoteServers) - { - var addChannels = chgRemoteServers.Select(s => s.EndPoint).Except(channels.Keys).ToList(); - foreach (var item in addChannels) - { - try - { - IChannel clientChannel = await bootstrap.ConnectAsync(item); - channels.TryAdd(item, clientChannel); - logger.LogInformation($"link to the remote server after the change {item}."); - } - catch (Exception ex) - { - logger.LogError(ex, $"reconnect the remote server after the exception changes {item}."); - } - } - } - - public void Dispose() - { - jT808ConfigurationOptionsMonitorDisposable.Dispose(); - StopAsync(); - } - } -} diff --git a/src/JT808.DotNetty/Internal/JT808SourcePackageDispatcherDefaultImpl.cs b/src/JT808.DotNetty/Internal/JT808SourcePackageDispatcherDefaultImpl.cs deleted file mode 100644 index 2c106e5..0000000 --- a/src/JT808.DotNetty/Internal/JT808SourcePackageDispatcherDefaultImpl.cs +++ /dev/null @@ -1,39 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Transport.Bootstrapping; -using DotNetty.Transport.Channels; -using DotNetty.Transport.Channels.Sockets; -using JT808.DotNetty.Configurations; -using JT808.DotNetty.Handlers; -using JT808.DotNetty.Interfaces; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading; -using System.Threading.Tasks; - -namespace JT808.DotNetty.Internal -{ - /// - /// 原包分发器默认实现 - /// - internal class JT808SourcePackageDispatcherDefaultImpl : IJT808SourcePackageDispatcher - { - private readonly JT808SourcePackageChannelService jT808SourcePackageChannelService; - - public JT808SourcePackageDispatcherDefaultImpl(JT808SourcePackageChannelService jT808SourcePackageChannelService) - { - this.jT808SourcePackageChannelService = jT808SourcePackageChannelService; - } - - public async Task SendAsync(byte[] data) - { - await jT808SourcePackageChannelService.SendAsync(data); - } - } -} diff --git a/src/JT808.DotNetty/Internal/JT808TransmitAddressFilterService.cs b/src/JT808.DotNetty/Internal/JT808TransmitAddressFilterService.cs deleted file mode 100644 index 3395403..0000000 --- a/src/JT808.DotNetty/Internal/JT808TransmitAddressFilterService.cs +++ /dev/null @@ -1,88 +0,0 @@ -using JT808.DotNetty.Configurations; -using JT808.DotNetty.Dtos; -using Microsoft.Extensions.Options; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Net; - -namespace JT808.DotNetty.Internal -{ - /// - /// JT808转发地址过滤服务 - /// 按照808的消息,有些请求必须要应答,但是转发可以不需要有应答可以节省部分资源包括: - // 1.消息的序列化 - // 2.消息的下发 - // 都有一定的性能损耗,那么不需要判断写超时 IdleState.WriterIdle - // 就跟神兽貔貅一样。。。 - /// - public class JT808TransmitAddressFilterService : IDisposable - { - private readonly IOptionsMonitor jT808ConfigurationOptionsMonitor; - - private ConcurrentDictionary ForwardingRemoteAddresssDict; - - private IDisposable jT808ConfigurationOptionsMonitorDisposable; - - public JT808TransmitAddressFilterService( - IOptionsMonitor jT808ConfigurationOptionsMonitor) - { - this.jT808ConfigurationOptionsMonitor = jT808ConfigurationOptionsMonitor; - ForwardingRemoteAddresssDict = new ConcurrentDictionary(); - InitForwardingRemoteAddress(jT808ConfigurationOptionsMonitor.CurrentValue.ForwardingRemoteAddress); - //OnChange 源码多播委托 - jT808ConfigurationOptionsMonitorDisposable = this.jT808ConfigurationOptionsMonitor.OnChange(options => - { - InitForwardingRemoteAddress(options.ForwardingRemoteAddress); - }); - } - - private void InitForwardingRemoteAddress(List jT808ClientConfigurations) - { - if (jT808ClientConfigurations != null && jT808ClientConfigurations.Count > 0) - { - foreach (var item in jT808ClientConfigurations) - { - string host = item.EndPoint.ToString(); - ForwardingRemoteAddresssDict.TryAdd(host, 0); - } - } - } - - public bool ContainsKey(EndPoint endPoint) - { - return ForwardingRemoteAddresssDict.ContainsKey(endPoint.ToString()); - } - - public JT808ResultDto Add(JT808IPAddressDto jT808IPAddressDto) - { - string host = jT808IPAddressDto.EndPoint.ToString(); - return new JT808ResultDto() { Code = JT808ResultCode.Ok, Data = ForwardingRemoteAddresssDict.TryAdd(host,0) }; - } - - public JT808ResultDto Remove(JT808IPAddressDto jT808IPAddressDto) - { - string host = jT808IPAddressDto.EndPoint.ToString(); - if(jT808ConfigurationOptionsMonitor.CurrentValue.ForwardingRemoteAddress!=null && - jT808ConfigurationOptionsMonitor.CurrentValue.ForwardingRemoteAddress.Any(w=>w.EndPoint.ToString()== host)) - { - return new JT808ResultDto() { Code = JT808ResultCode.Ok, Data = false,Message="不能删除服务器配置的地址" }; - } - else - { - return new JT808ResultDto() { Code = JT808ResultCode.Ok, Data = ForwardingRemoteAddresssDict.TryRemove(host,out var temp) }; - } - } - - public JT808ResultDto> GetAll() - { - return new JT808ResultDto>(){ Code = JT808ResultCode.Ok, Data = ForwardingRemoteAddresssDict.Select(s=>s.Key).ToList() }; - } - - public void Dispose() - { - jT808ConfigurationOptionsMonitorDisposable.Dispose(); - } - } -} diff --git a/src/JT808.DotNetty/Internal/JT808UnificationSendServiceDefaultImpl.cs b/src/JT808.DotNetty/Internal/JT808UnificationSendServiceDefaultImpl.cs deleted file mode 100644 index f4e7958..0000000 --- a/src/JT808.DotNetty/Internal/JT808UnificationSendServiceDefaultImpl.cs +++ /dev/null @@ -1,56 +0,0 @@ -using DotNetty.Buffers; -using JT808.DotNetty.Dtos; -using JT808.DotNetty.Interfaces; -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Internal -{ - internal class JT808UnificationSendServiceDefaultImpl : IJT808UnificationSendService - { - private readonly JT808SessionManager jT808SessionManager; - - public JT808UnificationSendServiceDefaultImpl(JT808SessionManager jT808SessionManager) - { - this.jT808SessionManager = jT808SessionManager; - } - - public JT808ResultDto Send(string terminalPhoneNo, byte[] data) - { - JT808ResultDto resultDto = new JT808ResultDto(); - try - { - var session = jT808SessionManager.GetSession(terminalPhoneNo); - if (session != null) - { - if (session.Channel.Open) - { - session.Channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(data)); - resultDto.Code = JT808ResultCode.Ok; - resultDto.Data = true; - } - else - { - resultDto.Code = JT808ResultCode.Ok; - resultDto.Data = false; - resultDto.Message = "offline"; - } - } - 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/Internal/JT808WebAPIService.cs b/src/JT808.DotNetty/Internal/JT808WebAPIService.cs deleted file mode 100644 index 6fcda9d..0000000 --- a/src/JT808.DotNetty/Internal/JT808WebAPIService.cs +++ /dev/null @@ -1,253 +0,0 @@ -using JT808.DotNetty.Dtos; -using JT808.DotNetty.Interfaces; -using JT808.DotNetty.Metadata; -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Internal -{ - /// - /// JT808 WebApi 业务服务 - /// - internal class JT808WebAPIService - { - public Dictionary> HandlerDict { get; protected set; } - - private const string RouteTablePrefix = "/jt808api"; - - private const string sessionRoutePrefix = "Session"; - - private const string sourcePackagePrefix = "SourcePackage"; - - private const string transmitPrefix = "Transmit"; - - private readonly IJT808SessionService jT808SessionService; - - private readonly IJT808UnificationSendService jT808UnificationSendService; - - private readonly JT808AtomicCounterService jT808AtomicCounterService; - - private readonly JT808SourcePackageChannelService jT808SourcePackageChannelService; - - private readonly JT808TransmitAddressFilterService jT808TransmitAddressFilterService; - - /// - /// 初始化消息处理业务 - /// - public JT808WebAPIService( - JT808AtomicCounterService jT808AtomicCounterService, - JT808SourcePackageChannelService jT808SourcePackageChannelService, - JT808TransmitAddressFilterService jT808TransmitAddressFilterService, - IJT808SessionService jT808SessionService, - IJT808UnificationSendService jT808UnificationSendService) - { - this.jT808AtomicCounterService = jT808AtomicCounterService; - this.jT808SourcePackageChannelService = jT808SourcePackageChannelService; - this.jT808TransmitAddressFilterService = jT808TransmitAddressFilterService; - this.jT808SessionService = jT808SessionService; - this.jT808UnificationSendService = jT808UnificationSendService; - HandlerDict = new Dictionary> - { - {$"{RouteTablePrefix}/UnificationSend", UnificationSend}, - {$"{RouteTablePrefix}/{sessionRoutePrefix}/GetAll", GetSessionAll}, - {$"{RouteTablePrefix}/{sessionRoutePrefix}/RemoveByTerminalPhoneNo", RemoveByTerminalPhoneNo}, - {$"{RouteTablePrefix}/GetAtomicCounter", GetAtomicCounter}, - {$"{RouteTablePrefix}/{sourcePackagePrefix}/Add", AddSourcePackageAddress}, - {$"{RouteTablePrefix}/{sourcePackagePrefix}/Remove", RemoveSourcePackageAddress}, - {$"{RouteTablePrefix}/{sourcePackagePrefix}/GetAll", GetSourcePackageAll}, - {$"{RouteTablePrefix}/{transmitPrefix}/Add", AddTransmitAddress}, - {$"{RouteTablePrefix}/{transmitPrefix}/Remove", RemoveTransmitAddress}, - {$"{RouteTablePrefix}/{transmitPrefix}/GetAll", GetTransmitAll}, - }; - } - - /// - /// 统一下发信息 - /// - /// - /// - public JT808HttpResponse UnificationSend(JT808HttpRequest request) - { - if (string.IsNullOrEmpty(request.Json)) - { - return EmptyHttpResponse(); - } - JT808UnificationSendRequestDto jT808UnificationSendRequestDto = JsonConvert.DeserializeObject(request.Json); - var result = jT808UnificationSendService.Send(jT808UnificationSendRequestDto.TerminalPhoneNo, jT808UnificationSendRequestDto.Data); - return CreateJT808HttpResponse(result); - } - - /// - /// 会话服务集合 - /// - /// - /// - public JT808HttpResponse GetSessionAll(JT808HttpRequest request) - { - var result = jT808SessionService.GetAll(); - return CreateJT808HttpResponse(result); - } - - /// - /// 会话服务-通过设备终端号移除对应会话 - /// - /// - /// - public JT808HttpResponse RemoveByTerminalPhoneNo(JT808HttpRequest request) - { - if (string.IsNullOrEmpty(request.Json)) - { - return EmptyHttpResponse(); - } - var result = jT808SessionService.RemoveByTerminalPhoneNo(request.Json); - return CreateJT808HttpResponse(result); - } - - /// - /// 获取包计数器 - /// - /// - /// - public JT808HttpResponse GetAtomicCounter(JT808HttpRequest request) - { - JT808AtomicCounterDto jT808AtomicCounterDto = new JT808AtomicCounterDto(); - jT808AtomicCounterDto.MsgFailCount = jT808AtomicCounterService.MsgFailCount; - jT808AtomicCounterDto.MsgSuccessCount = jT808AtomicCounterService.MsgSuccessCount; - return CreateJT808HttpResponse(new JT808ResultDto - { - Code=JT808ResultCode.Ok, - Data= jT808AtomicCounterDto - }); - } - - /// - /// 添加原包转发地址 - /// - /// - /// - public JT808HttpResponse AddSourcePackageAddress(JT808HttpRequest request) - { - if (string.IsNullOrEmpty(request.Json)) - { - return EmptyHttpResponse(); - } - JT808IPAddressDto jT808IPAddressDto = JsonConvert.DeserializeObject(request.Json); - return CreateJT808HttpResponse(jT808SourcePackageChannelService.Add(jT808IPAddressDto).Result); - } - - /// - /// 删除原包转发地址(不能删除在网关服务器配置文件配的地址) - /// - /// - /// - public JT808HttpResponse RemoveSourcePackageAddress(JT808HttpRequest request) - { - if (string.IsNullOrEmpty(request.Json)) - { - return EmptyHttpResponse(); - } - JT808IPAddressDto jT808IPAddressDto = JsonConvert.DeserializeObject(request.Json); - return CreateJT808HttpResponse(jT808SourcePackageChannelService.Remove(jT808IPAddressDto).Result); - } - - /// - /// 获取原包信息集合 - /// - /// - /// - public JT808HttpResponse GetSourcePackageAll(JT808HttpRequest request) - { - return CreateJT808HttpResponse(jT808SourcePackageChannelService.GetAll()); - } - - /// - /// 添加转发过滤地址 - /// - /// - /// - public JT808HttpResponse AddTransmitAddress(JT808HttpRequest request) - { - if (string.IsNullOrEmpty(request.Json)) - { - return EmptyHttpResponse(); - } - JT808IPAddressDto jT808IPAddressDto = JsonConvert.DeserializeObject(request.Json); - return CreateJT808HttpResponse(jT808TransmitAddressFilterService.Add(jT808IPAddressDto)); - } - - /// - /// 删除转发过滤地址(不能删除在网关服务器配置文件配的地址) - /// - /// - /// - public JT808HttpResponse RemoveTransmitAddress(JT808HttpRequest request) - { - if (string.IsNullOrEmpty(request.Json)) - { - return EmptyHttpResponse(); - } - JT808IPAddressDto jT808IPAddressDto = JsonConvert.DeserializeObject(request.Json); - return CreateJT808HttpResponse(jT808TransmitAddressFilterService.Remove(jT808IPAddressDto)); - } - - /// - /// 获取转发过滤地址信息集合 - /// - /// - /// - public JT808HttpResponse GetTransmitAll(JT808HttpRequest request) - { - return CreateJT808HttpResponse(jT808TransmitAddressFilterService.GetAll()); - } - - private JT808HttpResponse CreateJT808HttpResponse(dynamic dynamicObject) - { - byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(dynamicObject)); - return new JT808HttpResponse() - { - Data = data - }; - } - - public JT808HttpResponse DefaultHttpResponse() - { - byte[] json = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new JT808DefaultResultDto())); - return new JT808HttpResponse(json); - } - - public JT808HttpResponse EmptyHttpResponse() - { - byte[] json = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new JT808ResultDto() - { - Code = JT808ResultCode.Empty, - Message ="内容为空", - Data="Content Empty" - })); - return new JT808HttpResponse(json); - } - - public JT808HttpResponse NotFoundHttpResponse() - { - byte[] json = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new JT808ResultDto() - { - Code= JT808ResultCode.NotFound, - Message="没有该服务", - Data= "没有该服务" - })); - return new JT808HttpResponse(json); - } - - public JT808HttpResponse ErrorHttpResponse(Exception ex) - { - byte[] json = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new JT808ResultDto() - { - Code = JT808ResultCode.Error, - Message = JsonConvert.SerializeObject(ex), - Data= ex.Message - })); - return new JT808HttpResponse(json); - } - } -} diff --git a/src/JT808.DotNetty/JT808.DotNetty.csproj b/src/JT808.DotNetty/JT808.DotNetty.csproj deleted file mode 100644 index 3531ddd..0000000 --- a/src/JT808.DotNetty/JT808.DotNetty.csproj +++ /dev/null @@ -1,46 +0,0 @@ - - - - netstandard2.0 - latest - Copyright 2018. - SmallChi - JT808DotNetty - JT808DotNetty - 基于DotNetty封装的JT808DotNetty专注消息业务处理 - 基于DotNetty封装的JT808DotNetty专注消息业务处理 - false - https://github.com/SmallChi/JT808DotNetty - https://github.com/SmallChi/JT808DotNetty - https://github.com/SmallChi/JT808DotNetty/blob/master/LICENSE - true - 1.0.0 - - - - - - - - E:\koike\My Project\JT808DotNetty\src\JT808.DotNetty\JT808.DotNetty.xml - - - - - - - - - - - - - - - - - - - - - diff --git a/src/JT808.DotNetty/JT808.DotNetty.xml b/src/JT808.DotNetty/JT808.DotNetty.xml deleted file mode 100644 index 59a88d3..0000000 --- a/src/JT808.DotNetty/JT808.DotNetty.xml +++ /dev/null @@ -1,418 +0,0 @@ - - - - JT808.DotNetty - - - - - JT808客户端解码(测试客户端) - - - - - JT808解码 - - - - - WebApi服务 - 默认828端口 - - - - - 源包分发器配置 - - - - - - 包计数器服务 - - - - - 通道Id - - - - - 最后上线时间 - - - - - 上线时间 - - - - - 终端手机号 - - - - - 本地ip地址 - - - - - WebApi端口号 - - - - - 远程ip地址 - - - - - 原包通道信息 - - - - - 远程地址 - - - - - 本地地址 - - - - - 是否注册 - - - - - 是否活动 - - - - - 是否打开 - - - - - 统一下发请求参数 - - - - - 通道激活 - - - - - - 设备主动断开 - - - - - - 服务器主动断开 - - - - - - - 超时策略 - - - - - - - jt808 webapi服务 - 请求量不大,只支持JSON格式并且只支持post发数据 - ref: dotnetty HttpServer - - - - - JT808会话服务 - - - - - 获取会话集合 - - - - - - 通过通道Id移除对应会话 - - - - - - - 通过设备终端号移除对应会话 - - - - - - - 源包分发器 - 自定义源包分发器业务 - ConfigureServices: - services.Replace(new ServiceDescriptor(typeof(IJT808SourcePackageDispatcher),typeof(JT808SourcePackageDispatcherDefaultImpl),ServiceLifetime.Singleton)); - - - - - JT808统一下发命令 - - - - - 计数包服务 - - - - - 默认消息处理业务实现 - - - - - - 原包分发器通道服务 - - - - - 下发数据 - - - - - - - 获取通道信息集合 - - - - - - 添加地址 - - - - - - 删除地址 - - - - - - 动态删除远程服务器 - - - - - - 动态添加远程服务器 - - - - - - - 原包分发器默认实现 - - - - - 初始化消息处理业务 - - - - - 统一下发信息 - - - - - - - 会话服务集合 - - - - - - - 会话服务-通过通道Id移除对应会话 - - - - - - - 会话服务-通过设备终端号移除对应会话 - - - - - - - 获取包计数器 - - - - - - - 添加原包转发地址 - - - - - - - 删除原包转发地址(不能删除在网关服务器配置文件配的地址) - - - - - - - 获取原包信息集合 - - - - - - - 抽象消息处理业务 - 自定义消息处理业务 - ConfigureServices: - services.Replace(new ServiceDescriptor(typeof(JT808MsgIdHandlerBase),typeof(JT808MsgIdCustomHandlerImpl),ServiceLifetime.Singleton)); - - - - - 初始化消息处理业务 - - - - - 终端通用应答 - - - - - - - - 终端心跳 - - - - - - - - 终端注销 - - - - - - - - 终端注册 - - - - - - - - 终端鉴权 - - - - - - - - 位置信息汇报 - - - - - - - - 定位数据批量上传 - - - - - - - - 数据上行透传 - - - - - - - - Netty生成的sessionID和Session的对应关系 - key = seession id - value = Session - - - - - 终端手机号和netty生成的sessionID的对应关系 - key = 终端手机号 - value = seession id - - - - - 获取实际连接数 - - - - - 获取设备相关连的连接数 - - - - - 集成一个webapi服务 - - - - - - - - - - - 根据实际情况适当调整包的大小 - - - - - 终端手机号 - - - - diff --git a/src/JT808.DotNetty/JT808Constants.cs b/src/JT808.DotNetty/JT808Constants.cs deleted file mode 100644 index 309041c..0000000 --- a/src/JT808.DotNetty/JT808Constants.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty -{ - public static class JT808Constants - { - public const string SessionOnline= "JT808SessionOnline"; - - public const string SessionOffline = "JT808SessionOffline"; - } -} diff --git a/src/JT808.DotNetty/JT808DotnettyExtensions.cs b/src/JT808.DotNetty/JT808DotnettyExtensions.cs deleted file mode 100644 index a62a177..0000000 --- a/src/JT808.DotNetty/JT808DotnettyExtensions.cs +++ /dev/null @@ -1,58 +0,0 @@ -using JT808.DotNetty.Codecs; -using JT808.DotNetty.Configurations; -using JT808.DotNetty.Handlers; -using JT808.DotNetty.Interfaces; -using JT808.DotNetty.Internal; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Hosting; -using Newtonsoft.Json; -using System; -using System.Reflection; -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("JT808.DotNetty.Test")] -namespace JT808.DotNetty -{ - public static class JT808DotnettyExtensions - { - static JT808DotnettyExtensions() - { - JsonSerializerSettings setting = new JsonSerializerSettings(); - JsonConvert.DefaultSettings = new Func(() => - { - //日期类型默认格式化处理 - setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; - setting.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat; - setting.DateFormatString = "yyyy-MM-dd HH:mm:ss"; - setting.NullValueHandling = NullValueHandling.Include; - return setting; - }); - } - - public static IHostBuilder UseJT808Host(this IHostBuilder builder) - { - return builder.ConfigureServices((hostContext, services) => - { - services.Configure(hostContext.Configuration.GetSection("JT808Configuration")); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddScoped(); - services.TryAddScoped(); - services.TryAddScoped(); - services.TryAddScoped(); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddScoped(); - services.AddHostedService(); - services.AddHostedService(); - }); - } - } -} \ No newline at end of file diff --git a/src/JT808.DotNetty/JT808MsgIdHandlerBase.cs b/src/JT808.DotNetty/JT808MsgIdHandlerBase.cs deleted file mode 100644 index 332698b..0000000 --- a/src/JT808.DotNetty/JT808MsgIdHandlerBase.cs +++ /dev/null @@ -1,160 +0,0 @@ -using System; -using System.Collections.Generic; -using JT808.DotNetty.Metadata; -using JT808.Protocol.Enums; -using JT808.Protocol.Extensions; -using JT808.Protocol.MessageBody; - -namespace JT808.DotNetty -{ - /// - /// 抽象消息处理业务 - /// 自定义消息处理业务 - /// 注意: - /// 1.ConfigureServices: - /// services.Replace(new ServiceDescriptor(typeof(JT808MsgIdHandlerBase),typeof(JT808MsgIdCustomHandlerImpl),ServiceLifetime.Singleton)); - /// 2.解析具体的消息体,具体消息调用具体的JT808Serializer.Deserialize - /// - public abstract class JT808MsgIdHandlerBase - { - protected JT808SessionManager sessionManager { get; } - /// - /// 初始化消息处理业务 - /// - protected JT808MsgIdHandlerBase(JT808SessionManager sessionManager) - { - this.sessionManager = sessionManager; - HandlerDict = new Dictionary> - { - {JT808MsgId.终端通用应答.ToUInt16Value(), Msg0x0001}, - {JT808MsgId.终端鉴权.ToUInt16Value(), Msg0x0102}, - {JT808MsgId.终端心跳.ToUInt16Value(), Msg0x0002}, - {JT808MsgId.终端注销.ToUInt16Value(), Msg0x0003}, - {JT808MsgId.终端注册.ToUInt16Value(), Msg0x0100}, - {JT808MsgId.位置信息汇报.ToUInt16Value(),Msg0x0200 }, - {JT808MsgId.定位数据批量上传.ToUInt16Value(),Msg0x0704 }, - {JT808MsgId.数据上行透传.ToUInt16Value(),Msg0x0900 } - }; - } - - public Dictionary> HandlerDict { get; protected set; } - /// - /// 终端通用应答 - /// 平台无需回复 - /// 实现自己的业务 - /// - /// - /// - /// - public virtual JT808Response Msg0x0001(JT808Request request) - { - return null; - } - /// - /// 终端心跳 - /// - /// - /// - /// - public virtual JT808Response Msg0x0002(JT808Request request) - { - sessionManager.Heartbeat(request.Package.Header.TerminalPhoneNo); - return new JT808Response(JT808MsgId.平台通用应答.Create(request.Package.Header.TerminalPhoneNo, new JT808_0x8001() - { - MsgId = request.Package.Header.MsgId, - JT808PlatformResult = JT808PlatformResult.成功, - MsgNum = request.Package.Header.MsgNum - })); - } - /// - /// 终端注销 - /// - /// - /// - /// - public virtual JT808Response Msg0x0003(JT808Request request) - { - return new JT808Response(JT808MsgId.平台通用应答.Create(request.Package.Header.TerminalPhoneNo, new JT808_0x8001() - { - MsgId = request.Package.Header.MsgId, - JT808PlatformResult = JT808PlatformResult.成功, - MsgNum = request.Package.Header.MsgNum - })); - } - /// - /// 终端注册 - /// - /// - /// - /// - public virtual JT808Response Msg0x0100(JT808Request request) - { - return new JT808Response(JT808MsgId.终端注册应答.Create(request.Package.Header.TerminalPhoneNo, new JT808_0x8100() - { - Code = "J" + request.Package.Header.TerminalPhoneNo, - JT808TerminalRegisterResult = JT808TerminalRegisterResult.成功, - MsgNum = request.Package.Header.MsgNum - })); - } - /// - /// 终端鉴权 - /// - /// - /// - /// - public virtual JT808Response Msg0x0102(JT808Request request) - { - return new JT808Response(JT808MsgId.平台通用应答.Create(request.Package.Header.TerminalPhoneNo, new JT808_0x8001() - { - MsgId = request.Package.Header.MsgId, - JT808PlatformResult = JT808PlatformResult.成功, - MsgNum = request.Package.Header.MsgNum - })); - } - /// - /// 位置信息汇报 - /// - /// - /// - /// - public virtual JT808Response Msg0x0200(JT808Request request) - { - return new JT808Response(JT808MsgId.平台通用应答.Create(request.Package.Header.TerminalPhoneNo, new JT808_0x8001() - { - MsgId = request.Package.Header.MsgId, - JT808PlatformResult = JT808PlatformResult.成功, - MsgNum = request.Package.Header.MsgNum - })); - } - /// - /// 定位数据批量上传 - /// - /// - /// - /// - public virtual JT808Response Msg0x0704(JT808Request request) - { - return new JT808Response(JT808MsgId.平台通用应答.Create(request.Package.Header.TerminalPhoneNo, new JT808_0x8001() - { - MsgId =request.Package.Header.MsgId, - JT808PlatformResult = JT808PlatformResult.成功, - MsgNum = request.Package.Header.MsgNum - })); - } - /// - /// 数据上行透传 - /// - /// - /// - /// - public virtual JT808Response Msg0x0900(JT808Request request) - { - return new JT808Response(JT808MsgId.平台通用应答.Create(request.Package.Header.TerminalPhoneNo, new JT808_0x8001() - { - MsgId =request.Package.Header.MsgId, - JT808PlatformResult = JT808PlatformResult.成功, - MsgNum = request.Package.Header.MsgNum - })); - } - } -} diff --git a/src/JT808.DotNetty/JT808ServerHost.cs b/src/JT808.DotNetty/JT808ServerHost.cs deleted file mode 100644 index b93ec05..0000000 --- a/src/JT808.DotNetty/JT808ServerHost.cs +++ /dev/null @@ -1,96 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Codecs; -using DotNetty.Handlers.Timeout; -using DotNetty.Transport.Bootstrapping; -using DotNetty.Transport.Channels; -using DotNetty.Transport.Libuv; -using JT808.DotNetty.Codecs; -using JT808.DotNetty.Configurations; -using JT808.DotNetty.Handlers; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System; -using System.Collections.Generic; -using System.Net; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading; -using System.Threading.Tasks; - -namespace JT808.DotNetty -{ - /// - /// JT808 网关服务 - /// - internal class JT808ServerHost : IHostedService - { - private readonly IServiceProvider serviceProvider; - private readonly JT808Configuration configuration; - private readonly ILogger logger; - private DispatcherEventLoopGroup bossGroup; - private WorkerEventLoopGroup workerGroup; - private IChannel bootstrapChannel; - private IByteBufferAllocator serverBufferAllocator; - - public JT808ServerHost( - IServiceProvider provider, - ILoggerFactory loggerFactory, - IOptions jT808ConfigurationAccessor) - { - serviceProvider = provider; - configuration = jT808ConfigurationAccessor.Value; - logger=loggerFactory.CreateLogger(); - } - - public Task StartAsync(CancellationToken cancellationToken) - { - bossGroup = new DispatcherEventLoopGroup(); - workerGroup = new WorkerEventLoopGroup(bossGroup, configuration.EventLoopCount); - serverBufferAllocator = new PooledByteBufferAllocator(); - ServerBootstrap bootstrap = new ServerBootstrap(); - bootstrap.Group(bossGroup, workerGroup); - bootstrap.Channel(); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) - || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - bootstrap - .Option(ChannelOption.SoReuseport, true) - .ChildOption(ChannelOption.SoReuseaddr, true); - } - bootstrap - .Option(ChannelOption.SoBacklog, configuration.SoBacklog) - .ChildOption(ChannelOption.Allocator, serverBufferAllocator) - .ChildHandler(new ActionChannelInitializer(channel => - { - IChannelPipeline pipeline = channel.Pipeline; - using (var scope = serviceProvider.CreateScope()) - { - channel.Pipeline.AddLast("systemIdleState", new IdleStateHandler( - configuration.ReaderIdleTimeSeconds, - configuration.WriterIdleTimeSeconds, - configuration.AllIdleTimeSeconds)); - channel.Pipeline.AddLast("jt808Connection", scope.ServiceProvider.GetRequiredService()); - channel.Pipeline.AddLast("jt808Buffer", new DelimiterBasedFrameDecoder(int.MaxValue, - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.BeginFlag }), - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.EndFlag }))); - channel.Pipeline.AddLast("jt808Decode", scope.ServiceProvider.GetRequiredService()); - channel.Pipeline.AddLast("jt808Service", scope.ServiceProvider.GetRequiredService()); - } - })); - logger.LogInformation($"Server start at {IPAddress.Any}:{configuration.Port}."); - return bootstrap.BindAsync(configuration.Port) - .ContinueWith(i => bootstrapChannel = i.Result); - } - - public async Task StopAsync(CancellationToken cancellationToken) - { - await bootstrapChannel.CloseAsync(); - var quietPeriod = configuration.QuietPeriodTimeSpan; - var shutdownTimeout = configuration.ShutdownTimeoutTimeSpan; - await workerGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout); - await bossGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout); - } - } -} diff --git a/src/JT808.DotNetty/JT808SessionManager.cs b/src/JT808.DotNetty/JT808SessionManager.cs deleted file mode 100644 index 4dcf996..0000000 --- a/src/JT808.DotNetty/JT808SessionManager.cs +++ /dev/null @@ -1,141 +0,0 @@ -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using JT808.DotNetty.Metadata; -using DotNetty.Transport.Channels; -using JT808.DotNetty.Interfaces; - -namespace JT808.DotNetty -{ - /// - /// JT808会话管理 - /// - public class JT808SessionManager - { - private readonly ILogger logger; - - private readonly IJT808SessionPublishing jT808SessionPublishing; - - public JT808SessionManager( - 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 JT808Session GetSession(string terminalPhoneNo) - { - if (string.IsNullOrEmpty(terminalPhoneNo)) - return default; - if (SessionIdDict.TryGetValue(terminalPhoneNo, out JT808Session targetSession)) - { - return targetSession; - } - else - { - return default; - } - } - - public void Heartbeat(string terminalPhoneNo) - { - if (string.IsNullOrEmpty(terminalPhoneNo)) return; - if (SessionIdDict.TryGetValue(terminalPhoneNo, out JT808Session oldjT808Session)) - { - oldjT808Session.LastActiveTime = DateTime.Now; - SessionIdDict.TryUpdate(terminalPhoneNo, oldjT808Session, oldjT808Session); - } - } - - public void TryAdd(JT808Session appSession) - { - // 解决了设备号跟通道绑定到一起,不需要用到通道本身的SessionId - // 不管设备下发更改了设备终端号,只要是没有在内存中就当是新的 - // 存在的问题: - // 1.原先老的如何销毁 - // 2.这时候用的通道是相同的,设备终端是不同的 - // 当设备主动或者服务器断开以后,可以释放,这点内存忽略不计,况且更改设备号不是很频繁。 - if (SessionIdDict.TryAdd(appSession.TerminalPhoneNo, appSession)) - { - //使用场景: - //部标的超长待机设备,不会像正常的设备一样一直连着,可能10几分钟连上了,然后发完就关闭连接, - //这时候想下发数据需要知道设备什么时候上线,在这边做通知最好不过了。 - //todo: 有设备关联上来可以进行通知 例如:使用Redis发布订阅 - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOnline,null, appSession.TerminalPhoneNo); - } - } - - public JT808Session RemoveSession(string terminalPhoneNo) - { - //todo: 设备离线可以进行通知 - //todo: 使用Redis 发布订阅 - if (string.IsNullOrEmpty(terminalPhoneNo)) return default; - if (!SessionIdDict.TryGetValue(terminalPhoneNo, out JT808Session 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 JT808Session jT808SessionRemove); - } - string nos = string.Join(",", terminalPhoneNos); - logger.LogInformation($">>>{terminalPhoneNo}-{nos} 1-n Session Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, null, nos); - return jT808Session; - } - else - { - if (SessionIdDict.TryRemove(terminalPhoneNo, out JT808Session jT808SessionRemove)) - { - logger.LogInformation($">>>{terminalPhoneNo} Session Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, null, terminalPhoneNo); - return jT808SessionRemove; - } - else - { - return default; - } - } - } - - internal void RemoveSessionByChannel(IChannel channel) - { - //todo: 设备离线可以进行通知 - //todo: 使用Redis 发布订阅 - var terminalPhoneNos = SessionIdDict.Where(w => w.Value.Channel.Id == channel.Id).Select(s => s.Key).ToList(); - foreach (var key in terminalPhoneNos) - { - SessionIdDict.TryRemove(key, out JT808Session jT808SessionRemove); - } - string nos = string.Join(",", terminalPhoneNos); - logger.LogInformation($">>>{nos} Channel Remove."); - jT808SessionPublishing.PublishAsync(JT808Constants.SessionOffline, null, nos); - } - - public IEnumerable GetAll() - { - return SessionIdDict.Select(s => s.Value).ToList(); - } - } -} - diff --git a/src/JT808.DotNetty/JT808SimpleTcpClient.cs b/src/JT808.DotNetty/JT808SimpleTcpClient.cs deleted file mode 100644 index 5ca8d96..0000000 --- a/src/JT808.DotNetty/JT808SimpleTcpClient.cs +++ /dev/null @@ -1,77 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Codecs; -using DotNetty.Transport.Bootstrapping; -using DotNetty.Transport.Channels; -using DotNetty.Transport.Channels.Sockets; -using DotNetty.Transport.Libuv; -using JT808.DotNetty.Codecs; -using JT808.DotNetty.Configurations; -using JT808.DotNetty.Handlers; -using System; -using System.Collections.Generic; -using System.Net; -using System.Text; -using System.Threading.Tasks; - -namespace JT808.DotNetty -{ - public class JT808SimpleTcpClient - { - private Bootstrap cb; - - private MultithreadEventLoopGroup clientGroup; - - private IChannel clientChannel; - - private IByteBufferAllocator clientBufferAllocator; - - public JT808SimpleTcpClient(EndPoint remoteAddress) - { - clientBufferAllocator = new PooledByteBufferAllocator(); - clientGroup = new MultithreadEventLoopGroup(1); - cb = new Bootstrap() - .Group(clientGroup) - .Channel() - .Option(ChannelOption.TcpNodelay, true) - .Option(ChannelOption.Allocator, clientBufferAllocator) - .Handler(new ActionChannelInitializer(channel => - { - channel.Pipeline.AddLast("jt808Buffer", new DelimiterBasedFrameDecoder(int.MaxValue, - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.BeginFlag }), - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.EndFlag }))); - channel.Pipeline.AddLast("jt808Decode", new JT808ClientDecoder()); - })); - clientChannel = cb.ConnectAsync(remoteAddress).Result; - } - - public JT808SimpleTcpClient(EndPoint remoteAddress, EndPoint localAddress) - { - clientBufferAllocator = new PooledByteBufferAllocator(); - clientGroup = new MultithreadEventLoopGroup(1); - cb = new Bootstrap() - .Group(clientGroup) - .Channel() - .Option(ChannelOption.TcpNodelay, true) - .Option(ChannelOption.Allocator, clientBufferAllocator) - .Handler(new ActionChannelInitializer(channel => - { - channel.Pipeline.AddLast("jt808Buffer", new DelimiterBasedFrameDecoder(int.MaxValue, - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.BeginFlag }), - Unpooled.CopiedBuffer(new byte[] { JT808.Protocol.JT808Package.EndFlag }))); - channel.Pipeline.AddLast("jt808Decode", new JT808ClientDecoder()); - })); - clientChannel = cb.ConnectAsync(remoteAddress, localAddress).Result; - } - - public void WriteAsync(byte[] data) - { - clientChannel.WriteAndFlushAsync(Unpooled.WrappedBuffer(data)); - } - - public void Down() - { - this.clientChannel?.CloseAsync().Wait(); - Task.WaitAll(this.clientGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1))); - } - } -} diff --git a/src/JT808.DotNetty/JT808UDPServerHost.cs b/src/JT808.DotNetty/JT808UDPServerHost.cs deleted file mode 100644 index 95e5a58..0000000 --- a/src/JT808.DotNetty/JT808UDPServerHost.cs +++ /dev/null @@ -1,82 +0,0 @@ -using DotNetty.Buffers; -using DotNetty.Codecs; -using DotNetty.Handlers.Timeout; -using DotNetty.Transport.Bootstrapping; -using DotNetty.Transport.Channels; -using DotNetty.Transport.Channels.Sockets; -using DotNetty.Transport.Libuv; -using JT808.DotNetty.Codecs; -using JT808.DotNetty.Configurations; -using JT808.DotNetty.Handlers; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System; -using System.Collections.Generic; -using System.Net; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading; -using System.Threading.Tasks; - -namespace JT808.DotNetty -{ - /// - /// JT808 Udp网关服务 - /// - internal class JT808UdpServerHost : IHostedService - { - private readonly IServiceProvider serviceProvider; - private readonly JT808Configuration configuration; - private readonly ILogger logger; - private MultithreadEventLoopGroup group; - private IChannel bootstrapChannel; - - public JT808UdpServerHost( - IServiceProvider provider, - ILoggerFactory loggerFactory, - IOptions jT808ConfigurationAccessor) - { - serviceProvider = provider; - configuration = jT808ConfigurationAccessor.Value; - logger=loggerFactory.CreateLogger(); - } - - public Task StartAsync(CancellationToken cancellationToken) - { - group = new MultithreadEventLoopGroup(); - Bootstrap bootstrap = new Bootstrap(); - bootstrap.Group(group); - bootstrap.Channel(); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) - || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - bootstrap - .Option(ChannelOption.SoReuseport, true); - } - bootstrap - .Option(ChannelOption.SoBacklog, configuration.SoBacklog) - .Handler(new ActionChannelInitializer(channel => - { - IChannelPipeline pipeline = channel.Pipeline; - using (var scope = serviceProvider.CreateScope()) - { - pipeline.AddLast(new JT808UDPDecoder()); - pipeline.AddLast("jt808UDPService", scope.ServiceProvider.GetRequiredService()); - } - })); - logger.LogInformation($"Udp Server start at {IPAddress.Any}:{configuration.UDPPort}."); - return bootstrap.BindAsync(configuration.UDPPort) - .ContinueWith(i => bootstrapChannel = i.Result); - } - - public async Task StopAsync(CancellationToken cancellationToken) - { - await bootstrapChannel.CloseAsync(); - var quietPeriod = configuration.QuietPeriodTimeSpan; - var shutdownTimeout = configuration.ShutdownTimeoutTimeSpan; - await group.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout); - } - } -} diff --git a/src/JT808.DotNetty/JT808WebAPIServerHost.cs b/src/JT808.DotNetty/JT808WebAPIServerHost.cs deleted file mode 100644 index c5b1cb1..0000000 --- a/src/JT808.DotNetty/JT808WebAPIServerHost.cs +++ /dev/null @@ -1,82 +0,0 @@ -using DotNetty.Codecs.Http; -using DotNetty.Transport.Bootstrapping; -using DotNetty.Transport.Channels; -using DotNetty.Transport.Libuv; -using JT808.DotNetty.Configurations; -using JT808.DotNetty.Handlers; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System; -using System.Net; -using System.Runtime.InteropServices; -using System.Threading; -using System.Threading.Tasks; - -namespace JT808.DotNetty -{ - /// - /// 集成一个webapi服务 - /// - internal class JT808WebAPIServerHost : IHostedService - { - private readonly IServiceProvider serviceProvider; - private readonly JT808Configuration configuration; - private readonly ILogger logger; - private DispatcherEventLoopGroup bossGroup; - private WorkerEventLoopGroup workerGroup; - private IChannel bootstrapChannel; - - public JT808WebAPIServerHost( - IServiceProvider provider, - ILoggerFactory loggerFactory, - IOptions jT808ConfigurationAccessor) - { - serviceProvider = provider; - configuration = jT808ConfigurationAccessor.Value; - logger = loggerFactory.CreateLogger(); - } - - public Task StartAsync(CancellationToken cancellationToken) - { - bossGroup = new DispatcherEventLoopGroup(); - workerGroup = new WorkerEventLoopGroup(bossGroup, 1); - ServerBootstrap bootstrap = new ServerBootstrap(); - bootstrap.Group(bossGroup, workerGroup); - bootstrap.Channel(); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) - || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - bootstrap - .Option(ChannelOption.SoReuseport, true) - .ChildOption(ChannelOption.SoReuseaddr, true); - } - bootstrap - .Option(ChannelOption.SoBacklog, 8192) - .ChildHandler(new ActionChannelInitializer(channel => - { - IChannelPipeline pipeline = channel.Pipeline; - using (var scope = serviceProvider.CreateScope()) - { - pipeline.AddLast("http_encoder", new HttpResponseEncoder()); - pipeline.AddLast("http_decoder", new HttpRequestDecoder(4096, 8192, 8192, false)); - //将多个消息转换为单一的request或者response对象 =>IFullHttpRequest - pipeline.AddLast("http_aggregator", new HttpObjectAggregator(65536)); - pipeline.AddLast("http_jt808webapihandler", scope.ServiceProvider.GetRequiredService()); - } - })); - logger.LogInformation($"WebAPI Server start at {IPAddress.Any}:{configuration.WebApiPort}."); - return bootstrap.BindAsync(configuration.WebApiPort).ContinueWith(i => bootstrapChannel = i.Result); - } - - public async Task StopAsync(CancellationToken cancellationToken) - { - await bootstrapChannel.CloseAsync(); - var quietPeriod = configuration.QuietPeriodTimeSpan; - var shutdownTimeout = configuration.ShutdownTimeoutTimeSpan; - await workerGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout); - await bossGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout); - } - } -} diff --git a/src/JT808.DotNetty/Metadata/JT808AtomicCounter.cs b/src/JT808.DotNetty/Metadata/JT808AtomicCounter.cs deleted file mode 100644 index 1f9b442..0000000 --- a/src/JT808.DotNetty/Metadata/JT808AtomicCounter.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading; - -namespace JT808.DotNetty.Metadata -{ - /// - /// - /// - /// - internal class JT808AtomicCounter - { - long counter = 0; - - public JT808AtomicCounter(long initialCount = 0) - { - this.counter = initialCount; - } - - public long Increment() - { - return Interlocked.Increment(ref counter); - } - - public long Add(long len) - { - return Interlocked.Add(ref counter,len); - } - - public long Decrement() - { - return Interlocked.Decrement(ref counter); - } - - public long Count - { - get - { - return Interlocked.Read(ref counter); - } - } - } -} diff --git a/src/JT808.DotNetty/Metadata/JT808HttpRequest.cs b/src/JT808.DotNetty/Metadata/JT808HttpRequest.cs deleted file mode 100644 index 6a16264..0000000 --- a/src/JT808.DotNetty/Metadata/JT808HttpRequest.cs +++ /dev/null @@ -1,22 +0,0 @@ -using JT808.Protocol; -using System; -using System.Collections.Generic; -using System.Reflection; - -namespace JT808.DotNetty.Metadata -{ - public class JT808HttpRequest - { - public string Json { get; set; } - - public JT808HttpRequest() - { - - } - - public JT808HttpRequest(string json) - { - Json = json; - } - } -} \ No newline at end of file diff --git a/src/JT808.DotNetty/Metadata/JT808HttpResponse.cs b/src/JT808.DotNetty/Metadata/JT808HttpResponse.cs deleted file mode 100644 index 4ab0b95..0000000 --- a/src/JT808.DotNetty/Metadata/JT808HttpResponse.cs +++ /dev/null @@ -1,22 +0,0 @@ -using JT808.Protocol; -using System; -using System.Collections.Generic; -using System.Reflection; - -namespace JT808.DotNetty.Metadata -{ - public class JT808HttpResponse - { - public byte[] Data { get; set; } - - public JT808HttpResponse() - { - - } - - public JT808HttpResponse(byte[] data) - { - this.Data = data; - } - } -} \ No newline at end of file diff --git a/src/JT808.DotNetty/Metadata/JT808Request.cs b/src/JT808.DotNetty/Metadata/JT808Request.cs deleted file mode 100644 index e914a7f..0000000 --- a/src/JT808.DotNetty/Metadata/JT808Request.cs +++ /dev/null @@ -1,23 +0,0 @@ -using JT808.Protocol; -using System; -using System.Collections.Generic; -using System.Reflection; - -namespace JT808.DotNetty.Metadata -{ - public class JT808Request - { - public JT808HeaderPackage Package { get; } - - /// - /// 用于消息发送 - /// - public byte[] OriginalPackage { get;} - - public JT808Request(JT808HeaderPackage package, byte[] originalPackage) - { - Package = package; - OriginalPackage = originalPackage; - } - } -} \ No newline at end of file diff --git a/src/JT808.DotNetty/Metadata/JT808Response.cs b/src/JT808.DotNetty/Metadata/JT808Response.cs deleted file mode 100644 index a40ef63..0000000 --- a/src/JT808.DotNetty/Metadata/JT808Response.cs +++ /dev/null @@ -1,27 +0,0 @@ -using JT808.Protocol; -using System; -using System.Collections.Generic; -using System.Reflection; - -namespace JT808.DotNetty.Metadata -{ - public class JT808Response - { - public JT808Package Package { get; set; } - /// - /// 根据实际情况适当调整包的大小 - /// - public int MinBufferSize { get; set; } - - public JT808Response() - { - - } - - public JT808Response(JT808Package package, int minBufferSize = 1024) - { - Package = package; - MinBufferSize = minBufferSize; - } - } -} \ No newline at end of file diff --git a/src/JT808.DotNetty/Metadata/JT808Session.cs b/src/JT808.DotNetty/Metadata/JT808Session.cs deleted file mode 100644 index a3a5a61..0000000 --- a/src/JT808.DotNetty/Metadata/JT808Session.cs +++ /dev/null @@ -1,31 +0,0 @@ -using DotNetty.Transport.Channels; -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT808.DotNetty.Metadata -{ - public class JT808Session - { - public JT808Session(IChannel channel, string terminalPhoneNo) - { - Channel = channel; - TerminalPhoneNo = terminalPhoneNo; - StartTime = DateTime.Now; - LastActiveTime = DateTime.Now; - } - - public JT808Session() { } - - /// - /// 终端手机号 - /// - public string TerminalPhoneNo { get; set; } - - public IChannel Channel { get; set; } - - public DateTime LastActiveTime { get; set; } - - public DateTime StartTime { get; set; } - } -} diff --git a/src/JT808.DotNetty/Metadata/JT808UDPPackage.cs b/src/JT808.DotNetty/Metadata/JT808UDPPackage.cs deleted file mode 100644 index 530a57c..0000000 --- a/src/JT808.DotNetty/Metadata/JT808UDPPackage.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net; -using System.Text; - -namespace JT808.DotNetty.Metadata -{ - internal class JT808UDPPackage - { - public JT808UDPPackage(byte[] buffer, EndPoint sender) - { - Buffer = buffer; - Sender = sender; - } - - public byte[] Buffer { get; } - - public EndPoint Sender { get; } - } -}