HandlerBase.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using Aitex.Core.RT.Log;
  9. namespace MECF.Framework.Common.Communications
  10. {
  11. public enum EnumHandlerState
  12. {
  13. Create,
  14. Sent,
  15. Acked,
  16. Completed,
  17. }
  18. public abstract class HandlerBase
  19. {
  20. public bool IsComplete
  21. {
  22. get { return _state == EnumHandlerState.Completed; }
  23. }
  24. public bool IsCompleteTimeout { get; set; }
  25. public bool IsAcked
  26. {
  27. get { return _state == EnumHandlerState.Acked; }
  28. }
  29. public bool IsAckTimeout { get; set; }
  30. private EnumHandlerState _state;
  31. public string Name { get; set; }
  32. public int MutexId { get; set; }
  33. public TimeSpan AckTimeout { get; set; }
  34. public TimeSpan CompleteTimeout { get; set; }
  35. public MessageBase ResponseMessage { get; set; }
  36. public string SendText { get; set; }
  37. public byte[] SendBinary { get; set; }
  38. protected Stopwatch _timerAck = new Stopwatch();
  39. protected Stopwatch _timerComplete = new Stopwatch();
  40. public Stopwatch TimerAck => _timerAck;
  41. public Stopwatch TimerComplete => _timerComplete;
  42. protected HandlerBase(string text)
  43. {
  44. SendText = text;
  45. SendBinary = Encoding.ASCII.GetBytes(text);
  46. AckTimeout = TimeSpan.FromSeconds(10);
  47. CompleteTimeout = TimeSpan.FromSeconds(60);
  48. IsAckTimeout = false;
  49. IsCompleteTimeout = false;
  50. _state = EnumHandlerState.Create;
  51. }
  52. protected HandlerBase(byte[] buffer)
  53. {
  54. SendText = "";
  55. SendBinary = buffer;
  56. AckTimeout = TimeSpan.FromSeconds(60);
  57. CompleteTimeout = TimeSpan.FromSeconds(90);
  58. IsAckTimeout = false;
  59. IsCompleteTimeout = false;
  60. _state = EnumHandlerState.Create;
  61. }
  62. public abstract bool HandleMessage(MessageBase msg, out bool transactionComplete);
  63. public void OnSent()
  64. {
  65. SetState(EnumHandlerState.Sent);
  66. }
  67. public void OnAcked()
  68. {
  69. SetState(EnumHandlerState.Acked);
  70. }
  71. public void OnComplete()
  72. {
  73. SetState(EnumHandlerState.Completed);
  74. }
  75. public void SetState(EnumHandlerState state)
  76. {
  77. _state = state;
  78. if (_state == EnumHandlerState.Sent) //connection's responsibility
  79. {
  80. _timerAck.Restart();
  81. _timerComplete.Restart();
  82. }
  83. if (_state == EnumHandlerState.Acked) //handler's responsibility
  84. {
  85. _timerAck.Stop();
  86. }
  87. if (_state == EnumHandlerState.Completed)//handler's responsibility
  88. {
  89. _timerAck.Stop();
  90. _timerComplete.Stop();
  91. }
  92. }
  93. public bool CheckTimeout( )
  94. {
  95. if (_state == EnumHandlerState.Sent && _timerAck.IsRunning && _timerAck.Elapsed > AckTimeout)
  96. {
  97. _timerAck.Stop();
  98. _state = EnumHandlerState.Completed;
  99. IsAckTimeout = true;
  100. return true;
  101. }
  102. if ((_state == EnumHandlerState.Sent || _state==EnumHandlerState.Acked) && _timerComplete.IsRunning && _timerComplete.Elapsed > CompleteTimeout)
  103. {
  104. _timerAck.Stop();
  105. _timerComplete.Stop();
  106. _state = EnumHandlerState.Completed;
  107. IsCompleteTimeout = true;
  108. return true;
  109. }
  110. return false;
  111. }
  112. public virtual bool MatchMessage(MessageBase msg)
  113. {
  114. return false;
  115. }
  116. }
  117. }