TBuffer.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Base
  4. {
  5. public class TBuffer
  6. {
  7. public const int ChunkSize = 8192;
  8. private readonly LinkedList<byte[]> bufferList = new LinkedList<byte[]>();
  9. public int LastIndex { get; set; }
  10. public int FirstIndex { get; set; }
  11. public TBuffer()
  12. {
  13. this.bufferList.AddLast(new byte[ChunkSize]);
  14. }
  15. public int Count
  16. {
  17. get
  18. {
  19. int c = 0;
  20. if (this.bufferList.Count == 0)
  21. {
  22. c = 0;
  23. }
  24. else
  25. {
  26. c = (this.bufferList.Count - 1) * ChunkSize + this.LastIndex - this.FirstIndex;
  27. }
  28. if (c < 0)
  29. {
  30. Log.Error("TBuffer count < 0: {0}, {1}, {2}".Fmt(bufferList.Count, this.LastIndex, this.FirstIndex));
  31. }
  32. return c;
  33. }
  34. }
  35. public void AddLast()
  36. {
  37. this.bufferList.AddLast(new byte[ChunkSize]);
  38. }
  39. public void RemoveFirst()
  40. {
  41. this.bufferList.RemoveFirst();
  42. }
  43. public byte[] First
  44. {
  45. get
  46. {
  47. if (this.bufferList.First == null)
  48. {
  49. this.AddLast();
  50. }
  51. return this.bufferList.First.Value;
  52. }
  53. }
  54. public byte[] Last
  55. {
  56. get
  57. {
  58. if (this.bufferList.Last == null)
  59. {
  60. this.AddLast();
  61. }
  62. return this.bufferList.Last.Value;
  63. }
  64. }
  65. public void RecvFrom(byte[] buffer)
  66. {
  67. if (this.Count < buffer.Length || buffer.Length == 0)
  68. {
  69. throw new Exception($"bufferList size < n, bufferList: {this.Count} buffer length: {buffer.Length}");
  70. }
  71. int alreadyCopyCount = 0;
  72. while (alreadyCopyCount < buffer.Length)
  73. {
  74. int n = buffer.Length - alreadyCopyCount;
  75. if (ChunkSize - this.FirstIndex > n)
  76. {
  77. Array.Copy(this.bufferList.First.Value, this.FirstIndex, buffer, alreadyCopyCount, n);
  78. this.FirstIndex += n;
  79. alreadyCopyCount += n;
  80. }
  81. else
  82. {
  83. Array.Copy(this.bufferList.First.Value, this.FirstIndex, buffer, alreadyCopyCount, ChunkSize - this.FirstIndex);
  84. alreadyCopyCount += ChunkSize - this.FirstIndex;
  85. this.FirstIndex = 0;
  86. this.bufferList.RemoveFirst();
  87. }
  88. }
  89. }
  90. public void SendTo(byte[] buffer)
  91. {
  92. int alreadyCopyCount = 0;
  93. while (alreadyCopyCount < buffer.Length)
  94. {
  95. if (this.LastIndex == ChunkSize)
  96. {
  97. this.bufferList.AddLast(new byte[ChunkSize]);
  98. this.LastIndex = 0;
  99. }
  100. int n = buffer.Length - alreadyCopyCount;
  101. if (ChunkSize - this.LastIndex > n)
  102. {
  103. Array.Copy(buffer, alreadyCopyCount, this.bufferList.Last.Value, this.LastIndex, n);
  104. this.LastIndex += buffer.Length - alreadyCopyCount;
  105. alreadyCopyCount += n;
  106. }
  107. else
  108. {
  109. Array.Copy(buffer, alreadyCopyCount, this.bufferList.Last.Value, this.LastIndex, ChunkSize - this.LastIndex);
  110. alreadyCopyCount += ChunkSize - this.LastIndex;
  111. this.LastIndex = ChunkSize;
  112. }
  113. }
  114. }
  115. }
  116. }