You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.6KB

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