From 511a658fd35f0ee45aec25a96d232085b2a56ad1 Mon Sep 17 00:00:00 2001 From: noisyfox Date: Fri, 16 Dec 2016 12:18:59 +1100 Subject: [PATCH] Revert "Reuse buffer from first pakcage" This reverts commit f2b8e91f81fd7975f19826cac2a9a94e59833d69. --- .../Controller/Service/Http2Socks5.cs | 10 +++-- shadowsocks-csharp/ForwardProxy/HttpProxy.cs | 2 +- shadowsocks-csharp/Util/Sockets/LineReader.cs | 40 +++++++------------ 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/shadowsocks-csharp/Controller/Service/Http2Socks5.cs b/shadowsocks-csharp/Controller/Service/Http2Socks5.cs index adaa3896..6ec9f6ab 100644 --- a/shadowsocks-csharp/Controller/Service/Http2Socks5.cs +++ b/shadowsocks-csharp/Controller/Service/Http2Socks5.cs @@ -70,8 +70,8 @@ namespace Shadowsocks.Controller.Service _socks5Port = socks5Port; _localSocket = new WrappedSocket(socket); _localSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); - new LineReader(firstPacket, _localSocket, firstPacket, 0, length, OnLineRead, OnException, OnFinish, - Encoding.UTF8, HTTP_CRLF, null); + new LineReader(_localSocket, firstPacket, 0, length, OnLineRead, OnException, OnFinish, + Encoding.UTF8, HTTP_CRLF, 1024, null); } private void CheckClose() @@ -164,8 +164,8 @@ namespace Shadowsocks.Controller.Service private void SendConnectResponse() { - var len = Encoding.UTF8.GetBytes(HTTP_CONNECT_200, 0, HTTP_CONNECT_200.Length, _remoteRecvBuffer, 0); - _localSocket.BeginSend(_remoteRecvBuffer, 0, len, SocketFlags.None, Http200SendCallback, null); + var b = Encoding.UTF8.GetBytes(HTTP_CONNECT_200); + _localSocket.BeginSend(b, 0, b.Length, SocketFlags.None, Http200SendCallback, null); } private void Http200SendCallback(IAsyncResult ar) @@ -273,6 +273,7 @@ namespace Shadowsocks.Controller.Service try { int bytesRead = _socks5.EndReceive(ar); + Logging.Debug("Read from remote: " + bytesRead); if (bytesRead > 0) { @@ -301,6 +302,7 @@ namespace Shadowsocks.Controller.Service try { int bytesRead = _localSocket.EndReceive(ar); + Logging.Debug("Read from local: " + bytesRead); if (bytesRead > 0) { diff --git a/shadowsocks-csharp/ForwardProxy/HttpProxy.cs b/shadowsocks-csharp/ForwardProxy/HttpProxy.cs index dbc8bd71..cff91beb 100644 --- a/shadowsocks-csharp/ForwardProxy/HttpProxy.cs +++ b/shadowsocks-csharp/ForwardProxy/HttpProxy.cs @@ -134,7 +134,7 @@ namespace Shadowsocks.ForwardProxy _remote.EndSend(ar); // start line read - new LineReader(1024, _remote, OnLineRead, OnException, OnFinish, Encoding.UTF8, HTTP_CRLF, new FakeAsyncResult(ar, state)); + new LineReader(_remote, OnLineRead, OnException, OnFinish, Encoding.UTF8, HTTP_CRLF, 1024, new FakeAsyncResult(ar, state)); } catch (Exception ex) { diff --git a/shadowsocks-csharp/Util/Sockets/LineReader.cs b/shadowsocks-csharp/Util/Sockets/LineReader.cs index 05c2f008..9bd40321 100644 --- a/shadowsocks-csharp/Util/Sockets/LineReader.cs +++ b/shadowsocks-csharp/Util/Sockets/LineReader.cs @@ -20,16 +20,12 @@ namespace Shadowsocks.Util.Sockets private int _bufferDataIndex; private int _bufferDataLength; - public LineReader(byte[] buffer, WrappedSocket socket, byte[] firstPackge, int index, int length, + public LineReader(WrappedSocket socket, byte[] firstPackge, int index, int length, Func onLineRead, Action onException, Action onFinish, - Encoding encoding, string delimiter, + Encoding encoding, string delimiter, int maxLineBytes, object state) { - if (buffer == null) - { - throw new ArgumentNullException(nameof(buffer)); - } if (socket == null) { throw new ArgumentNullException(nameof(socket)); @@ -47,9 +43,9 @@ namespace Shadowsocks.Util.Sockets throw new ArgumentNullException(nameof(delimiter)); } - if (buffer.Length < length) + if (maxLineBytes < length) { - throw new ArgumentException("Line buffer length can't less than first package length!", nameof(buffer)); + throw new ArgumentException("Line buffer length can't less than first package length!", nameof(maxLineBytes)); } if (length > 0) @@ -76,26 +72,19 @@ namespace Shadowsocks.Util.Sockets throw new ArgumentException("Too short!", nameof(delimiter)); } - if (buffer.Length < _delimiterBytes.Length) + if (maxLineBytes < _delimiterBytes.Length) { - throw new ArgumentException("Too small!", nameof(buffer)); + throw new ArgumentException("Too small!", nameof(maxLineBytes)); } _delimiterSearch = new ByteSearch.SearchTarget(_delimiterBytes); - _lineBuffer = buffer; + _lineBuffer = new byte[maxLineBytes]; if (length > 0) { // process first package - if (buffer == firstPackge) - { - _bufferDataIndex = index; - } - else - { - Array.Copy(firstPackge, index, _lineBuffer, 0, length); - } + Array.Copy(firstPackge, index, _lineBuffer, 0, length); _bufferDataLength = length; try @@ -111,16 +100,15 @@ namespace Shadowsocks.Util.Sockets else { // start reading - socket.BeginReceive(_lineBuffer, 0, _lineBuffer.Length, 0, ReceiveCallback, 0); + socket.BeginReceive(_lineBuffer, 0, maxLineBytes, 0, ReceiveCallback, 0); } } - public LineReader(int maxLineBytes, WrappedSocket socket, Func onLineRead, - Action onException, Action onFinish, Encoding encoding, - string delimiter, object state) - : this( - new byte[maxLineBytes], socket, null, 0, 0, onLineRead, onException, onFinish, encoding, delimiter, - state) + public LineReader(WrappedSocket socket, Func onLineRead, + Action onException, + Action onFinish, Encoding encoding, string delimiter, int maxLineBytes, + object state) + : this(socket, null, 0, 0, onLineRead, onException, onFinish, encoding, delimiter, maxLineBytes, state) { }