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.

124 lines
5.2 KiB

  1. using JT808.Gateway.CleintBenchmark.Configs;
  2. using JT808.Gateway.Client;
  3. using JT808.Protocol.Enums;
  4. using JT808.Protocol.Extensions;
  5. using JT808.Protocol.MessageBody;
  6. using Microsoft.Extensions.Hosting;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using System;
  10. using System.Collections.Concurrent;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace JT808.Gateway.CleintBenchmark.Services
  16. {
  17. public class CleintBenchmarkHostedService : IHostedService
  18. {
  19. private readonly ClientBenchmarkOptions clientBenchmarkOptions;
  20. private readonly ILogger logger;
  21. private readonly IJT808TcpClientFactory jT808TcpClientFactory;
  22. private ConcurrentQueue<string> failDeviceNoQueue;
  23. public CleintBenchmarkHostedService(
  24. ILoggerFactory loggerFactory,
  25. IJT808TcpClientFactory jT808TcpClientFactory,
  26. IOptions<ClientBenchmarkOptions> clientBenchmarkOptionsAccessor)
  27. {
  28. this.jT808TcpClientFactory = jT808TcpClientFactory;
  29. clientBenchmarkOptions = clientBenchmarkOptionsAccessor.Value;
  30. logger = loggerFactory.CreateLogger("CleintBenchmarkHostedService");
  31. }
  32. public Task StartAsync(CancellationToken cancellationToken)
  33. {
  34. logger.LogInformation("StartAsync...");
  35. ThreadPool.GetMinThreads(out var minWorkerThreads, out var minCompletionPortThreads);
  36. ThreadPool.GetMaxThreads(out var maxWorkerThreads, out var maxCompletionPortThreads);
  37. logger.LogInformation($"GetMinThreads:{minWorkerThreads}-{minCompletionPortThreads}");
  38. logger.LogInformation($"GetMaxThreads:{maxWorkerThreads}-{maxCompletionPortThreads}");
  39. //先建立连接
  40. failDeviceNoQueue = new ConcurrentQueue<string>();
  41. for (int i=0;i< clientBenchmarkOptions.DeviceCount; i++)
  42. {
  43. string deviceNo = (i + 1 + clientBenchmarkOptions.DeviceTemplate).ToString();
  44. var client = jT808TcpClientFactory.Create(new JT808DeviceConfig(deviceNo,
  45. clientBenchmarkOptions.IP,
  46. clientBenchmarkOptions.Port), cancellationToken);
  47. if (client == null)
  48. {
  49. failDeviceNoQueue.Enqueue(deviceNo);
  50. }
  51. }
  52. int successCount = clientBenchmarkOptions.DeviceCount - failDeviceNoQueue.Count;
  53. logger.LogInformation($"总连接数:{clientBenchmarkOptions.DeviceCount}");
  54. logger.LogInformation($"已建立连接数:{successCount}");
  55. logger.LogInformation($"失败连接数:{failDeviceNoQueue.Count}");
  56. Task.Run(() => {
  57. while (!cancellationToken.IsCancellationRequested)
  58. {
  59. if(failDeviceNoQueue.TryDequeue(out string deviceNo))
  60. {
  61. logger.LogInformation($"尝试重连{deviceNo}...");
  62. var client = jT808TcpClientFactory.Create(new JT808DeviceConfig(deviceNo,
  63. clientBenchmarkOptions.IP,
  64. clientBenchmarkOptions.Port), cancellationToken);
  65. if (client == null)
  66. {
  67. failDeviceNoQueue.Enqueue(deviceNo);
  68. }
  69. Thread.Sleep(1000);
  70. }
  71. else
  72. {
  73. Thread.Sleep(3000);
  74. }
  75. }
  76. }, cancellationToken);
  77. ThreadPool.QueueUserWorkItem((state) =>
  78. {
  79. while (!cancellationToken.IsCancellationRequested)
  80. {
  81. Parallel.ForEach(jT808TcpClientFactory.GetAll(), new ParallelOptions { MaxDegreeOfParallelism = 100 }, (item) =>
  82. {
  83. try
  84. {
  85. int lat = new Random(1000).Next(100000, 180000);
  86. int Lng = new Random(1000).Next(100000, 180000);
  87. item.Send(JT808MsgId.位置信息汇报.Create(item.DeviceConfig.TerminalPhoneNo, new JT808_0x0200()
  88. {
  89. Lat = lat,
  90. Lng = Lng,
  91. GPSTime = DateTime.Now,
  92. Speed = 50,
  93. Direction = 30,
  94. AlarmFlag = 5,
  95. Altitude = 50,
  96. StatusFlag = 10
  97. }));
  98. }
  99. catch (Exception ex)
  100. {
  101. logger.LogError(ex.Message);
  102. }
  103. });
  104. Thread.Sleep(clientBenchmarkOptions.Interval);
  105. }
  106. });
  107. return Task.CompletedTask;
  108. }
  109. public Task StopAsync(CancellationToken cancellationToken)
  110. {
  111. jT808TcpClientFactory.Dispose();
  112. logger.LogInformation("StopAsync...");
  113. return Task.CompletedTask;
  114. }
  115. }
  116. }