IMessageCutter.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. public void Add(List<T> list)
  63. {
  64. this.list.AddRange(list);
  65. if (OnWindowFull != null)
  66. this.OnWindowFull(this.list);
  67. }
  68. #region MyRegion
  69. public void Clear()
  70. {
  71. this.list.Clear();
  72. this.size = this.initialSize;
  73. }
  74. public IEnumerator<T> GetEnumerator()
  75. {
  76. return this.list.GetEnumerator();
  77. }
  78. IEnumerator IEnumerable.GetEnumerator()
  79. {
  80. return this.list.GetEnumerator();
  81. }
  82. public int IndexOf(T item)
  83. {
  84. return this.list.IndexOf(item);
  85. }
  86. public void Insert(int index, T item)
  87. {
  88. this.list.Insert(index, item);
  89. }
  90. public void RemoveAt(int index)
  91. {
  92. this.list.RemoveAt(index);
  93. }
  94. public bool Contains(T item)
  95. {
  96. return this.list.Contains(item);
  97. }
  98. public void CopyTo(T[] array, int arrayIndex)
  99. {
  100. this.list.CopyTo(array, arrayIndex);
  101. }
  102. public bool Remove(T item)
  103. {
  104. return this.list.Remove(item);
  105. }
  106. /// <summary>
  107. /// 删除特定范围的数据
  108. /// </summary>
  109. /// <param name="startIndex"></param>
  110. /// <param name="count"></param>
  111. public void RemoveRange(int startIndex,int count)
  112. {
  113. if (startIndex < 0 || startIndex >= this.list.Count || startIndex+count > this.list.Count) return;
  114. this.list.RemoveRange(startIndex, count);
  115. this.size = this.list.Count;
  116. }
  117. #endregion
  118. /// <summary>
  119. /// Gets or sets the internal window size, if data filled in reached this `NewSize`, `OnWindowFull` event
  120. /// will be called.
  121. /// </summary>
  122. public int? NewSize
  123. {
  124. get { return this.size; }
  125. set
  126. {
  127. if (this.list.Count > 0 && this.list.Count >= value)
  128. {
  129. throw new ArgumentOutOfRangeException($"The input NewSize: {value} should > the size of current internal window(current size: {this.list.Count})");
  130. }
  131. this.size = value;
  132. }
  133. }
  134. public int Count => this.list.Count;// this.size ?? -1;
  135. public bool IsReadOnly => false;
  136. public T this[int index] { get => this.list[index]; set => this.list[index] = value; }
  137. }
  138. }