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.

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