mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-05-01 22:36:33 +01:00
More DI
This commit is contained in:
@@ -112,6 +112,8 @@
|
||||
<Compile Include="Management\NetworkManager.cs" />
|
||||
<Compile Include="Management\NetworkShares.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Web\ServerFactory.cs" />
|
||||
<Compile Include="Web\NativeWebSocket.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MediaBrowser.Common\MediaBrowser.Common.csproj">
|
||||
|
||||
@@ -4,6 +4,7 @@ using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Common.Kernel;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
using ServiceStack.Api.Swagger;
|
||||
using ServiceStack.Common.Web;
|
||||
using ServiceStack.Configuration;
|
||||
@@ -60,6 +61,12 @@ namespace MediaBrowser.Networking.Web
|
||||
/// <value>The HTTP listener.</value>
|
||||
private IDisposable HttpListener { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the protobuf serializer.
|
||||
/// </summary>
|
||||
/// <value>The protobuf serializer.</value>
|
||||
private IProtobufSerializer ProtobufSerializer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when [web socket connected].
|
||||
/// </summary>
|
||||
@@ -82,17 +89,22 @@ namespace MediaBrowser.Networking.Web
|
||||
/// </summary>
|
||||
/// <param name="applicationHost">The application host.</param>
|
||||
/// <param name="kernel">The kernel.</param>
|
||||
/// <param name="protobufSerializer">The protobuf serializer.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <param name="serverName">Name of the server.</param>
|
||||
/// <param name="defaultRedirectpath">The default redirectpath.</param>
|
||||
/// <exception cref="System.ArgumentNullException">urlPrefix</exception>
|
||||
public HttpServer(IApplicationHost applicationHost, IKernel kernel, ILogger logger, string serverName, string defaultRedirectpath)
|
||||
public HttpServer(IApplicationHost applicationHost, IKernel kernel, IProtobufSerializer protobufSerializer, ILogger logger, string serverName, string defaultRedirectpath)
|
||||
: base()
|
||||
{
|
||||
if (kernel == null)
|
||||
{
|
||||
throw new ArgumentNullException("kernel");
|
||||
}
|
||||
if (protobufSerializer == null)
|
||||
{
|
||||
throw new ArgumentNullException("protobufSerializer");
|
||||
}
|
||||
if (logger == null)
|
||||
{
|
||||
throw new ArgumentNullException("logger");
|
||||
@@ -112,6 +124,7 @@ namespace MediaBrowser.Networking.Web
|
||||
|
||||
ServerName = serverName;
|
||||
DefaultRedirectPath = defaultRedirectpath;
|
||||
ProtobufSerializer = protobufSerializer;
|
||||
_logger = logger;
|
||||
ApplicationHost = applicationHost;
|
||||
|
||||
@@ -121,7 +134,7 @@ namespace MediaBrowser.Networking.Web
|
||||
Kernel = kernel;
|
||||
|
||||
EndpointHost.ConfigureHost(this, ServerName, CreateServiceManager());
|
||||
ContentTypeFilters.Register(ContentType.ProtoBuf, (reqCtx, res, stream) => Kernel.ProtobufSerializer.SerializeToStream(res, stream), (type, stream) => Kernel.ProtobufSerializer.DeserializeFromStream(stream, type));
|
||||
ContentTypeFilters.Register(ContentType.ProtoBuf, (reqCtx, res, stream) => ProtobufSerializer.SerializeToStream(res, stream), (type, stream) => ProtobufSerializer.DeserializeFromStream(stream, type));
|
||||
|
||||
Init();
|
||||
}
|
||||
@@ -132,6 +145,10 @@ namespace MediaBrowser.Networking.Web
|
||||
/// <param name="container">The container.</param>
|
||||
public override void Configure(Container container)
|
||||
{
|
||||
JsConfig.DateHandler = JsonDateHandler.ISO8601;
|
||||
JsConfig.ExcludeTypeInfo = true;
|
||||
JsConfig.IncludeNullValues = false;
|
||||
|
||||
SetConfig(new EndpointHostConfig
|
||||
{
|
||||
DefaultRedirectPath = DefaultRedirectPath,
|
||||
|
||||
145
MediaBrowser.Networking/Web/NativeWebSocket.cs
Normal file
145
MediaBrowser.Networking/Web/NativeWebSocket.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using MediaBrowser.Model.Logging;
|
||||
using System;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediaBrowser.Common.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// Class NativeWebSocket
|
||||
/// </summary>
|
||||
public class NativeWebSocket : IWebSocket
|
||||
{
|
||||
/// <summary>
|
||||
/// The logger
|
||||
/// </summary>
|
||||
private readonly ILogger _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the web socket.
|
||||
/// </summary>
|
||||
/// <value>The web socket.</value>
|
||||
private WebSocket WebSocket { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="NativeWebSocket" /> class.
|
||||
/// </summary>
|
||||
/// <param name="socket">The socket.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <exception cref="System.ArgumentNullException">socket</exception>
|
||||
public NativeWebSocket(WebSocket socket, ILogger logger)
|
||||
{
|
||||
if (socket == null)
|
||||
{
|
||||
throw new ArgumentNullException("socket");
|
||||
}
|
||||
|
||||
if (logger == null)
|
||||
{
|
||||
throw new ArgumentNullException("logger");
|
||||
}
|
||||
|
||||
_logger = logger;
|
||||
WebSocket = socket;
|
||||
|
||||
Receive();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the state.
|
||||
/// </summary>
|
||||
/// <value>The state.</value>
|
||||
public WebSocketState State
|
||||
{
|
||||
get { return WebSocket.State; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Receives this instance.
|
||||
/// </summary>
|
||||
private async void Receive()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
byte[] bytes;
|
||||
|
||||
try
|
||||
{
|
||||
bytes = await ReceiveBytesAsync(CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
catch (WebSocketException ex)
|
||||
{
|
||||
_logger.ErrorException("Error reveiving web socket message", ex);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (OnReceiveDelegate != null)
|
||||
{
|
||||
OnReceiveDelegate(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Receives the async.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task{WebSocketMessageInfo}.</returns>
|
||||
/// <exception cref="System.Net.WebSockets.WebSocketException">Connection closed</exception>
|
||||
private async Task<byte[]> ReceiveBytesAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var bytes = new byte[4096];
|
||||
var buffer = new ArraySegment<byte>(bytes);
|
||||
|
||||
var result = await WebSocket.ReceiveAsync(buffer, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (result.CloseStatus.HasValue)
|
||||
{
|
||||
throw new WebSocketException("Connection closed");
|
||||
}
|
||||
|
||||
return buffer.Array;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the async.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes.</param>
|
||||
/// <param name="type">The type.</param>
|
||||
/// <param name="endOfMessage">if set to <c>true</c> [end of message].</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task.</returns>
|
||||
public Task SendAsync(byte[] bytes, WebSocketMessageType type, bool endOfMessage, CancellationToken cancellationToken)
|
||||
{
|
||||
return WebSocket.SendAsync(new ArraySegment<byte>(bytes), type, true, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases unmanaged and - optionally - managed resources.
|
||||
/// </summary>
|
||||
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
||||
protected virtual void Dispose(bool dispose)
|
||||
{
|
||||
if (dispose)
|
||||
{
|
||||
WebSocket.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the receive action.
|
||||
/// </summary>
|
||||
/// <value>The receive action.</value>
|
||||
public Action<byte[]> OnReceiveDelegate { get; set; }
|
||||
}
|
||||
}
|
||||
28
MediaBrowser.Networking/Web/ServerFactory.cs
Normal file
28
MediaBrowser.Networking/Web/ServerFactory.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using MediaBrowser.Common.Kernel;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
|
||||
namespace MediaBrowser.Networking.Web
|
||||
{
|
||||
/// <summary>
|
||||
/// Class ServerFactory
|
||||
/// </summary>
|
||||
public static class ServerFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates the server.
|
||||
/// </summary>
|
||||
/// <param name="applicationHost">The application host.</param>
|
||||
/// <param name="kernel">The kernel.</param>
|
||||
/// <param name="protobufSerializer">The protobuf serializer.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <param name="serverName">Name of the server.</param>
|
||||
/// <param name="defaultRedirectpath">The default redirectpath.</param>
|
||||
/// <returns>IHttpServer.</returns>
|
||||
public static IHttpServer CreateServer(IApplicationHost applicationHost, IKernel kernel, IProtobufSerializer protobufSerializer, ILogger logger, string serverName, string defaultRedirectpath)
|
||||
{
|
||||
return new HttpServer(applicationHost, kernel, protobufSerializer, logger, serverName, defaultRedirectpath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
using Alchemy.Classes;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Common.Serialization;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using System;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -83,9 +83,9 @@ namespace MediaBrowser.Networking.WebSocket
|
||||
{
|
||||
try
|
||||
{
|
||||
var messageResult = JsonSerializer.DeserializeFromString<WebSocketMessageInfo>(json);
|
||||
var bytes = Encoding.UTF8.GetBytes(json);
|
||||
|
||||
OnReceiveDelegate(messageResult);
|
||||
OnReceiveDelegate(bytes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -128,6 +128,6 @@ namespace MediaBrowser.Networking.WebSocket
|
||||
/// Gets or sets the receive action.
|
||||
/// </summary>
|
||||
/// <value>The receive action.</value>
|
||||
public Action<WebSocketMessageInfo> OnReceiveDelegate { get; set; }
|
||||
public Action<byte[]> OnReceiveDelegate { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user