Refactored HttpStreamAsyncResult.cs

master
sta 12 years ago
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
{
class HttpStreamAsyncResult : IAsyncResult { internal class HttpStreamAsyncResult : IAsyncResult
{
#region Private Fields #region Private Fields
bool completed; private AsyncCallback _callback;
ManualResetEvent handle; private bool _completed;
object locker = new object (); private object _state;
private object _sync;
#endregion private ManualResetEvent _waitHandle;
#region Internal Fields #endregion
internal AsyncCallback Callback; #region Internal Fields
internal int Count;
internal byte [] Buffer; internal byte [] Buffer;
internal Exception Error; internal int Count;
internal int Offset; internal Exception Error;
internal object State; internal int Offset;
internal int SyncRead; internal int SyncRead;
#endregion #endregion
#region Properties #region Public Constructors
public object AsyncState { public HttpStreamAsyncResult (AsyncCallback callback, object state)
get { return State; } {
} _callback = callback;
_state = state;
public WaitHandle AsyncWaitHandle { _sync = new object ();
get { }
lock (locker) {
if (handle == null) #endregion
handle = new ManualResetEvent (completed);
} #region Public Properties
return handle; public object AsyncState {
} get {
} return _state;
}
public bool CompletedSynchronously { }
get { return (SyncRead == Count); }
} public WaitHandle AsyncWaitHandle {
get {
public bool IsCompleted { lock (_sync)
get { return _waitHandle ?? (_waitHandle = new ManualResetEvent (_completed));
lock (locker) { }
return completed; }
}
} public bool CompletedSynchronously {
} get {
return SyncRead == Count;
#endregion }
}
#region Public Methods
public bool IsCompleted {
public void Complete () get {
{ lock (_sync)
lock (locker) { return _completed;
if (completed) }
return; }
completed = true; #endregion
if (handle != null)
handle.Set (); #region Public Methods
if (Callback != null) public void Complete ()
Callback.BeginInvoke (this, null, null); {
} lock (_sync) {
} if (_completed)
return;
public void Complete (Exception e)
{ _completed = true;
Error = e; if (_waitHandle != null)
Complete (); _waitHandle.Set ();
}
if (_callback != null)
#endregion _callback.BeginInvoke (this, ar => _callback.EndInvoke (ar), null);
} }
}
public void Complete (Exception exception)
{
Error = exception;
Complete ();
}
#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…
Cancel
Save