separate tick steps
This commit is contained in:
parent
898a9cedc1
commit
a9aaf899a2
28 changed files with 239 additions and 194 deletions
111
TanksServer/Servers/ClientScreenServer.cs
Normal file
111
TanksServer/Servers/ClientScreenServer.cs
Normal file
|
@ -0,0 +1,111 @@
|
|||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Channels;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TanksServer.Helpers;
|
||||
|
||||
namespace TanksServer.Servers;
|
||||
|
||||
internal sealed class ClientScreenServer(
|
||||
ILogger<ClientScreenServer> logger,
|
||||
ILoggerFactory loggerFactory
|
||||
) : IHostedLifecycleService
|
||||
{
|
||||
private readonly ConcurrentDictionary<ClientScreenServerConnection, byte> _connections = new();
|
||||
private bool _closing;
|
||||
|
||||
public Task HandleClient(WebSocket socket)
|
||||
{
|
||||
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);
|
||||
var added = _connections.TryAdd(connection, 0);
|
||||
Debug.Assert(added);
|
||||
return connection.Done;
|
||||
}
|
||||
|
||||
public Task StoppingAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
logger.LogInformation("closing connections");
|
||||
_closing = true;
|
||||
return Task.WhenAll(_connections.Keys.Select(c => c.CloseAsync()));
|
||||
}
|
||||
|
||||
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 void Remove(ClientScreenServerConnection connection) => _connections.TryRemove(connection, out _);
|
||||
|
||||
public IEnumerable<ClientScreenServerConnection> GetConnections() => _connections.Keys;
|
||||
|
||||
internal sealed class ClientScreenServerConnection: IDisposable
|
||||
{
|
||||
private readonly ByteChannelWebSocket _channel;
|
||||
private readonly SemaphoreSlim _wantedFrames = new(1);
|
||||
private readonly ClientScreenServer _server;
|
||||
private readonly ILogger<ClientScreenServerConnection> _logger;
|
||||
|
||||
public ClientScreenServerConnection(WebSocket webSocket,
|
||||
ILogger<ClientScreenServerConnection> logger,
|
||||
ClientScreenServer server)
|
||||
{
|
||||
_server = server;
|
||||
_logger = logger;
|
||||
_channel = new(webSocket, logger, 0);
|
||||
Done = ReceiveAsync();
|
||||
}
|
||||
|
||||
public async Task SendAsync(DisplayPixelBuffer buf)
|
||||
{
|
||||
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
||||
{
|
||||
_logger.LogTrace("client does not want a frame yet");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogTrace("sending");
|
||||
try
|
||||
{
|
||||
await _channel.Writer.WriteAsync(buf.Data);
|
||||
}
|
||||
catch (ChannelClosedException)
|
||||
{
|
||||
_logger.LogWarning("send failed, channel is closed");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ReceiveAsync()
|
||||
{
|
||||
await foreach (var _ in _channel.Reader.ReadAllAsync())
|
||||
_wantedFrames.Release();
|
||||
|
||||
_logger.LogTrace("done receiving");
|
||||
_server.Remove(this);
|
||||
}
|
||||
|
||||
public Task CloseAsync()
|
||||
{
|
||||
_logger.LogDebug("closing connection");
|
||||
return _channel.CloseAsync();
|
||||
}
|
||||
|
||||
public Task Done { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_wantedFrames.Dispose();
|
||||
Done.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
115
TanksServer/Servers/ControlsServer.cs
Normal file
115
TanksServer/Servers/ControlsServer.cs
Normal file
|
@ -0,0 +1,115 @@
|
|||
using System.Net.WebSockets;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TanksServer.Helpers;
|
||||
|
||||
namespace TanksServer.Servers;
|
||||
|
||||
internal sealed class ControlsServer(ILogger<ControlsServer> logger, ILoggerFactory loggerFactory)
|
||||
: IHostedLifecycleService
|
||||
{
|
||||
private readonly List<ControlsServerConnection> _connections = new();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Task StoppingAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.WhenAll(_connections.Select(c => c.CloseAsync()));
|
||||
}
|
||||
|
||||
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 void Remove(ControlsServerConnection connection)
|
||||
{
|
||||
_connections.Remove(connection);
|
||||
}
|
||||
|
||||
private sealed class ControlsServerConnection
|
||||
{
|
||||
private readonly ByteChannelWebSocket _binaryWebSocket;
|
||||
private readonly ILogger<ControlsServerConnection> _logger;
|
||||
private readonly ControlsServer _server;
|
||||
private readonly Player _player;
|
||||
|
||||
public ControlsServerConnection(WebSocket socket, ILogger<ControlsServerConnection> logger,
|
||||
ControlsServer server, Player player)
|
||||
{
|
||||
_logger = logger;
|
||||
_server = server;
|
||||
_player = player;
|
||||
_binaryWebSocket = new(socket, logger, 2);
|
||||
Done = ReceiveAsync();
|
||||
}
|
||||
|
||||
public Task Done { get; }
|
||||
|
||||
private enum MessageType : byte
|
||||
{
|
||||
Enable = 0x01,
|
||||
Disable = 0x02,
|
||||
}
|
||||
|
||||
private enum InputType : byte
|
||||
{
|
||||
Forward = 0x01,
|
||||
Backward = 0x02,
|
||||
Left = 0x03,
|
||||
Right = 0x04,
|
||||
Shoot = 0x05
|
||||
}
|
||||
|
||||
private async Task ReceiveAsync()
|
||||
{
|
||||
await foreach (var buffer in _binaryWebSocket.Reader.ReadAllAsync())
|
||||
{
|
||||
var type = (MessageType)buffer[0];
|
||||
var control = (InputType)buffer[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")
|
||||
};
|
||||
|
||||
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() => _binaryWebSocket.CloseAsync();
|
||||
}
|
||||
}
|
39
TanksServer/Servers/PlayerServer.cs
Normal file
39
TanksServer/Servers/PlayerServer.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TanksServer.TickSteps;
|
||||
|
||||
namespace TanksServer.Servers;
|
||||
|
||||
internal sealed class PlayerServer(ILogger<PlayerServer> logger, SpawnNewTanks spawnNewTanks)
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, Player> _players = new();
|
||||
|
||||
public Player GetOrAdd(string name)
|
||||
{
|
||||
var player = _players.GetOrAdd(name, AddAndSpawn);
|
||||
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;
|
||||
}
|
||||
|
||||
private Player AddAndSpawn(string name)
|
||||
{
|
||||
var player = new Player(name);
|
||||
spawnNewTanks.SpawnTankForPlayer(player);
|
||||
return player;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue