|
@@ -20,12 +20,16 @@ namespace Shadowsocks.Util.Sockets |
|
|
private int _bufferDataIndex; |
|
|
private int _bufferDataIndex; |
|
|
private int _bufferDataLength; |
|
|
private int _bufferDataLength; |
|
|
|
|
|
|
|
|
public LineReader(WrappedSocket socket, byte[] firstPackge, int index, int length, |
|
|
|
|
|
|
|
|
public LineReader(byte[] buffer, WrappedSocket socket, byte[] firstPackge, int index, int length, |
|
|
Func<string, object, bool> onLineRead, Action<Exception, object> onException, |
|
|
Func<string, object, bool> onLineRead, Action<Exception, object> onException, |
|
|
Action<byte[], int, int, object> onFinish, |
|
|
Action<byte[], int, int, object> onFinish, |
|
|
Encoding encoding, string delimiter, int maxLineBytes, |
|
|
|
|
|
|
|
|
Encoding encoding, string delimiter, |
|
|
object state) |
|
|
object state) |
|
|
{ |
|
|
{ |
|
|
|
|
|
if (buffer == null) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new ArgumentNullException(nameof(buffer)); |
|
|
|
|
|
} |
|
|
if (socket == null) |
|
|
if (socket == null) |
|
|
{ |
|
|
{ |
|
|
throw new ArgumentNullException(nameof(socket)); |
|
|
throw new ArgumentNullException(nameof(socket)); |
|
@@ -43,9 +47,9 @@ namespace Shadowsocks.Util.Sockets |
|
|
throw new ArgumentNullException(nameof(delimiter)); |
|
|
throw new ArgumentNullException(nameof(delimiter)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (maxLineBytes < length) |
|
|
|
|
|
|
|
|
if (buffer.Length < length) |
|
|
{ |
|
|
{ |
|
|
throw new ArgumentException("Line buffer length can't less than first package length!", nameof(maxLineBytes)); |
|
|
|
|
|
|
|
|
throw new ArgumentException("Line buffer length can't less than first package length!", nameof(buffer)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (length > 0) |
|
|
if (length > 0) |
|
@@ -72,19 +76,26 @@ namespace Shadowsocks.Util.Sockets |
|
|
throw new ArgumentException("Too short!", nameof(delimiter)); |
|
|
throw new ArgumentException("Too short!", nameof(delimiter)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (maxLineBytes < _delimiterBytes.Length) |
|
|
|
|
|
|
|
|
if (buffer.Length < _delimiterBytes.Length) |
|
|
{ |
|
|
{ |
|
|
throw new ArgumentException("Too small!", nameof(maxLineBytes)); |
|
|
|
|
|
|
|
|
throw new ArgumentException("Too small!", nameof(buffer)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
_delimiterSearch = new ByteSearch.SearchTarget(_delimiterBytes); |
|
|
_delimiterSearch = new ByteSearch.SearchTarget(_delimiterBytes); |
|
|
|
|
|
|
|
|
_lineBuffer = new byte[maxLineBytes]; |
|
|
|
|
|
|
|
|
_lineBuffer = buffer; |
|
|
|
|
|
|
|
|
if (length > 0) |
|
|
if (length > 0) |
|
|
{ |
|
|
{ |
|
|
// process first package |
|
|
// process first package |
|
|
Array.Copy(firstPackge, index, _lineBuffer, 0, length); |
|
|
|
|
|
|
|
|
if (buffer == firstPackge) |
|
|
|
|
|
{ |
|
|
|
|
|
_bufferDataIndex = index; |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
Array.Copy(firstPackge, index, _lineBuffer, 0, length); |
|
|
|
|
|
} |
|
|
_bufferDataLength = length; |
|
|
_bufferDataLength = length; |
|
|
|
|
|
|
|
|
try |
|
|
try |
|
@@ -100,15 +111,16 @@ namespace Shadowsocks.Util.Sockets |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
// start reading |
|
|
// start reading |
|
|
socket.BeginReceive(_lineBuffer, 0, maxLineBytes, 0, ReceiveCallback, 0); |
|
|
|
|
|
|
|
|
socket.BeginReceive(_lineBuffer, 0, _lineBuffer.Length, 0, ReceiveCallback, 0); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public LineReader(WrappedSocket socket, Func<string, object, bool> onLineRead, |
|
|
|
|
|
Action<Exception, object> onException, |
|
|
|
|
|
Action<byte[], int, int, object> onFinish, Encoding encoding, string delimiter, int maxLineBytes, |
|
|
|
|
|
object state) |
|
|
|
|
|
: this(socket, null, 0, 0, onLineRead, onException, onFinish, encoding, delimiter, maxLineBytes, state) |
|
|
|
|
|
|
|
|
public LineReader(int maxLineBytes, WrappedSocket socket, Func<string, object, bool> onLineRead, |
|
|
|
|
|
Action<Exception, object> onException, Action<byte[], int, int, object> onFinish, Encoding encoding, |
|
|
|
|
|
string delimiter, object state) |
|
|
|
|
|
: this( |
|
|
|
|
|
new byte[maxLineBytes], socket, null, 0, 0, onLineRead, onException, onFinish, encoding, delimiter, |
|
|
|
|
|
state) |
|
|
{ |
|
|
{ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|