Meum/Meum.Client/Client.cs
2025-05-08 21:48:57 +05:00

58 lines
1.9 KiB
C#

global using System;
global using System.Collections.Generic;
global using System.Threading.Tasks;
global using Meum.Core;
using System.Net;
using System.Net.Quic;
using System.Net.Security;
using DTLib.Logging;
namespace Meum.Client;
public class Client
{
private readonly HashSet<ServerConnection> _connectedServers = new();
private readonly ILogger _logger;
public Client(ILogger logger)
{
_logger = logger;
}
public IReadOnlySet<ServerConnection> ConnectedServers => _connectedServers;
public UserAddress? Address { get; private set; }
public Task RegisterAsync(UserAddress address)
{
return Task.CompletedTask;
}
public Task LogInAsync(UserAddress address)
{
if(Address != null)
throw new InvalidOperationException("Already logged in");
Address = address;
return Task.CompletedTask;
}
public async Task<ServerConnection> ConnectToServerAsync(DnsEndPoint serverEndPoint)
{
var quicConn = await QuicConnection.ConnectAsync(new QuicClientConnectionOptions
{
// TODO serverEndPoint
RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, Constants.ServerPortDefault),
DefaultStreamErrorCode = Constants.DefaultStreamErrorCode,
DefaultCloseErrorCode = Constants.DefaultCloseErrorCode,
ClientAuthenticationOptions = new SslClientAuthenticationOptions
{
ApplicationProtocols = Constants.ApplicationProtocols,
TargetHost = serverEndPoint.Host
}
});
var serv = new ServerConnection(quicConn, serverEndPoint, _logger);
if(!_connectedServers.Add(serv))
throw new Exception($"Is already connected to server '{serverEndPoint.Host}'");
return serv;
}
}