servicepoint-tanks/tanks-backend/TanksServer/Interactivity/ByteChannelWebSocket.cs

95 lines
2.9 KiB
C#
Raw Normal View History

2024-04-07 17:17:11 +02:00
using System.Diagnostics;
using System.Net.WebSockets;
2024-04-10 19:25:45 +02:00
namespace TanksServer.Interactivity;
2024-04-07 17:17:11 +02:00
2024-04-13 12:33:08 +02:00
internal sealed class ByteChannelWebSocket(WebSocket socket, ILogger logger, int messageSize)
2024-04-07 17:17:11 +02:00
{
2024-04-13 12:33:08 +02:00
private readonly byte[] _buffer = new byte[messageSize];
2024-04-07 17:17:11 +02:00
public async ValueTask SendBinaryAsync(ReadOnlyMemory<byte> data, bool endOfMessage = true)
{
try
{
await socket.SendAsync(data, WebSocketMessageType.Binary, endOfMessage, CancellationToken.None);
}
catch (WebSocketException e)
{
logger.LogError(e, "could not send binary message");
await CloseAsync();
}
}
2024-04-07 17:17:11 +02:00
public async ValueTask SendTextAsync(ReadOnlyMemory<byte> data, bool endOfMessage = true)
{
try
{
await socket.SendAsync(data, WebSocketMessageType.Text, endOfMessage, CancellationToken.None);
}
catch (WebSocketException e)
{
logger.LogError(e, "could not send text message");
await CloseAsync();
}
}
2024-04-22 19:03:07 +02:00
2024-04-13 12:33:08 +02:00
public async IAsyncEnumerable<Memory<byte>> ReadAllAsync()
2024-04-07 17:17:11 +02:00
{
while (socket.State is WebSocketState.Open or WebSocketState.CloseSent)
2024-04-07 17:17:11 +02:00
{
if (await TryReadAsync())
yield return _buffer.ToArray();
}
2024-04-13 16:27:45 +02:00
if (socket.State is not WebSocketState.Closed and not WebSocketState.Aborted)
Debugger.Break();
}
public Task CloseWithErrorAsync(string error)
=> socket.CloseOutputAsync(WebSocketCloseStatus.InternalServerError, error, CancellationToken.None);
public async Task CloseAsync()
{
if (socket.State is not WebSocketState.Open and not WebSocketState.CloseReceived)
return;
try
{
await socket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
}
catch (WebSocketException socketException)
{
logger.LogDebug(socketException, "could not close socket properly");
}
}
2024-04-07 17:17:11 +02:00
private async Task<bool> TryReadAsync()
{
try
{
2024-04-13 12:33:08 +02:00
var response = await socket.ReceiveAsync(_buffer, CancellationToken.None);
2024-04-07 17:17:11 +02:00
if (response.MessageType == WebSocketMessageType.Close)
{
await socket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty,
CancellationToken.None);
return false;
2024-04-07 17:17:11 +02:00
}
if (response.Count != _buffer.Length)
{
await socket.CloseOutputAsync(WebSocketCloseStatus.InvalidPayloadData,
2024-04-07 17:17:11 +02:00
"response has unexpected size",
CancellationToken.None);
return false;
2024-04-07 17:17:11 +02:00
}
return true;
}
catch (WebSocketException socketException)
{
logger.LogDebug(socketException, "could not read");
return false;
2024-04-07 17:17:11 +02:00
}
}
}