SingleConnectLayer.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Data.Odbc;
  2. namespace TLVProtocal.SubLayers;
  3. internal class SingleConnectLayer : BaseFilter
  4. {
  5. private TcpConnection _currentConnection;
  6. public override void Connected(Connection connection)
  7. {
  8. if (connection is not TcpConnection tcpConnection)
  9. return;
  10. this._currentConnection ??= tcpConnection;
  11. base.Connected(connection);
  12. }
  13. public override void Disconnected(Connection connection)
  14. {
  15. this._currentConnection = null;
  16. base.Disconnected(connection);
  17. }
  18. public override bool Send(Data data)
  19. {
  20. if (this._currentConnection == null)
  21. return false;
  22. if (data.Connection is not TcpConnection tcpConnection)
  23. return false;
  24. if (this._currentConnection.RemoteEndPoint != tcpConnection.RemoteEndPoint)
  25. return false;
  26. return base.Send(data);
  27. }
  28. public override bool Receive(Data data)
  29. {
  30. if (this._currentConnection == null)
  31. return false;
  32. if (data.Connection is not TcpConnection tcpConnection)
  33. return false;
  34. if (this._currentConnection.RemoteEndPoint != tcpConnection.RemoteEndPoint)
  35. return false;
  36. return base.Receive(data);
  37. }
  38. }