improve ByteChannelWebSocket
This commit is contained in:
parent
de3d298475
commit
40eba7a7c7
1 changed files with 79 additions and 111 deletions
|
|
@ -14,6 +14,13 @@ internal sealed class ClientScreenServer(
|
|||
private readonly ConcurrentDictionary<ClientScreenServerConnection, byte> _connections = new();
|
||||
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)
|
||||
{
|
||||
if (_closing)
|
||||
|
|
@ -30,29 +37,26 @@ internal sealed class ClientScreenServer(
|
|||
return connection.Done;
|
||||
}
|
||||
|
||||
public Task StoppingAsync(CancellationToken cancellationToken)
|
||||
private void Remove(ClientScreenServerConnection connection)
|
||||
{
|
||||
logger.LogInformation("closing connections");
|
||||
_closing = true;
|
||||
return Task.WhenAll(_connections.Keys.Select(c => c.CloseAsync()));
|
||||
_connections.TryRemove(connection, out _);
|
||||
}
|
||||
|
||||
public IEnumerable<ClientScreenServerConnection> GetConnections() => _connections.Keys;
|
||||
|
||||
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;
|
||||
private readonly ClientScreenServer _server;
|
||||
private readonly SemaphoreSlim _wantedFrames = new(1);
|
||||
private PixelGrid? _lastSentPixels;
|
||||
|
||||
public ClientScreenServerConnection(WebSocket webSocket,
|
||||
ILogger<ClientScreenServerConnection> logger,
|
||||
|
|
@ -60,12 +64,23 @@ internal sealed class ClientScreenServer(
|
|||
{
|
||||
_server = server;
|
||||
_logger = logger;
|
||||
_channel = new(webSocket, logger, 0);
|
||||
_channel = new ByteChannelWebSocket(webSocket, logger, 0);
|
||||
Done = ReceiveAsync();
|
||||
}
|
||||
|
||||
public async Task SendAsync(PixelGrid buf)
|
||||
public Task Done { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_wantedFrames.Dispose();
|
||||
Done.Dispose();
|
||||
}
|
||||
|
||||
public async Task SendAsync(PixelGrid pixels)
|
||||
{
|
||||
if (_lastSentPixels == pixels)
|
||||
return;
|
||||
|
||||
if (!await _wantedFrames.WaitAsync(TimeSpan.Zero))
|
||||
{
|
||||
_logger.LogTrace("client does not want a frame yet");
|
||||
|
|
@ -75,7 +90,8 @@ internal sealed class ClientScreenServer(
|
|||
_logger.LogTrace("sending");
|
||||
try
|
||||
{
|
||||
await _channel.Writer.WriteAsync(buf.Data);
|
||||
await _channel.SendAsync(pixels.Data);
|
||||
_lastSentPixels = pixels;
|
||||
}
|
||||
catch (ChannelClosedException)
|
||||
{
|
||||
|
|
@ -85,7 +101,7 @@ internal sealed class ClientScreenServer(
|
|||
|
||||
private async Task ReceiveAsync()
|
||||
{
|
||||
await foreach (var _ in _channel.Reader.ReadAllAsync())
|
||||
await foreach (var _ in _channel.ReadAllAsync())
|
||||
_wantedFrames.Release();
|
||||
|
||||
_logger.LogTrace("done receiving");
|
||||
|
|
@ -97,13 +113,5 @@ internal sealed class ClientScreenServer(
|
|||
_logger.LogDebug("closing connection");
|
||||
return _channel.CloseAsync();
|
||||
}
|
||||
|
||||
public Task Done { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_wantedFrames.Dispose();
|
||||
Done.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue