Refactored ChunkStream.cs
This commit is contained in:
parent
73a4cae782
commit
cafac4ebc5
@ -100,7 +100,7 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
public bool WantMore {
|
public bool WantMore {
|
||||||
get {
|
get {
|
||||||
return _chunkRead != _chunkSize || _chunkSize != 0 || _state != InputChunkState.None;
|
return _state != InputChunkState.End;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,10 +108,11 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
#region Private Methods
|
#region Private Methods
|
||||||
|
|
||||||
private int readFromChunks (byte[] buffer, int offset, int count)
|
private int read (byte[] buffer, int offset, int count)
|
||||||
{
|
{
|
||||||
var cnt = _chunks.Count;
|
|
||||||
var nread = 0;
|
var nread = 0;
|
||||||
|
|
||||||
|
var cnt = _chunks.Count;
|
||||||
for (var i = 0; i < cnt; i++) {
|
for (var i = 0; i < cnt; i++) {
|
||||||
var chunk = _chunks[i];
|
var chunk = _chunks[i];
|
||||||
if (chunk == null)
|
if (chunk == null)
|
||||||
@ -203,14 +204,14 @@ namespace WebSocketSharp.Net
|
|||||||
return InputChunkState.Data;
|
return InputChunkState.Data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputChunkState setHeaders (byte[] buffer, ref int offset, int length)
|
private InputChunkState setTrailer (byte[] buffer, ref int offset, int length)
|
||||||
{
|
{
|
||||||
// 0\r\n\r\n
|
// Check if no trailer.
|
||||||
if (_trailerState == 2 && buffer[offset] == 13 && _saved.Length == 0) {
|
if (_trailerState == 2 && buffer[offset] == 13 && _saved.Length == 0) {
|
||||||
offset++;
|
offset++;
|
||||||
if (offset < length && buffer[offset] == 10) {
|
if (offset < length && buffer[offset] == 10) {
|
||||||
offset++;
|
offset++;
|
||||||
return InputChunkState.None;
|
return InputChunkState.End;
|
||||||
}
|
}
|
||||||
|
|
||||||
offset--;
|
offset--;
|
||||||
@ -251,7 +252,7 @@ namespace WebSocketSharp.Net
|
|||||||
while ((line = reader.ReadLine ()) != null && line.Length > 0)
|
while ((line = reader.ReadLine ()) != null && line.Length > 0)
|
||||||
_headers.Add (line);
|
_headers.Add (line);
|
||||||
|
|
||||||
return InputChunkState.None;
|
return InputChunkState.End;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void throwProtocolViolation (string message)
|
private static void throwProtocolViolation (string message)
|
||||||
@ -261,6 +262,9 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
private void write (byte[] buffer, ref int offset, int length)
|
private void write (byte[] buffer, ref int offset, int length)
|
||||||
{
|
{
|
||||||
|
if (_state == InputChunkState.End)
|
||||||
|
throwProtocolViolation ("The chunks were ended.");
|
||||||
|
|
||||||
if (_state == InputChunkState.None) {
|
if (_state == InputChunkState.None) {
|
||||||
_state = setChunkSize (buffer, ref offset, length);
|
_state = setChunkSize (buffer, ref offset, length);
|
||||||
if (_state == InputChunkState.None)
|
if (_state == InputChunkState.None)
|
||||||
@ -286,7 +290,7 @@ namespace WebSocketSharp.Net
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (_state == InputChunkState.Trailer && offset < length) {
|
if (_state == InputChunkState.Trailer && offset < length) {
|
||||||
_state = setHeaders (buffer, ref offset, length);
|
_state = setTrailer (buffer, ref offset, length);
|
||||||
if (_state == InputChunkState.Trailer)
|
if (_state == InputChunkState.Trailer)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -299,9 +303,6 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
private InputChunkState writeData (byte[] buffer, ref int offset, int length)
|
private InputChunkState writeData (byte[] buffer, ref int offset, int length)
|
||||||
{
|
{
|
||||||
if (_chunkSize == 0)
|
|
||||||
return InputChunkState.DataEnded;
|
|
||||||
|
|
||||||
var cnt = length - offset;
|
var cnt = length - offset;
|
||||||
var left = _chunkSize - _chunkRead;
|
var left = _chunkSize - _chunkRead;
|
||||||
if (cnt > left)
|
if (cnt > left)
|
||||||
@ -319,18 +320,31 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Internal Methods
|
||||||
|
|
||||||
|
internal void ResetBuffer ()
|
||||||
|
{
|
||||||
|
_chunkRead = 0;
|
||||||
|
_chunkSize = -1;
|
||||||
|
_chunks.Clear ();
|
||||||
|
}
|
||||||
|
|
||||||
|
internal int WriteAndReadBack (byte[] buffer, int offset, int writeCount, int readCount)
|
||||||
|
{
|
||||||
|
Write (buffer, offset, writeCount);
|
||||||
|
return Read (buffer, offset, readCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region Public Methods
|
#region Public Methods
|
||||||
|
|
||||||
public int Read (byte[] buffer, int offset, int count)
|
public int Read (byte[] buffer, int offset, int count)
|
||||||
{
|
{
|
||||||
return readFromChunks (buffer, offset, count);
|
if (count <= 0)
|
||||||
}
|
return 0;
|
||||||
|
|
||||||
public void ResetBuffer ()
|
return read (buffer, offset, count);
|
||||||
{
|
|
||||||
_chunkSize = -1;
|
|
||||||
_chunkRead = 0;
|
|
||||||
_chunks.Clear ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Write (byte[] buffer, int offset, int count)
|
public void Write (byte[] buffer, int offset, int count)
|
||||||
@ -341,12 +355,6 @@ namespace WebSocketSharp.Net
|
|||||||
write (buffer, ref offset, offset + count);
|
write (buffer, ref offset, offset + count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WriteAndReadBack (byte[] buffer, int offset, int count, ref int read)
|
|
||||||
{
|
|
||||||
Write (buffer, offset, read);
|
|
||||||
read = readFromChunks (buffer, offset, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user