2020-12-22 12:15:26 +00:00
|
|
|
using System.Net.WebSockets;
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace Myriad.Gateway;
|
|
|
|
|
|
|
|
public class ShardConnection: IAsyncDisposable
|
2020-12-22 12:15:26 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly ShardPacketSerializer _serializer;
|
|
|
|
private ClientWebSocket? _client;
|
|
|
|
|
|
|
|
public ShardConnection(JsonSerializerOptions jsonSerializerOptions, ILogger logger)
|
2020-12-22 12:15:26 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_logger = logger.ForContext<ShardConnection>();
|
|
|
|
_serializer = new ShardPacketSerializer(jsonSerializerOptions);
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public WebSocketState State => _client?.State ?? WebSocketState.Closed;
|
|
|
|
public WebSocketCloseStatus? CloseStatus => _client?.CloseStatus;
|
|
|
|
public string? CloseStatusDescription => _client?.CloseStatusDescription;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async ValueTask DisposeAsync()
|
|
|
|
{
|
|
|
|
await CloseInner(WebSocketCloseStatus.NormalClosure, null);
|
|
|
|
_client?.Dispose();
|
|
|
|
}
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task Connect(string url, CancellationToken ct)
|
|
|
|
{
|
|
|
|
_client?.Dispose();
|
|
|
|
_client = new ClientWebSocket();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
await _client.ConnectAsync(GetConnectionUri(url), ct);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Disconnect(WebSocketCloseStatus closeStatus, string? reason)
|
|
|
|
{
|
|
|
|
await CloseInner(closeStatus, reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Send(GatewayPacket packet)
|
|
|
|
{
|
|
|
|
// from `ManagedWebSocket.s_validSendStates`
|
|
|
|
if (_client is not { State: WebSocketState.Open or WebSocketState.CloseReceived })
|
|
|
|
return;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
try
|
2020-12-22 12:15:26 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
await _serializer.WritePacket(_client, packet);
|
2020-12-22 12:15:26 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
catch (Exception e)
|
2020-12-22 12:15:26 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_logger.Error(e, "Error sending WebSocket message");
|
2020-12-22 12:15:26 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<GatewayPacket?> Read()
|
|
|
|
{
|
|
|
|
// from `ManagedWebSocket.s_validReceiveStates`
|
|
|
|
if (_client is not { State: WebSocketState.Open or WebSocketState.CloseSent })
|
|
|
|
return null;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
try
|
2020-12-22 12:15:26 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
var (_, packet) = await _serializer.ReadPacket(_client);
|
|
|
|
return packet;
|
2020-12-22 12:15:26 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
catch (Exception e)
|
2020-12-22 12:15:26 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_logger.Error(e, "Error reading from WebSocket");
|
|
|
|
// force close so we can "reset"
|
|
|
|
await CloseInner(WebSocketCloseStatus.NormalClosure, null);
|
2020-12-22 12:15:26 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private Uri GetConnectionUri(string baseUri) => new UriBuilder(baseUri) { Query = "v=9&encoding=json" }.Uri;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private async Task CloseInner(WebSocketCloseStatus closeStatus, string? description)
|
|
|
|
{
|
|
|
|
if (_client == null)
|
|
|
|
return;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var client = _client;
|
|
|
|
_client = null;
|
2021-04-29 09:10:19 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// from `ManagedWebSocket.s_validCloseStates`
|
|
|
|
if (client.State is WebSocketState.Open or WebSocketState.CloseReceived or WebSocketState.CloseSent)
|
|
|
|
{
|
|
|
|
// Close with timeout, mostly to work around https://github.com/dotnet/runtime/issues/51590
|
|
|
|
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
|
2021-04-29 09:10:19 +00:00
|
|
|
try
|
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
await client.CloseAsync(closeStatus, description, cts.Token);
|
2021-04-29 09:10:19 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_logger.Error(e, "Error closing WebSocket connection");
|
2021-04-29 09:10:19 +00:00
|
|
|
}
|
2020-12-22 12:15:26 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
// This shouldn't need to be wrapped in a try/catch but doing it anyway :/
|
|
|
|
try
|
|
|
|
{
|
|
|
|
client.Dispose();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.Error(e, "Error disposing WebSocket connection");
|
|
|
|
}
|
2020-12-22 12:15:26 +00:00
|
|
|
}
|
|
|
|
}
|