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
|
@ -7,11 +7,31 @@ internal sealed class ByteChannelWebSocket(WebSocket socket, ILogger logger, int
|
|||
{
|
||||
private readonly byte[] _buffer = new byte[messageSize];
|
||||
|
||||
public ValueTask SendBinaryAsync(ReadOnlyMemory<byte> data, bool endOfMessage = true) =>
|
||||
socket.SendAsync(data, WebSocketMessageType.Binary, endOfMessage, CancellationToken.None);
|
||||
public async ValueTask SendBinaryAsync(ReadOnlyMemory<byte> data, bool endOfMessage = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
await socket.SendAsync(data, WebSocketMessageType.Binary, endOfMessage, CancellationToken.None);
|
||||
}
|
||||
catch (WebSocketException e)
|
||||
{
|
||||
logger.LogError(e, "could not send binary message");
|
||||
await CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public ValueTask SendTextAsync(ReadOnlyMemory<byte> data, bool endOfMessage = true) =>
|
||||
socket.SendAsync(data, WebSocketMessageType.Text, endOfMessage, CancellationToken.None);
|
||||
public async ValueTask SendTextAsync(ReadOnlyMemory<byte> data, bool endOfMessage = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
await socket.SendAsync(data, WebSocketMessageType.Text, endOfMessage, CancellationToken.None);
|
||||
}
|
||||
catch (WebSocketException e)
|
||||
{
|
||||
logger.LogError(e, "could not send text message");
|
||||
await CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<Memory<byte>> ReadAllAsync()
|
||||
{
|
||||
|
@ -25,9 +45,12 @@ internal sealed class ByteChannelWebSocket(WebSocket socket, ILogger logger, int
|
|||
Debugger.Break();
|
||||
}
|
||||
|
||||
public Task CloseWithErrorAsync(string error)
|
||||
=> socket.CloseOutputAsync(WebSocketCloseStatus.InternalServerError, error, CancellationToken.None);
|
||||
|
||||
public async Task CloseAsync()
|
||||
{
|
||||
if (socket.State != WebSocketState.Open)
|
||||
if (socket.State is not WebSocketState.Open and not WebSocketState.CloseReceived)
|
||||
return;
|
||||
|
||||
try
|
||||
|
|
|
@ -6,20 +6,16 @@ namespace TanksServer.Interactivity;
|
|||
|
||||
internal sealed class ClientScreenServer(
|
||||
ILogger<ClientScreenServer> logger,
|
||||
ILoggerFactory loggerFactory,
|
||||
IOptions<HostConfiguration> hostConfig
|
||||
ILoggerFactory loggerFactory
|
||||
) : WebsocketServer<ClientScreenServerConnection>(logger), IFrameConsumer
|
||||
{
|
||||
private readonly TimeSpan _minFrameTime = TimeSpan.FromMilliseconds(hostConfig.Value.ClientDisplayMinFrameTimeMs);
|
||||
|
||||
public Task HandleClientAsync(WebSocket socket, Guid? playerGuid)
|
||||
public Task HandleClientAsync(WebSocket socket, string? player)
|
||||
=> base.HandleClientAsync(new(
|
||||
socket,
|
||||
loggerFactory.CreateLogger<ClientScreenServerConnection>(),
|
||||
_minFrameTime,
|
||||
playerGuid
|
||||
player
|
||||
));
|
||||
|
||||
public Task OnFrameDoneAsync(GamePixelGrid gamePixelGrid, PixelGrid observerPixels)
|
||||
=> ParallelForEachConnectionAsync(c => c.SendAsync(observerPixels, gamePixelGrid));
|
||||
=> ParallelForEachConnectionAsync(c => c.OnGameTickAsync(observerPixels, gamePixelGrid).AsTask());
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ internal sealed class ControlsServer(
|
|||
{
|
||||
public Task HandleClientAsync(WebSocket ws, Player player)
|
||||
{
|
||||
logger.LogDebug("control client connected {}", player.Id);
|
||||
logger.LogDebug("control client connected {}", player.Name);
|
||||
var clientLogger = loggerFactory.CreateLogger<ControlsServerConnection>();
|
||||
var sock = new ControlsServerConnection(ws, clientLogger, player);
|
||||
return HandleClientAsync(sock);
|
||||
|
|
|
@ -28,7 +28,7 @@ internal sealed class ControlsServerConnection(
|
|||
var type = (MessageType)buffer.Span[0];
|
||||
var control = (InputType)buffer.Span[1];
|
||||
|
||||
Logger.LogTrace("player input {} {} {}", player.Id, type, control);
|
||||
Logger.LogTrace("player input {} {} {}", player.Name, type, control);
|
||||
|
||||
var isEnable = type switch
|
||||
{
|
||||
|
|
|
@ -11,40 +11,58 @@ internal sealed class PlayerServer(
|
|||
MapEntityManager entityManager
|
||||
) : WebsocketServer<PlayerInfoConnection>(logger), ITickStep
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, Player> _players = new();
|
||||
private readonly Dictionary<string, Player> _players = [];
|
||||
private readonly SemaphoreSlim _mutex = new(1, 1);
|
||||
|
||||
public Player? GetOrAdd(string name, Guid id)
|
||||
public Player? GetOrAdd(string name)
|
||||
{
|
||||
var existingOrAddedPlayer = _players.GetOrAdd(name, _ => AddAndSpawn());
|
||||
if (existingOrAddedPlayer.Id != id)
|
||||
return null;
|
||||
|
||||
logger.LogInformation("player {} (re)joined", existingOrAddedPlayer.Id);
|
||||
return existingOrAddedPlayer;
|
||||
|
||||
Player AddAndSpawn()
|
||||
_mutex.Wait();
|
||||
try
|
||||
{
|
||||
var newPlayer = new Player(name, id);
|
||||
if (_players.TryGetValue(name, out var existingPlayer))
|
||||
{
|
||||
logger.LogInformation("player {} rejoined", existingPlayer.Name);
|
||||
return existingPlayer;
|
||||
}
|
||||
|
||||
var newPlayer = new Player { Name = name };
|
||||
logger.LogInformation("player {} joined", newPlayer.Name);
|
||||
_players.Add(name, newPlayer);
|
||||
tankSpawnQueue.EnqueueForImmediateSpawn(newPlayer);
|
||||
return newPlayer;
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGet(Guid? playerId, [MaybeNullWhen(false)] out Player foundPlayer)
|
||||
{
|
||||
foreach (var player in _players.Values)
|
||||
finally
|
||||
{
|
||||
if (player.Id != playerId)
|
||||
continue;
|
||||
foundPlayer = player;
|
||||
return true;
|
||||
_mutex.Release();
|
||||
}
|
||||
|
||||
foundPlayer = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public IEnumerable<Player> GetAll() => _players.Values;
|
||||
public bool TryGet(string name, [MaybeNullWhen(false)] out Player foundPlayer)
|
||||
{
|
||||
_mutex.Wait();
|
||||
try
|
||||
{
|
||||
foundPlayer = _players.Values.FirstOrDefault(player => player.Name == name);
|
||||
return foundPlayer != null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_mutex.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Player> GetAll()
|
||||
{
|
||||
_mutex.Wait();
|
||||
try
|
||||
{
|
||||
return _players.Values.ToList();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_mutex.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public Task HandleClientAsync(WebSocket webSocket, Player player)
|
||||
=> HandleClientAsync(new PlayerInfoConnection(player, connectionLogger, webSocket, entityManager));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue