Refactored ListenerAsyncResult.cs

This commit is contained in:
sta 2014-10-27 14:37:45 +09:00
parent 72867a26da
commit 713c258989

View File

@ -50,10 +50,10 @@ namespace WebSocketSharp.Net
private bool _completed; private bool _completed;
private HttpListenerContext _context; private HttpListenerContext _context;
private Exception _exception; private Exception _exception;
private ManualResetEvent _waitHandle;
private object _state; private object _state;
private object _sync; private object _sync;
private bool _syncCompleted; private bool _syncCompleted;
private ManualResetEvent _waitHandle;
#endregion #endregion
@ -105,20 +105,6 @@ namespace WebSocketSharp.Net
#endregion #endregion
#region Private Methods
private static void invokeCallback (object state)
{
try {
var ares = (ListenerAsyncResult) state;
ares._callback (ares);
}
catch {
}
}
#endregion
#region Internal Methods #region Internal Methods
internal void Complete (Exception exception) internal void Complete (Exception exception)
@ -133,7 +119,15 @@ namespace WebSocketSharp.Net
_waitHandle.Set (); _waitHandle.Set ();
if (_callback != null) if (_callback != null)
ThreadPool.UnsafeQueueUserWorkItem (invokeCallback, this); ThreadPool.UnsafeQueueUserWorkItem (
state => {
try {
_callback (this);
}
catch {
}
},
null);
} }
} }
@ -145,17 +139,17 @@ namespace WebSocketSharp.Net
internal void Complete (HttpListenerContext context, bool syncCompleted) internal void Complete (HttpListenerContext context, bool syncCompleted)
{ {
var listener = context.Listener; var listener = context.Listener;
var scheme = listener.SelectAuthenticationScheme (context); var schm = listener.SelectAuthenticationScheme (context);
if (scheme == AuthenticationSchemes.None) { if (schm == AuthenticationSchemes.None) {
context.Response.Close (HttpStatusCode.Forbidden); context.Response.Close (HttpStatusCode.Forbidden);
listener.BeginGetContext (this); listener.BeginGetContext (this);
return; return;
} }
var header = context.Request.Headers ["Authorization"]; var res = context.Request.Headers["Authorization"];
if (scheme == AuthenticationSchemes.Basic && if (schm == AuthenticationSchemes.Basic &&
(header == null || !header.StartsWith ("basic", StringComparison.OrdinalIgnoreCase))) { (res == null || !res.StartsWith ("basic", StringComparison.OrdinalIgnoreCase))) {
context.Response.CloseWithAuthChallenge ( context.Response.CloseWithAuthChallenge (
AuthenticationChallenge.CreateBasicChallenge (listener.Realm).ToBasicString ()); AuthenticationChallenge.CreateBasicChallenge (listener.Realm).ToBasicString ());
@ -163,8 +157,8 @@ namespace WebSocketSharp.Net
return; return;
} }
if (scheme == AuthenticationSchemes.Digest && if (schm == AuthenticationSchemes.Digest &&
(header == null || !header.StartsWith ("digest", StringComparison.OrdinalIgnoreCase))) { (res == null || !res.StartsWith ("digest", StringComparison.OrdinalIgnoreCase))) {
context.Response.CloseWithAuthChallenge ( context.Response.CloseWithAuthChallenge (
AuthenticationChallenge.CreateDigestChallenge (listener.Realm).ToDigestString ()); AuthenticationChallenge.CreateDigestChallenge (listener.Realm).ToDigestString ());
@ -181,7 +175,15 @@ namespace WebSocketSharp.Net
_waitHandle.Set (); _waitHandle.Set ();
if (_callback != null) if (_callback != null)
ThreadPool.UnsafeQueueUserWorkItem (invokeCallback, this); ThreadPool.UnsafeQueueUserWorkItem (
state => {
try {
_callback (this);
}
catch {
}
},
null);
} }
} }