TBuffer.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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(
  61. string.Format("bufferList size < n, bufferList: {0} buffer length: {1}",
  62. this.Count, buffer.Length));
  63. }
  64. int alreadyCopyCount = 0;
  65. while (alreadyCopyCount < buffer.Length)
  66. {
  67. int n = buffer.Length - alreadyCopyCount;
  68. if (ChunkSize - this.FirstIndex > n)
  69. {
  70. Array.Copy(this.bufferList.First.Value, this.FirstIndex, buffer, alreadyCopyCount, n);
  71. this.FirstIndex += n;
  72. alreadyCopyCount += n;
  73. }
  74. else
  75. {
  76. Array.Copy(this.bufferList.First.Value, this.FirstIndex, buffer, alreadyCopyCount,
  77. ChunkSize - this.FirstIndex);
  78. alreadyCopyCount += ChunkSize - this.FirstIndex;
  79. this.FirstIndex = 0;
  80. this.bufferList.RemoveFirst();
  81. }
  82. }
  83. }
  84. public void SendTo(byte[] buffer)
  85. {
  86. int alreadyCopyCount = 0;
  87. while (alreadyCopyCount < buffer.Length)
  88. {
  89. if (this.LastIndex == ChunkSize)
  90. {
  91. this.bufferList.AddLast(new byte[ChunkSize]);
  92. this.LastIndex = 0;
  93. }
  94. int n = buffer.Length - alreadyCopyCount;
  95. if (ChunkSize - this.LastIndex > n)
  96. {
  97. Array.Copy(buffer, alreadyCopyCount, this.bufferList.Last.Value, this.LastIndex, n);
  98. this.LastIndex += buffer.Length - alreadyCopyCount;
  99. alreadyCopyCount += n;
  100. }
  101. else
  102. {
  103. Array.Copy(buffer, alreadyCopyCount, this.bufferList.Last.Value, this.LastIndex,
  104. ChunkSize - this.LastIndex);
  105. alreadyCopyCount += ChunkSize - this.LastIndex;
  106. this.LastIndex = ChunkSize;
  107. }
  108. }
  109. }
  110. }
  111. }