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.
 
 
 

63 lines
1.6 KiB

  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. namespace JT808.Gateway.Client
  8. {
  9. public interface IJT808TcpClientFactory : IDisposable
  10. {
  11. JT808TcpClient Create(DeviceConfig deviceConfig);
  12. List<JT808TcpClient> GetAll();
  13. }
  14. public class JT808TcpClientFactory: IJT808TcpClientFactory
  15. {
  16. private readonly ConcurrentDictionary<string, JT808TcpClient> dict;
  17. private readonly IServiceProvider serviceProvider;
  18. public JT808TcpClientFactory(IServiceProvider serviceProvider)
  19. {
  20. dict = new ConcurrentDictionary<string, JT808TcpClient>(StringComparer.OrdinalIgnoreCase);
  21. this.serviceProvider = serviceProvider;
  22. }
  23. public JT808TcpClient Create(DeviceConfig deviceConfig)
  24. {
  25. if(dict.TryGetValue(deviceConfig.TerminalPhoneNo,out var client))
  26. {
  27. return client;
  28. }
  29. else
  30. {
  31. JT808TcpClient jT808TcpClient = new JT808TcpClient(deviceConfig, serviceProvider);
  32. dict.TryAdd(deviceConfig.TerminalPhoneNo, jT808TcpClient);
  33. return jT808TcpClient;
  34. }
  35. }
  36. public void Dispose()
  37. {
  38. foreach(var client in dict)
  39. {
  40. try
  41. {
  42. client.Value.Dispose();
  43. }
  44. catch
  45. {
  46. }
  47. }
  48. }
  49. public List<JT808TcpClient> GetAll()
  50. {
  51. return dict.Values.ToList();
  52. }
  53. }
  54. }