2022-10-28 16:53:28 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
2022-12-16 17:16:26 +08:00
|
|
|
using System.Net;
|
2022-10-28 16:53:28 +08:00
|
|
|
using System.Net.WebSockets;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using osu.Framework.Extensions.TypeExtensions;
|
|
|
|
using osu.Framework.Logging;
|
|
|
|
|
|
|
|
namespace osu.Game.Online.Notifications.WebSocket
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A notifications client which receives events via a websocket.
|
|
|
|
/// </summary>
|
2024-01-25 19:25:27 +08:00
|
|
|
public class WebSocketNotificationsClient : PersistentEndpointClient
|
2022-10-28 16:53:28 +08:00
|
|
|
{
|
2024-01-25 19:25:27 +08:00
|
|
|
public event Action<SocketMessage>? MessageReceived;
|
|
|
|
|
2022-10-28 16:53:28 +08:00
|
|
|
private readonly ClientWebSocket socket;
|
|
|
|
private readonly string endpoint;
|
|
|
|
|
2024-01-25 19:25:27 +08:00
|
|
|
public WebSocketNotificationsClient(ClientWebSocket socket, string endpoint)
|
2022-10-28 16:53:28 +08:00
|
|
|
{
|
|
|
|
this.socket = socket;
|
|
|
|
this.endpoint = endpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task ConnectAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
await socket.ConnectAsync(new Uri(endpoint), cancellationToken).ConfigureAwait(false);
|
|
|
|
runReadLoop(cancellationToken);
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:54:34 +08:00
|
|
|
private void runReadLoop(CancellationToken cancellationToken) => Task.Run(async () =>
|
2022-10-28 16:53:28 +08:00
|
|
|
{
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
StringBuilder messageResult = new StringBuilder();
|
|
|
|
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-12-16 17:16:26 +08:00
|
|
|
WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, cancellationToken).ConfigureAwait(false);
|
2022-10-28 16:53:28 +08:00
|
|
|
|
|
|
|
switch (result.MessageType)
|
|
|
|
{
|
|
|
|
case WebSocketMessageType.Text:
|
|
|
|
messageResult.Append(Encoding.UTF8.GetString(buffer[..result.Count]));
|
|
|
|
|
|
|
|
if (result.EndOfMessage)
|
|
|
|
{
|
|
|
|
SocketMessage? message = JsonConvert.DeserializeObject<SocketMessage>(messageResult.ToString());
|
|
|
|
messageResult.Clear();
|
|
|
|
|
|
|
|
Debug.Assert(message != null);
|
|
|
|
|
|
|
|
if (message.Error != null)
|
|
|
|
{
|
|
|
|
Logger.Log($"{GetType().ReadableName()} error: {message.Error}", LoggingTarget.Network);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-01-25 19:25:27 +08:00
|
|
|
MessageReceived?.Invoke(message);
|
2022-10-28 16:53:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WebSocketMessageType.Binary:
|
2022-11-07 11:11:44 +08:00
|
|
|
throw new NotImplementedException("Binary message type not supported.");
|
2022-10-28 16:53:28 +08:00
|
|
|
|
|
|
|
case WebSocketMessageType.Close:
|
2022-12-16 17:16:26 +08:00
|
|
|
throw new WebException("Connection closed by remote host.");
|
2022-10-28 16:53:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2022-12-16 17:16:26 +08:00
|
|
|
await InvokeClosed(ex).ConfigureAwait(false);
|
2022-10-28 16:53:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-10-28 17:54:34 +08:00
|
|
|
}, cancellationToken);
|
2022-10-28 16:53:28 +08:00
|
|
|
|
|
|
|
private async Task closeAsync()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, @"Disconnecting", CancellationToken.None).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
// Closure can fail if the connection is aborted. Don't really care since it's disposed anyway.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-25 19:25:27 +08:00
|
|
|
public async Task SendAsync(SocketMessage message, CancellationToken? cancellationToken = default)
|
2022-10-28 16:53:28 +08:00
|
|
|
{
|
|
|
|
if (socket.State != WebSocketState.Open)
|
|
|
|
return;
|
|
|
|
|
2024-01-25 19:25:27 +08:00
|
|
|
await socket.SendAsync(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)), WebSocketMessageType.Text, true, cancellationToken ?? CancellationToken.None).ConfigureAwait(false);
|
2022-10-28 16:53:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override async ValueTask DisposeAsync()
|
|
|
|
{
|
2022-12-16 17:16:26 +08:00
|
|
|
await base.DisposeAsync().ConfigureAwait(false);
|
|
|
|
await closeAsync().ConfigureAwait(false);
|
2022-10-28 16:53:28 +08:00
|
|
|
socket.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|