2024-04-15 20:34:23 +02:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
using DisplayCommands;
|
|
|
|
using TanksServer.Graphics;
|
|
|
|
|
|
|
|
namespace TanksServer.Interactivity;
|
|
|
|
|
2024-04-21 23:21:15 +02:00
|
|
|
internal sealed class ClientScreenServerConnection(
|
|
|
|
WebSocket webSocket,
|
|
|
|
ILogger<ClientScreenServerConnection> logger,
|
|
|
|
TimeSpan minFrameTime,
|
|
|
|
Guid? playerGuid = null
|
|
|
|
) : WebsocketServerConnection(logger, new ByteChannelWebSocket(webSocket, logger, 0)),
|
|
|
|
IDisposable
|
2024-04-15 20:34:23 +02:00
|
|
|
{
|
|
|
|
private readonly SemaphoreSlim _wantedFrames = new(1);
|
2024-04-21 23:21:15 +02:00
|
|
|
private readonly PlayerScreenData? _playerScreenData = playerGuid.HasValue ? new PlayerScreenData(logger) : null;
|
2024-04-16 21:34:54 +02:00
|
|
|
private DateTime _nextFrameAfter = DateTime.Now;
|
2024-04-15 20:34:23 +02:00
|
|
|
|
2024-04-21 23:21:15 +02:00
|
|
|
public void Dispose() => _wantedFrames.Dispose();
|
2024-04-15 20:34:23 +02:00
|
|
|
|
|
|
|
public async Task SendAsync(PixelGrid pixels, GamePixelGrid gamePixelGrid)
|
|
|
|
{
|
2024-04-16 21:34:54 +02:00
|
|
|
if (_nextFrameAfter > DateTime.Now)
|
|
|
|
return;
|
|
|
|
|
2024-04-15 20:34:23 +02:00
|
|
|
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
|
|
|
{
|
2024-04-22 19:03:07 +02:00
|
|
|
Logger.LogTrace("client does not want a frame yet");
|
2024-04-15 20:34:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-21 23:21:15 +02:00
|
|
|
_nextFrameAfter = DateTime.Today + minFrameTime;
|
2024-04-16 21:34:54 +02:00
|
|
|
|
2024-04-15 20:34:23 +02:00
|
|
|
if (_playerScreenData != null)
|
|
|
|
RefreshPlayerSpecificData(gamePixelGrid);
|
|
|
|
|
2024-04-22 19:03:07 +02:00
|
|
|
Logger.LogTrace("sending");
|
2024-04-15 20:34:23 +02:00
|
|
|
try
|
|
|
|
{
|
2024-04-22 19:03:07 +02:00
|
|
|
Logger.LogTrace("sending {} bytes of pixel data", pixels.Data.Length);
|
|
|
|
await Socket.SendBinaryAsync(pixels.Data, _playerScreenData == null);
|
2024-04-15 20:34:23 +02:00
|
|
|
if (_playerScreenData != null)
|
2024-04-22 19:03:07 +02:00
|
|
|
await Socket.SendBinaryAsync(_playerScreenData.GetPacket());
|
2024-04-15 20:34:23 +02:00
|
|
|
}
|
|
|
|
catch (WebSocketException ex)
|
|
|
|
{
|
2024-04-22 19:03:07 +02:00
|
|
|
Logger.LogWarning(ex, "send failed");
|
2024-04-15 20:34:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RefreshPlayerSpecificData(GamePixelGrid gamePixelGrid)
|
|
|
|
{
|
|
|
|
Debug.Assert(_playerScreenData != null);
|
|
|
|
_playerScreenData.Clear();
|
|
|
|
foreach (var gamePixel in gamePixelGrid)
|
|
|
|
{
|
|
|
|
if (!gamePixel.EntityType.HasValue)
|
|
|
|
continue;
|
2024-04-21 23:21:15 +02:00
|
|
|
_playerScreenData.Add(gamePixel.EntityType.Value, gamePixel.BelongsTo?.Id == playerGuid);
|
2024-04-15 20:34:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-22 19:03:07 +02:00
|
|
|
protected override ValueTask HandleMessageAsync(Memory<byte> _)
|
|
|
|
{
|
|
|
|
_wantedFrames.Release();
|
|
|
|
return ValueTask.CompletedTask;
|
|
|
|
}
|
2024-04-15 20:34:23 +02:00
|
|
|
}
|