Refactored a few for ChunkedRequestStream.cs
This commit is contained in:
parent
195e3c9adb
commit
e73cfe4bc2
@ -44,14 +44,9 @@ namespace WebSocketSharp.Net
|
|||||||
{
|
{
|
||||||
internal class ChunkedRequestStream : RequestStream
|
internal class ChunkedRequestStream : RequestStream
|
||||||
{
|
{
|
||||||
#region Private Const Fields
|
|
||||||
|
|
||||||
private const int _bufferSize = 8192;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Private Fields
|
#region Private Fields
|
||||||
|
|
||||||
|
private const int _bufferSize = 8192;
|
||||||
private HttpListenerContext _context;
|
private HttpListenerContext _context;
|
||||||
private ChunkStream _decoder;
|
private ChunkStream _decoder;
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
@ -71,9 +66,9 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public Properties
|
#region Internal Properties
|
||||||
|
|
||||||
public ChunkStream Decoder {
|
internal ChunkStream Decoder {
|
||||||
get {
|
get {
|
||||||
return _decoder;
|
return _decoder;
|
||||||
}
|
}
|
||||||
@ -89,17 +84,17 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
private void onRead (IAsyncResult asyncResult)
|
private void onRead (IAsyncResult asyncResult)
|
||||||
{
|
{
|
||||||
var readState = (ReadBufferState) asyncResult.AsyncState;
|
var rstate = (ReadBufferState) asyncResult.AsyncState;
|
||||||
var ares = readState.AsyncResult;
|
var ares = rstate.AsyncResult;
|
||||||
try {
|
try {
|
||||||
var nread = base.EndRead (asyncResult);
|
var nread = base.EndRead (asyncResult);
|
||||||
_decoder.Write (ares.Buffer, ares.Offset, nread);
|
_decoder.Write (ares.Buffer, ares.Offset, nread);
|
||||||
nread = _decoder.Read (readState.Buffer, readState.Offset, readState.Count);
|
nread = _decoder.Read (rstate.Buffer, rstate.Offset, rstate.Count);
|
||||||
readState.Offset += nread;
|
rstate.Offset += nread;
|
||||||
readState.Count -= nread;
|
rstate.Count -= nread;
|
||||||
if (readState.Count == 0 || !_decoder.WantMore || nread == 0) {
|
if (rstate.Count == 0 || !_decoder.WantMore || nread == 0) {
|
||||||
_noMoreData = !_decoder.WantMore && nread == 0;
|
_noMoreData = !_decoder.WantMore && nread == 0;
|
||||||
ares.Count = readState.InitialCount - readState.Count;
|
ares.Count = rstate.InitialCount - rstate.Count;
|
||||||
ares.Complete ();
|
ares.Complete ();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@ -107,7 +102,7 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
ares.Offset = 0;
|
ares.Offset = 0;
|
||||||
ares.Count = Math.Min (_bufferSize, _decoder.ChunkLeft + 6);
|
ares.Count = Math.Min (_bufferSize, _decoder.ChunkLeft + 6);
|
||||||
base.BeginRead (ares.Buffer, ares.Offset, ares.Count, onRead, readState);
|
base.BeginRead (ares.Buffer, ares.Offset, ares.Count, onRead, rstate);
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
_context.Connection.SendError (ex.Message, 400);
|
_context.Connection.SendError (ex.Message, 400);
|
||||||
@ -128,12 +123,16 @@ namespace WebSocketSharp.Net
|
|||||||
if (buffer == null)
|
if (buffer == null)
|
||||||
throw new ArgumentNullException ("buffer");
|
throw new ArgumentNullException ("buffer");
|
||||||
|
|
||||||
var len = buffer.Length;
|
if (offset < 0)
|
||||||
if (offset < 0 || offset > len)
|
throw new ArgumentOutOfRangeException ("offset", "A negative value.");
|
||||||
throw new ArgumentOutOfRangeException ("'offset' exceeds the size of buffer.");
|
|
||||||
|
|
||||||
if (count < 0 || offset > len - count)
|
if (count < 0)
|
||||||
throw new ArgumentOutOfRangeException ("'offset' + 'count' exceeds the size of buffer.");
|
throw new ArgumentOutOfRangeException ("count", "A negative value.");
|
||||||
|
|
||||||
|
var len = buffer.Length;
|
||||||
|
if (offset + count > len)
|
||||||
|
throw new ArgumentException (
|
||||||
|
"The sum of 'offset' and 'count' is greater than 'buffer' length.");
|
||||||
|
|
||||||
var ares = new HttpStreamAsyncResult (callback, state);
|
var ares = new HttpStreamAsyncResult (callback, state);
|
||||||
if (_noMoreData) {
|
if (_noMoreData) {
|
||||||
@ -164,9 +163,9 @@ namespace WebSocketSharp.Net
|
|||||||
ares.Offset = 0;
|
ares.Offset = 0;
|
||||||
ares.Count = _bufferSize;
|
ares.Count = _bufferSize;
|
||||||
|
|
||||||
var readState = new ReadBufferState (buffer, offset, count, ares);
|
var rstate = new ReadBufferState (buffer, offset, count, ares);
|
||||||
readState.InitialCount += nread;
|
rstate.InitialCount += nread;
|
||||||
base.BeginRead (ares.Buffer, ares.Offset, ares.Count, onRead, readState);
|
base.BeginRead (ares.Buffer, ares.Offset, ares.Count, onRead, rstate);
|
||||||
|
|
||||||
return ares;
|
return ares;
|
||||||
}
|
}
|
||||||
@ -190,7 +189,7 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
var ares = asyncResult as HttpStreamAsyncResult;
|
var ares = asyncResult as HttpStreamAsyncResult;
|
||||||
if (ares == null)
|
if (ares == null)
|
||||||
throw new ArgumentException ("Wrong IAsyncResult.", "asyncResult");
|
throw new ArgumentException ("A wrong IAsyncResult.", "asyncResult");
|
||||||
|
|
||||||
if (!ares.IsCompleted)
|
if (!ares.IsCompleted)
|
||||||
ares.AsyncWaitHandle.WaitOne ();
|
ares.AsyncWaitHandle.WaitOne ();
|
||||||
|
Loading…
Reference in New Issue
Block a user