IMessageCutter.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Edge.Core.Processor.Communicator
  8. {
  9. public interface IMessageCutter<T>
  10. {
  11. /// <summary>
  12. /// fired once a valid message was cut from raw data
  13. /// </summary>
  14. event EventHandler OnMessageCut;
  15. /// <summary>
  16. /// fired once an invalid raw data is on cutting.
  17. /// </summary>
  18. event EventHandler<MessageCutterInvalidMessageReadEventArg> OnInvalidMessageRead;
  19. void Feed(T data);
  20. T Message { get; }
  21. }
  22. public class MessageCutterInvalidMessageReadEventArg : System.EventArgs
  23. {
  24. public string Message { get; set; }
  25. }
  26. /// <summary>
  27. /// It's indeed a buffer and will fire `OnWindowFull` event once it get full filled.
  28. /// </summary>
  29. /// <typeparam name="T"></typeparam>
  30. public class SizableWindow<T> : IList<T>
  31. {
  32. public Action<List<T>> OnWindowFull;
  33. private List<T> list = new List<T>();
  34. private int initialSize;
  35. private int? size;
  36. public int RealSize => this.list.Count;
  37. /// <summary>
  38. /// will default init a buffer with size 1.
  39. /// </summary>
  40. public SizableWindow() : this(1)
  41. {
  42. }
  43. public SizableWindow(int initialSize)
  44. {
  45. this.initialSize = initialSize;
  46. this.size = initialSize;
  47. }
  48. public void Add(T data)
  49. {
  50. this.list.Add(data);
  51. if (this.size == null || this.size.Value == 0)
  52. {
  53. if (OnWindowFull != null)
  54. this.OnWindowFull(this.list);
  55. }
  56. else if (this.list.Count == this.NewSize)
  57. {
  58. if (OnWindowFull != null)
  59. this.OnWindowFull(this.list);
  60. }
  61. }
  62. #region MyRegion
  63. public void Clear()
  64. {
  65. this.list.Clear();
  66. this.size = this.initialSize;
  67. }
  68. public IEnumerator<T> GetEnumerator()
  69. {
  70. return this.list.GetEnumerator();
  71. }
  72. IEnumerator IEnumerable.GetEnumerator()
  73. {
  74. return this.list.GetEnumerator();
  75. }
  76. public int IndexOf(T item)
  77. {
  78. return this.list.IndexOf(item);
  79. }
  80. public void Insert(int index, T item)
  81. {
  82. this.list.Insert(index, item);
  83. }
  84. public void RemoveAt(int index)
  85. {
  86. this.list.RemoveAt(index);
  87. }
  88. public bool Contains(T item)
  89. {
  90. return this.list.Contains(item);
  91. }
  92. public void CopyTo(T[] array, int arrayIndex)
  93. {
  94. this.list.CopyTo(array, arrayIndex);
  95. }
  96. public bool Remove(T item)
  97. {
  98. return this.list.Remove(item);
  99. }
  100. #endregion
  101. /// <summary>
  102. /// Gets or sets the internal window size, if data filled in reached this `NewSize`, `OnWindowFull` event
  103. /// will be called.
  104. /// </summary>
  105. public int? NewSize
  106. {
  107. get { return this.size; }
  108. set
  109. {
  110. if (this.list.Count > 0 && this.list.Count >= value)
  111. {
  112. throw new ArgumentOutOfRangeException($"The input NewSize: {value} should > the size of current internal window(current size: {this.list.Count})");
  113. }
  114. this.size = value;
  115. }
  116. }
  117. public int Count => this.list.Count;// this.size ?? -1;
  118. public bool IsReadOnly => false;
  119. public T this[int index] { get => this.list[index]; set => this.list[index] = value; }
  120. }
  121. }