fix server does not shut down
This commit is contained in:
parent
53cbdd8440
commit
8b44168b66
9 changed files with 59 additions and 47 deletions
|
@ -1,5 +1,4 @@
|
|||
using System.Net.WebSockets;
|
||||
using ServicePoint;
|
||||
using TanksServer.Graphics;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
using System.Buffers;
|
||||
using System.Net.WebSockets;
|
||||
using ServicePoint;
|
||||
using TanksServer.Graphics;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
internal class LoggingLifecycleService(ILogger logger) : IHostedLifecycleService
|
||||
{
|
||||
private protected readonly ILogger Logger = logger;
|
||||
|
||||
public virtual Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Logger.LogDebug("StartAsync");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual Task StartedAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Logger.LogDebug("StartedAsync");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual Task StartingAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Logger.LogDebug("StartingAsync");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Logger.LogDebug("StopAsync");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual Task StoppedAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Logger.LogDebug("StoppedAsync");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual Task StoppingAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Logger.LogDebug("StoppingAsync");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using ServicePoint;
|
||||
using TanksServer.GameLogic;
|
||||
using TanksServer.Graphics;
|
||||
|
||||
|
@ -42,13 +41,13 @@ internal sealed class SendToServicePointDisplay : IFrameConsumer, IDisposable
|
|||
|
||||
var localIp = GetLocalIPv4(displayConfig.Value).Split('.');
|
||||
Debug.Assert(localIp.Length == 4);
|
||||
_scoresBuffer = new CharGrid(12, 20);
|
||||
_scoresBuffer = new CharGrid(ScoresWidth, ScoresHeight);
|
||||
|
||||
_scoresBuffer.SetRow(00, "== TANKS! ==");
|
||||
_scoresBuffer.SetRow(01, "-- scores --");
|
||||
_scoresBuffer.SetRow(17, "-- join --");
|
||||
_scoresBuffer.SetRow(18, string.Join('.', localIp[..2]));
|
||||
_scoresBuffer.SetRow(19, string.Join('.', localIp[2..]));
|
||||
_scoresBuffer.SetRow(18, $"{localIp[0]}.{localIp[1]}".PadRight(ScoresWidth));
|
||||
_scoresBuffer.SetRow(19, $"{localIp[2]}.{localIp[3]}".PadRight(ScoresWidth));
|
||||
}
|
||||
|
||||
public async Task OnFrameDoneAsync(GamePixelGrid gamePixelGrid, Bitmap observerPixels)
|
||||
|
@ -101,7 +100,7 @@ internal sealed class SendToServicePointDisplay : IFrameConsumer, IDisposable
|
|||
for (; row < 16; row++)
|
||||
_scoresBuffer.SetRow(row, new string(' ', ScoresWidth));
|
||||
|
||||
_scoresBuffer.SetRow(16, _mapService.Current.Name[..(Math.Min(ScoresWidth, _mapService.Current.Name.Length) - 1)]);
|
||||
_scoresBuffer.SetRow(16, _mapService.Current.Name[..(Math.Min(ScoresWidth, _mapService.Current.Name.Length) - 1)].PadRight(ScoresWidth));
|
||||
}
|
||||
|
||||
private static string GetLocalIPv4(DisplayConfiguration configuration)
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
using System.Diagnostics;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace TanksServer.Interactivity;
|
||||
|
||||
internal abstract class WebsocketServer<T>(
|
||||
ILogger logger
|
||||
) : IHostedLifecycleService
|
||||
) : LoggingLifecycleService(logger)
|
||||
where T : WebsocketServerConnection
|
||||
{
|
||||
private bool _closing;
|
||||
private readonly ConcurrentDictionary<T, byte> _connections = [];
|
||||
|
||||
public async Task StoppingAsync(CancellationToken cancellationToken)
|
||||
public async override Task StoppingAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await base.StoppingAsync(cancellationToken);
|
||||
_closing = true;
|
||||
logger.LogInformation("closing connections");
|
||||
Logger.LogInformation("closing connections");
|
||||
await _connections.Keys.Select(c => c.CloseAsync())
|
||||
.WhenAll();
|
||||
logger.LogInformation("closed connections");
|
||||
Logger.LogInformation("closed connections");
|
||||
}
|
||||
|
||||
protected IEnumerable<T> Connections => _connections.Keys;
|
||||
|
@ -26,7 +26,7 @@ internal abstract class WebsocketServer<T>(
|
|||
{
|
||||
if (_closing)
|
||||
{
|
||||
logger.LogWarning("refusing connection because server is shutting down");
|
||||
Logger.LogWarning("refusing connection because server is shutting down");
|
||||
await connection.CloseAsync();
|
||||
return;
|
||||
}
|
||||
|
@ -39,14 +39,4 @@ internal abstract class WebsocketServer<T>(
|
|||
_ = _connections.TryRemove(connection, out _);
|
||||
connection.Dispose();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue