remove guid, reduce latency (gets stuck sometimes tho)
This commit is contained in:
parent
6bc6a039bd
commit
7044ffda79
19 changed files with 291 additions and 251 deletions
|
@ -1,4 +1,3 @@
|
|||
using System.Diagnostics;
|
||||
using System.Net.WebSockets;
|
||||
using DisplayCommands;
|
||||
using TanksServer.Graphics;
|
||||
|
@ -8,40 +7,88 @@ namespace TanksServer.Interactivity;
|
|||
internal sealed class ClientScreenServerConnection(
|
||||
WebSocket webSocket,
|
||||
ILogger<ClientScreenServerConnection> logger,
|
||||
TimeSpan minFrameTime,
|
||||
Guid? playerGuid = null
|
||||
string? playerName = null
|
||||
) : WebsocketServerConnection(logger, new ByteChannelWebSocket(webSocket, logger, 0)),
|
||||
IDisposable
|
||||
{
|
||||
private readonly SemaphoreSlim _wantedFrames = new(1);
|
||||
private readonly PlayerScreenData? _playerScreenData = playerGuid.HasValue ? new PlayerScreenData(logger) : null;
|
||||
private DateTime _nextFrameAfter = DateTime.Now;
|
||||
private readonly SemaphoreSlim _wantedFramesOnTick = new(0, 2);
|
||||
private readonly SemaphoreSlim _mutex = new(1);
|
||||
|
||||
public void Dispose() => _wantedFrames.Dispose();
|
||||
private PixelGrid? _lastSentPixels = null;
|
||||
private PixelGrid? _nextPixels = null;
|
||||
private readonly PlayerScreenData? _nextPlayerData = playerName != null ? new PlayerScreenData(logger) : null;
|
||||
|
||||
public async Task SendAsync(PixelGrid pixels, GamePixelGrid gamePixelGrid)
|
||||
protected override async ValueTask HandleMessageAsync(Memory<byte> _)
|
||||
{
|
||||
if (_nextFrameAfter > DateTime.Now)
|
||||
return;
|
||||
|
||||
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
||||
await _mutex.WaitAsync();
|
||||
try
|
||||
{
|
||||
Logger.LogTrace("client does not want a frame yet");
|
||||
return;
|
||||
if (_nextPixels == null)
|
||||
{
|
||||
_wantedFramesOnTick.Release();
|
||||
return;
|
||||
}
|
||||
|
||||
_lastSentPixels = _nextPixels;
|
||||
_nextPixels = null;
|
||||
await SendNowAsync(_lastSentPixels);
|
||||
}
|
||||
catch (SemaphoreFullException)
|
||||
{
|
||||
logger.LogWarning("ignoring request for more frames");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_mutex.Release();
|
||||
}
|
||||
}
|
||||
|
||||
_nextFrameAfter = DateTime.Today + minFrameTime;
|
||||
public async ValueTask OnGameTickAsync(PixelGrid pixels, GamePixelGrid gamePixelGrid)
|
||||
{
|
||||
await _mutex.WaitAsync();
|
||||
try
|
||||
{
|
||||
if (pixels == _lastSentPixels)
|
||||
return;
|
||||
|
||||
if (_playerScreenData != null)
|
||||
RefreshPlayerSpecificData(gamePixelGrid);
|
||||
if (_nextPlayerData != null)
|
||||
{
|
||||
_nextPlayerData.Clear();
|
||||
foreach (var gamePixel in gamePixelGrid)
|
||||
{
|
||||
if (!gamePixel.EntityType.HasValue)
|
||||
continue;
|
||||
_nextPlayerData.Add(gamePixel.EntityType.Value, gamePixel.BelongsTo?.Name == playerName);
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
Logger.LogTrace("sending");
|
||||
try
|
||||
{
|
||||
Logger.LogTrace("sending {} bytes of pixel data", pixels.Data.Length);
|
||||
await Socket.SendBinaryAsync(pixels.Data, _playerScreenData == null);
|
||||
if (_playerScreenData != null)
|
||||
await Socket.SendBinaryAsync(_playerScreenData.GetPacket());
|
||||
await Socket.SendBinaryAsync(pixels.Data, _nextPlayerData == null);
|
||||
if (_nextPlayerData != null)
|
||||
{
|
||||
await Socket.SendBinaryAsync(_nextPlayerData.GetPacket());
|
||||
}
|
||||
}
|
||||
catch (WebSocketException ex)
|
||||
{
|
||||
|
@ -49,21 +96,5 @@ internal sealed class ClientScreenServerConnection(
|
|||
}
|
||||
}
|
||||
|
||||
private void RefreshPlayerSpecificData(GamePixelGrid gamePixelGrid)
|
||||
{
|
||||
Debug.Assert(_playerScreenData != null);
|
||||
_playerScreenData.Clear();
|
||||
foreach (var gamePixel in gamePixelGrid)
|
||||
{
|
||||
if (!gamePixel.EntityType.HasValue)
|
||||
continue;
|
||||
_playerScreenData.Add(gamePixel.EntityType.Value, gamePixel.BelongsTo?.Id == playerGuid);
|
||||
}
|
||||
}
|
||||
|
||||
protected override ValueTask HandleMessageAsync(Memory<byte> _)
|
||||
{
|
||||
_wantedFrames.Release();
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
public void Dispose() => _wantedFramesOnTick.Dispose();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue