Upgraded SignalR to 1.2.2

This commit is contained in:
Keivan Beigi
2015-02-07 07:02:45 -08:00
parent 15b0bc0333
commit 127e38feb7
33 changed files with 367 additions and 239 deletions

View File

@@ -1,20 +1,25 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
namespace Microsoft.AspNet.SignalR.Infrastructure
{
internal static class CancellationTokenExtensions
{
private delegate CancellationTokenRegistration RegisterDelegate(ref CancellationToken token, Action<object> callback, object state);
private static readonly RegisterDelegate _tokenRegister = ResolveRegisterDelegate();
public static IDisposable SafeRegister(this CancellationToken cancellationToken, Action<object> callback, object state)
{
var callbackWrapper = new CancellationCallbackWrapper(callback, state);
// Ensure delegate continues to use the C# Compiler static delegate caching optimization.
CancellationTokenRegistration registration = cancellationToken.Register(s => Cancel(s),
callbackWrapper,
useSynchronizationContext: false);
CancellationTokenRegistration registration = _tokenRegister(ref cancellationToken, s => InvokeCallback(s), callbackWrapper);
var disposeCancellationState = new DiposeCancellationState(callbackWrapper, registration);
@@ -22,7 +27,7 @@ namespace Microsoft.AspNet.SignalR.Infrastructure
return new DisposableAction(s => Dispose(s), disposeCancellationState);
}
private static void Cancel(object state)
private static void InvokeCallback(object state)
{
((CancellationCallbackWrapper)state).TryInvoke();
}
@@ -32,6 +37,56 @@ namespace Microsoft.AspNet.SignalR.Infrastructure
((DiposeCancellationState)state).TryDispose();
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "This method should never throw since it runs as part of field initialzation")]
private static RegisterDelegate ResolveRegisterDelegate()
{
// The fallback is just a normal register that capatures the execution context.
RegisterDelegate fallback = (ref CancellationToken token, Action<object> callback, object state) =>
{
return token.Register(callback, state);
};
#if NETFX_CORE || PORTABLE
return fallback;
#else
MethodInfo methodInfo = null;
try
{
// By default we don't want to capture the execution context,
// since this is internal we need to create a delegate to this up front
methodInfo = typeof(CancellationToken).GetMethod("InternalRegisterWithoutEC",
BindingFlags.NonPublic | BindingFlags.Instance,
binder: null,
types: new[] { typeof(Action<object>), typeof(object) },
modifiers: null);
}
catch
{
// Swallow this exception. Being extra paranoid, we don't want anything to break in case this dirty
// reflection hack fails for any reason
}
// If the method was removed then fallback to the regular method
if (methodInfo == null)
{
return fallback;
}
try
{
return (RegisterDelegate)Delegate.CreateDelegate(typeof(RegisterDelegate), null, methodInfo);
}
catch
{
// If this fails for whatever reason just fallback to normal register
return fallback;
}
#endif
}
private class DiposeCancellationState
{
private readonly CancellationCallbackWrapper _callbackWrapper;
@@ -48,8 +103,14 @@ namespace Microsoft.AspNet.SignalR.Infrastructure
// This normally waits until the callback is finished invoked but we don't care
if (_callbackWrapper.TrySetInvoked())
{
// Bug #1549, .NET 4.0 has a bug where this throws if the CTS
_registration.Dispose();
try
{
_registration.Dispose();
}
catch (ObjectDisposedException)
{
// Bug #1549, .NET 4.0 has a bug where this throws if the CTS is disposed.
}
}
}
}