using System.IO; using System.Net.Quic; using DTLib.Logging; using Meum.Core.Messages; namespace Meum.Server; public class ClientConnection : IAsyncDisposable { private readonly QuicConnection _quicConnection; private QuicStreamWrapper? _systemStream; private ILogger _logger; public ClientConnection(QuicConnection quicConnection, ILogger logger) { _quicConnection = quicConnection; _logger = logger; } private async Task AcceptStreamAsync(QuicStreamType streamType, CancellationToken ct = default) { var s = await _quicConnection.AcceptInboundStreamAsync(ct); if (s.Type != streamType) throw new Exception($"Accepted stream type is invalid: {s.Type} instead of {streamType}"); var w = new QuicStreamWrapper(s); return w; } public async void HandleClientRequestsAsync(CancellationToken ct = default) { if(_systemStream != null) throw new Exception("Already connected to client"); _systemStream = await AcceptStreamAsync(QuicStreamType.Bidirectional, ct); // receive "Ping" if(await _systemStream.ReadCodeMessageAsync(ct) != MessageTypeCode.Ping) throw new Exception("Failed to test application protocol"); // send "Pong" await _systemStream.WriteCodeMessageAsync(MessageTypeCode.Pong, ct); DataMessageHeader header = await _systemStream.ReadDataMessageHeaderAsync(ct); switch (header.type_code) { case MessageTypeCode.RegistrationRequest: break; case MessageTypeCode.AuthorizationRequest: break; default: throw new Exception($"New connection sent unexpected message: {header.type_code}"); } } public async ValueTask DisposeAsync() { await _quicConnection.DisposeAsync(); } }