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.cs59
1 files changed, 26 insertions, 33 deletions
diff --git a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
index b1a99853a..f83da566b 100644
--- a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
+++ b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
@@ -9,7 +9,8 @@ using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Extensions.Json;
using MediaBrowser.Controller.Net;
-using MediaBrowser.Model.Net;
+using MediaBrowser.Controller.Net.WebSocketMessages;
+using MediaBrowser.Controller.Net.WebSocketMessages.Outbound;
using MediaBrowser.Model.Session;
using Microsoft.Extensions.Logging;
@@ -42,14 +43,17 @@ namespace Emby.Server.Implementations.HttpServer
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="socket">The socket.</param>
+ /// <param name="authorizationInfo">The authorization information.</param>
/// <param name="remoteEndPoint">The remote end point.</param>
public WebSocketConnection(
ILogger<WebSocketConnection> logger,
WebSocket socket,
+ AuthorizationInfo authorizationInfo,
IPAddress? remoteEndPoint)
{
_logger = logger;
_socket = socket;
+ AuthorizationInfo = authorizationInfo;
RemoteEndPoint = remoteEndPoint;
_jsonOptions = JsonDefaults.Options;
@@ -59,47 +63,40 @@ namespace Emby.Server.Implementations.HttpServer
/// <inheritdoc />
public event EventHandler<EventArgs>? Closed;
- /// <summary>
- /// Gets the remote end point.
- /// </summary>
+ /// <inheritdoc />
+ public AuthorizationInfo AuthorizationInfo { get; }
+
+ /// <inheritdoc />
public IPAddress? RemoteEndPoint { get; }
- /// <summary>
- /// Gets or sets the receive action.
- /// </summary>
- /// <value>The receive action.</value>
+ /// <inheritdoc />
public Func<WebSocketMessageInfo, Task>? OnReceive { get; set; }
- /// <summary>
- /// Gets the last activity date.
- /// </summary>
- /// <value>The last activity date.</value>
+ /// <inheritdoc />
public DateTime LastActivityDate { get; private set; }
/// <inheritdoc />
public DateTime LastKeepAliveDate { get; set; }
- /// <summary>
- /// Gets the state.
- /// </summary>
- /// <value>The state.</value>
+ /// <inheritdoc />
public WebSocketState State => _socket.State;
- /// <summary>
- /// Sends a message asynchronously.
- /// </summary>
- /// <typeparam name="T">The type of the message.</typeparam>
- /// <param name="message">The message.</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <returns>Task.</returns>
- public Task SendAsync<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
+ /// <inheritdoc />
+ public Task SendAsync(OutboundWebSocketMessage message, CancellationToken cancellationToken)
+ {
+ var json = JsonSerializer.SerializeToUtf8Bytes(message, _jsonOptions);
+ return _socket.SendAsync(json, WebSocketMessageType.Text, true, cancellationToken);
+ }
+
+ /// <inheritdoc />
+ public Task SendAsync<T>(OutboundWebSocketMessage<T> message, CancellationToken cancellationToken)
{
var json = JsonSerializer.SerializeToUtf8Bytes(message, _jsonOptions);
return _socket.SendAsync(json, WebSocketMessageType.Text, true, cancellationToken);
}
/// <inheritdoc />
- public async Task ProcessAsync(CancellationToken cancellationToken = default)
+ public async Task ReceiveAsync(CancellationToken cancellationToken = default)
{
var pipe = new Pipe();
var writer = pipe.Writer;
@@ -171,7 +168,7 @@ namespace Emby.Server.Implementations.HttpServer
return;
}
- WebSocketMessage<object>? stub;
+ InboundWebSocketMessage<object>? stub;
long bytesConsumed;
try
{
@@ -212,10 +209,10 @@ namespace Emby.Server.Implementations.HttpServer
}
}
- internal WebSocketMessage<object>? DeserializeWebSocketMessage(ReadOnlySequence<byte> bytes, out long bytesConsumed)
+ internal InboundWebSocketMessage<object>? DeserializeWebSocketMessage(ReadOnlySequence<byte> bytes, out long bytesConsumed)
{
var jsonReader = new Utf8JsonReader(bytes);
- var ret = JsonSerializer.Deserialize<WebSocketMessage<object>>(ref jsonReader, _jsonOptions);
+ var ret = JsonSerializer.Deserialize<InboundWebSocketMessage<object>>(ref jsonReader, _jsonOptions);
bytesConsumed = jsonReader.BytesConsumed;
return ret;
}
@@ -224,11 +221,7 @@ namespace Emby.Server.Implementations.HttpServer
{
LastKeepAliveDate = DateTime.UtcNow;
return SendAsync(
- new WebSocketMessage<string>
- {
- MessageId = Guid.NewGuid(),
- MessageType = SessionMessageType.KeepAlive
- },
+ new OutboundKeepAliveMessage(),
CancellationToken.None);
}