move backend to subfolder
This commit is contained in:
parent
d4d1f2f981
commit
8d09663eff
80 changed files with 98 additions and 88 deletions
|
@ -0,0 +1,10 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
[JsonSerializable(typeof(Player))]
|
||||
[JsonSerializable(typeof(IEnumerable<Player>))]
|
||||
[JsonSerializable(typeof(Guid))]
|
||||
[JsonSerializable(typeof(NameId))]
|
||||
[JsonSerializable(typeof(IEnumerable<string>))]
|
||||
internal sealed partial class AppSerializerContext : JsonSerializerContext;
|
|
@ -0,0 +1,68 @@
|
|||
using System.Diagnostics;
|
||||
using System.Net.WebSockets;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class ByteChannelWebSocket(WebSocket socket, ILogger logger, int messageSize)
|
||||
{
|
||||
private readonly byte[] _buffer = new byte[messageSize];
|
||||
|
||||
public ValueTask SendAsync(ReadOnlyMemory<byte> data, bool endOfMessage = true) =>
|
||||
socket.SendAsync(data, WebSocketMessageType.Binary, endOfMessage, CancellationToken.None);
|
||||
|
||||
public async IAsyncEnumerable<Memory<byte>> ReadAllAsync()
|
||||
{
|
||||
while (socket.State is WebSocketState.Open or WebSocketState.CloseSent)
|
||||
{
|
||||
if (await TryReadAsync())
|
||||
yield return _buffer.ToArray();
|
||||
}
|
||||
|
||||
if (socket.State is not WebSocketState.Closed and not WebSocketState.Aborted)
|
||||
Debugger.Break();
|
||||
}
|
||||
|
||||
public async Task CloseAsync()
|
||||
{
|
||||
if (socket.State != WebSocketState.Open)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await socket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
|
||||
}
|
||||
catch (WebSocketException socketException)
|
||||
{
|
||||
logger.LogDebug(socketException, "could not close socket properly");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<bool> TryReadAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await socket.ReceiveAsync(_buffer, CancellationToken.None);
|
||||
if (response.MessageType == WebSocketMessageType.Close)
|
||||
{
|
||||
await socket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty,
|
||||
CancellationToken.None);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (response.Count != _buffer.Length)
|
||||
{
|
||||
await socket.CloseOutputAsync(WebSocketCloseStatus.InvalidPayloadData,
|
||||
"response has unexpected size",
|
||||
CancellationToken.None);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (WebSocketException socketException)
|
||||
{
|
||||
logger.LogDebug(socketException, "could not read");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
using System.Diagnostics;
|
||||
using System.Net.WebSockets;
|
||||
using DisplayCommands;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using TanksServer.Graphics;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class ClientScreenServer(
|
||||
ILogger<ClientScreenServer> logger,
|
||||
ILoggerFactory loggerFactory,
|
||||
IOptions<HostConfiguration> hostConfig
|
||||
) : IHostedLifecycleService, IFrameConsumer
|
||||
{
|
||||
private readonly ConcurrentDictionary<ClientScreenServerConnection, byte> _connections = new();
|
||||
private readonly TimeSpan _minFrameTime = TimeSpan.FromMilliseconds(hostConfig.Value.ClientDisplayMinFrameTimeMs);
|
||||
private bool _closing;
|
||||
|
||||
public Task StoppingAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
logger.LogInformation("closing connections");
|
||||
_closing = true;
|
||||
return Task.WhenAll(_connections.Keys.Select(c => c.CloseAsync()));
|
||||
}
|
||||
|
||||
public Task HandleClient(WebSocket socket, Guid? playerGuid)
|
||||
{
|
||||
if (_closing)
|
||||
{
|
||||
logger.LogWarning("ignoring request because connections are closing");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
logger.LogDebug("HandleClient");
|
||||
var connection = new ClientScreenServerConnection(
|
||||
socket,
|
||||
loggerFactory.CreateLogger<ClientScreenServerConnection>(),
|
||||
this,
|
||||
_minFrameTime,
|
||||
playerGuid);
|
||||
var added = _connections.TryAdd(connection, 0);
|
||||
Debug.Assert(added);
|
||||
return connection.Done;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
using System.Diagnostics;
|
||||
using System.Net.WebSockets;
|
||||
using DisplayCommands;
|
||||
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;
|
||||
private readonly PlayerScreenData? _playerScreenData;
|
||||
private readonly TimeSpan _minFrameTime;
|
||||
|
||||
private DateTime _nextFrameAfter = DateTime.Now;
|
||||
|
||||
public ClientScreenServerConnection(
|
||||
WebSocket webSocket,
|
||||
ILogger<ClientScreenServerConnection> logger,
|
||||
ClientScreenServer server,
|
||||
TimeSpan minFrameTime,
|
||||
Guid? playerGuid = null
|
||||
)
|
||||
{
|
||||
_server = server;
|
||||
_logger = logger;
|
||||
_minFrameTime = minFrameTime;
|
||||
|
||||
_playerGuid = playerGuid;
|
||||
if (playerGuid.HasValue)
|
||||
_playerScreenData = new PlayerScreenData(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, GamePixelGrid gamePixelGrid)
|
||||
{
|
||||
if (_nextFrameAfter > DateTime.Now)
|
||||
return;
|
||||
|
||||
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
||||
{
|
||||
_logger.LogTrace("client does not want a frame yet");
|
||||
return;
|
||||
}
|
||||
|
||||
_nextFrameAfter = DateTime.Today + _minFrameTime;
|
||||
|
||||
if (_playerScreenData != null)
|
||||
RefreshPlayerSpecificData(gamePixelGrid);
|
||||
|
||||
_logger.LogTrace("sending");
|
||||
try
|
||||
{
|
||||
_logger.LogTrace("sending {} bytes of pixel data", pixels.Data.Length);
|
||||
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();
|
||||
}
|
||||
}
|
115
tanks-backend/TanksServer/Interactivity/ControlsServer.cs
Normal file
115
tanks-backend/TanksServer/Interactivity/ControlsServer.cs
Normal file
|
@ -0,0 +1,115 @@
|
|||
using System.Net.WebSockets;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class ControlsServer(ILogger<ControlsServer> logger, ILoggerFactory loggerFactory)
|
||||
: IHostedLifecycleService
|
||||
{
|
||||
private readonly List<ControlsServerConnection> _connections = [];
|
||||
|
||||
public Task StoppingAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.WhenAll(_connections.Select(c => c.CloseAsync()));
|
||||
}
|
||||
|
||||
public Task HandleClient(WebSocket ws, Player player)
|
||||
{
|
||||
logger.LogDebug("control client connected {}", player.Id);
|
||||
var clientLogger = loggerFactory.CreateLogger<ControlsServerConnection>();
|
||||
var sock = new ControlsServerConnection(ws, clientLogger, this, player);
|
||||
_connections.Add(sock);
|
||||
return sock.Done;
|
||||
}
|
||||
|
||||
private void Remove(ControlsServerConnection connection) => _connections.Remove(connection);
|
||||
|
||||
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;
|
||||
|
||||
private sealed class ControlsServerConnection
|
||||
{
|
||||
private readonly ByteChannelWebSocket _binaryWebSocket;
|
||||
private readonly ILogger<ControlsServerConnection> _logger;
|
||||
private readonly Player _player;
|
||||
private readonly ControlsServer _server;
|
||||
|
||||
public ControlsServerConnection(WebSocket socket, ILogger<ControlsServerConnection> logger,
|
||||
ControlsServer server, Player player)
|
||||
{
|
||||
_logger = logger;
|
||||
_server = server;
|
||||
_player = player;
|
||||
_binaryWebSocket = new ByteChannelWebSocket(socket, logger, 2);
|
||||
Done = ReceiveAsync();
|
||||
}
|
||||
|
||||
public Task Done { get; }
|
||||
|
||||
private async Task ReceiveAsync()
|
||||
{
|
||||
await foreach (var buffer in _binaryWebSocket.ReadAllAsync())
|
||||
{
|
||||
var type = (MessageType)buffer.Span[0];
|
||||
var control = (InputType)buffer.Span[1];
|
||||
|
||||
_logger.LogTrace("player input {} {} {}", _player.Id, type, control);
|
||||
|
||||
var isEnable = type switch
|
||||
{
|
||||
MessageType.Enable => true,
|
||||
MessageType.Disable => false,
|
||||
_ => throw new ArgumentException("invalid message type")
|
||||
};
|
||||
|
||||
_player.LastInput = DateTime.Now;
|
||||
|
||||
switch (control)
|
||||
{
|
||||
case InputType.Forward:
|
||||
_player.Controls.Forward = isEnable;
|
||||
break;
|
||||
case InputType.Backward:
|
||||
_player.Controls.Backward = isEnable;
|
||||
break;
|
||||
case InputType.Left:
|
||||
_player.Controls.TurnLeft = isEnable;
|
||||
break;
|
||||
case InputType.Right:
|
||||
_player.Controls.TurnRight = isEnable;
|
||||
break;
|
||||
case InputType.Shoot:
|
||||
_player.Controls.Shoot = isEnable;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("invalid control type");
|
||||
}
|
||||
}
|
||||
|
||||
_server.Remove(this);
|
||||
}
|
||||
|
||||
public Task CloseAsync()
|
||||
{
|
||||
return _binaryWebSocket.CloseAsync();
|
||||
}
|
||||
|
||||
private enum MessageType : byte
|
||||
{
|
||||
Enable = 0x01,
|
||||
Disable = 0x02
|
||||
}
|
||||
|
||||
private enum InputType : byte
|
||||
{
|
||||
Forward = 0x01,
|
||||
Backward = 0x02,
|
||||
Left = 0x03,
|
||||
Right = 0x04,
|
||||
Shoot = 0x05
|
||||
}
|
||||
}
|
||||
}
|
39
tanks-backend/TanksServer/Interactivity/PlayerScreenData.cs
Normal file
39
tanks-backend/TanksServer/Interactivity/PlayerScreenData.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
using System.Diagnostics;
|
||||
using TanksServer.GameLogic;
|
||||
using TanksServer.Graphics;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class PlayerScreenData(ILogger logger)
|
||||
{
|
||||
private readonly Memory<byte> _data = new byte[MapService.PixelsPerRow * MapService.PixelsPerColumn / 2];
|
||||
private int _count = 0;
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_count = 0;
|
||||
_data.Span.Clear();
|
||||
}
|
||||
|
||||
public ReadOnlyMemory<byte> GetPacket()
|
||||
{
|
||||
var index = _count / 2 + (_count % 2 == 0 ? 0 : 1);
|
||||
logger.LogTrace("packet length: {} (count={})", index, _count);
|
||||
return _data[..index];
|
||||
}
|
||||
|
||||
public void Add(GamePixelEntityType entityKind, bool isCurrentPlayer)
|
||||
{
|
||||
var result = (byte)(isCurrentPlayer ? 0x1 : 0x0);
|
||||
var kind = (byte)entityKind;
|
||||
Debug.Assert(kind <= 3);
|
||||
result += (byte)(kind << 2);
|
||||
|
||||
var index = _count / 2;
|
||||
if (_count % 2 != 0)
|
||||
_data.Span[index] |= (byte)(result << 4);
|
||||
else
|
||||
_data.Span[index] = result;
|
||||
_count++;
|
||||
}
|
||||
}
|
42
tanks-backend/TanksServer/Interactivity/PlayerServer.cs
Normal file
42
tanks-backend/TanksServer/Interactivity/PlayerServer.cs
Normal file
|
@ -0,0 +1,42 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using TanksServer.GameLogic;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class PlayerServer(ILogger<PlayerServer> logger, TankSpawnQueue tankSpawnQueue)
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, Player> _players = new();
|
||||
|
||||
public Player? GetOrAdd(string name, Guid id)
|
||||
{
|
||||
Player AddAndSpawn()
|
||||
{
|
||||
var player = new Player(name, id);
|
||||
tankSpawnQueue.EnqueueForImmediateSpawn(player);
|
||||
return player;
|
||||
}
|
||||
|
||||
var player = _players.GetOrAdd(name, _ => AddAndSpawn());
|
||||
if (player.Id != id)
|
||||
return null;
|
||||
|
||||
logger.LogInformation("player {} (re)joined", player.Id);
|
||||
return player;
|
||||
}
|
||||
|
||||
public bool TryGet(Guid? playerId, [MaybeNullWhen(false)] out Player foundPlayer)
|
||||
{
|
||||
foreach (var player in _players.Values)
|
||||
{
|
||||
if (player.Id != playerId)
|
||||
continue;
|
||||
foundPlayer = player;
|
||||
return true;
|
||||
}
|
||||
|
||||
foundPlayer = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public IEnumerable<Player> GetAll() => _players.Values;
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
using System.Diagnostics;
|
||||
using System.Net.Sockets;
|
||||
using DisplayCommands;
|
||||
using TanksServer.GameLogic;
|
||||
using TanksServer.Graphics;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal sealed class SendToServicePointDisplay : IFrameConsumer
|
||||
{
|
||||
private const int ScoresWidth = 12;
|
||||
private const int ScoresHeight = 20;
|
||||
private const int ScoresPlayerRows = ScoresHeight - 6;
|
||||
|
||||
private readonly IDisplayConnection _displayConnection;
|
||||
private readonly MapService _mapService;
|
||||
private readonly ILogger<SendToServicePointDisplay> _logger;
|
||||
private readonly PlayerServer _players;
|
||||
private readonly Cp437Grid _scoresBuffer;
|
||||
private readonly TimeSpan _minFrameTime;
|
||||
|
||||
private DateTime _nextFailLogAfter = DateTime.Now;
|
||||
private DateTime _nextFrameAfter = DateTime.Now;
|
||||
|
||||
public SendToServicePointDisplay(
|
||||
PlayerServer players,
|
||||
ILogger<SendToServicePointDisplay> logger,
|
||||
IDisplayConnection displayConnection,
|
||||
IOptions<HostConfiguration> hostOptions,
|
||||
MapService mapService
|
||||
)
|
||||
{
|
||||
_players = players;
|
||||
_logger = logger;
|
||||
_displayConnection = displayConnection;
|
||||
_mapService = mapService;
|
||||
_minFrameTime = TimeSpan.FromMilliseconds(hostOptions.Value.ServicePointDisplayMinFrameTimeMs);
|
||||
|
||||
var localIp = _displayConnection.GetLocalIPv4().Split('.');
|
||||
Debug.Assert(localIp.Length == 4);
|
||||
_scoresBuffer = new Cp437Grid(12, 20)
|
||||
{
|
||||
[00] = "== TANKS! ==",
|
||||
[01] = "-- scores --",
|
||||
[17] = "-- join --",
|
||||
[18] = string.Join('.', localIp[..2]),
|
||||
[19] = string.Join('.', localIp[2..])
|
||||
};
|
||||
}
|
||||
|
||||
public async Task OnFrameDoneAsync(GamePixelGrid gamePixelGrid, PixelGrid observerPixels)
|
||||
{
|
||||
if (DateTime.Now < _nextFrameAfter)
|
||||
return;
|
||||
_nextFrameAfter = DateTime.Now + _minFrameTime;
|
||||
|
||||
RefreshScores();
|
||||
|
||||
try
|
||||
{
|
||||
await _displayConnection.SendBitmapLinearWindowAsync(0, 0, observerPixels);
|
||||
await _displayConnection.SendCp437DataAsync(MapService.TilesPerRow, 0, _scoresBuffer);
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
if (DateTime.Now > _nextFailLogAfter)
|
||||
{
|
||||
_logger.LogWarning("could not send data to service point display: {}", ex.Message);
|
||||
_nextFailLogAfter = DateTime.Now + TimeSpan.FromSeconds(5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshScores()
|
||||
{
|
||||
var playersToDisplay = _players.GetAll()
|
||||
.OrderByDescending(p => p.Scores.Kills)
|
||||
.Take(ScoresPlayerRows);
|
||||
|
||||
ushort row = 2;
|
||||
foreach (var p in playersToDisplay)
|
||||
{
|
||||
var score = p.Scores.Kills.ToString();
|
||||
var nameLength = Math.Min(p.Name.Length, ScoresWidth - score.Length - 1);
|
||||
|
||||
var name = p.Name[..nameLength];
|
||||
var spaces = new string(' ', ScoresWidth - score.Length - nameLength);
|
||||
|
||||
_scoresBuffer[row] = name + spaces + score;
|
||||
row++;
|
||||
}
|
||||
|
||||
for (; row < 16; row++)
|
||||
_scoresBuffer[row] = string.Empty;
|
||||
|
||||
_scoresBuffer[16] = _mapService.Current.Name[..(Math.Min(ScoresWidth, _mapService.Current.Name.Length) - 1)];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue