Add native QUIC realtime transport for Unity
This commit is contained in:
parent
1742473845
commit
1d126f4a35
37 changed files with 1150 additions and 86 deletions
|
|
@ -0,0 +1,104 @@
|
|||
using System.Net;
|
||||
using System.Net.Quic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using FluentAssertions;
|
||||
using GameCult.Mesh.Quic;
|
||||
using GameCult.Mesh.Quic.Native;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace GameCult.Mesh.Quic.Native.Tests;
|
||||
|
||||
[TestFixture]
|
||||
public sealed class CultMeshNativeQuicRealtimeTransportTests
|
||||
{
|
||||
[Test]
|
||||
public async Task NativeConnectorAuthenticatesAndReceivesLatestOnlyFrame()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows() || !QuicListener.IsSupported)
|
||||
Assert.Ignore("Native MsQuic integration requires Windows QUIC support.");
|
||||
using var certificate = CreateCertificate();
|
||||
await using var server = await CultMeshQuicRealtimeServer.ListenAsync(new CultMeshQuicRealtimeServerOptions
|
||||
{
|
||||
ListenEndPoint = new IPEndPoint(IPAddress.Loopback, 0),
|
||||
ServerCertificate = certificate
|
||||
});
|
||||
var pin = Convert.ToHexString(SHA256.HashData(certificate.RawData));
|
||||
var endpoint = $"cultmesh-state+quic://127.0.0.1:{server.LocalEndPoint.Port}?cert-sha256={pin}";
|
||||
var connector = new CultMeshNativeQuicRealtimeTransportConnector();
|
||||
|
||||
using var client = await connector.ConnectAsync(
|
||||
new CultMeshTransportCandidate(endpoint),
|
||||
CultMeshEndpointId.Parse("aetheria.local"));
|
||||
var expected = new CultMeshRealtimeFrame
|
||||
{
|
||||
ChannelId = "aetheria.entities",
|
||||
SchemaId = "gamecult.aetheria.entity_soa.v1",
|
||||
BodyId = "aetheria.entities.current-zone",
|
||||
ProducerEpoch = 17,
|
||||
Sequence = 23,
|
||||
Delivery = CultMeshRealtimeDelivery.LatestOnly,
|
||||
Payload = new byte[] { 1, 3, 3, 7 }
|
||||
};
|
||||
await WaitUntilAsync(() => server.ConnectionCount == 1);
|
||||
await server.BroadcastAsync(expected);
|
||||
|
||||
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||
var actual = await client.ReceiveAsync(timeout.Token);
|
||||
|
||||
client.TransportId.Should().Be("msquic-native-realtime");
|
||||
actual.ChannelId.Should().Be(expected.ChannelId);
|
||||
actual.SchemaId.Should().Be(expected.SchemaId);
|
||||
actual.BodyId.Should().Be(expected.BodyId);
|
||||
actual.ProducerEpoch.Should().Be(expected.ProducerEpoch);
|
||||
actual.Sequence.Should().Be(expected.Sequence);
|
||||
actual.Delivery.Should().Be(CultMeshRealtimeDelivery.LatestOnly);
|
||||
actual.Payload.ToArray().Should().Equal(expected.Payload.ToArray());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task NativeConnectorRejectsWrongAdvertisedPin()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows() || !QuicListener.IsSupported)
|
||||
Assert.Ignore("Native MsQuic integration requires Windows QUIC support.");
|
||||
using var certificate = CreateCertificate();
|
||||
await using var server = await CultMeshQuicRealtimeServer.ListenAsync(new CultMeshQuicRealtimeServerOptions
|
||||
{
|
||||
ListenEndPoint = new IPEndPoint(IPAddress.Loopback, 0),
|
||||
ServerCertificate = certificate
|
||||
});
|
||||
var connector = new CultMeshNativeQuicRealtimeTransportConnector();
|
||||
var endpoint = $"cultmesh-state+quic://127.0.0.1:{server.LocalEndPoint.Port}?cert-sha256={new string('0', 64)}";
|
||||
|
||||
var connect = () => connector.ConnectAsync(
|
||||
new CultMeshTransportCandidate(endpoint),
|
||||
CultMeshEndpointId.Parse("aetheria.local"));
|
||||
|
||||
await connect.Should().ThrowAsync<IOException>()
|
||||
.WithMessage("*certificate*pin*");
|
||||
}
|
||||
|
||||
private static async Task WaitUntilAsync(Func<bool> predicate)
|
||||
{
|
||||
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||
while (!predicate())
|
||||
await Task.Delay(10, timeout.Token);
|
||||
}
|
||||
|
||||
private static X509Certificate2 CreateCertificate()
|
||||
{
|
||||
using var key = RSA.Create(2048);
|
||||
var request = new CertificateRequest(
|
||||
"CN=localhost",
|
||||
key,
|
||||
HashAlgorithmName.SHA256,
|
||||
RSASignaturePadding.Pkcs1);
|
||||
request.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, false));
|
||||
request.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.DigitalSignature, false));
|
||||
request.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(request.PublicKey, false));
|
||||
using var generated = request.CreateSelfSigned(
|
||||
DateTimeOffset.UtcNow.AddMinutes(-1),
|
||||
DateTimeOffset.UtcNow.AddMinutes(10));
|
||||
return X509CertificateLoader.LoadPkcs12(generated.Export(X509ContentType.Pfx), null);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<NoWarn>$(NoWarn);CA1416</NoWarn>
|
||||
<NativeQuicOutput>$(MSBuildThisFileDirectory)..\..\artifacts\quic-native-test</NativeQuicOutput>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\GameCult.Mesh.Quic\GameCult.Mesh.Quic.csproj" />
|
||||
<ProjectReference Include="..\..\src\GameCult.Mesh.Quic.Native\GameCult.Mesh.Quic.Native.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NUnit" Version="4.*" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="6.2.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.4.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="8.9.0" />
|
||||
</ItemGroup>
|
||||
<Target Name="BuildNativeQuicTestRuntime" BeforeTargets="Build" Condition="'$(OS)' == 'Windows_NT'">
|
||||
<Exec Command=""$(SystemRoot)\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "$(MSBuildThisFileDirectory)..\..\scripts\build-quic-native.ps1" -Configuration Debug -OutputDirectory "$(NativeQuicOutput)"" />
|
||||
</Target>
|
||||
<Target Name="CopyNativeQuicTestRuntime" AfterTargets="Build" Condition="'$(OS)' == 'Windows_NT'">
|
||||
<ItemGroup>
|
||||
<NativeQuicRuntime Include="$(NativeQuicOutput)\gamecult_mesh_quic_native.dll" />
|
||||
<NativeQuicRuntime Include="$(NativeQuicOutput)\msquic.dll" />
|
||||
</ItemGroup>
|
||||
<Copy SourceFiles="@(NativeQuicRuntime)" DestinationFolder="$(OutDir)" />
|
||||
</Target>
|
||||
</Project>
|
||||
Loading…
Reference in a new issue