prepare to send different data per client
This commit is contained in:
parent
fcd84d2c83
commit
fbaad86555
15 changed files with 245 additions and 128 deletions
|
@ -7,8 +7,8 @@ internal sealed class ByteChannelWebSocket(WebSocket socket, ILogger logger, int
|
|||
{
|
||||
private readonly byte[] _buffer = new byte[messageSize];
|
||||
|
||||
public ValueTask SendAsync(ReadOnlyMemory<byte> data) =>
|
||||
socket.SendAsync(data, WebSocketMessageType.Binary, true, CancellationToken.None);
|
||||
public ValueTask SendAsync(ReadOnlyMemory<byte> data, bool endOfMessage = true) =>
|
||||
socket.SendAsync(data, WebSocketMessageType.Binary, endOfMessage, CancellationToken.None);
|
||||
|
||||
public async IAsyncEnumerable<Memory<byte>> ReadAllAsync()
|
||||
{
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
using System.Diagnostics;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Channels;
|
||||
using DisplayCommands;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using TanksServer.Graphics;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class ClientScreenServer(
|
||||
ILogger<ClientScreenServer> logger,
|
||||
ILoggerFactory loggerFactory
|
||||
) : IHostedLifecycleService
|
||||
) : IHostedLifecycleService, IFrameConsumer
|
||||
{
|
||||
private readonly ConcurrentDictionary<ClientScreenServerConnection, byte> _connections = new();
|
||||
private bool _closing;
|
||||
|
@ -37,76 +37,21 @@ internal sealed class ClientScreenServer(
|
|||
return connection.Done;
|
||||
}
|
||||
|
||||
private void Remove(ClientScreenServerConnection connection)
|
||||
{
|
||||
_connections.TryRemove(connection, out _);
|
||||
}
|
||||
public void Remove(ClientScreenServerConnection connection) => _connections.TryRemove(connection, out _);
|
||||
|
||||
public IEnumerable<ClientScreenServerConnection> GetConnections() => _connections.Keys;
|
||||
|
||||
|
||||
public Task OnFrameDoneAsync(GamePixelGrid gamePixelGrid, PixelGrid observerPixels)
|
||||
{
|
||||
var tasks = _connections.Keys
|
||||
.Select(c => c.SendAsync(observerPixels, gamePixelGrid));
|
||||
return Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
public Task StartedAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
public Task StartingAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
public Task StoppedAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
|
||||
internal sealed class ClientScreenServerConnection : IDisposable
|
||||
{
|
||||
private readonly ByteChannelWebSocket _channel;
|
||||
private readonly ILogger<ClientScreenServerConnection> _logger;
|
||||
private readonly ClientScreenServer _server;
|
||||
private readonly SemaphoreSlim _wantedFrames = new(1);
|
||||
|
||||
public ClientScreenServerConnection(WebSocket webSocket,
|
||||
ILogger<ClientScreenServerConnection> logger,
|
||||
ClientScreenServer server)
|
||||
{
|
||||
_server = server;
|
||||
_logger = logger;
|
||||
_channel = new ByteChannelWebSocket(webSocket, logger, 0);
|
||||
Done = ReceiveAsync();
|
||||
}
|
||||
|
||||
public Task Done { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_wantedFrames.Dispose();
|
||||
Done.Dispose();
|
||||
}
|
||||
|
||||
public async Task SendAsync(PixelGrid pixels)
|
||||
{
|
||||
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
||||
{
|
||||
_logger.LogTrace("client does not want a frame yet");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogTrace("sending");
|
||||
try
|
||||
{
|
||||
await _channel.SendAsync(pixels.Data);
|
||||
}
|
||||
catch (WebSocketException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "send failed");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReceiveAsync()
|
||||
{
|
||||
await foreach (var _ in _channel.ReadAllAsync())
|
||||
_wantedFrames.Release();
|
||||
|
||||
_logger.LogTrace("done receiving");
|
||||
_server.Remove(this);
|
||||
}
|
||||
|
||||
public Task CloseAsync()
|
||||
{
|
||||
_logger.LogDebug("closing connection");
|
||||
return _channel.CloseAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
115
TanksServer/Interactivity/ClientScreenServerConnection.cs
Normal file
115
TanksServer/Interactivity/ClientScreenServerConnection.cs
Normal file
|
@ -0,0 +1,115 @@
|
|||
using System.Diagnostics;
|
||||
using System.Net.WebSockets;
|
||||
using DisplayCommands;
|
||||
using TanksServer.GameLogic;
|
||||
using TanksServer.Graphics;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class ClientScreenServerConnection : IDisposable
|
||||
{
|
||||
private readonly ByteChannelWebSocket _channel;
|
||||
private readonly ILogger<ClientScreenServerConnection> _logger;
|
||||
private readonly ClientScreenServer _server;
|
||||
private readonly SemaphoreSlim _wantedFrames = new(1);
|
||||
private readonly Guid? _playerGuid = null;
|
||||
private readonly PlayerScreenData? _playerScreenData = null;
|
||||
|
||||
public ClientScreenServerConnection(
|
||||
WebSocket webSocket,
|
||||
ILogger<ClientScreenServerConnection> logger,
|
||||
ClientScreenServer server,
|
||||
Guid? playerGuid = null
|
||||
)
|
||||
{
|
||||
_server = server;
|
||||
_logger = logger;
|
||||
|
||||
_playerGuid = playerGuid;
|
||||
if (playerGuid.HasValue)
|
||||
_playerScreenData = new PlayerScreenData();
|
||||
|
||||
_channel = new ByteChannelWebSocket(webSocket, logger, 0);
|
||||
Done = ReceiveAsync();
|
||||
}
|
||||
|
||||
public Task Done { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_wantedFrames.Dispose();
|
||||
Done.Dispose();
|
||||
}
|
||||
|
||||
public async Task SendAsync(PixelGrid pixels, GamePixelGrid gamePixelGrid)
|
||||
{
|
||||
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
||||
{
|
||||
_logger.LogTrace("client does not want a frame yet");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_playerScreenData != null)
|
||||
RefreshPlayerSpecificData(gamePixelGrid);
|
||||
|
||||
_logger.LogTrace("sending");
|
||||
try
|
||||
{
|
||||
await _channel.SendAsync(pixels.Data, _playerScreenData == null);
|
||||
if (_playerScreenData != null)
|
||||
await _channel.SendAsync(_playerScreenData.GetPacket());
|
||||
}
|
||||
catch (WebSocketException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "send failed");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReceiveAsync()
|
||||
{
|
||||
await foreach (var _ in _channel.ReadAllAsync())
|
||||
_wantedFrames.Release();
|
||||
|
||||
_logger.LogTrace("done receiving");
|
||||
_server.Remove(this);
|
||||
}
|
||||
|
||||
public Task CloseAsync()
|
||||
{
|
||||
_logger.LogDebug("closing connection");
|
||||
return _channel.CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class PlayerScreenData
|
||||
{
|
||||
private Memory<byte> _data = new byte[MapService.PixelsPerRow * MapService.PixelsPerColumn];
|
||||
|
||||
public int Count { get; private set; } = 0;
|
||||
|
||||
public void Clear() => Count = 0;
|
||||
|
||||
public ReadOnlyMemory<byte> GetPacket() => _data[..Count];
|
||||
|
||||
public void Add(GamePixelEntityType entityKind, bool isCurrentPlayer)
|
||||
{
|
||||
var result = (byte)(isCurrentPlayer ? 0x1b : 0x0b);
|
||||
var kind = (byte)entityKind;
|
||||
Debug.Assert(kind < 3);
|
||||
result += (byte)(kind << 2);
|
||||
_data.Span[Count] = result;
|
||||
Count++;
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
using TanksServer.GameLogic;
|
||||
using TanksServer.Graphics;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class SendToClientScreen(
|
||||
ClientScreenServer clientScreenServer,
|
||||
LastFinishedFrameProvider lastFinishedFrameProvider
|
||||
) : ITickStep
|
||||
{
|
||||
public Task TickAsync()
|
||||
{
|
||||
var tasks = clientScreenServer
|
||||
.GetConnections()
|
||||
.Select(c => c.SendAsync(lastFinishedFrameProvider.LastFrame));
|
||||
return Task.WhenAll(tasks);
|
||||
}
|
||||
}
|
|
@ -6,14 +6,13 @@ using TanksServer.Graphics;
|
|||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class SendToServicePointDisplay : ITickStep
|
||||
internal sealed class SendToServicePointDisplay : IFrameConsumer
|
||||
{
|
||||
private const int ScoresWidth = 12;
|
||||
private const int ScoresHeight = 20;
|
||||
private const int ScoresPlayerRows = ScoresHeight - 5;
|
||||
|
||||
private readonly IDisplayConnection _displayConnection;
|
||||
private readonly LastFinishedFrameProvider _lastFinishedFrameProvider;
|
||||
private readonly ILogger<SendToServicePointDisplay> _logger;
|
||||
private readonly PlayerServer _players;
|
||||
private readonly Cp437Grid _scoresBuffer;
|
||||
|
@ -22,13 +21,11 @@ internal sealed class SendToServicePointDisplay : ITickStep
|
|||
private DateTime _nextFailLog = DateTime.Now;
|
||||
|
||||
public SendToServicePointDisplay(
|
||||
LastFinishedFrameProvider lastFinishedFrameProvider,
|
||||
PlayerServer players,
|
||||
ILogger<SendToServicePointDisplay> logger,
|
||||
IDisplayConnection displayConnection
|
||||
)
|
||||
{
|
||||
_lastFinishedFrameProvider = lastFinishedFrameProvider;
|
||||
_players = players;
|
||||
_logger = logger;
|
||||
_displayConnection = displayConnection;
|
||||
|
@ -45,17 +42,16 @@ internal sealed class SendToServicePointDisplay : ITickStep
|
|||
};
|
||||
}
|
||||
|
||||
public async Task TickAsync()
|
||||
public async Task OnFrameDoneAsync(GamePixelGrid gamePixelGrid, PixelGrid observerPixels)
|
||||
{
|
||||
RefreshScores();
|
||||
try
|
||||
{
|
||||
await _displayConnection.SendCp437DataAsync(MapService.TilesPerRow, 0, _scoresBuffer);
|
||||
|
||||
var currentFrame = _lastFinishedFrameProvider.LastFrame;
|
||||
if (_lastSentFrame == currentFrame)
|
||||
if (_lastSentFrame == observerPixels)
|
||||
return;
|
||||
_lastSentFrame = currentFrame;
|
||||
_lastSentFrame = observerPixels;
|
||||
await _displayConnection.SendBitmapLinearWindowAsync(0, 0, _lastSentFrame);
|
||||
}
|
||||
catch (SocketException ex)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue