potential fix for locking issues
This commit is contained in:
parent
7044ffda79
commit
c0172963d5
20 changed files with 112 additions and 141 deletions
|
@ -8,93 +8,66 @@ internal sealed class ClientScreenServerConnection(
|
|||
WebSocket webSocket,
|
||||
ILogger<ClientScreenServerConnection> logger,
|
||||
string? playerName = null
|
||||
) : WebsocketServerConnection(logger, new ByteChannelWebSocket(webSocket, logger, 0)),
|
||||
IDisposable
|
||||
) : WebsocketServerConnection(logger, new ByteChannelWebSocket(webSocket, logger, 0))
|
||||
{
|
||||
private readonly SemaphoreSlim _wantedFramesOnTick = new(0, 2);
|
||||
private readonly SemaphoreSlim _mutex = new(1);
|
||||
private bool _wantsFrameOnTick = true;
|
||||
|
||||
private PixelGrid? _lastSentPixels = null;
|
||||
private PixelGrid? _nextPixels = null;
|
||||
private PixelGrid? _lastSentPixels;
|
||||
private PixelGrid? _nextPixels;
|
||||
private readonly PlayerScreenData? _nextPlayerData = playerName != null ? new PlayerScreenData(logger) : null;
|
||||
|
||||
protected override async ValueTask HandleMessageAsync(Memory<byte> _)
|
||||
protected override async ValueTask HandleMessageLockedAsync(Memory<byte> _)
|
||||
{
|
||||
await _mutex.WaitAsync();
|
||||
if (_nextPixels == null)
|
||||
{
|
||||
_wantsFrameOnTick = true;
|
||||
return;
|
||||
}
|
||||
|
||||
await SendNowAsync();
|
||||
}
|
||||
|
||||
public ValueTask OnGameTickAsync(PixelGrid pixels, GamePixelGrid gamePixelGrid) => LockedAsync(async () =>
|
||||
{
|
||||
if (pixels == _lastSentPixels)
|
||||
return;
|
||||
|
||||
if (_nextPlayerData != null)
|
||||
{
|
||||
_nextPlayerData.Clear();
|
||||
foreach (var gamePixel in gamePixelGrid)
|
||||
{
|
||||
if (!gamePixel.EntityType.HasValue)
|
||||
continue;
|
||||
_nextPlayerData.Add(gamePixel.EntityType.Value, gamePixel.BelongsTo?.Name == playerName);
|
||||
}
|
||||
}
|
||||
|
||||
_nextPixels = pixels;
|
||||
if (_wantsFrameOnTick)
|
||||
_ = await SendNowAsync();
|
||||
});
|
||||
|
||||
private async ValueTask<bool> SendNowAsync()
|
||||
{
|
||||
var pixels = _nextPixels
|
||||
?? throw new InvalidOperationException("next pixels not set");
|
||||
|
||||
try
|
||||
{
|
||||
if (_nextPixels == null)
|
||||
{
|
||||
_wantedFramesOnTick.Release();
|
||||
return;
|
||||
}
|
||||
await Socket.SendBinaryAsync(pixels.Data, _nextPlayerData == null);
|
||||
if (_nextPlayerData != null)
|
||||
await Socket.SendBinaryAsync(_nextPlayerData.GetPacket());
|
||||
|
||||
_lastSentPixels = _nextPixels;
|
||||
_nextPixels = null;
|
||||
await SendNowAsync(_lastSentPixels);
|
||||
}
|
||||
catch (SemaphoreFullException)
|
||||
{
|
||||
logger.LogWarning("ignoring request for more frames");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_mutex.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask OnGameTickAsync(PixelGrid pixels, GamePixelGrid gamePixelGrid)
|
||||
{
|
||||
await _mutex.WaitAsync();
|
||||
try
|
||||
{
|
||||
if (pixels == _lastSentPixels)
|
||||
return;
|
||||
|
||||
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, _nextPlayerData == null);
|
||||
if (_nextPlayerData != null)
|
||||
{
|
||||
await Socket.SendBinaryAsync(_nextPlayerData.GetPacket());
|
||||
}
|
||||
_wantsFrameOnTick = false;
|
||||
return true;
|
||||
}
|
||||
catch (WebSocketException ex)
|
||||
{
|
||||
Logger.LogWarning(ex, "send failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() => _wantedFramesOnTick.Dispose();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue