Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

91 wiersze
3.6 KiB

  1. using JT808.DotNetty.CleintBenchmark.Configs;
  2. using JT808.DotNetty.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.Generic;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace JT808.DotNetty.CleintBenchmark.Services
  15. {
  16. public class CleintBenchmarkHostedService : IHostedService
  17. {
  18. private readonly ClientBenchmarkOptions clientBenchmarkOptions;
  19. private readonly ILogger logger;
  20. private readonly IJT808TcpClientFactory jT808TcpClientFactory;
  21. public CleintBenchmarkHostedService(
  22. ILoggerFactory loggerFactory,
  23. IJT808TcpClientFactory jT808TcpClientFactory,
  24. IOptions<ClientBenchmarkOptions> clientBenchmarkOptionsAccessor)
  25. {
  26. this.jT808TcpClientFactory = jT808TcpClientFactory;
  27. clientBenchmarkOptions = clientBenchmarkOptionsAccessor.Value;
  28. logger = loggerFactory.CreateLogger("CleintBenchmarkHostedService");
  29. }
  30. public Task StartAsync(CancellationToken cancellationToken)
  31. {
  32. logger.LogInformation("StartAsync...");
  33. ThreadPool.GetMinThreads(out var minWorkerThreads, out var minCompletionPortThreads);
  34. ThreadPool.GetMaxThreads(out var maxWorkerThreads, out var maxCompletionPortThreads);
  35. logger.LogInformation($"GetMinThreads:{minWorkerThreads}-{minCompletionPortThreads}");
  36. logger.LogInformation($"GetMaxThreads:{maxWorkerThreads}-{maxCompletionPortThreads}");
  37. //先建立连接
  38. for (int i=0;i< clientBenchmarkOptions.DeviceCount; i++)
  39. {
  40. var client = jT808TcpClientFactory.Create(new JT808DeviceConfig((i+1+ clientBenchmarkOptions.DeviceTemplate).ToString(),
  41. clientBenchmarkOptions.IP,
  42. clientBenchmarkOptions.Port));
  43. }
  44. ThreadPool.QueueUserWorkItem((state) =>
  45. {
  46. while (!cancellationToken.IsCancellationRequested)
  47. {
  48. Parallel.ForEach(jT808TcpClientFactory.GetAll(), new ParallelOptions { MaxDegreeOfParallelism = 100 }, (item) =>
  49. {
  50. try
  51. {
  52. int lat = new Random(1000).Next(100000, 180000);
  53. int Lng = new Random(1000).Next(100000, 180000);
  54. item.Send(JT808MsgId.位置信息汇报.Create(item.DeviceConfig.TerminalPhoneNo, new JT808_0x0200()
  55. {
  56. Lat = lat,
  57. Lng = Lng,
  58. GPSTime = DateTime.Now,
  59. Speed = 50,
  60. Direction = 30,
  61. AlarmFlag = 5,
  62. Altitude = 50,
  63. StatusFlag = 10
  64. }));
  65. }
  66. catch (Exception ex)
  67. {
  68. logger.LogError(ex, "");
  69. }
  70. });
  71. Thread.Sleep(clientBenchmarkOptions.Interval);
  72. }
  73. });
  74. return Task.CompletedTask;
  75. }
  76. public Task StopAsync(CancellationToken cancellationToken)
  77. {
  78. jT808TcpClientFactory.Dispose();
  79. logger.LogInformation("StopAsync...");
  80. return Task.CompletedTask;
  81. }
  82. }
  83. }