2024-04-15 20:34:23 +02:00
|
|
|
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,
|
2024-04-28 12:53:18 +02:00
|
|
|
string? playerName = null
|
2024-04-21 23:21:15 +02:00
|
|
|
) : WebsocketServerConnection(logger, new ByteChannelWebSocket(webSocket, logger, 0)),
|
|
|
|
IDisposable
|
2024-04-15 20:34:23 +02:00
|
|
|
{
|
2024-04-28 12:53:18 +02:00
|
|
|
private readonly SemaphoreSlim _wantedFramesOnTick = new(0, 2);
|
|
|
|
private readonly SemaphoreSlim _mutex = new(1);
|
2024-04-15 20:34:23 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
private PixelGrid? _lastSentPixels = null;
|
|
|
|
private PixelGrid? _nextPixels = null;
|
|
|
|
private readonly PlayerScreenData? _nextPlayerData = playerName != null ? new PlayerScreenData(logger) : null;
|
2024-04-15 20:34:23 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
protected override async ValueTask HandleMessageAsync(Memory<byte> _)
|
2024-04-15 20:34:23 +02:00
|
|
|
{
|
2024-04-28 12:53:18 +02:00
|
|
|
await _mutex.WaitAsync();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (_nextPixels == null)
|
|
|
|
{
|
|
|
|
_wantedFramesOnTick.Release();
|
|
|
|
return;
|
|
|
|
}
|
2024-04-16 21:34:54 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
_lastSentPixels = _nextPixels;
|
|
|
|
_nextPixels = null;
|
|
|
|
await SendNowAsync(_lastSentPixels);
|
|
|
|
}
|
|
|
|
catch (SemaphoreFullException)
|
2024-04-15 20:34:23 +02:00
|
|
|
{
|
2024-04-28 12:53:18 +02:00
|
|
|
logger.LogWarning("ignoring request for more frames");
|
2024-04-15 20:34:23 +02:00
|
|
|
}
|
2024-04-28 12:53:18 +02:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
_mutex.Release();
|
|
|
|
}
|
|
|
|
}
|
2024-04-15 20:34:23 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
public async ValueTask OnGameTickAsync(PixelGrid pixels, GamePixelGrid gamePixelGrid)
|
|
|
|
{
|
|
|
|
await _mutex.WaitAsync();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (pixels == _lastSentPixels)
|
|
|
|
return;
|
2024-04-16 21:34:54 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
if (_nextPlayerData != null)
|
|
|
|
{
|
|
|
|
_nextPlayerData.Clear();
|
|
|
|
foreach (var gamePixel in gamePixelGrid)
|
|
|
|
{
|
|
|
|
if (!gamePixel.EntityType.HasValue)
|
|
|
|
continue;
|
|
|
|
_nextPlayerData.Add(gamePixel.EntityType.Value, gamePixel.BelongsTo?.Name == playerName);
|
|
|
|
}
|
|
|
|
}
|
2024-04-15 20:34:23 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
var sendImmediately = await _wantedFramesOnTick.WaitAsync(TimeSpan.Zero);
|
|
|
|
if (sendImmediately)
|
|
|
|
{
|
|
|
|
await SendNowAsync(pixels);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_wantedFramesOnTick.Release();
|
|
|
|
_nextPixels = pixels;
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
_mutex.Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async ValueTask SendNowAsync(PixelGrid pixels)
|
|
|
|
{
|
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);
|
2024-04-28 12:53:18 +02:00
|
|
|
await Socket.SendBinaryAsync(pixels.Data, _nextPlayerData == null);
|
|
|
|
if (_nextPlayerData != null)
|
|
|
|
{
|
|
|
|
await Socket.SendBinaryAsync(_nextPlayerData.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
public void Dispose() => _wantedFramesOnTick.Dispose();
|
2024-04-15 20:34:23 +02:00
|
|
|
}
|