aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Server.Implementations/HttpServer/WebSocketConnection.cs')
-rw-r--r--Emby.Server.Implementations/HttpServer/WebSocketConnection.cs55
1 files changed, 25 insertions, 30 deletions
diff --git a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
index 914fa9dbc..e9d0bac74 100644
--- a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
+++ b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
@@ -1,15 +1,15 @@
-using System.Text;
+using System;
+using System.Net.WebSockets;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using Emby.Server.Implementations.Net;
using MediaBrowser.Controller.Net;
-using Microsoft.Extensions.Logging;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Serialization;
-using System;
-using System.Threading;
-using System.Threading.Tasks;
using MediaBrowser.Model.Services;
-using MediaBrowser.Model.Text;
-using System.Net.WebSockets;
-using Emby.Server.Implementations.Net;
+using Microsoft.Extensions.Logging;
+using UtfUnknown;
namespace Emby.Server.Implementations.HttpServer
{
@@ -68,7 +68,6 @@ namespace Emby.Server.Implementations.HttpServer
/// </summary>
/// <value>The query string.</value>
public QueryParamCollection QueryString { get; set; }
- private readonly ITextEncoding _textEncoding;
/// <summary>
/// Initializes a new instance of the <see cref="WebSocketConnection" /> class.
@@ -77,24 +76,24 @@ namespace Emby.Server.Implementations.HttpServer
/// <param name="remoteEndPoint">The remote end point.</param>
/// <param name="jsonSerializer">The json serializer.</param>
/// <param name="logger">The logger.</param>
- /// <exception cref="System.ArgumentNullException">socket</exception>
- public WebSocketConnection(IWebSocket socket, string remoteEndPoint, IJsonSerializer jsonSerializer, ILogger logger, ITextEncoding textEncoding)
+ /// <exception cref="ArgumentNullException">socket</exception>
+ public WebSocketConnection(IWebSocket socket, string remoteEndPoint, IJsonSerializer jsonSerializer, ILogger logger)
{
if (socket == null)
{
- throw new ArgumentNullException("socket");
+ throw new ArgumentNullException(nameof(socket));
}
if (string.IsNullOrEmpty(remoteEndPoint))
{
- throw new ArgumentNullException("remoteEndPoint");
+ throw new ArgumentNullException(nameof(remoteEndPoint));
}
if (jsonSerializer == null)
{
- throw new ArgumentNullException("jsonSerializer");
+ throw new ArgumentNullException(nameof(jsonSerializer));
}
if (logger == null)
{
- throw new ArgumentNullException("logger");
+ throw new ArgumentNullException(nameof(logger));
}
Id = Guid.NewGuid();
@@ -110,7 +109,6 @@ namespace Emby.Server.Implementations.HttpServer
RemoteEndPoint = remoteEndPoint;
_logger = logger;
- _textEncoding = textEncoding;
socket.Closed += socket_Closed;
}
@@ -132,8 +130,7 @@ namespace Emby.Server.Implementations.HttpServer
{
return;
}
-
- var charset = _textEncoding.GetDetectedEncodingName(bytes, bytes.Length, null, false);
+ var charset = CharsetDetector.DetectFromBytes(bytes).Detected?.EncodingName;
if (string.Equals(charset, "utf-8", StringComparison.OrdinalIgnoreCase))
{
@@ -141,14 +138,15 @@ namespace Emby.Server.Implementations.HttpServer
}
else
{
- OnReceiveInternal(_textEncoding.GetASCIIEncoding().GetString(bytes, 0, bytes.Length));
+ OnReceiveInternal(Encoding.ASCII.GetString(bytes, 0, bytes.Length));
}
}
/// <summary>
/// Called when [receive].
/// </summary>
- /// <param name="bytes">The bytes.</param>
+ /// <param name="memory">The memory block.</param>
+ /// <param name="length">The length of the memory block.</param>
private void OnReceiveInternal(Memory<byte> memory, int length)
{
LastActivityDate = DateTime.UtcNow;
@@ -160,7 +158,7 @@ namespace Emby.Server.Implementations.HttpServer
var bytes = memory.Slice(0, length).ToArray();
- var charset = _textEncoding.GetDetectedEncodingName(bytes, bytes.Length, null, false);
+ var charset = CharsetDetector.DetectFromBytes(bytes).Detected?.EncodingName;
if (string.Equals(charset, "utf-8", StringComparison.OrdinalIgnoreCase))
{
@@ -168,7 +166,7 @@ namespace Emby.Server.Implementations.HttpServer
}
else
{
- OnReceiveInternal(_textEncoding.GetASCIIEncoding().GetString(bytes, 0, bytes.Length));
+ OnReceiveInternal(Encoding.ASCII.GetString(bytes, 0, bytes.Length));
}
}
@@ -214,12 +212,12 @@ namespace Emby.Server.Implementations.HttpServer
/// <param name="message">The message.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
- /// <exception cref="System.ArgumentNullException">message</exception>
+ /// <exception cref="ArgumentNullException">message</exception>
public Task SendAsync<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
{
if (message == null)
{
- throw new ArgumentNullException("message");
+ throw new ArgumentNullException(nameof(message));
}
var json = _jsonSerializer.SerializeToString(message);
@@ -237,7 +235,7 @@ namespace Emby.Server.Implementations.HttpServer
{
if (buffer == null)
{
- throw new ArgumentNullException("buffer");
+ throw new ArgumentNullException(nameof(buffer));
}
cancellationToken.ThrowIfCancellationRequested();
@@ -249,7 +247,7 @@ namespace Emby.Server.Implementations.HttpServer
{
if (string.IsNullOrEmpty(text))
{
- throw new ArgumentNullException("text");
+ throw new ArgumentNullException(nameof(text));
}
cancellationToken.ThrowIfCancellationRequested();
@@ -261,10 +259,7 @@ namespace Emby.Server.Implementations.HttpServer
/// Gets the state.
/// </summary>
/// <value>The state.</value>
- public WebSocketState State
- {
- get { return _socket.State; }
- }
+ public WebSocketState State => _socket.State;
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.