RingBufferManager.cs 5.1 KB

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