RingBufferManager.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. namespace ivf_tl_SerialHelper
  2. {
  3. public class RingBufferManager
  4. {
  5. public byte[] Buffer { get; set; } // 存放内存的数组
  6. public int DataCount { get; set; } // 写入数据大小
  7. public int DataStart { get; set; } // 数据起始索引
  8. public int DataEnd { get; set; } // 数据结束索引
  9. public RingBufferManager(int bufferSize)
  10. {
  11. DataCount = 0;
  12. DataStart = 0;
  13. DataEnd = 0;
  14. Buffer = new byte[bufferSize];
  15. }
  16. public byte this[int index]
  17. {
  18. get
  19. {
  20. if (index >= DataCount) throw new Exception("环形缓冲区异常,索引溢出");
  21. if (DataStart + index < Buffer.Length)
  22. {
  23. return Buffer[DataStart + index];
  24. }
  25. else
  26. {
  27. return Buffer[(DataStart + index) - Buffer.Length];
  28. }
  29. }
  30. }
  31. public int GetDataCount() // 获得当前写入的字节数
  32. {
  33. return DataCount;
  34. }
  35. public int GetReserveCount() // 获得剩余的字节数
  36. {
  37. return Buffer.Length - DataCount;
  38. }
  39. public void Clear()
  40. {
  41. DataCount = 0;
  42. DataStart = 0;
  43. DataEnd = 0;
  44. }
  45. public void Clear(int count) // 清空指定大小的数据
  46. {
  47. if (count >= DataCount) // 如果需要清理的数据大于现有数据大小,则全部清理
  48. {
  49. DataCount = 0;
  50. DataStart = 0;
  51. DataEnd = 0;
  52. }
  53. else
  54. {
  55. if (DataStart + count >= Buffer.Length)
  56. {
  57. DataStart = (DataStart + count) - Buffer.Length;
  58. }
  59. else
  60. {
  61. DataStart += count;
  62. }
  63. DataCount -= count;
  64. }
  65. }
  66. public void WriteBuffer(byte[] buffer, int offset, int count)
  67. {
  68. Int32 reserveCount = Buffer.Length - DataCount;
  69. if (reserveCount >= count) // 可用空间够使用
  70. {
  71. if (DataEnd + count < Buffer.Length) // 数据没到结尾
  72. {
  73. Array.Copy(buffer, offset, Buffer, DataEnd, count);
  74. DataEnd += count;
  75. DataCount += count;
  76. }
  77. else // 数据结束索引超出结尾 循环到开始
  78. {
  79. System.Diagnostics.Debug.WriteLine("缓存重新开始....");
  80. Int32 overflowIndexLength = (DataEnd + count) - Buffer.Length; // 超出索引长度
  81. Int32 endPushIndexLength = count - overflowIndexLength; // 填充在末尾的数据长度
  82. Array.Copy(buffer, offset, Buffer, DataEnd, endPushIndexLength);
  83. DataEnd = 0;
  84. offset += endPushIndexLength;
  85. DataCount += endPushIndexLength;
  86. if (overflowIndexLength != 0)
  87. {
  88. Array.Copy(buffer, offset, Buffer, DataEnd, overflowIndexLength);
  89. }
  90. DataEnd += overflowIndexLength; // 结束索引
  91. DataCount += overflowIndexLength; // 缓存大小
  92. }
  93. }
  94. else
  95. {
  96. throw new Exception("环形缓冲区异常,写入长度大于剩余空间");
  97. // 缓存溢出,不处理
  98. }
  99. }
  100. public void ReadBuffer(byte[] targetBytes, Int32 offset, Int32 count)
  101. {
  102. if (count > DataCount) throw new Exception("环形缓冲区异常,读取长度大于数据长度");
  103. Int32 tempDataStart = DataStart;
  104. if (DataStart + count < Buffer.Length)
  105. {
  106. Array.Copy(Buffer, DataStart, targetBytes, offset, count);
  107. }
  108. else
  109. {
  110. Int32 overflowIndexLength = (DataStart + count) - Buffer.Length; // 超出索引长度
  111. Int32 endPushIndexLength = count - overflowIndexLength; // 填充在末尾的数据长度
  112. Array.Copy(Buffer, DataStart, targetBytes, offset, endPushIndexLength);
  113. offset += endPushIndexLength;
  114. if (overflowIndexLength != 0)
  115. {
  116. Array.Copy(Buffer, 0, targetBytes, offset, overflowIndexLength);
  117. }
  118. }
  119. }
  120. public void WriteBuffer(byte[] buffer)
  121. {
  122. WriteBuffer(buffer, 0, buffer.Length);
  123. }
  124. }
  125. }