DeliveryConnection.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using Sers.Core.CL.MessageDelivery;
  4. using Vit.Core.Module.Log;
  5. using Vit.Extensions;
  6. namespace Sers.CL.ClrZmq.ThreadWait
  7. {
  8. public class DeliveryConnection : IDeliveryConnection
  9. {
  10. ~DeliveryConnection()
  11. {
  12. Close();
  13. }
  14. public Sers.Core.Util.StreamSecurity.SecurityManager securityManager { set => _securityManager = value; }
  15. Sers.Core.Util.StreamSecurity.SecurityManager _securityManager;
  16. /// <summary>
  17. /// 连接状态(0:waitForCertify; 2:certified; 4:waitForClose; 8:closed;)
  18. /// </summary>
  19. public byte state { get; set; } = DeliveryConnState.waitForCertify;
  20. internal byte[] zmqIdentity { get; set; }
  21. Action<IDeliveryConnection, ArraySegment<byte>> _OnGetFrame;
  22. public Action<IDeliveryConnection, ArraySegment<byte>> OnGetFrame
  23. {
  24. internal get=> _OnGetFrame;
  25. set
  26. {
  27. if (_securityManager != null)
  28. {
  29. value =
  30. (conn, data) => { _securityManager.Decryption(data); }
  31. + value;
  32. }
  33. _OnGetFrame = value;
  34. }
  35. }
  36. public Action<DeliveryConnection, byte[]> OnSendFrameAsync { private get; set; }
  37. public void SendFrameAsync(List<ArraySegment<byte>> data)
  38. {
  39. var bytes = data.ByteDataToBytes();
  40. _securityManager?.Encryption(bytes.BytesToArraySegmentByte());
  41. OnSendFrameAsync(this, bytes);
  42. }
  43. public Action<IDeliveryConnection> Conn_OnDisconnected { get; set; }
  44. public void Close()
  45. {
  46. state = DeliveryConnState.closed;
  47. try
  48. {
  49. Conn_OnDisconnected?.Invoke(this);
  50. }
  51. catch (Exception ex)
  52. {
  53. Logger.Error(ex);
  54. }
  55. }
  56. }
  57. }