Refactored HttpStreamAsyncResult.cs
This commit is contained in:
parent
4bf079b1a2
commit
a0ca7a8cb8
@ -135,9 +135,7 @@ namespace WebSocketSharp.Net
|
|||||||
if (count < 0 || offset > len - count)
|
if (count < 0 || offset > len - count)
|
||||||
throw new ArgumentOutOfRangeException ("'offset' + 'count' exceeds the size of buffer.");
|
throw new ArgumentOutOfRangeException ("'offset' + 'count' exceeds the size of buffer.");
|
||||||
|
|
||||||
var ares = new HttpStreamAsyncResult ();
|
var ares = new HttpStreamAsyncResult (callback, state);
|
||||||
ares.Callback = callback;
|
|
||||||
ares.State = state;
|
|
||||||
if (_noMoreData) {
|
if (_noMoreData) {
|
||||||
ares.Complete ();
|
ares.Complete ();
|
||||||
return ares;
|
return ares;
|
||||||
|
@ -1,113 +1,133 @@
|
|||||||
//
|
#region License
|
||||||
// HttpStreamAsyncResult.cs
|
/*
|
||||||
// Copied from System.Net.HttpStreamAsyncResult.cs
|
* HttpStreamAsyncResult.cs
|
||||||
//
|
*
|
||||||
// Authors:
|
* This code is derived from System.Net.HttpStreamAsyncResult.cs of Mono
|
||||||
// Gonzalo Paniagua Javier (gonzalo@novell.com)
|
* (http://www.mono-project.com).
|
||||||
//
|
*
|
||||||
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
|
* The MIT License
|
||||||
//
|
*
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining
|
* Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
|
||||||
// a copy of this software and associated documentation files (the
|
* Copyright (c) 2012-2014 sta.blockhead
|
||||||
// "Software"), to deal in the Software without restriction, including
|
*
|
||||||
// without limitation the rights to use, copy, modify, merge, publish,
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
// permit persons to whom the Software is furnished to do so, subject to
|
* in the Software without restriction, including without limitation the rights
|
||||||
// the following conditions:
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
//
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
// The above copyright notice and this permission notice shall be
|
* furnished to do so, subject to the following conditions:
|
||||||
// included in all copies or substantial portions of the Software.
|
*
|
||||||
//
|
* The above copyright notice and this permission notice shall be included in
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
* all copies or substantial portions of the Software.
|
||||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
*
|
||||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
//
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Authors
|
||||||
|
/*
|
||||||
|
* Authors:
|
||||||
|
* - Gonzalo Paniagua Javier <gonzalo@novell.com>
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace WebSocketSharp.Net {
|
namespace WebSocketSharp.Net
|
||||||
|
{
|
||||||
|
internal class HttpStreamAsyncResult : IAsyncResult
|
||||||
|
{
|
||||||
|
#region Private Fields
|
||||||
|
|
||||||
class HttpStreamAsyncResult : IAsyncResult {
|
private AsyncCallback _callback;
|
||||||
|
private bool _completed;
|
||||||
|
private object _state;
|
||||||
|
private object _sync;
|
||||||
|
private ManualResetEvent _waitHandle;
|
||||||
|
|
||||||
#region Private Fields
|
#endregion
|
||||||
|
|
||||||
bool completed;
|
#region Internal Fields
|
||||||
ManualResetEvent handle;
|
|
||||||
object locker = new object ();
|
|
||||||
|
|
||||||
#endregion
|
internal byte [] Buffer;
|
||||||
|
internal int Count;
|
||||||
|
internal Exception Error;
|
||||||
|
internal int Offset;
|
||||||
|
internal int SyncRead;
|
||||||
|
|
||||||
#region Internal Fields
|
#endregion
|
||||||
|
|
||||||
internal AsyncCallback Callback;
|
#region Public Constructors
|
||||||
internal int Count;
|
|
||||||
internal byte [] Buffer;
|
|
||||||
internal Exception Error;
|
|
||||||
internal int Offset;
|
|
||||||
internal object State;
|
|
||||||
internal int SyncRead;
|
|
||||||
|
|
||||||
#endregion
|
public HttpStreamAsyncResult (AsyncCallback callback, object state)
|
||||||
|
{
|
||||||
|
_callback = callback;
|
||||||
|
_state = state;
|
||||||
|
_sync = new object ();
|
||||||
|
}
|
||||||
|
|
||||||
#region Properties
|
#endregion
|
||||||
|
|
||||||
public object AsyncState {
|
#region Public Properties
|
||||||
get { return State; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public WaitHandle AsyncWaitHandle {
|
public object AsyncState {
|
||||||
get {
|
get {
|
||||||
lock (locker) {
|
return _state;
|
||||||
if (handle == null)
|
}
|
||||||
handle = new ManualResetEvent (completed);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return handle;
|
public WaitHandle AsyncWaitHandle {
|
||||||
}
|
get {
|
||||||
}
|
lock (_sync)
|
||||||
|
return _waitHandle ?? (_waitHandle = new ManualResetEvent (_completed));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public bool CompletedSynchronously {
|
public bool CompletedSynchronously {
|
||||||
get { return (SyncRead == Count); }
|
get {
|
||||||
}
|
return SyncRead == Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public bool IsCompleted {
|
public bool IsCompleted {
|
||||||
get {
|
get {
|
||||||
lock (locker) {
|
lock (_sync)
|
||||||
return completed;
|
return _completed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public Methods
|
#region Public Methods
|
||||||
|
|
||||||
public void Complete ()
|
public void Complete ()
|
||||||
{
|
{
|
||||||
lock (locker) {
|
lock (_sync) {
|
||||||
if (completed)
|
if (_completed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
completed = true;
|
_completed = true;
|
||||||
if (handle != null)
|
if (_waitHandle != null)
|
||||||
handle.Set ();
|
_waitHandle.Set ();
|
||||||
|
|
||||||
if (Callback != null)
|
if (_callback != null)
|
||||||
Callback.BeginInvoke (this, null, null);
|
_callback.BeginInvoke (this, ar => _callback.EndInvoke (ar), null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Complete (Exception e)
|
public void Complete (Exception exception)
|
||||||
{
|
{
|
||||||
Error = e;
|
Error = exception;
|
||||||
Complete ();
|
Complete ();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -173,12 +173,10 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
var nread = fillFromBuffer (buffer, offset, count);
|
var nread = fillFromBuffer (buffer, offset, count);
|
||||||
if (nread > 0 || nread == -1) {
|
if (nread > 0 || nread == -1) {
|
||||||
var ares = new HttpStreamAsyncResult ();
|
var ares = new HttpStreamAsyncResult (callback, state);
|
||||||
ares.Buffer = buffer;
|
ares.Buffer = buffer;
|
||||||
ares.Offset = offset;
|
ares.Offset = offset;
|
||||||
ares.Count = count;
|
ares.Count = count;
|
||||||
ares.Callback = callback;
|
|
||||||
ares.State = state;
|
|
||||||
ares.SyncRead = nread;
|
ares.SyncRead = nread;
|
||||||
ares.Complete ();
|
ares.Complete ();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user