Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 9 měsíci
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using DotNetty.Codecs;
  2. using DotNetty.Transport.Bootstrapping;
  3. using DotNetty.Transport.Channels;
  4. using DotNetty.Transport.Channels.Sockets;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Hosting;
  8. using NearCardAttendance.Common;
  9. using NearCardAttendance.Common.helper;
  10. using NearCardAttendance.Service.TcpServer.Handler;
  11. using NearCardAttendance.Service.TcpServer.Mapper;
  12. using NearCardAttendance.TcpServer.Config;
  13. using Serilog;
  14. using System.Text;
  15. namespace NearCardAttendance.TcpServer
  16. {
  17. internal class Program
  18. {
  19. static void Main(string[] args)
  20. {
  21. var config = new ConfigurationBuilder()
  22. .SetBasePath(Directory.GetCurrentDirectory())
  23. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
  24. .Build();
  25. Log.Logger = new LoggerConfiguration()
  26. .ReadFrom.Configuration(config).Enrich.WithThreadInfo()
  27. .Filter.ByExcluding(logEvent => logEvent.Level == Serilog.Events.LogEventLevel.Verbose) // 过滤掉VRB级别的日志
  28. .CreateLogger();
  29. try
  30. {
  31. Log.Information("Starting up");
  32. CreateHostBuilder(args).Build().Run();
  33. }
  34. catch (Exception ex)
  35. {
  36. Log.Fatal(ex, "Application start-up failed");
  37. }
  38. finally
  39. {
  40. Log.CloseAndFlush();
  41. }
  42. }
  43. public static IHostBuilder CreateHostBuilder(string[] args) =>
  44. Host.CreateDefaultBuilder(args)
  45. .UseSerilog()
  46. .ConfigureServices((hostContext, services) => {
  47. var configuration = hostContext.Configuration;
  48. #region 配置信息
  49. //services
  50. // .Configure<ServiceConfig>(configuration.GetSection("ServiceConfig"))
  51. // ;
  52. #endregion
  53. #region Http请求
  54. services
  55. .AddSingleton<HttpHelper>()
  56. .AddHttpClient(Consts.DEFAULT_HTTPCLIENT_NAME, c =>
  57. {
  58. c.Timeout = TimeSpan.FromSeconds(10); //超时限制
  59. c.DefaultRequestHeaders.Add("Accept", "application/json");
  60. })
  61. ;
  62. #endregion
  63. #region TcpService
  64. services
  65. .AddSingleton<ServerBootstrap>(provider =>
  66. {
  67. var bossGroup = new MultithreadEventLoopGroup();
  68. var workerGroup = new MultithreadEventLoopGroup();
  69. var bootstrap = new ServerBootstrap();
  70. bootstrap.Group(bossGroup, workerGroup)
  71. .Channel<TcpServerSocketChannel>()
  72. .Option(ChannelOption.SoBacklog, 100)
  73. .ChildOption(ChannelOption.TcpNodelay, true) // 低延迟
  74. .ChildHandler(new ActionChannelInitializer<IChannel>(channel =>
  75. {
  76. // var handler = provider.GetRequiredService<ProtocolHandler>();
  77. // var handler = provider.GetRequiredService<SomeServerHandler>();
  78. var pipeline = channel.Pipeline;
  79. pipeline.AddLast(new StringEncoder(Encoding.ASCII));
  80. pipeline.AddLast(
  81. provider.GetRequiredService<ProtocolHandler>(),
  82. provider.GetRequiredService<RegisterHandler>()
  83. //provider.GetRequiredService<HeartBeatHandler>(),
  84. //provider.GetRequiredService<PassThroughHandler>()
  85. ); // Add the injected handler
  86. }));
  87. return bootstrap;
  88. })
  89. .AddSingleton<TcpClientsManager>()
  90. .AddSingleton<ScheduleResendManager>()
  91. .AddTransient<ProtocolHandler>()
  92. .AddTransient<RegisterHandler>()
  93. //.AddTransient<HeartBeatHandler>()
  94. //.AddTransient<PassThroughHandler>()
  95. .AddHostedService<Server>()
  96. ;
  97. #endregion
  98. });
  99. }
  100. }