TlvCommunicatorServer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System.Threading.Tasks;
  2. using UniversalNetFrame451;
  3. namespace TLVProtocal;
  4. internal class TlvCommunicatorServer : BaseFilter, ITlvCommunicatorServer
  5. {
  6. private ITcpServer _tcpServer;
  7. private ITlvProvider _provider;
  8. private Connection _connection;
  9. private MessageQueue _messageQueue;
  10. private MessageReceiveQueue _receiveQueue;
  11. private TlvAckLayer _ackLayer;
  12. private TlvPackLayer _packLayer;
  13. private TlvContentLayer _contentLayer;
  14. private RawDataLog _rawDataLog;
  15. bool ITlvCommunicatorServer.Initialize(ITlvProvider provider, bool ConvertToBigEnd, int timeoutTime)
  16. {
  17. if (this._tcpServer is not null)
  18. return false;
  19. this._tcpServer = IOFactory.GetTcpServer();
  20. this._provider = provider;
  21. this._ackLayer = new(ConvertToBigEnd, timeoutTime);
  22. this._packLayer = new(ConvertToBigEnd);
  23. this._contentLayer = new(ConvertToBigEnd);
  24. this._messageQueue = new();
  25. this._messageQueue.Initialize();
  26. this._receiveQueue = new();
  27. this._receiveQueue.Initialize();
  28. this._rawDataLog = new();
  29. string filePath = Path.Combine(Environment.CurrentDirectory, "Log", "TlvCommunicatorServer.log");
  30. this._rawDataLog.Initialize(filePath);
  31. _ = this >> this._contentLayer >> this._receiveQueue >> this._ackLayer >> this._packLayer >> this._messageQueue >> (BaseFilter)this._tcpServer;
  32. //this._rawDataLog >>
  33. return true;
  34. }
  35. bool ITlvCommunicatorServer.Open(string ip, ushort port)
  36. {
  37. if (this._tcpServer is null)
  38. return false;
  39. if (this._provider is null)
  40. return false;
  41. if (!this._tcpServer.Initialize($"TlvServer {ip}:{port}"))
  42. return false;
  43. return this._tcpServer.Open(ip, port);
  44. }
  45. bool ITlvCommunicatorServer.Send(TlvData tlvData)
  46. {
  47. if (this._tcpServer is null)
  48. return false;
  49. if (this._provider is null)
  50. return false;
  51. if (this._connection is null)
  52. return false;
  53. if (tlvData.Connection is null)
  54. tlvData = new(tlvData.Tag, tlvData.RawData, this._connection, tlvData.DateTime);
  55. return base.Send(tlvData);
  56. }
  57. public bool Send<T>(byte tag, T data) where T : struct
  58. {
  59. if (!StructConverter.TryGetBytes<T>(data, out byte[] alarmBytes) || alarmBytes is null)
  60. return false;
  61. TlvData tlv = new(tag, alarmBytes);
  62. return this.Send(tlv);
  63. }
  64. void IDisposable.Dispose()
  65. {
  66. this._tcpServer?.Close();
  67. this._tcpServer = null;
  68. ((IDisposable)this._messageQueue)?.Dispose();
  69. ((IDisposable)this._receiveQueue)?.Dispose();
  70. this._messageQueue = null;
  71. this._receiveQueue = null;
  72. this._ackLayer = null;
  73. this._packLayer = null;
  74. this._contentLayer = null;
  75. this._rawDataLog = null;
  76. }
  77. #region IFilter
  78. public override bool Receive(Data data)
  79. {
  80. if (data is not TlvData tlv)
  81. return false;
  82. if (tlv.RequestID.HasValue)
  83. {
  84. TlvData reply = this._provider?.RequestReply(tlv);
  85. Task.Factory.StartNew(() =>
  86. {
  87. base.Send(reply);
  88. });
  89. }
  90. else
  91. this._provider?.Received(tlv);
  92. return true;
  93. }
  94. public override void Connected(Connection connection)
  95. {
  96. if (connection is not TcpConnection tcpConnection)
  97. return;
  98. this._connection = connection;
  99. this._provider?.Connected(tcpConnection);
  100. }
  101. public override void Disconnected(Connection connection)
  102. {
  103. if (connection is not TcpConnection tcpConnection)
  104. return;
  105. this._connection = null;
  106. this._provider?.Disconnected(tcpConnection);
  107. }
  108. #endregion
  109. }