您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

83 行
3.7 KiB

  1. using DotNetty.Codecs.Http;
  2. using DotNetty.Transport.Bootstrapping;
  3. using DotNetty.Transport.Channels;
  4. using DotNetty.Transport.Libuv;
  5. using JT809.DotNetty.Core.Configurations;
  6. using JT809.DotNetty.Core.Handlers;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.Hosting;
  9. using Microsoft.Extensions.Logging;
  10. using Microsoft.Extensions.Options;
  11. using System;
  12. using System.Net;
  13. using System.Runtime.InteropServices;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace JT808.DotNetty.WebApi
  17. {
  18. /// <summary>
  19. /// JT809 集成一个webapi服务
  20. /// </summary>
  21. internal class JT809MainWebAPIServerHost : IHostedService
  22. {
  23. private readonly IServiceProvider serviceProvider;
  24. private readonly ILogger<JT809MainWebAPIServerHost> logger;
  25. private DispatcherEventLoopGroup bossGroup;
  26. private WorkerEventLoopGroup workerGroup;
  27. private IChannel bootstrapChannel;
  28. private readonly JT809SuperiorPlatformOptions configuration;
  29. public JT809MainWebAPIServerHost(
  30. IServiceProvider provider,
  31. IOptions<JT809SuperiorPlatformOptions> jT809SuperiorPlatformOptionsAccessor,
  32. ILoggerFactory loggerFactory)
  33. {
  34. serviceProvider = provider;
  35. configuration = jT809SuperiorPlatformOptionsAccessor.Value;
  36. logger = loggerFactory.CreateLogger<JT809MainWebAPIServerHost>();
  37. }
  38. public Task StartAsync(CancellationToken cancellationToken)
  39. {
  40. bossGroup = new DispatcherEventLoopGroup();
  41. workerGroup = new WorkerEventLoopGroup(bossGroup, 1);
  42. ServerBootstrap bootstrap = new ServerBootstrap();
  43. bootstrap.Group(bossGroup, workerGroup);
  44. bootstrap.Channel<TcpServerChannel>();
  45. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
  46. || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  47. {
  48. bootstrap
  49. .Option(ChannelOption.SoReuseport, true)
  50. .ChildOption(ChannelOption.SoReuseaddr, true);
  51. }
  52. bootstrap
  53. .Option(ChannelOption.SoBacklog, 8192)
  54. .ChildHandler(new ActionChannelInitializer<IChannel>(channel =>
  55. {
  56. IChannelPipeline pipeline = channel.Pipeline;
  57. using (var scope = serviceProvider.CreateScope())
  58. {
  59. pipeline.AddLast("http_encoder", new HttpResponseEncoder());
  60. pipeline.AddLast("http_decoder", new HttpRequestDecoder(4096, 8192, 8192, false));
  61. //将多个消息转换为单一的request或者response对象 =>IFullHttpRequest
  62. pipeline.AddLast("http_aggregator", new HttpObjectAggregator(65536));
  63. pipeline.AddLast("http_jt809webapihandler", scope.ServiceProvider.GetRequiredService<JT809SuperiorWebAPIServerHandler>());
  64. }
  65. }));
  66. logger.LogInformation($"JT809 Superior WebAPI Server start at {IPAddress.Any}:{configuration.WebApiPort}.");
  67. return bootstrap.BindAsync(configuration.WebApiPort).ContinueWith(i => bootstrapChannel = i.Result);
  68. }
  69. public async Task StopAsync(CancellationToken cancellationToken)
  70. {
  71. await bootstrapChannel.CloseAsync();
  72. var quietPeriod = configuration.QuietPeriodTimeSpan;
  73. var shutdownTimeout = configuration.ShutdownTimeoutTimeSpan;
  74. await workerGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout);
  75. await bossGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout);
  76. }
  77. }
  78. }