Updated
This commit is contained in:
1 parent
2c5b69bf2c
commit
22dee59f47
132 files changed
+19653
-7192
No files matched your search
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\EonaCat.Connections.Quic\EonaCat.Connections.Quic.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,117 @@
|
||||
using EonaCat.Connections.Models;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace EonaCat.Connections.Quic.Server.Example
|
||||
{
|
||||
[RequiresPreviewFeatures]
|
||||
public class Program
|
||||
{
|
||||
private const string LISTEN_IP = "127.0.0.1";
|
||||
private const int LISTEN_PORT = 8443;
|
||||
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
|
||||
using var certificate = GenerateSelfSignedCertificate();
|
||||
|
||||
var config = new Configuration
|
||||
{
|
||||
Host = LISTEN_IP,
|
||||
Port = LISTEN_PORT,
|
||||
Certificate = certificate,
|
||||
IsSelfSignedEnabled = true,
|
||||
};
|
||||
|
||||
using var server = new QuicNetworkServer(config);
|
||||
|
||||
server.OnConnected += (sender, e) =>
|
||||
{
|
||||
Console.WriteLine($"[QUIC Server] Client connected: {e.ClientId} from {e.RemoteEndPoint}");
|
||||
};
|
||||
|
||||
server.OnDataReceived += async (sender, e) =>
|
||||
{
|
||||
Console.WriteLine($"[QUIC Server] Received from {e.Nickname ?? e.ClientId}: {e.StringData}");
|
||||
|
||||
if (sender is QuicNetworkServer srv)
|
||||
{
|
||||
await srv.SendToClientAsync(e.ClientId, $"Echo: {e.StringData}");
|
||||
}
|
||||
};
|
||||
|
||||
server.OnDisconnected += (sender, e) =>
|
||||
{
|
||||
Console.WriteLine($"[QUIC Server] Client disconnected: {e.Nickname ?? e.ClientId} ({e.Reason})");
|
||||
};
|
||||
|
||||
server.OnGeneralError += (sender, e) =>
|
||||
{
|
||||
Console.WriteLine($"[QUIC Server] Error: {e.Message} - {e.Exception?.Message}");
|
||||
};
|
||||
|
||||
Console.WriteLine($"[QUIC Server] Starting on {LISTEN_IP}:{LISTEN_PORT}...");
|
||||
await server.StartAsync();
|
||||
Console.WriteLine("[QUIC Server] Started. Press 'q' to stop.");
|
||||
|
||||
while (true)
|
||||
{
|
||||
var key = Console.ReadKey(true);
|
||||
if (key.KeyChar == 'q')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (key.KeyChar == 'b')
|
||||
{
|
||||
Console.Write("Broadcast message: ");
|
||||
var msg = Console.ReadLine();
|
||||
if (!string.IsNullOrEmpty(msg))
|
||||
{
|
||||
await server.BroadcastAsync(msg);
|
||||
}
|
||||
}
|
||||
|
||||
if (key.KeyChar == 'c')
|
||||
{
|
||||
var clients = server.GetClients();
|
||||
Console.WriteLine($"[QUIC Server] Active connections: {server.ActiveConnections}");
|
||||
foreach (var client in clients)
|
||||
{
|
||||
Console.WriteLine($" - {client.Value.Nickname} ({client.Key}) from {client.Value.RemoteEndPoint}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("[QUIC Server] Stopping...");
|
||||
await server.StopAsync();
|
||||
Console.WriteLine("[QUIC Server] Stopped.");
|
||||
}
|
||||
|
||||
private static X509Certificate2 GenerateSelfSignedCertificate()
|
||||
{
|
||||
|
||||
using var ecdsa = ECDsa.Create(ECCurve.NamedCurves.nistP256);
|
||||
var request = new CertificateRequest("CN=localhost", ecdsa, HashAlgorithmName.SHA256);
|
||||
|
||||
request.CertificateExtensions.Add(
|
||||
new X509KeyUsageExtension(
|
||||
X509KeyUsageFlags.DigitalSignature,
|
||||
critical: false));
|
||||
|
||||
request.CertificateExtensions.Add(
|
||||
new X509EnhancedKeyUsageExtension(
|
||||
new OidCollection { new Oid("1.3.6.1.5.5.7.3.1") },
|
||||
critical: false));
|
||||
|
||||
var sanBuilder = new SubjectAlternativeNameBuilder();
|
||||
sanBuilder.AddDnsName("localhost");
|
||||
sanBuilder.AddIpAddress(System.Net.IPAddress.Loopback);
|
||||
request.CertificateExtensions.Add(sanBuilder.Build());
|
||||
|
||||
using var certificate = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-5), DateTimeOffset.UtcNow.AddYears(200));
|
||||
return new X509Certificate2(certificate.Export(X509ContentType.Pfx), (string?)null, X509KeyStorageFlags.Exportable);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user