2024-04-07 17:17:11 +02:00
|
|
|
using System.Diagnostics;
|
2024-04-06 16:38:26 +02:00
|
|
|
using System.Net.WebSockets;
|
2024-04-07 18:18:26 +02:00
|
|
|
using System.Threading.Channels;
|
2024-04-12 16:05:24 +02:00
|
|
|
using DisplayCommands;
|
2024-04-06 20:32:54 +02:00
|
|
|
using Microsoft.Extensions.Hosting;
|
2024-04-06 16:38:26 +02:00
|
|
|
|
2024-04-10 19:25:45 +02:00
|
|
|
namespace TanksServer.Interactivity;
|
2024-04-06 16:38:26 +02:00
|
|
|
|
2024-04-06 20:32:54 +02:00
|
|
|
internal sealed class ClientScreenServer(
|
|
|
|
ILogger<ClientScreenServer> logger,
|
2024-04-07 19:52:16 +02:00
|
|
|
ILoggerFactory loggerFactory
|
|
|
|
) : IHostedLifecycleService
|
2024-04-06 16:38:26 +02:00
|
|
|
{
|
2024-04-07 17:17:11 +02:00
|
|
|
private readonly ConcurrentDictionary<ClientScreenServerConnection, byte> _connections = new();
|
|
|
|
private bool _closing;
|
2024-04-06 16:38:26 +02:00
|
|
|
|
2024-04-13 12:33:08 +02:00
|
|
|
public Task StoppingAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
logger.LogInformation("closing connections");
|
|
|
|
_closing = true;
|
|
|
|
return Task.WhenAll(_connections.Keys.Select(c => c.CloseAsync()));
|
|
|
|
}
|
|
|
|
|
2024-04-06 20:32:54 +02:00
|
|
|
public Task HandleClient(WebSocket socket)
|
2024-04-06 16:38:26 +02:00
|
|
|
{
|
2024-04-07 17:17:11 +02:00
|
|
|
if (_closing)
|
|
|
|
{
|
|
|
|
logger.LogWarning("ignoring request because connections are closing");
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
2024-04-06 20:32:54 +02:00
|
|
|
logger.LogDebug("HandleClient");
|
|
|
|
var connection =
|
2024-04-06 21:15:26 +02:00
|
|
|
new ClientScreenServerConnection(socket, loggerFactory.CreateLogger<ClientScreenServerConnection>(), this);
|
2024-04-07 17:17:11 +02:00
|
|
|
var added = _connections.TryAdd(connection, 0);
|
|
|
|
Debug.Assert(added);
|
2024-04-06 20:32:54 +02:00
|
|
|
return connection.Done;
|
2024-04-06 16:38:26 +02:00
|
|
|
}
|
|
|
|
|
2024-04-13 12:33:08 +02:00
|
|
|
private void Remove(ClientScreenServerConnection connection)
|
2024-04-06 16:38:26 +02:00
|
|
|
{
|
2024-04-13 12:33:08 +02:00
|
|
|
_connections.TryRemove(connection, out _);
|
2024-04-06 16:38:26 +02:00
|
|
|
}
|
|
|
|
|
2024-04-13 12:33:08 +02:00
|
|
|
public IEnumerable<ClientScreenServerConnection> GetConnections() => _connections.Keys;
|
|
|
|
|
2024-04-06 20:32:54 +02:00
|
|
|
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;
|
2024-04-07 17:17:11 +02:00
|
|
|
|
2024-04-12 16:05:24 +02:00
|
|
|
internal sealed class ClientScreenServerConnection : IDisposable
|
2024-04-06 21:15:26 +02:00
|
|
|
{
|
2024-04-07 17:17:11 +02:00
|
|
|
private readonly ByteChannelWebSocket _channel;
|
|
|
|
private readonly ILogger<ClientScreenServerConnection> _logger;
|
2024-04-13 12:33:08 +02:00
|
|
|
private readonly ClientScreenServer _server;
|
|
|
|
private readonly SemaphoreSlim _wantedFrames = new(1);
|
|
|
|
private PixelGrid? _lastSentPixels;
|
2024-04-06 16:38:26 +02:00
|
|
|
|
2024-04-07 17:17:11 +02:00
|
|
|
public ClientScreenServerConnection(WebSocket webSocket,
|
|
|
|
ILogger<ClientScreenServerConnection> logger,
|
|
|
|
ClientScreenServer server)
|
2024-04-06 21:15:26 +02:00
|
|
|
{
|
2024-04-07 17:17:11 +02:00
|
|
|
_server = server;
|
|
|
|
_logger = logger;
|
2024-04-13 12:33:08 +02:00
|
|
|
_channel = new ByteChannelWebSocket(webSocket, logger, 0);
|
2024-04-07 17:17:11 +02:00
|
|
|
Done = ReceiveAsync();
|
2024-04-06 21:15:26 +02:00
|
|
|
}
|
2024-04-06 16:38:26 +02:00
|
|
|
|
2024-04-13 12:33:08 +02:00
|
|
|
public Task Done { get; }
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
_wantedFrames.Dispose();
|
|
|
|
Done.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task SendAsync(PixelGrid pixels)
|
2024-04-06 21:15:26 +02:00
|
|
|
{
|
2024-04-13 12:33:08 +02:00
|
|
|
if (_lastSentPixels == pixels)
|
|
|
|
return;
|
|
|
|
|
2024-04-07 19:05:50 +02:00
|
|
|
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
2024-04-07 17:17:11 +02:00
|
|
|
{
|
2024-04-07 19:05:50 +02:00
|
|
|
_logger.LogTrace("client does not want a frame yet");
|
|
|
|
return;
|
2024-04-07 17:17:11 +02:00
|
|
|
}
|
2024-04-07 19:05:50 +02:00
|
|
|
|
|
|
|
_logger.LogTrace("sending");
|
|
|
|
try
|
2024-04-07 17:17:11 +02:00
|
|
|
{
|
2024-04-13 12:33:08 +02:00
|
|
|
await _channel.SendAsync(pixels.Data);
|
|
|
|
_lastSentPixels = pixels;
|
2024-04-07 19:05:50 +02:00
|
|
|
}
|
|
|
|
catch (ChannelClosedException)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("send failed, channel is closed");
|
2024-04-07 17:17:11 +02:00
|
|
|
}
|
2024-04-06 21:15:26 +02:00
|
|
|
}
|
2024-04-06 20:32:54 +02:00
|
|
|
|
2024-04-07 17:17:11 +02:00
|
|
|
private async Task ReceiveAsync()
|
2024-04-06 21:15:26 +02:00
|
|
|
{
|
2024-04-13 12:33:08 +02:00
|
|
|
await foreach (var _ in _channel.ReadAllAsync())
|
2024-04-07 17:17:11 +02:00
|
|
|
_wantedFrames.Release();
|
2024-04-12 16:05:24 +02:00
|
|
|
|
2024-04-07 17:17:11 +02:00
|
|
|
_logger.LogTrace("done receiving");
|
|
|
|
_server.Remove(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task CloseAsync()
|
|
|
|
{
|
|
|
|
_logger.LogDebug("closing connection");
|
|
|
|
return _channel.CloseAsync();
|
|
|
|
}
|
2024-04-06 16:38:26 +02:00
|
|
|
}
|
2024-04-13 12:33:08 +02:00
|
|
|
}
|