66 lines
2.2 KiB
C#
66 lines
2.2 KiB
C#
global using System;
|
|
global using System.Collections.Generic;
|
|
global using System.Threading;
|
|
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, CancellationToken ct = default)
|
|
{
|
|
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
|
|
}
|
|
}, ct);
|
|
|
|
var timeOutCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
|
timeOutCts.CancelAfter(Constants.ConnectionTimeout);
|
|
var serv = await ServerConnection.OpenAsync(quicConn,
|
|
serverEndPoint,
|
|
_logger,
|
|
timeOutCts.Token);
|
|
|
|
if(!_connectedServers.Add(serv))
|
|
throw new Exception($"Is already connected to server '{serverEndPoint.Host}'");
|
|
return serv;
|
|
}
|
|
} |