aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Net
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Controller/Net')
-rw-r--r--MediaBrowser.Controller/Net/BasePeriodicWebSocketListener.cs41
-rw-r--r--MediaBrowser.Controller/Net/IHttpServer.cs27
-rw-r--r--MediaBrowser.Controller/Net/IWebSocketConnection.cs43
-rw-r--r--MediaBrowser.Controller/Net/SecurityException.cs32
4 files changed, 68 insertions, 75 deletions
diff --git a/MediaBrowser.Controller/Net/BasePeriodicWebSocketListener.cs b/MediaBrowser.Controller/Net/BasePeriodicWebSocketListener.cs
index b710318ee..7dca793c6 100644
--- a/MediaBrowser.Controller/Net/BasePeriodicWebSocketListener.cs
+++ b/MediaBrowser.Controller/Net/BasePeriodicWebSocketListener.cs
@@ -40,9 +40,9 @@ namespace MediaBrowser.Controller.Net
/// <summary>
/// The logger
/// </summary>
- protected ILogger Logger;
+ protected ILogger<BasePeriodicWebSocketListener<TReturnDataType, TStateType>> Logger;
- protected BasePeriodicWebSocketListener(ILogger logger)
+ protected BasePeriodicWebSocketListener(ILogger<BasePeriodicWebSocketListener<TReturnDataType, TStateType>> logger)
{
if (logger == null)
{
@@ -77,8 +77,6 @@ namespace MediaBrowser.Controller.Net
return Task.CompletedTask;
}
- protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
-
/// <summary>
/// Starts sending messages over a web socket
/// </summary>
@@ -87,12 +85,12 @@ namespace MediaBrowser.Controller.Net
{
var vals = message.Data.Split(',');
- var dueTimeMs = long.Parse(vals[0], UsCulture);
- var periodMs = long.Parse(vals[1], UsCulture);
+ var dueTimeMs = long.Parse(vals[0], CultureInfo.InvariantCulture);
+ var periodMs = long.Parse(vals[1], CultureInfo.InvariantCulture);
var cancellationTokenSource = new CancellationTokenSource();
- Logger.LogDebug("{1} Begin transmitting over websocket to {0}", message.Connection.RemoteEndPoint, GetType().Name);
+ Logger.LogDebug("WS {1} begin transmitting to {0}", message.Connection.RemoteEndPoint, GetType().Name);
var state = new TStateType
{
@@ -106,7 +104,7 @@ namespace MediaBrowser.Controller.Net
}
}
- protected void SendData(bool force)
+ protected async Task SendData(bool force)
{
Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>[] tuples;
@@ -130,13 +128,18 @@ namespace MediaBrowser.Controller.Net
.ToArray();
}
- foreach (var tuple in tuples)
+ IEnumerable<Task> GetTasks()
{
- SendData(tuple);
+ foreach (var tuple in tuples)
+ {
+ yield return SendData(tuple);
+ }
}
+
+ await Task.WhenAll(GetTasks()).ConfigureAwait(false);
}
- private async void SendData(Tuple<IWebSocketConnection, CancellationTokenSource, TStateType> tuple)
+ private async Task SendData(Tuple<IWebSocketConnection, CancellationTokenSource, TStateType> tuple)
{
var connection = tuple.Item1;
@@ -150,12 +153,13 @@ namespace MediaBrowser.Controller.Net
if (data != null)
{
- await connection.SendAsync(new WebSocketMessage<TReturnDataType>
- {
- MessageType = Name,
- Data = data
-
- }, cancellationToken).ConfigureAwait(false);
+ await connection.SendAsync(
+ new WebSocketMessage<TReturnDataType>
+ {
+ MessageType = Name,
+ Data = data
+ },
+ cancellationToken).ConfigureAwait(false);
state.DateLastSendUtc = DateTime.UtcNow;
}
@@ -197,7 +201,7 @@ namespace MediaBrowser.Controller.Net
/// <param name="connection">The connection.</param>
private void DisposeConnection(Tuple<IWebSocketConnection, CancellationTokenSource, TStateType> connection)
{
- Logger.LogDebug("{1} stop transmitting over websocket to {0}", connection.Item1.RemoteEndPoint, GetType().Name);
+ Logger.LogDebug("WS {1} stop transmitting to {0}", connection.Item1.RemoteEndPoint, GetType().Name);
// TODO disposing the connection seems to break websockets in subtle ways, so what is the purpose of this function really...
// connection.Item1.Dispose();
@@ -242,6 +246,7 @@ namespace MediaBrowser.Controller.Net
public void Dispose()
{
Dispose(true);
+ GC.SuppressFinalize(this);
}
}
diff --git a/MediaBrowser.Controller/Net/IHttpServer.cs b/MediaBrowser.Controller/Net/IHttpServer.cs
index 806478864..efb5f4ac3 100644
--- a/MediaBrowser.Controller/Net/IHttpServer.cs
+++ b/MediaBrowser.Controller/Net/IHttpServer.cs
@@ -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;
@@ -9,9 +8,9 @@ using Microsoft.AspNetCore.Http;
namespace MediaBrowser.Controller.Net
{
/// <summary>
- /// Interface IHttpServer
+ /// Interface IHttpServer.
/// </summary>
- public interface IHttpServer : IDisposable
+ public interface IHttpServer
{
/// <summary>
/// Gets the URL prefix.
@@ -20,11 +19,6 @@ namespace MediaBrowser.Controller.Net
string[] UrlPrefixes { get; }
/// <summary>
- /// Stops this instance.
- /// </summary>
- void Stop();
-
- /// <summary>
/// Occurs when [web socket connected].
/// </summary>
event EventHandler<GenericEventArgs<IWebSocketConnection>> WebSocketConnected;
@@ -40,22 +34,17 @@ namespace MediaBrowser.Controller.Net
string GlobalResponse { get; set; }
/// <summary>
- /// Sends the http context to the socket listener
+ /// The HTTP request handler
/// </summary>
- /// <param name="ctx"></param>
+ /// <param name="context"></param>
/// <returns></returns>
- Task ProcessWebSocketRequest(HttpContext ctx);
+ Task RequestHandler(HttpContext context);
/// <summary>
- /// The HTTP request handler
+ /// Get the default CORS headers
/// </summary>
- /// <param name="httpReq"></param>
- /// <param name="urlString"></param>
- /// <param name="host"></param>
- /// <param name="localPath"></param>
- /// <param name="cancellationToken"></param>
+ /// <param name="req"></param>
/// <returns></returns>
- Task RequestHandler(IHttpRequest httpReq, string urlString, string host, string localPath,
- CancellationToken cancellationToken);
+ IDictionary<string, string> GetDefaultCorsHeaders(IRequest req);
}
}
diff --git a/MediaBrowser.Controller/Net/IWebSocketConnection.cs b/MediaBrowser.Controller/Net/IWebSocketConnection.cs
index 31eb7ccb7..3ef8e5f6d 100644
--- a/MediaBrowser.Controller/Net/IWebSocketConnection.cs
+++ b/MediaBrowser.Controller/Net/IWebSocketConnection.cs
@@ -1,4 +1,7 @@
+#nullable enable
+
using System;
+using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
@@ -7,18 +10,12 @@ using Microsoft.AspNetCore.Http;
namespace MediaBrowser.Controller.Net
{
- public interface IWebSocketConnection : IDisposable
+ public interface IWebSocketConnection
{
/// <summary>
/// Occurs when [closed].
/// </summary>
- event EventHandler<EventArgs> Closed;
-
- /// <summary>
- /// Gets the id.
- /// </summary>
- /// <value>The id.</value>
- Guid Id { get; }
+ event EventHandler<EventArgs>? Closed;
/// <summary>
/// Gets the last activity date.
@@ -27,21 +24,22 @@ namespace MediaBrowser.Controller.Net
DateTime LastActivityDate { get; }
/// <summary>
- /// Gets or sets the URL.
+ /// Gets or sets the date of last Keeplive received.
/// </summary>
- /// <value>The URL.</value>
- string Url { get; set; }
+ /// <value>The date of last Keeplive received.</value>
+ DateTime LastKeepAliveDate { get; set; }
+
/// <summary>
/// Gets or sets the query string.
/// </summary>
/// <value>The query string.</value>
- IQueryCollection QueryString { get; set; }
+ IQueryCollection QueryString { get; }
/// <summary>
/// Gets or sets the receive action.
/// </summary>
/// <value>The receive action.</value>
- Func<WebSocketMessageInfo, Task> OnReceive { get; set; }
+ Func<WebSocketMessageInfo, Task>? OnReceive { get; set; }
/// <summary>
/// Gets the state.
@@ -53,7 +51,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.
@@ -65,21 +63,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);
}
}
diff --git a/MediaBrowser.Controller/Net/SecurityException.cs b/MediaBrowser.Controller/Net/SecurityException.cs
index 3ccecf0eb..a5b94ea5e 100644
--- a/MediaBrowser.Controller/Net/SecurityException.cs
+++ b/MediaBrowser.Controller/Net/SecurityException.cs
@@ -2,20 +2,36 @@ using System;
namespace MediaBrowser.Controller.Net
{
+ /// <summary>
+ /// The exception that is thrown when a user is authenticated, but not authorized to access a requested resource.
+ /// </summary>
public class SecurityException : Exception
{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="SecurityException"/> class.
+ /// </summary>
+ public SecurityException()
+ : base()
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="SecurityException"/> class.
+ /// </summary>
+ /// <param name="message">The message that describes the error.</param>
public SecurityException(string message)
: base(message)
{
-
}
- public SecurityExceptionType SecurityExceptionType { get; set; }
- }
-
- public enum SecurityExceptionType
- {
- Unauthenticated = 0,
- ParentalControl = 1
+ /// <summary>
+ /// Initializes a new instance of the <see cref="SecurityException"/> class.
+ /// </summary>
+ /// <param name="message">The message that describes the error</param>
+ /// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
+ public SecurityException(string message, Exception innerException)
+ : base(message, innerException)
+ {
+ }
}
}