mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-20 21:54:25 -04:00
imported signalr 1.1.3 into NzbDrone.
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Hosting
|
||||
{
|
||||
public static class HostConstants
|
||||
{
|
||||
/// <summary>
|
||||
/// The host should set this if they need to enable debug mode
|
||||
/// </summary>
|
||||
public static readonly string DebugMode = "debugMode";
|
||||
|
||||
/// <summary>
|
||||
/// The host should set this is web sockets can be supported
|
||||
/// </summary>
|
||||
public static readonly string SupportsWebSockets = "supportsWebSockets";
|
||||
|
||||
/// <summary>
|
||||
/// The host should set this if the web socket url is different
|
||||
/// </summary>
|
||||
public static readonly string WebSocketServerUrl = "webSocketServerUrl";
|
||||
|
||||
public static readonly string ShutdownToken = "shutdownToken";
|
||||
|
||||
public static readonly string InstanceName = "instanceName";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Principal;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Hosting
|
||||
{
|
||||
public class HostContext
|
||||
{
|
||||
public IRequest Request { get; private set; }
|
||||
public IResponse Response { get; private set; }
|
||||
public IDictionary<string, object> Items { get; private set; }
|
||||
|
||||
public HostContext(IRequest request, IResponse response)
|
||||
{
|
||||
Request = request;
|
||||
Response = response;
|
||||
Items = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Hosting
|
||||
{
|
||||
public static class HostContextExtensions
|
||||
{
|
||||
public static T GetValue<T>(this HostContext context, string key)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException("context");
|
||||
}
|
||||
|
||||
object value;
|
||||
if (context.Items.TryGetValue(key, out value))
|
||||
{
|
||||
return (T)value;
|
||||
}
|
||||
return default(T);
|
||||
}
|
||||
|
||||
public static bool IsDebuggingEnabled(this HostContext context)
|
||||
{
|
||||
return context.GetValue<bool>(HostConstants.DebugMode);
|
||||
}
|
||||
|
||||
public static bool SupportsWebSockets(this HostContext context)
|
||||
{
|
||||
// The server needs to implement IWebSocketRequest for websockets to be supported.
|
||||
// It also needs to set the flag in the items collection.
|
||||
return context.GetValue<bool>(HostConstants.SupportsWebSockets) &&
|
||||
context.Request is IWebSocketRequest;
|
||||
}
|
||||
|
||||
public static string WebSocketServerUrl(this HostContext context)
|
||||
{
|
||||
return context.GetValue<string>(HostConstants.WebSocketServerUrl);
|
||||
}
|
||||
|
||||
public static CancellationToken HostShutdownToken(this HostContext context)
|
||||
{
|
||||
return context.GetValue<CancellationToken>(HostConstants.ShutdownToken);
|
||||
}
|
||||
|
||||
public static string InstanceName(this HostContext context)
|
||||
{
|
||||
return context.GetValue<string>(HostConstants.InstanceName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Microsoft.AspNet.SignalR.Infrastructure;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Hosting
|
||||
{
|
||||
public static class HostDependencyResolverExtensions
|
||||
{
|
||||
public static void InitializeHost(this IDependencyResolver resolver, string instanceName, CancellationToken hostShutdownToken)
|
||||
{
|
||||
if (resolver == null)
|
||||
{
|
||||
throw new ArgumentNullException("resolver");
|
||||
}
|
||||
|
||||
if (String.IsNullOrEmpty(instanceName))
|
||||
{
|
||||
throw new ArgumentNullException("instanceName");
|
||||
}
|
||||
|
||||
// Initialize the performance counters
|
||||
resolver.InitializePerformanceCounters(instanceName, hostShutdownToken);
|
||||
|
||||
// Dispose the dependency resolver on host shut down (cleanly)
|
||||
resolver.InitializeResolverDispose(hostShutdownToken);
|
||||
}
|
||||
|
||||
private static void InitializePerformanceCounters(this IDependencyResolver resolver, string instanceName, CancellationToken hostShutdownToken)
|
||||
{
|
||||
var counters = resolver.Resolve<IPerformanceCounterManager>();
|
||||
if (counters != null)
|
||||
{
|
||||
counters.Initialize(instanceName, hostShutdownToken);
|
||||
}
|
||||
}
|
||||
|
||||
private static void InitializeResolverDispose(this IDependencyResolver resolver, CancellationToken hostShutdownToken)
|
||||
{
|
||||
// TODO: Guard against multiple calls to this
|
||||
|
||||
// When the host triggers the shutdown token, dispose the resolver
|
||||
hostShutdownToken.Register(state =>
|
||||
{
|
||||
((IDependencyResolver)state).Dispose();
|
||||
},
|
||||
resolver,
|
||||
useSynchronizationContext: false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// 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.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Hosting
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a connection to the client.
|
||||
/// </summary>
|
||||
public interface IResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a cancellation token that represents the client's lifetime.
|
||||
/// </summary>
|
||||
CancellationToken CancellationToken { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the content type of the response.
|
||||
/// </summary>
|
||||
string ContentType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Writes buffered data.
|
||||
/// </summary>
|
||||
/// <param name="data">The data to write to the buffer.</param>
|
||||
void Write(ArraySegment<byte> data);
|
||||
|
||||
/// <summary>
|
||||
/// Flushes the buffered response to the client.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents when the data has been flushed.</returns>
|
||||
Task Flush();
|
||||
|
||||
/// <summary>
|
||||
/// Closes the connection to the client.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents when the connection is closed.</returns>
|
||||
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "End", Justification = "Ends the response thus the name is appropriate.")]
|
||||
Task End();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Hosting
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a web socket.
|
||||
/// </summary>
|
||||
public interface IWebSocket
|
||||
{
|
||||
/// <summary>
|
||||
/// Invoked when data is sent over the websocket
|
||||
/// </summary>
|
||||
Action<string> OnMessage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when the websocket gracefully closes
|
||||
/// </summary>
|
||||
Action<bool> OnClose { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when there is an error
|
||||
/// </summary>
|
||||
Action<Exception> OnError { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Sends data over the websocket.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to send.</param>
|
||||
/// <returns>A <see cref="Task"/> that represents the send is complete.</returns>
|
||||
Task Send(string value);
|
||||
|
||||
/// <summary>
|
||||
/// Sends a chunk of data over the websocket ("endOfMessage" flag set to false.)
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns>A <see cref="Task"/> that represents the send is complete.</returns>
|
||||
Task SendChunk(ArraySegment<byte> message);
|
||||
|
||||
/// <summary>
|
||||
/// Sends a zero byte data chunk with the "endOfMessage" flag set to true.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="Task"/> that represents the flush is complete.</returns>
|
||||
Task Flush();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Hosting
|
||||
{
|
||||
public interface IWebSocketRequest : IRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Accepts an websocket request using the specified user function.
|
||||
/// </summary>
|
||||
/// <param name="callback">The callback that fires when the websocket is ready.</param>
|
||||
Task AcceptWebSocketRequest(Func<IWebSocket, Task> callback);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Hosting
|
||||
{
|
||||
/// <summary>
|
||||
/// Responsible for creating <see cref="PersistentConnection"/> instances.
|
||||
/// </summary>
|
||||
public class PersistentConnectionFactory
|
||||
{
|
||||
private readonly IDependencyResolver _resolver;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="PersistentConnectionFactory"/> class.
|
||||
/// </summary>
|
||||
/// <param name="resolver">The dependency resolver to use for when creating the <see cref="PersistentConnection"/>.</param>
|
||||
public PersistentConnectionFactory(IDependencyResolver resolver)
|
||||
{
|
||||
if (resolver == null)
|
||||
{
|
||||
throw new ArgumentNullException("resolver");
|
||||
}
|
||||
|
||||
_resolver = resolver;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instance of the specified type using the dependency resolver or the type's default constructor.
|
||||
/// </summary>
|
||||
/// <param name="connectionType">The type of <see cref="PersistentConnection"/> to create.</param>
|
||||
/// <returns>An instance of a <see cref="PersistentConnection"/>. </returns>
|
||||
public PersistentConnection CreateInstance(Type connectionType)
|
||||
{
|
||||
if (connectionType == null)
|
||||
{
|
||||
throw new ArgumentNullException("connectionType");
|
||||
}
|
||||
|
||||
var connection = (_resolver.Resolve(connectionType) ??
|
||||
Activator.CreateInstance(connectionType)) as PersistentConnection;
|
||||
|
||||
if (connection == null)
|
||||
{
|
||||
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Error_IsNotA, connectionType.FullName, typeof(PersistentConnection).FullName));
|
||||
}
|
||||
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Hosting
|
||||
{
|
||||
internal static class RequestExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a value from the QueryString, and if it's null or empty, gets it from the Form instead.
|
||||
/// </summary>
|
||||
public static string QueryStringOrForm(this IRequest request, string key)
|
||||
{
|
||||
var value = request.QueryString[key];
|
||||
if (String.IsNullOrEmpty(value))
|
||||
{
|
||||
value = request.Form[key];
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNet.SignalR.Hosting
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="IResponse"/>.
|
||||
/// </summary>
|
||||
public static class ResponseExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Closes the connection to a client with optional data.
|
||||
/// </summary>
|
||||
/// <param name="response">The <see cref="IResponse"/>.</param>
|
||||
/// <param name="data">The data to write to the connection.</param>
|
||||
/// <returns>A task that represents when the connection is closed.</returns>
|
||||
public static Task End(this IResponse response, string data)
|
||||
{
|
||||
if (response == null)
|
||||
{
|
||||
throw new ArgumentNullException("response");
|
||||
}
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(data);
|
||||
response.Write(new ArraySegment<byte>(bytes, 0, bytes.Length));
|
||||
return response.End();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user