TBuffer.cs 2.7 KB

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