47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
using System.Net;
|
|
using System.Net.Quic;
|
|
using System.Net.Security;
|
|
using DTLib.Extensions;
|
|
|
|
namespace Meum.Client;
|
|
|
|
public class ServerConnection : IDisposable
|
|
{
|
|
public DnsEndPoint ServerEndPoint { get; }
|
|
|
|
private QuicConnection? _quicConnection;
|
|
|
|
public ServerConnection(DnsEndPoint serverEndPoint)
|
|
{
|
|
ServerEndPoint = serverEndPoint;
|
|
}
|
|
|
|
public async Task ConnectAsync()
|
|
{
|
|
_quicConnection = await QuicConnection.ConnectAsync(new QuicClientConnectionOptions
|
|
{
|
|
RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, Network.ServerPortDefault),
|
|
DefaultStreamErrorCode = Network.DefaultStreamErrorCode,
|
|
DefaultCloseErrorCode = Network.DefaultCloseErrorCode,
|
|
ClientAuthenticationOptions = new SslClientAuthenticationOptions
|
|
{
|
|
ApplicationProtocols = Network.ApplicationProtocols.ToList(),
|
|
TargetHost = ServerEndPoint.Host
|
|
}
|
|
});
|
|
}
|
|
|
|
public async Task PingAsync()
|
|
{
|
|
var stream = await _quicConnection!.OpenOutboundStreamAsync(QuicStreamType.Bidirectional);
|
|
await stream.WriteAsync("Ping\n".ToBytes());
|
|
StreamReader reader = new StreamReader(stream);
|
|
string line = await reader.ReadLineAsync() ?? String.Empty;
|
|
Console.WriteLine(line);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_quicConnection?.DisposeAsync();
|
|
}
|
|
} |