You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CachedNetworkStream.cs 7.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net.Sockets;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace Shadowsocks.Net
  10. {
  11. // cache first packet for duty-chain pattern listener
  12. public class CachedNetworkStream : Stream
  13. {
  14. // 256 byte first packet buffer should enough for 99.999...% situation
  15. // socks5: 0x05 0x....
  16. // http-pac: GET /pac HTTP/1.1
  17. // http-proxy: /[a-z]+ .+ HTTP\/1\.[01]/i
  18. public const int MaxCache = 256;
  19. public Socket Socket { get; private set; }
  20. private readonly Stream s;
  21. private byte[] cache = new byte[MaxCache];
  22. private long cachePtr = 0;
  23. private long readPtr = 0;
  24. public CachedNetworkStream(Socket socket)
  25. {
  26. s = new NetworkStream(socket);
  27. Socket = socket;
  28. }
  29. /// <summary>
  30. /// Only for test purpose
  31. /// </summary>
  32. /// <param name="stream"></param>
  33. public CachedNetworkStream(Stream stream)
  34. {
  35. s = stream;
  36. }
  37. public override bool CanRead => s.CanRead;
  38. // we haven't run out of cache
  39. public override bool CanSeek => cachePtr == readPtr;
  40. public override bool CanWrite => s.CanWrite;
  41. public override long Length => s.Length;
  42. public override long Position { get => readPtr; set => Seek(value, SeekOrigin.Begin); }
  43. public override void Flush()
  44. {
  45. s.Flush();
  46. }
  47. //public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
  48. //{
  49. // var endPtr = buffer.Length + readPtr; // expected ptr after operation
  50. // var uncachedLen = Math.Max(endPtr - cachePtr, 0); // how many data from socket
  51. // var cachedLen = buffer.Length - uncachedLen; // how many data from cache
  52. // var emptyCacheLen = MaxCache - cachePtr; // how many cache remain
  53. // int readLen = 0;
  54. // var cachedMem = buffer[..(int)cachedLen];
  55. // var uncachedMem = buffer[(int)cachedLen..];
  56. // if (cachedLen > 0)
  57. // {
  58. // cache[(int)readPtr..(int)(readPtr + cachedLen)].CopyTo(cachedMem);
  59. // readPtr += cachedLen;
  60. // readLen += (int)cachedLen;
  61. // }
  62. // if (uncachedLen > 0)
  63. // {
  64. // int readStreamLen = await s.ReadAsync(cachedMem, cancellationToken);
  65. // int lengthToCache = (int)Math.Min(emptyCacheLen, readStreamLen); // how many data need to cache
  66. // if (lengthToCache > 0)
  67. // {
  68. // uncachedMem[0..lengthToCache].CopyTo(cache[(int)cachePtr..]);
  69. // cachePtr += lengthToCache;
  70. // }
  71. // readPtr += readStreamLen;
  72. // readLen += readStreamLen;
  73. // }
  74. // return readLen;
  75. //}
  76. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  77. public override int Read(byte[] buffer, int offset, int count)
  78. {
  79. Span<byte> span = buffer.AsSpan(offset, count);
  80. return Read(span);
  81. }
  82. [MethodImpl(MethodImplOptions.AggressiveOptimization)]
  83. public override int Read(Span<byte> buffer)
  84. {
  85. // how many data from socket
  86. // r: readPtr, c: cachePtr, e: endPtr
  87. // ptr 0 r c e
  88. // cached ####+++++
  89. // read ++++
  90. // ptr 0 c r e
  91. // cached #####
  92. // read +++++
  93. var endPtr = buffer.Length + readPtr; // expected ptr after operation
  94. var uncachedLen = Math.Max(endPtr - Math.Max(cachePtr, readPtr), 0);
  95. var cachedLen = buffer.Length - uncachedLen; // how many data from cache
  96. var emptyCacheLen = MaxCache - cachePtr; // how many cache remain
  97. int readLen = 0;
  98. Span<byte> cachedSpan = buffer[..(int)cachedLen];
  99. Span<byte> uncachedSpan = buffer[(int)cachedLen..];
  100. if (cachedLen > 0)
  101. {
  102. cache[(int)readPtr..(int)(readPtr + cachedLen)].CopyTo(cachedSpan);
  103. readPtr += cachedLen;
  104. readLen += (int)cachedLen;
  105. }
  106. if (uncachedLen > 0)
  107. {
  108. int readStreamLen = s.Read(uncachedSpan);
  109. // how many data need to cache
  110. int lengthToCache = (int)Math.Min(emptyCacheLen, readStreamLen);
  111. if (lengthToCache > 0)
  112. {
  113. uncachedSpan[0..lengthToCache].ToArray().CopyTo(cache, cachePtr);
  114. cachePtr += lengthToCache;
  115. }
  116. readPtr += readStreamLen;
  117. readLen += readStreamLen;
  118. }
  119. return readLen;
  120. }
  121. /// <summary>
  122. /// Read first block, will never read into non-cache range
  123. /// </summary>
  124. /// <param name="buffer"></param>
  125. /// <returns></returns>
  126. public int ReadFirstBlock(Span<byte> buffer)
  127. {
  128. Seek(0, SeekOrigin.Begin);
  129. int len = Math.Min(MaxCache, buffer.Length);
  130. return Read(buffer[0..len]);
  131. }
  132. /// <summary>
  133. /// Seek position, only support seek to cached range when we haven't read into non-cache range
  134. /// </summary>
  135. /// <param name="offset"></param>
  136. /// <param name="origin">Set it to System.IO.SeekOrigin.Begin, otherwise it will throw System.NotSupportedException</param>
  137. /// <exception cref="IOException"></exception>
  138. /// <exception cref="NotSupportedException"></exception>
  139. /// <exception cref="ObjectDisposedException"></exception>
  140. /// <returns></returns>
  141. public override long Seek(long offset, SeekOrigin origin)
  142. {
  143. if (!CanSeek) throw new NotSupportedException("Non cache data has been read");
  144. if (origin != SeekOrigin.Begin) throw new NotSupportedException("We don't know network stream's length");
  145. if (offset < 0 || offset > cachePtr) throw new NotSupportedException("Can't seek to uncached position");
  146. readPtr = offset;
  147. return Position;
  148. }
  149. /// <summary>
  150. /// Useless
  151. /// </summary>
  152. /// <param name="value"></param>
  153. /// <exception cref="NotSupportedException"></exception>
  154. public override void SetLength(long value)
  155. {
  156. s.SetLength(value);
  157. }
  158. /// <summary>
  159. /// Write to underly stream
  160. /// </summary>
  161. /// <param name="buffer"></param>
  162. /// <param name="offset"></param>
  163. /// <param name="count"></param>
  164. /// <param name="cancellationToken"></param>
  165. /// <returns></returns>
  166. public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  167. {
  168. return s.WriteAsync(buffer, offset, count, cancellationToken);
  169. }
  170. /// <summary>
  171. /// Write to underly stream
  172. /// </summary>
  173. /// <param name="buffer"></param>
  174. /// <param name="offset"></param>
  175. /// <param name="count"></param>
  176. public override void Write(byte[] buffer, int offset, int count)
  177. {
  178. s.Write(buffer, offset, count);
  179. }
  180. protected override void Dispose(bool disposing)
  181. {
  182. s.Dispose();
  183. base.Dispose(disposing);
  184. }
  185. }
  186. }