move more websocket logic into base classes

This commit is contained in:
Vinzenz Schroeter 2024-04-21 23:21:15 +02:00
parent 57c0d229f1
commit fb675e59ff
7 changed files with 89 additions and 123 deletions

View file

@ -5,42 +5,19 @@ using TanksServer.Graphics;
namespace TanksServer.Interactivity;
internal sealed class ClientScreenServerConnection : IWebsocketServerConnection, IDisposable
internal sealed class ClientScreenServerConnection(
WebSocket webSocket,
ILogger<ClientScreenServerConnection> logger,
TimeSpan minFrameTime,
Guid? playerGuid = null
) : WebsocketServerConnection(logger, new ByteChannelWebSocket(webSocket, logger, 0)),
IDisposable
{
private readonly ByteChannelWebSocket _channel;
private readonly ILogger<ClientScreenServerConnection> _logger;
private readonly SemaphoreSlim _wantedFrames = new(1);
private readonly Guid? _playerGuid;
private readonly PlayerScreenData? _playerScreenData;
private readonly TimeSpan _minFrameTime;
private readonly PlayerScreenData? _playerScreenData = playerGuid.HasValue ? new PlayerScreenData(logger) : null;
private DateTime _nextFrameAfter = DateTime.Now;
public ClientScreenServerConnection(
WebSocket webSocket,
ILogger<ClientScreenServerConnection> logger,
TimeSpan minFrameTime,
Guid? playerGuid = null
)
{
_logger = logger;
_minFrameTime = minFrameTime;
_playerGuid = playerGuid;
if (playerGuid.HasValue)
_playerScreenData = new PlayerScreenData(logger);
_channel = new ByteChannelWebSocket(webSocket, logger, 0);
Done = ReceiveAsync();
}
public Task Done { get; }
public void Dispose()
{
_wantedFrames.Dispose();
Done.Dispose();
}
public void Dispose() => _wantedFrames.Dispose();
public async Task SendAsync(PixelGrid pixels, GamePixelGrid gamePixelGrid)
{
@ -49,26 +26,26 @@ internal sealed class ClientScreenServerConnection : IWebsocketServerConnection,
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
{
_logger.LogTrace("client does not want a frame yet");
logger.LogTrace("client does not want a frame yet");
return;
}
_nextFrameAfter = DateTime.Today + _minFrameTime;
_nextFrameAfter = DateTime.Today + minFrameTime;
if (_playerScreenData != null)
RefreshPlayerSpecificData(gamePixelGrid);
_logger.LogTrace("sending");
logger.LogTrace("sending");
try
{
_logger.LogTrace("sending {} bytes of pixel data", pixels.Data.Length);
await _channel.SendAsync(pixels.Data, _playerScreenData == null);
logger.LogTrace("sending {} bytes of pixel data", pixels.Data.Length);
await Socket.SendAsync(pixels.Data, _playerScreenData == null);
if (_playerScreenData != null)
await _channel.SendAsync(_playerScreenData.GetPacket());
await Socket.SendAsync(_playerScreenData.GetPacket());
}
catch (WebSocketException ex)
{
_logger.LogWarning(ex, "send failed");
logger.LogWarning(ex, "send failed");
}
}
@ -80,20 +57,9 @@ internal sealed class ClientScreenServerConnection : IWebsocketServerConnection,
{
if (!gamePixel.EntityType.HasValue)
continue;
_playerScreenData.Add(gamePixel.EntityType.Value, gamePixel.BelongsTo?.Id == _playerGuid);
_playerScreenData.Add(gamePixel.EntityType.Value, gamePixel.BelongsTo?.Id == playerGuid);
}
}
private async Task ReceiveAsync()
{
await foreach (var _ in _channel.ReadAllAsync())
_wantedFrames.Release();
_logger.LogTrace("done receiving");
}
public Task CloseAsync()
{
_logger.LogDebug("closing connection");
return _channel.CloseAsync();
}
protected override void HandleMessage(Memory<byte> _) => _wantedFrames.Release();
}