HandlerBase.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. protected HandlerBase(string text)
  41. {
  42. SendText = text;
  43. SendBinary = Encoding.ASCII.GetBytes(text);
  44. AckTimeout = TimeSpan.FromSeconds(10);
  45. CompleteTimeout = TimeSpan.FromSeconds(60);
  46. IsAckTimeout = false;
  47. IsCompleteTimeout = false;
  48. _state = EnumHandlerState.Create;
  49. }
  50. protected HandlerBase(byte[] buffer)
  51. {
  52. SendText = "";
  53. SendBinary = buffer;
  54. AckTimeout = TimeSpan.FromSeconds(60);
  55. CompleteTimeout = TimeSpan.FromSeconds(90);
  56. IsAckTimeout = false;
  57. IsCompleteTimeout = false;
  58. _state = EnumHandlerState.Create;
  59. }
  60. public abstract bool HandleMessage(MessageBase msg, out bool transactionComplete);
  61. public void OnSent()
  62. {
  63. SetState(EnumHandlerState.Sent);
  64. }
  65. public void OnAcked()
  66. {
  67. SetState(EnumHandlerState.Acked);
  68. }
  69. public void OnComplete()
  70. {
  71. SetState(EnumHandlerState.Completed);
  72. }
  73. public void SetState(EnumHandlerState state)
  74. {
  75. _state = state;
  76. if (_state == EnumHandlerState.Sent) //connection's responsibility
  77. {
  78. _timerAck.Restart();
  79. _timerComplete.Restart();
  80. }
  81. if (_state == EnumHandlerState.Acked) //handler's responsibility
  82. {
  83. _timerAck.Stop();
  84. }
  85. if (_state == EnumHandlerState.Completed)//handler's responsibility
  86. {
  87. _timerAck.Stop();
  88. _timerComplete.Stop();
  89. }
  90. }
  91. public bool CheckTimeout( )
  92. {
  93. if (_state == EnumHandlerState.Sent && _timerAck.IsRunning && _timerAck.Elapsed > AckTimeout)
  94. {
  95. _timerAck.Stop();
  96. _state = EnumHandlerState.Completed;
  97. IsAckTimeout = true;
  98. return true;
  99. }
  100. if ((_state == EnumHandlerState.Sent || _state==EnumHandlerState.Acked) && _timerComplete.IsRunning && _timerComplete.Elapsed > CompleteTimeout)
  101. {
  102. _timerAck.Stop();
  103. _timerComplete.Stop();
  104. _state = EnumHandlerState.Completed;
  105. IsCompleteTimeout = true;
  106. return true;
  107. }
  108. return false;
  109. }
  110. public virtual bool MatchMessage(MessageBase msg)
  111. {
  112. return false;
  113. }
  114. }
  115. }