WIP switch to ServicePoint2 library

This commit is contained in:
Vinzenz Schroeter 2024-05-13 01:23:34 +02:00
parent eb999b0d1a
commit e1cfd714c1
31 changed files with 66 additions and 791 deletions

View file

@ -1,5 +1,5 @@
using System.Net.WebSockets;
using DisplayCommands;
using ServicePoint2;
using TanksServer.Graphics;
namespace TanksServer.Interactivity;

View file

@ -1,6 +1,6 @@
using System.Buffers;
using System.Net.WebSockets;
using DisplayCommands;
using ServicePoint2;
using TanksServer.Graphics;
namespace TanksServer.Interactivity;
@ -36,8 +36,9 @@ internal sealed class ClientScreenServerConnection
private Package BuildNextPackage(PixelGrid pixels, GamePixelGrid gamePixelGrid)
{
var nextPixels = _bufferPool.Rent(pixels.Data.Length);
pixels.Data.CopyTo(nextPixels.Memory);
var pixelsData = pixels.Data;
var nextPixels = _bufferPool.Rent(pixelsData.Length);
pixelsData.CopyTo(nextPixels.Memory.Span);
if (_playerDataBuilder == null)
return new Package(nextPixels, null);

View file

@ -13,7 +13,7 @@ internal sealed class PlayerInfoConnection
private readonly MapEntityManager _entityManager;
private readonly BufferPool _bufferPool;
private readonly MemoryStream _tempStream = new();
private IMemoryOwner<byte>? _lastMessage = null;
private IMemoryOwner<byte>? _lastMessage;
public PlayerInfoConnection(
Player player,

View file

@ -1,6 +1,7 @@
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using DisplayCommands;
using ServicePoint2;
using TanksServer.GameLogic;
using TanksServer.Graphics;
@ -12,11 +13,11 @@ internal sealed class SendToServicePointDisplay : IFrameConsumer
private const int ScoresHeight = 20;
private const int ScoresPlayerRows = ScoresHeight - 6;
private readonly IDisplayConnection _displayConnection;
private readonly Connection _displayConnection;
private readonly MapService _mapService;
private readonly ILogger<SendToServicePointDisplay> _logger;
private readonly PlayerServer _players;
private readonly Cp437Grid _scoresBuffer;
private readonly ByteGrid _scoresBuffer;
private readonly TimeSpan _minFrameTime;
private readonly IOptionsMonitor<HostConfiguration> _options;
@ -26,11 +27,11 @@ internal sealed class SendToServicePointDisplay : IFrameConsumer
public SendToServicePointDisplay(
PlayerServer players,
ILogger<SendToServicePointDisplay> logger,
IDisplayConnection displayConnection,
Connection displayConnection,
IOptions<HostConfiguration> hostOptions,
MapService mapService,
IOptionsMonitor<HostConfiguration> options
)
IOptionsMonitor<HostConfiguration> options,
IOptions<DisplayConfiguration> displayConfig)
{
_players = players;
_logger = logger;
@ -39,16 +40,15 @@ internal sealed class SendToServicePointDisplay : IFrameConsumer
_minFrameTime = TimeSpan.FromMilliseconds(hostOptions.Value.ServicePointDisplayMinFrameTimeMs);
_options = options;
var localIp = _displayConnection.GetLocalIPv4().Split('.');
var localIp = GetLocalIPv4(displayConfig.Value).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..])
};
_scoresBuffer = ByteGrid.New(12, 20);
_scoresBuffer[00] = "== TANKS! ==";
_scoresBuffer[01] = "-- scores --";
_scoresBuffer[17] = "-- join --";
_scoresBuffer[18] = string.Join('.', localIp[..2]);
_scoresBuffer[19] = string.Join('.', localIp[2..]);
}
public async Task OnFrameDoneAsync(GamePixelGrid gamePixelGrid, PixelGrid observerPixels)
@ -66,8 +66,8 @@ internal sealed class SendToServicePointDisplay : IFrameConsumer
try
{
await _displayConnection.SendBitmapLinearWindowAsync(0, 0, observerPixels);
await _displayConnection.SendCp437DataAsync(MapService.TilesPerRow, 0, _scoresBuffer);
_displayConnection.Send(Command.BitmapLinearWin(0, 0, observerPixels.Clone()));
_displayConnection.Send(Command.Cp437Data(MapService.TilesPerRow, 0, _scoresBuffer.Clone()));
}
catch (SocketException ex)
{
@ -103,4 +103,12 @@ internal sealed class SendToServicePointDisplay : IFrameConsumer
_scoresBuffer[16] = _mapService.Current.Name[..(Math.Min(ScoresWidth, _mapService.Current.Name.Length) - 1)];
}
private static string GetLocalIPv4(DisplayConfiguration configuration)
{
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0);
socket.Connect(configuration.Hostname, configuration.Port);
var endPoint = socket.LocalEndPoint as IPEndPoint ?? throw new NotSupportedException();
return endPoint.Address.ToString();
}
}