Rewrite WebSocket handling code

This commit is contained in:
Bond_009
2019-12-17 23:15:02 +01:00
committed by Bond_009
parent ce7744806c
commit 976459d3e8
23 changed files with 325 additions and 971 deletions

View File

@@ -92,7 +92,5 @@ namespace MediaBrowser.Controller
string ReverseVirtualPath(string path);
Task ExecuteHttpHandlerAsync(HttpContext context, Func<Task> next);
Task ExecuteWebsocketHandlerAsync(HttpContext context, Func<Task> next);
}
}

View File

@@ -154,7 +154,6 @@ namespace MediaBrowser.Controller.Net
{
MessageType = Name,
Data = data
}, cancellationToken).ConfigureAwait(false);
state.DateLastSendUtc = DateTime.UtcNow;

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Services;
@@ -19,11 +18,6 @@ namespace MediaBrowser.Controller.Net
/// <value>The URL prefix.</value>
string[] UrlPrefixes { get; }
/// <summary>
/// Stops this instance.
/// </summary>
void Stop();
/// <summary>
/// Occurs when [web socket connected].
/// </summary>
@@ -39,23 +33,11 @@ namespace MediaBrowser.Controller.Net
/// </summary>
string GlobalResponse { get; set; }
/// <summary>
/// Sends the http context to the socket listener
/// </summary>
/// <param name="ctx"></param>
/// <returns></returns>
Task ProcessWebSocketRequest(HttpContext ctx);
/// <summary>
/// The HTTP request handler
/// </summary>
/// <param name="httpReq"></param>
/// <param name="urlString"></param>
/// <param name="host"></param>
/// <param name="localPath"></param>
/// <param name="cancellationToken"></param>
/// <param name="context"></param>
/// <returns></returns>
Task RequestHandler(IHttpRequest httpReq, string urlString, string host, string localPath,
CancellationToken cancellationToken);
Task RequestHandler(HttpContext context);
}
}

View File

@@ -1,9 +1,9 @@
using System;
using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Services;
using Microsoft.AspNetCore.Http;
namespace MediaBrowser.Controller.Net
@@ -15,12 +15,6 @@ namespace MediaBrowser.Controller.Net
/// </summary>
event EventHandler<EventArgs> Closed;
/// <summary>
/// Gets the id.
/// </summary>
/// <value>The id.</value>
Guid Id { get; }
/// <summary>
/// Gets the last activity date.
/// </summary>
@@ -32,6 +26,7 @@ namespace MediaBrowser.Controller.Net
/// </summary>
/// <value>The URL.</value>
string Url { get; set; }
/// <summary>
/// Gets or sets the query string.
/// </summary>
@@ -54,7 +49,7 @@ namespace MediaBrowser.Controller.Net
/// Gets the remote end point.
/// </summary>
/// <value>The remote end point.</value>
string RemoteEndPoint { get; }
IPAddress RemoteEndPoint { get; }
/// <summary>
/// Sends a message asynchronously.
@@ -66,21 +61,6 @@ namespace MediaBrowser.Controller.Net
/// <exception cref="ArgumentNullException">message</exception>
Task SendAsync<T>(WebSocketMessage<T> message, CancellationToken cancellationToken);
/// <summary>
/// Sends a message asynchronously.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
Task SendAsync(byte[] buffer, CancellationToken cancellationToken);
/// <summary>
/// Sends a message asynchronously.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
/// <exception cref="ArgumentNullException">buffer</exception>
Task SendAsync(string text, CancellationToken cancellationToken);
Task ProcessAsync(CancellationToken cancellationToken = default);
}
}

View File

@@ -1,3 +1,4 @@
using System;
using System.Threading;
using System.Threading.Tasks;
@@ -20,6 +21,6 @@ namespace MediaBrowser.Controller.Session
/// <summary>
/// Sends the message.
/// </summary>
Task SendMessage<T>(string name, string messageId, T data, ISessionController[] allControllers, CancellationToken cancellationToken);
Task SendMessage<T>(string name, Guid messageId, T data, CancellationToken cancellationToken);
}
}

View File

@@ -10,13 +10,23 @@ using Microsoft.Extensions.Logging;
namespace MediaBrowser.Controller.Session
{
/// <summary>
/// Class SessionInfo
/// Class SessionInfo.
/// </summary>
public class SessionInfo : IDisposable
public sealed class SessionInfo : IDisposable
{
private ISessionManager _sessionManager;
// 1 second
private const long ProgressIncrement = 10000000;
private readonly ISessionManager _sessionManager;
private readonly ILogger _logger;
private readonly object _progressLock = new object();
private Timer _progressTimer;
private PlaybackProgressInfo _lastProgressInfo;
private bool _disposed = false;
public SessionInfo(ISessionManager sessionManager, ILogger logger)
{
_sessionManager = sessionManager;
@@ -97,8 +107,6 @@ namespace MediaBrowser.Controller.Session
/// <value>The name of the device.</value>
public string DeviceName { get; set; }
public string DeviceType { get; set; }
/// <summary>
/// Gets or sets the now playing item.
/// </summary>
@@ -126,28 +134,6 @@ namespace MediaBrowser.Controller.Session
[JsonIgnore]
public ISessionController[] SessionControllers { get; set; }
/// <summary>
/// Gets or sets the application icon URL.
/// </summary>
/// <value>The application icon URL.</value>
public string AppIconUrl { get; set; }
/// <summary>
/// Gets or sets the supported commands.
/// </summary>
/// <value>The supported commands.</value>
public string[] SupportedCommands
{
get
{
if (Capabilities == null)
{
return new string[] { };
}
return Capabilities.SupportedCommands;
}
}
public TranscodingInfo TranscodingInfo { get; set; }
/// <summary>
@@ -219,6 +205,14 @@ namespace MediaBrowser.Controller.Session
}
}
public QueueItem[] NowPlayingQueue { get; set; }
public bool HasCustomDeviceName { get; set; }
public string PlaylistItemId { get; set; }
public string UserPrimaryImageTag { get; set; }
public Tuple<ISessionController, bool> EnsureController<T>(Func<SessionInfo, ISessionController> factory)
{
var controllers = SessionControllers.ToList();
@@ -267,10 +261,6 @@ namespace MediaBrowser.Controller.Session
return false;
}
private readonly object _progressLock = new object();
private Timer _progressTimer;
private PlaybackProgressInfo _lastProgressInfo;
public void StartAutomaticProgress(PlaybackProgressInfo progressInfo)
{
if (_disposed)
@@ -293,9 +283,6 @@ namespace MediaBrowser.Controller.Session
}
}
// 1 second
private const long ProgressIncrement = 10000000;
private async void OnProgressTimerCallback(object state)
{
if (_disposed)
@@ -354,8 +341,7 @@ namespace MediaBrowser.Controller.Session
}
}
private bool _disposed = false;
/// <inheritdoc />
public void Dispose()
{
_disposed = true;
@@ -367,30 +353,12 @@ namespace MediaBrowser.Controller.Session
foreach (var controller in controllers)
{
var disposable = controller as IDisposable;
if (disposable != null)
if (controller is IDisposable disposable)
{
_logger.LogDebug("Disposing session controller {0}", disposable.GetType().Name);
try
{
disposable.Dispose();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error disposing session controller");
}
disposable.Dispose();
}
}
_sessionManager = null;
}
public QueueItem[] NowPlayingQueue { get; set; }
public bool HasCustomDeviceName { get; set; }
public string PlaylistItemId { get; set; }
public string ServerId { get; set; }
public string UserPrimaryImageTag { get; set; }
}
}