TBuffer.cs 2.6 KB

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