OEMListener.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.DigitalMatter;
  4. using NPOI.HSSF.Record.CF;
  5. using PRSServer.Engines;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows.Media.Media3D;
  16. namespace PRSServer
  17. {
  18. internal class OEMListener
  19. {
  20. private TcpListener listener;
  21. private GPSDeviceCache Cache;
  22. private GPSUpdateQueue Queue;
  23. private object connectionsLock = new object();
  24. private List<OEMConnection> Connections = new();
  25. public int Port { get; set; }
  26. public OEMListener(int port, GPSDeviceCache cache, GPSUpdateQueue queue)
  27. {
  28. Port = port;
  29. Cache = cache;
  30. Queue = queue;
  31. }
  32. public void Start()
  33. {
  34. listener = new TcpListener(IPAddress.Any, Port);
  35. listener.Start();
  36. AcceptClient();
  37. }
  38. private void AcceptClient()
  39. {
  40. Logger.Send(LogType.Information, "", "Waiting for OEM connection.");
  41. listener.AcceptTcpClientAsync().ContinueWith(t =>
  42. {
  43. try
  44. {
  45. Logger.Send(LogType.Information, "", $"OEM Connection opened; {Connections.Count} connections open.");
  46. var connection = new OEMConnection(t.Result, Cache, Queue);
  47. lock (connectionsLock)
  48. {
  49. Connections.Add(connection);
  50. }
  51. connection.OnCompletion = () =>
  52. {
  53. // When the connection finishes, we should remove it from our list.
  54. lock (connectionsLock)
  55. {
  56. Logger.Send(LogType.Information, "", $"OEM Connection closed; {Connections.Count} connections still open.");
  57. Connections.Remove(connection);
  58. }
  59. };
  60. connection.Run();
  61. // Accept a new client.
  62. AcceptClient();
  63. }
  64. catch (Exception e)
  65. {
  66. Logger.Send(LogType.Error, "", CoreUtils.FormatException(e));
  67. }
  68. }, TaskContinuationOptions.OnlyOnRanToCompletion);
  69. }
  70. public void Stop()
  71. {
  72. listener.Stop();
  73. lock (connectionsLock)
  74. {
  75. // Running in parallel because each connection can take a second to stop.
  76. Parallel.ForEach(Connections, connection =>
  77. {
  78. connection.Stop();
  79. });
  80. Connections.Clear();
  81. }
  82. }
  83. }
  84. }