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.
 
 
 

59 line
2.0 KiB

  1. using JT808.Gateway.Configurations;
  2. using JT808.Gateway.Session;
  3. using Microsoft.Extensions.Hosting;
  4. using Microsoft.Extensions.Logging;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace JT808.Gateway.Services
  11. {
  12. internal class JT808TcpReceiveTimeoutHostedService : BackgroundService
  13. {
  14. private readonly ILogger Logger;
  15. private readonly JT808SessionManager SessionManager;
  16. private readonly JT808Configuration Configuration;
  17. public JT808TcpReceiveTimeoutHostedService(
  18. JT808Configuration jT808Configuration,
  19. ILoggerFactory loggerFactory,
  20. JT808SessionManager jT808SessionManager
  21. )
  22. {
  23. SessionManager = jT808SessionManager;
  24. Logger = loggerFactory.CreateLogger("JT808TcpReceiveTimeout");
  25. Configuration = jT808Configuration;
  26. }
  27. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  28. {
  29. while (!stoppingToken.IsCancellationRequested)
  30. {
  31. try
  32. {
  33. foreach (var item in SessionManager.GetTcpAll())
  34. {
  35. if (item.ActiveTime.AddSeconds(Configuration.TcpReaderIdleTimeSeconds) < DateTime.Now)
  36. {
  37. item.ReceiveTimeout.Cancel();
  38. }
  39. }
  40. Logger.LogInformation($"[Check Receive Timeout]");
  41. Logger.LogInformation($"[Session Online Count]:{SessionManager.TcpSessionCount}");
  42. }
  43. catch (Exception ex)
  44. {
  45. Logger.LogError(ex, $"[Receive Timeout]");
  46. }
  47. finally
  48. {
  49. await Task.Delay(TimeSpan.FromSeconds(Configuration.TcpReceiveTimeoutCheckTimeSeconds), stoppingToken);
  50. }
  51. }
  52. }
  53. }
  54. }