25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

100 satır
3.6 KiB

  1. using DotNetty.Buffers;
  2. using DotNetty.Codecs;
  3. using DotNetty.Handlers.Timeout;
  4. using DotNetty.Transport.Bootstrapping;
  5. using DotNetty.Transport.Channels;
  6. using DotNetty.Transport.Channels.Sockets;
  7. using DotNetty.Transport.Libuv;
  8. using JT809Netty.Core.Configs;
  9. using JT809Netty.Core.Handlers;
  10. using Microsoft.Extensions.DependencyInjection;
  11. using Microsoft.Extensions.Hosting;
  12. using Microsoft.Extensions.Options;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Net;
  16. using System.Text;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. namespace JT809Netty.Core
  20. {
  21. /// <summary>
  22. /// 下级平台主链路
  23. /// </summary
  24. public class JT809DownMasterLinkNettyService : IHostedService
  25. {
  26. IEventLoopGroup workerGroup;
  27. Bootstrap bootstrap;
  28. readonly IServiceProvider serviceProvider;
  29. readonly IOptionsMonitor<JT809NettyOptions> nettyOptions;
  30. public JT809DownMasterLinkNettyService(
  31. IOptionsMonitor<JT809NettyOptions> nettyOptionsAccessor,
  32. IServiceProvider serviceProvider)
  33. {
  34. nettyOptions = nettyOptionsAccessor;
  35. this.serviceProvider = serviceProvider;
  36. }
  37. public Task StartAsync(CancellationToken cancellationToken)
  38. {
  39. Task.Run(async () =>
  40. {
  41. try
  42. {
  43. workerGroup = new MultithreadEventLoopGroup();
  44. bootstrap = new Bootstrap();
  45. bootstrap.Group(workerGroup)
  46. .Channel<TcpSocketChannel>()
  47. .Handler(new ActionChannelInitializer<IChannel>(channel =>
  48. {
  49. InitChannel(channel);
  50. }))
  51. .Option(ChannelOption.SoBacklog, 1048576);
  52. IChannel clientChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(nettyOptions.CurrentValue.Host), nettyOptions.CurrentValue.Port));
  53. }
  54. catch (Exception ex)
  55. {
  56. }
  57. });
  58. return Task.CompletedTask;
  59. }
  60. public Task StopAsync(CancellationToken cancellationToken)
  61. {
  62. try
  63. {
  64. Task.WhenAll(workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)));
  65. }
  66. catch (Exception ex)
  67. {
  68. }
  69. return Task.CompletedTask;
  70. }
  71. private void InitChannel(IChannel channel)
  72. {
  73. var scope = serviceProvider.CreateScope();
  74. try
  75. {
  76. //下级平台应每 1min 发送一个主链路保持清求数据包到上级平台以保持链路连接
  77. channel.Pipeline.AddLast("systemIdleState", new WriteTimeoutHandler(60));
  78. channel.Pipeline.AddLast("jt809DownMasterLinkConnection", scope.ServiceProvider.GetRequiredService<JT809DownMasterLinkConnectionHandler>());
  79. channel.Pipeline.AddLast("jt809Buffer", new DelimiterBasedFrameDecoder(int.MaxValue, Unpooled.CopiedBuffer(new byte[] { JT809.Protocol.JT809Package.BEGINFLAG }), Unpooled.CopiedBuffer(new byte[] { JT809.Protocol.JT809Package.ENDFLAG })));
  80. channel.Pipeline.AddLast("jt809Decode", scope.ServiceProvider.GetRequiredService<JT809DecodeHandler>());
  81. channel.Pipeline.AddLast("jT809DownMasterLinkServiceHandler", scope.ServiceProvider.GetRequiredService<JT809DownMasterLinkServiceHandler>());
  82. }
  83. finally
  84. {
  85. scope.Dispose();
  86. }
  87. }
  88. }
  89. }