TBuffer.cs 2.6 KB

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