move a bunch of stuff into subfolders
This commit is contained in:
parent
8f281d65b2
commit
151cad4cee
13 changed files with 44 additions and 27 deletions
6
TanksServer/Helpers/AppSerializerContext.cs
Normal file
6
TanksServer/Helpers/AppSerializerContext.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace TanksServer;
|
||||
|
||||
[JsonSerializable(typeof(Player))]
|
||||
internal partial class AppSerializerContext: JsonSerializerContext;
|
57
TanksServer/Helpers/DisplayPixelBuffer.cs
Normal file
57
TanksServer/Helpers/DisplayPixelBuffer.cs
Normal file
|
@ -0,0 +1,57 @@
|
|||
using TanksServer.Helpers;
|
||||
|
||||
namespace TanksServer;
|
||||
|
||||
internal sealed class DisplayPixelBuffer(byte[] data)
|
||||
{
|
||||
public byte[] Data => data;
|
||||
|
||||
public byte Magic1
|
||||
{
|
||||
get => data[0];
|
||||
set => data[0] = value;
|
||||
}
|
||||
|
||||
public byte Magic2
|
||||
{
|
||||
get => data[1];
|
||||
set => data[1] = value;
|
||||
}
|
||||
|
||||
public ushort X
|
||||
{
|
||||
get => GetTwoBytes(2);
|
||||
set => SetTwoBytes(2, value);
|
||||
}
|
||||
|
||||
public ushort Y
|
||||
{
|
||||
get => GetTwoBytes(4);
|
||||
set => SetTwoBytes(4, value);
|
||||
}
|
||||
|
||||
public ushort WidthInTiles
|
||||
{
|
||||
get => GetTwoBytes(6);
|
||||
set => SetTwoBytes(6, value);
|
||||
}
|
||||
|
||||
public ushort HeightInPixels
|
||||
{
|
||||
get => GetTwoBytes(8);
|
||||
set => SetTwoBytes(8, value);
|
||||
}
|
||||
|
||||
public FixedSizeBitFieldView Pixels { get; } = new(data.AsMemory(10));
|
||||
|
||||
private ushort GetTwoBytes(int index)
|
||||
{
|
||||
return (ushort)(data[index] * byte.MaxValue + data[index + 1]);
|
||||
}
|
||||
|
||||
private void SetTwoBytes(int index, ushort value)
|
||||
{
|
||||
data[index] = (byte)(value / byte.MaxValue);
|
||||
data[index + 1] = (byte)(value % byte.MaxValue);
|
||||
}
|
||||
}
|
74
TanksServer/Helpers/EasyWebSocket.cs
Normal file
74
TanksServer/Helpers/EasyWebSocket.cs
Normal file
|
@ -0,0 +1,74 @@
|
|||
using System.Net.WebSockets;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace TanksServer.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// Hacky class for easier semantics
|
||||
/// </summary>
|
||||
internal abstract class EasyWebSocket
|
||||
{
|
||||
private readonly TaskCompletionSource _completionSource = new();
|
||||
|
||||
private readonly ILogger _logger;
|
||||
private readonly WebSocket _socket;
|
||||
private readonly Task _readLoop;
|
||||
private readonly ArraySegment<byte> _buffer;
|
||||
private int _closed;
|
||||
|
||||
protected EasyWebSocket(WebSocket socket, ILogger logger, ArraySegment<byte> buffer)
|
||||
{
|
||||
_socket = socket;
|
||||
_logger = logger;
|
||||
_buffer = buffer;
|
||||
_readLoop = ReadLoopAsync();
|
||||
}
|
||||
|
||||
public Task Done => _completionSource.Task;
|
||||
|
||||
private async Task ReadLoopAsync()
|
||||
{
|
||||
do
|
||||
{
|
||||
var response = await _socket.ReceiveAsync(_buffer, CancellationToken.None);
|
||||
if (response.CloseStatus.HasValue)
|
||||
break;
|
||||
|
||||
await ReceiveAsync(_buffer[..response.Count]);
|
||||
} while (_socket.State == WebSocketState.Open);
|
||||
}
|
||||
|
||||
protected abstract Task ReceiveAsync(ArraySegment<byte> buffer);
|
||||
protected abstract Task ClosingAsync();
|
||||
|
||||
protected async Task TrySendAsync(byte[] data)
|
||||
{
|
||||
if (_socket.State != WebSocketState.Open)
|
||||
await CloseAsync();
|
||||
|
||||
_logger.LogTrace("sending {} bytes of data", _buffer.Count);
|
||||
|
||||
try
|
||||
{
|
||||
await _socket.SendAsync(data, WebSocketMessageType.Binary, true, CancellationToken.None);
|
||||
}
|
||||
catch (WebSocketException wsEx)
|
||||
{
|
||||
_logger.LogDebug(wsEx, "send failed");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CloseAsync(
|
||||
WebSocketCloseStatus status = WebSocketCloseStatus.NormalClosure,
|
||||
string? description = null
|
||||
)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _closed, 1) == 1)
|
||||
return;
|
||||
_logger.LogDebug("closing socket");
|
||||
await _socket.CloseAsync(status, description, CancellationToken.None);
|
||||
await _readLoop;
|
||||
await ClosingAsync();
|
||||
_completionSource.SetResult();
|
||||
}
|
||||
}
|
74
TanksServer/Helpers/FixedSizeBitFieldView.cs
Normal file
74
TanksServer/Helpers/FixedSizeBitFieldView.cs
Normal file
|
@ -0,0 +1,74 @@
|
|||
using System.Collections;
|
||||
|
||||
namespace TanksServer.Helpers;
|
||||
|
||||
internal sealed class FixedSizeBitFieldView(Memory<byte> data) : IList<bool>
|
||||
{
|
||||
public int Count => data.Length * 8;
|
||||
public bool IsReadOnly => false;
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
public IEnumerator<bool> GetEnumerator()
|
||||
{
|
||||
return Enumerable().GetEnumerator();
|
||||
|
||||
IEnumerable<bool> Enumerable()
|
||||
{
|
||||
for (var i = 0; i < Count; i++)
|
||||
yield return this[i];
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
var span = data.Span;
|
||||
for (var i = 0; i < data.Length; i++)
|
||||
span[i] = 0;
|
||||
}
|
||||
|
||||
public void CopyTo(bool[] array, int arrayIndex)
|
||||
{
|
||||
for (var i = 0; i < Count && i + arrayIndex < array.Length; i++)
|
||||
array[i + arrayIndex] = this[i];
|
||||
}
|
||||
|
||||
private static (int byteIndex, int bitInByteIndex) GetIndexes(int bitIndex)
|
||||
{
|
||||
var byteIndex = bitIndex / 8;
|
||||
var bitInByteIndex = 7 - bitIndex % 8;
|
||||
return (byteIndex, bitInByteIndex);
|
||||
}
|
||||
|
||||
public bool this[int bitIndex]
|
||||
{
|
||||
get
|
||||
{
|
||||
var (byteIndex, bitInByteIndex) = GetIndexes(bitIndex);
|
||||
var bitInByteMask = (byte)(1 << bitInByteIndex);
|
||||
return (data.Span[byteIndex] & bitInByteMask) != 0;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
var (byteIndex, bitInByteIndex) = GetIndexes(bitIndex);
|
||||
var bitInByteMask = (byte)(1 << bitInByteIndex);
|
||||
|
||||
if (value)
|
||||
{
|
||||
data.Span[byteIndex] |= bitInByteMask;
|
||||
}
|
||||
else
|
||||
{
|
||||
var withoutBitMask = (byte)(ushort.MaxValue ^ bitInByteMask);
|
||||
data.Span[byteIndex] &= withoutBitMask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(bool item) => throw new NotSupportedException();
|
||||
public bool Contains(bool item) => throw new NotSupportedException();
|
||||
public bool Remove(bool item) => throw new NotSupportedException();
|
||||
public int IndexOf(bool item) => throw new NotSupportedException();
|
||||
public void Insert(int index, bool item) => throw new NotSupportedException();
|
||||
public void RemoveAt(int index) => throw new NotSupportedException();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue