feat(tooling): add standalone rendezvous test client (#25)
quality-gate / quality (push) Failing after 1m3s
quality-gate / quality (push) Failing after 1m3s
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using LiteNetLib;
|
||||
using LiteNetLib.Utils;
|
||||
|
||||
namespace FinalFactory.Rendezvous.TestClient;
|
||||
|
||||
internal sealed class DirectEchoProtocol : IDisposable
|
||||
{
|
||||
private const string PingPrefix = "rv1-ping:";
|
||||
private const string EchoPrefix = "rv1-echo:";
|
||||
private const string AckPrefix = "rv1-ack:";
|
||||
private const string DonePrefix = "rv1-done:";
|
||||
private readonly EventBasedNetListener _events;
|
||||
private readonly bool _host;
|
||||
private readonly Dictionary<NetPeer, string> _hostNonces = [];
|
||||
private readonly TaskCompletionSource<bool> _completed = new(
|
||||
TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
private string? _nonce;
|
||||
private bool _disposed;
|
||||
|
||||
internal DirectEchoProtocol(EventBasedNetListener events, bool host)
|
||||
{
|
||||
_events = events ?? throw new ArgumentNullException(nameof(events));
|
||||
_host = host;
|
||||
_events.NetworkReceiveEvent += OnReceive;
|
||||
_events.PeerDisconnectedEvent += OnPeerDisconnected;
|
||||
}
|
||||
|
||||
internal Task Completion => _completed.Task;
|
||||
internal int PendingHostExchangeCount => _hostNonces.Count;
|
||||
internal event Action<NetPeer>? ExchangeCompleted;
|
||||
|
||||
internal void BeginJoin(NetPeer peer)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
if (_host || _nonce is not null)
|
||||
{
|
||||
throw new InvalidOperationException("The direct echo exchange is already active.");
|
||||
}
|
||||
|
||||
_nonce = Convert.ToHexString(RandomNumberGenerator.GetBytes(16)).ToLowerInvariant();
|
||||
Send(peer, PingPrefix + _nonce);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_events.NetworkReceiveEvent -= OnReceive;
|
||||
_events.PeerDisconnectedEvent -= OnPeerDisconnected;
|
||||
_hostNonces.Clear();
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
private void OnReceive(
|
||||
NetPeer peer,
|
||||
NetPacketReader reader,
|
||||
byte channel,
|
||||
DeliveryMethod deliveryMethod)
|
||||
{
|
||||
try
|
||||
{
|
||||
ReadOnlySpan<byte> payload = reader.GetRemainingBytesSpan();
|
||||
if (payload.Length is < 9 or > 64)
|
||||
{
|
||||
return;
|
||||
}
|
||||
string message = Encoding.ASCII.GetString(payload);
|
||||
if (_host && TryNonce(message, PingPrefix, out string? pingNonce))
|
||||
{
|
||||
_hostNonces[peer] = pingNonce!;
|
||||
Send(peer, EchoPrefix + pingNonce);
|
||||
}
|
||||
else if (_host
|
||||
&& _hostNonces.TryGetValue(peer, out string? hostNonce)
|
||||
&& string.Equals(message, AckPrefix + hostNonce, StringComparison.Ordinal))
|
||||
{
|
||||
_hostNonces.Remove(peer);
|
||||
Send(peer, DonePrefix + hostNonce);
|
||||
ExchangeCompleted?.Invoke(peer);
|
||||
_completed.TrySetResult(true);
|
||||
}
|
||||
else if (!_host
|
||||
&& _nonce is not null
|
||||
&& string.Equals(message, EchoPrefix + _nonce, StringComparison.Ordinal))
|
||||
{
|
||||
Send(peer, AckPrefix + _nonce);
|
||||
}
|
||||
else if (!_host
|
||||
&& _nonce is not null
|
||||
&& string.Equals(message, DonePrefix + _nonce, StringComparison.Ordinal))
|
||||
{
|
||||
ExchangeCompleted?.Invoke(peer);
|
||||
_completed.TrySetResult(true);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
reader.Recycle();
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryNonce(string message, string prefix, out string? nonce)
|
||||
{
|
||||
nonce = null;
|
||||
if (!message.StartsWith(prefix, StringComparison.Ordinal)
|
||||
|| message.Length != prefix.Length + 32)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string candidate = message[prefix.Length..];
|
||||
if (!candidate.All(static character => character is >= '0' and <= '9'
|
||||
or >= 'a' and <= 'f'))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
nonce = candidate;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void Send(NetPeer peer, string message) => peer.Send(
|
||||
Encoding.ASCII.GetBytes(message),
|
||||
DeliveryMethod.ReliableOrdered);
|
||||
|
||||
private void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo) =>
|
||||
_hostNonces.Remove(peer);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using FinalFactory.Rendezvous.Contracts;
|
||||
|
||||
namespace FinalFactory.Rendezvous.TestClient;
|
||||
|
||||
internal sealed class HostServiceFailureBudget
|
||||
{
|
||||
private const int MaximumConsecutiveTransientFailures = 3;
|
||||
private int _consecutiveTransientFailures;
|
||||
|
||||
internal bool ShouldStop(
|
||||
RendezvousErrorCode error,
|
||||
DateTimeOffset leaseExpiresAt,
|
||||
DateTimeOffset now)
|
||||
{
|
||||
if (error == RendezvousErrorCode.None)
|
||||
{
|
||||
Reset();
|
||||
return false;
|
||||
}
|
||||
if (!IsTransient(error))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
_consecutiveTransientFailures++;
|
||||
return _consecutiveTransientFailures >= MaximumConsecutiveTransientFailures
|
||||
|| now >= leaseExpiresAt;
|
||||
}
|
||||
|
||||
internal void Reset() => _consecutiveTransientFailures = 0;
|
||||
|
||||
private static bool IsTransient(RendezvousErrorCode error) => error is
|
||||
RendezvousErrorCode.RateLimited
|
||||
or RendezvousErrorCode.ServiceUnavailable
|
||||
or RendezvousErrorCode.InternalError;
|
||||
}
|
||||
@@ -1,16 +1,29 @@
|
||||
namespace FinalFactory.Rendezvous.TestClient;
|
||||
|
||||
/// <summary>
|
||||
/// Bootstrap entry point for the public-SDK-only diagnostic client.
|
||||
/// </summary>
|
||||
public static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// Runs the bootstrap diagnostic.
|
||||
/// </summary>
|
||||
public static int Main()
|
||||
public static async Task<int> Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Rendezvous TestClient bootstrap is ready.");
|
||||
return 0;
|
||||
using CancellationTokenSource shutdown = new();
|
||||
ConsoleCancelEventHandler cancelHandler = (_, eventArgs) =>
|
||||
{
|
||||
eventArgs.Cancel = true;
|
||||
shutdown.Cancel();
|
||||
};
|
||||
Console.CancelKeyPress += cancelHandler;
|
||||
try
|
||||
{
|
||||
TestClientApplication application = new(new RendezvousCommandRunner());
|
||||
return await application.RunAsync(
|
||||
args,
|
||||
Console.In,
|
||||
Console.Out,
|
||||
Console.Error,
|
||||
shutdown.Token).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.CancelKeyPress -= cancelHandler;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("FinalFactory.Rendezvous.Tests")]
|
||||
@@ -0,0 +1,56 @@
|
||||
# FinalFactory.Rendezvous.TestClient
|
||||
|
||||
This is a diagnostic executable for exercising Rendezvous through the same public
|
||||
Client and Contracts API available to a game. It is not a production game client,
|
||||
server browser, dedicated server, relay, account system, or gameplay host.
|
||||
|
||||
The executable has three explicit modes:
|
||||
|
||||
- `host` publishes a session, maintains presence and its lease, accepts an
|
||||
authenticated direct peer, and answers a bounded ping/echo/ack/completion exchange;
|
||||
- `browse` prints compatible public listings; and
|
||||
- `join` selects or accepts a listing, drives traversal on its caller-owned
|
||||
LiteNetLib socket, proves direct traffic, reports the typed outcome, and exits.
|
||||
|
||||
Run `dotnet run --project src/FinalFactory.Rendezvous.TestClient -- --help` for
|
||||
the complete option reference. A typical script-mode invocation is:
|
||||
|
||||
```bash
|
||||
export RENDEZVOUS_PUBLISHER_CREDENTIAL='<credential from the deployment boundary>'
|
||||
dotnet run --project src/FinalFactory.Rendezvous.TestClient -- \
|
||||
host --service http://127.0.0.1:5000/ --mediator 127.0.0.1:9050 \
|
||||
--game space-game --environment development --region local --protocol 1 \
|
||||
--script --json --exit-after-echo
|
||||
```
|
||||
|
||||
Publisher credentials are accepted only through a named environment variable.
|
||||
There is deliberately no command-line credential option because process command
|
||||
lines are routinely exposed to other local tools and diagnostics. Output uses an
|
||||
allowlisted event model and never includes lease tokens, punch capabilities,
|
||||
connection tickets, raw metadata, signing material, or reusable credentials.
|
||||
|
||||
Script mode never prompts. Join mode selects the first compatible listing unless
|
||||
`--listing UUID` fixes the choice. `--json` emits one JSON object per line with
|
||||
`version: 1`; event names and the process exit codes below are stable automation
|
||||
contracts. A script-mode host without `--run-seconds` uses `--timeout-seconds` as
|
||||
its total runtime bound. New optional event properties may be added without changing
|
||||
the version. JSON help and usage failures are versioned events as well; informational
|
||||
events use stdout and failures use stderr.
|
||||
|
||||
| Exit | Meaning |
|
||||
|---:|---|
|
||||
| `0` | Requested diagnostic flow completed successfully |
|
||||
| `2` | Invalid command or options |
|
||||
| `3` | Missing or invalid local configuration |
|
||||
| `10` | HTTP, registration, browser, lease, or socket failure |
|
||||
| `11` | No compatible session was available or selected |
|
||||
| `12` | Authorization or traversal reached a typed terminal failure |
|
||||
| `13` | A requested direct ping/echo proof did not complete |
|
||||
| `130` | Caller cancellation or Ctrl+C |
|
||||
|
||||
The client prints the selected direct endpoint category (`loopback`, `private`, or
|
||||
`public`) but never the raw endpoint. A traversal failure reports whether an
|
||||
authoritative dedicated fallback is available; the diagnostic does not connect to
|
||||
that fallback automatically. A host may publish a policy-authorized endpoint with
|
||||
`--fallback IP:PORT`. See the repository integration guide for process
|
||||
orchestration and topology limitations.
|
||||
@@ -0,0 +1,774 @@
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using FinalFactory.Rendezvous.Client;
|
||||
using FinalFactory.Rendezvous.Contracts;
|
||||
using LiteNetLib;
|
||||
|
||||
namespace FinalFactory.Rendezvous.TestClient;
|
||||
|
||||
internal sealed class RendezvousCommandRunner : ITestClientCommandRunner
|
||||
{
|
||||
private static readonly TimeSpan PollDelay = TimeSpan.FromMilliseconds(5);
|
||||
private static readonly TimeSpan HostRefreshInterval = TimeSpan.FromMilliseconds(250);
|
||||
private static readonly TimeSpan DirectTrafficFlushGrace = TimeSpan.FromMilliseconds(500);
|
||||
|
||||
public Task<TestClientExitCode> RunAsync(
|
||||
TestClientOptions options,
|
||||
TestClientOutput output,
|
||||
TextReader input,
|
||||
CancellationToken cancellationToken) => options.Mode switch
|
||||
{
|
||||
TestClientMode.Host => RunHostAsync(options, output, cancellationToken),
|
||||
TestClientMode.Browse => RunBrowseAsync(options, output, cancellationToken),
|
||||
TestClientMode.Join => RunJoinAsync(options, output, input, cancellationToken),
|
||||
_ => Task.FromResult(TestClientExitCode.Usage),
|
||||
};
|
||||
|
||||
private static async Task<TestClientExitCode> RunHostAsync(
|
||||
TestClientOptions options,
|
||||
TestClientOutput output,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
string? publisherCredential = Environment.GetEnvironmentVariable(
|
||||
options.PublisherCredentialEnvironmentVariable);
|
||||
if (!ContractValidation.IsOpaqueHttpCredentialValid(publisherCredential))
|
||||
{
|
||||
output.WriteError(
|
||||
"host.configuration",
|
||||
"failed",
|
||||
"The publisher credential environment variable is missing or invalid.",
|
||||
phase: "configuration");
|
||||
return TestClientExitCode.Configuration;
|
||||
}
|
||||
string credential = publisherCredential!;
|
||||
|
||||
using HttpClient http = CreateHttpClient(options);
|
||||
RendezvousPublisherClient publisher = new(http, ClientOptions(options));
|
||||
RendezvousSessionBrowserClient browser = new(http, ClientOptions(options));
|
||||
RendezvousJoinClient joins = new(http, ClientOptions(options));
|
||||
RendezvousNetListener events = new();
|
||||
NetManager manager = events.CreateManager();
|
||||
if (!manager.Start(options.LocalPort))
|
||||
{
|
||||
output.WriteError("host.socket", "failed", "The gameplay UDP socket could not start.", phase: "presence");
|
||||
return TestClientExitCode.ServiceFailure;
|
||||
}
|
||||
|
||||
PublishedSession? session = null;
|
||||
using CancellationTokenSource hostOperations = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
Task<RendezvousClientResult<int>>? refresh = null;
|
||||
Task<RendezvousClientResult<RenewLeaseResponse>>? renewal = null;
|
||||
Task<RendezvousClientResult<GetSessionResponse>>? readiness = null;
|
||||
DirectEchoProtocol? echo = null;
|
||||
RendezvousHostCoordinator? coordinator = null;
|
||||
TestClientExitCode hostResult = TestClientExitCode.ServiceFailure;
|
||||
bool cleanupFailed = false;
|
||||
try
|
||||
{
|
||||
output.Write("host.registration", "started", phase: "registration");
|
||||
RendezvousClientResult<PublishedSession> registration;
|
||||
using (CancellationTokenSource registrationTimeout = CreateOperationTimeout(options, cancellationToken))
|
||||
{
|
||||
try
|
||||
{
|
||||
registration = await publisher.RegisterAsync(
|
||||
new RegisterSessionRequest
|
||||
{
|
||||
IdempotencyKey = Guid.NewGuid().ToString("N"),
|
||||
GameId = options.GameId,
|
||||
EnvironmentId = options.EnvironmentId,
|
||||
RegionId = options.RegionId,
|
||||
ProtocolVersion = options.ProtocolVersion,
|
||||
BuildVersion = options.BuildVersion,
|
||||
DisplayName = options.DisplayName,
|
||||
Visibility = ListingVisibility.Public,
|
||||
Capacity = new SessionCapacity { CurrentPlayers = 1, MaximumPlayers = 8 },
|
||||
Metadata = new Dictionary<string, string>(options.Metadata, StringComparer.Ordinal),
|
||||
DedicatedFallback = options.DedicatedFallback,
|
||||
},
|
||||
credential,
|
||||
registrationTimeout.Token).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
output.WriteError(
|
||||
"host.registration",
|
||||
"timed-out",
|
||||
"Host registration exceeded the bounded startup stage.",
|
||||
phase: "registration");
|
||||
return TestClientExitCode.ServiceFailure;
|
||||
}
|
||||
}
|
||||
if (!registration.IsSuccess || registration.Value is null)
|
||||
{
|
||||
WriteServiceFailure(output, "host.registration", "registration", registration);
|
||||
return TestClientExitCode.ServiceFailure;
|
||||
}
|
||||
|
||||
session = registration.Value;
|
||||
output.Write(
|
||||
"host.registered",
|
||||
"registered",
|
||||
phase: "registration",
|
||||
listingId: session.ListingId.ToString(),
|
||||
displayName: options.DisplayName);
|
||||
echo = new DirectEchoProtocol(events.GameplayEvents, host: true);
|
||||
echo.ExchangeCompleted += _ => output.Write(
|
||||
"host.direct-traffic",
|
||||
"verified",
|
||||
phase: "direct-traffic",
|
||||
endpointType: "peer-to-peer");
|
||||
coordinator = new RendezvousHostCoordinator(
|
||||
manager,
|
||||
events,
|
||||
options.Mediator,
|
||||
session,
|
||||
joins,
|
||||
CoordinatorOptions(options));
|
||||
coordinator.AttemptCompleted += (_, completion) =>
|
||||
{
|
||||
output.Write(
|
||||
"host.attempt.completed",
|
||||
completion.Outcome.IsSuccess ? "connected" : "failed",
|
||||
phase: completion.Outcome.Phase.ToString(),
|
||||
outcome: completion.Outcome.Kind.ToString(),
|
||||
elapsedMilliseconds: ToMilliseconds(completion.Outcome.Elapsed));
|
||||
if (completion.Outcome.IsSuccess)
|
||||
{
|
||||
output.Write(
|
||||
"host.direct-connect",
|
||||
"connected",
|
||||
phase: "direct-connection",
|
||||
endpointType: "peer-to-peer");
|
||||
}
|
||||
};
|
||||
|
||||
Stopwatch running = Stopwatch.StartNew();
|
||||
TimeSpan nextRefresh = TimeSpan.Zero;
|
||||
TimeSpan nextRenewal = TimeSpan.FromSeconds(session.LeaseRenewAfterSeconds);
|
||||
TimeSpan nextReadinessProbe = TimeSpan.Zero;
|
||||
bool directTrafficReported = false;
|
||||
TimeSpan? directTrafficCompletedAt = null;
|
||||
bool ready = false;
|
||||
bool terminalFailure = false;
|
||||
int previousPendingAttempts = 0;
|
||||
HostServiceFailureBudget refreshFailures = new();
|
||||
HostServiceFailureBudget renewalFailures = new();
|
||||
using PeriodicTimer pollTimer = new(PollDelay);
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
coordinator.Poll();
|
||||
if (coordinator.State != RendezvousHostState.Active)
|
||||
{
|
||||
output.WriteError(
|
||||
"host.lifecycle",
|
||||
"failed",
|
||||
"The host coordinator stopped before shutdown was requested.",
|
||||
phase: "lifecycle",
|
||||
outcome: coordinator.State.ToString());
|
||||
terminalFailure = true;
|
||||
break;
|
||||
}
|
||||
if (coordinator.PendingAttemptCount > previousPendingAttempts)
|
||||
{
|
||||
output.Write(
|
||||
"host.punch",
|
||||
"started",
|
||||
phase: "nat-traversal",
|
||||
count: coordinator.PendingAttemptCount);
|
||||
}
|
||||
previousPendingAttempts = coordinator.PendingAttemptCount;
|
||||
if (readiness is { IsCompleted: true })
|
||||
{
|
||||
RendezvousClientResult<GetSessionResponse> result = await readiness.ConfigureAwait(false);
|
||||
readiness = null;
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
ready = true;
|
||||
output.Write(
|
||||
"host.ready",
|
||||
"ready",
|
||||
phase: "presence",
|
||||
listingId: session.ListingId.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
nextReadinessProbe = running.Elapsed + TimeSpan.FromMilliseconds(50);
|
||||
}
|
||||
}
|
||||
if (!ready && readiness is null && running.Elapsed >= nextReadinessProbe)
|
||||
{
|
||||
readiness = browser.GetAsync(
|
||||
session.ListingId,
|
||||
options.GameId,
|
||||
options.EnvironmentId,
|
||||
options.ProtocolVersion,
|
||||
hostOperations.Token);
|
||||
}
|
||||
if (refresh is { IsCompleted: true })
|
||||
{
|
||||
RendezvousClientResult<int> result = await refresh.ConfigureAwait(false);
|
||||
refresh = null;
|
||||
nextRefresh = running.Elapsed + (result.IsSuccess
|
||||
? HostRefreshInterval
|
||||
: TimeSpan.FromSeconds(1));
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
WriteServiceFailure(output, "host.authorization", "authorization", result);
|
||||
if (refreshFailures.ShouldStop(result.Error, session.ExpiresAt, DateTimeOffset.UtcNow))
|
||||
{
|
||||
terminalFailure = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
refreshFailures.Reset();
|
||||
}
|
||||
}
|
||||
if (refresh is null && running.Elapsed >= nextRefresh)
|
||||
{
|
||||
refresh = coordinator.RefreshJoinAttemptsAsync(hostOperations.Token);
|
||||
}
|
||||
|
||||
if (renewal is { IsCompleted: true })
|
||||
{
|
||||
RendezvousClientResult<RenewLeaseResponse> result = await renewal.ConfigureAwait(false);
|
||||
renewal = null;
|
||||
nextRenewal = running.Elapsed + (result.IsSuccess && result.Value is not null
|
||||
? TimeSpan.FromSeconds(result.Value.RenewAfterSeconds)
|
||||
: TimeSpan.FromSeconds(1));
|
||||
output.Write(
|
||||
"host.lease",
|
||||
result.IsSuccess ? "renewed" : "failed",
|
||||
phase: "lease",
|
||||
message: result.IsSuccess ? null : SafeServiceMessage(result));
|
||||
if (!result.IsSuccess
|
||||
&& renewalFailures.ShouldStop(result.Error, session.ExpiresAt, DateTimeOffset.UtcNow))
|
||||
{
|
||||
terminalFailure = true;
|
||||
break;
|
||||
}
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
renewalFailures.Reset();
|
||||
}
|
||||
}
|
||||
if (renewal is null && running.Elapsed >= nextRenewal)
|
||||
{
|
||||
renewal = publisher.RenewAsync(session, credential, hostOperations.Token);
|
||||
}
|
||||
|
||||
if (echo.Completion.IsCompleted && !directTrafficReported)
|
||||
{
|
||||
directTrafficReported = true;
|
||||
directTrafficCompletedAt = running.Elapsed;
|
||||
}
|
||||
if (options.ExitAfterEcho
|
||||
&& directTrafficCompletedAt.HasValue
|
||||
&& running.Elapsed - directTrafficCompletedAt.Value >= DirectTrafficFlushGrace)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (options.RunDuration.HasValue && running.Elapsed >= options.RunDuration.Value)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (!await pollTimer.WaitForNextTickAsync(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
hostResult = TestClientExitCode.Cancelled;
|
||||
}
|
||||
else if (terminalFailure)
|
||||
{
|
||||
hostResult = TestClientExitCode.ServiceFailure;
|
||||
}
|
||||
else if (options.ExitAfterEcho && !directTrafficReported)
|
||||
{
|
||||
output.WriteError(
|
||||
"host.direct-traffic",
|
||||
"timed-out",
|
||||
"No authenticated ping/echo/ack exchange completed within the host runtime.",
|
||||
phase: "direct-traffic");
|
||||
hostResult = TestClientExitCode.DirectTrafficFailed;
|
||||
}
|
||||
else
|
||||
{
|
||||
hostResult = TestClientExitCode.Success;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
hostOperations.Cancel();
|
||||
await ObserveCancellationAsync(refresh).ConfigureAwait(false);
|
||||
await ObserveCancellationAsync(renewal).ConfigureAwait(false);
|
||||
await ObserveCancellationAsync(readiness).ConfigureAwait(false);
|
||||
coordinator?.Dispose();
|
||||
echo?.Dispose();
|
||||
if (session is not null)
|
||||
{
|
||||
using CancellationTokenSource cleanup = new(TimeSpan.FromSeconds(5));
|
||||
try
|
||||
{
|
||||
RendezvousClientResult<bool> deregistered = await publisher.DeregisterAsync(
|
||||
session,
|
||||
credential,
|
||||
cleanup.Token).ConfigureAwait(false);
|
||||
output.Write(
|
||||
"host.deregistered",
|
||||
deregistered.IsSuccess ? "complete" : "failed",
|
||||
phase: "lifecycle",
|
||||
listingId: session.ListingId.ToString());
|
||||
if (!deregistered.IsSuccess)
|
||||
{
|
||||
cleanupFailed = true;
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
output.WriteError(
|
||||
"host.deregistered",
|
||||
"timed-out",
|
||||
"Deregistration did not complete within the cleanup budget.",
|
||||
phase: "lifecycle");
|
||||
cleanupFailed = true;
|
||||
}
|
||||
}
|
||||
manager.Stop();
|
||||
}
|
||||
return cleanupFailed && !cancellationToken.IsCancellationRequested
|
||||
? TestClientExitCode.ServiceFailure
|
||||
: hostResult;
|
||||
}
|
||||
|
||||
private static async Task<TestClientExitCode> RunBrowseAsync(
|
||||
TestClientOptions options,
|
||||
TestClientOutput output,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
using CancellationTokenSource operation = CreateOperationTimeout(options, cancellationToken);
|
||||
using HttpClient http = CreateHttpClient(options);
|
||||
RendezvousSessionBrowserClient browser = new(http, ClientOptions(options));
|
||||
output.Write("browse.sessions", "started", phase: "directory");
|
||||
RendezvousClientResult<IReadOnlyList<SessionListing>> result = await browser.BrowseAllAsync(
|
||||
BrowseRequest(options),
|
||||
maximumPages: 10,
|
||||
cancellationToken: operation.Token).ConfigureAwait(false);
|
||||
if (!result.IsSuccess || result.Value is null)
|
||||
{
|
||||
WriteServiceFailure(output, "browse.sessions", "directory", result);
|
||||
return TestClientExitCode.ServiceFailure;
|
||||
}
|
||||
|
||||
WriteListings(output, result.Value);
|
||||
return result.Value.Count == 0
|
||||
? TestClientExitCode.NoCompatibleSession
|
||||
: TestClientExitCode.Success;
|
||||
}
|
||||
|
||||
private static async Task<TestClientExitCode> RunJoinAsync(
|
||||
TestClientOptions options,
|
||||
TestClientOutput output,
|
||||
TextReader input,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
using CancellationTokenSource operation = CreateOperationTimeout(options, cancellationToken);
|
||||
using HttpClient http = CreateHttpClient(options);
|
||||
RendezvousSessionBrowserClient browser = new(http, ClientOptions(options));
|
||||
RendezvousJoinClient joins = new(http, ClientOptions(options));
|
||||
SessionSelection selection = await SelectListingAsync(
|
||||
options,
|
||||
output,
|
||||
input,
|
||||
browser,
|
||||
operation.Token).ConfigureAwait(false);
|
||||
if (selection.Listing is null)
|
||||
{
|
||||
return selection.ExitCode;
|
||||
}
|
||||
SessionListing listing = selection.Listing;
|
||||
|
||||
RendezvousNetListener events = new();
|
||||
NetManager manager = events.CreateManager();
|
||||
if (!manager.Start(options.LocalPort))
|
||||
{
|
||||
output.WriteError("join.socket", "failed", "The gameplay UDP socket could not start.", phase: "mediation");
|
||||
return TestClientExitCode.ServiceFailure;
|
||||
}
|
||||
|
||||
RendezvousClientCoordinator? coordinator = null;
|
||||
bool directConnected = false;
|
||||
try
|
||||
{
|
||||
output.Write(
|
||||
"join.authorization",
|
||||
"started",
|
||||
phase: "authorization",
|
||||
listingId: listing.ListingId.ToString());
|
||||
RendezvousConnectionStartResult start = await joins.CreateConnectionAttemptAsync(
|
||||
new CreateJoinAttemptRequest
|
||||
{
|
||||
IdempotencyKey = Guid.NewGuid().ToString("N"),
|
||||
GameId = options.GameId,
|
||||
EnvironmentId = options.EnvironmentId,
|
||||
ListingId = listing.ListingId,
|
||||
ProtocolVersion = options.ProtocolVersion,
|
||||
},
|
||||
listing.DedicatedFallback,
|
||||
operation.Token).ConfigureAwait(false);
|
||||
if (start.Outcome is { } serviceOutcome)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
WriteOutcome(output, "join.authorization", serviceOutcome);
|
||||
WriteFallback(output, serviceOutcome);
|
||||
return TestClientExitCode.TraversalFailed;
|
||||
}
|
||||
|
||||
CreateJoinAttemptResponse attempt = start.Attempt
|
||||
?? throw new InvalidOperationException("The typed start result had no attempt or outcome.");
|
||||
using DirectEchoProtocol echo = new(events.GameplayEvents, host: false);
|
||||
coordinator = new RendezvousClientCoordinator(
|
||||
manager,
|
||||
events,
|
||||
options.Mediator,
|
||||
attempt,
|
||||
CoordinatorOptions(options));
|
||||
output.Write("join.punch", "started", phase: "nat-traversal");
|
||||
using (CancellationTokenSource traversal = CreateOperationTimeout(options, cancellationToken))
|
||||
using (PeriodicTimer traversalPoll = new(PollDelay))
|
||||
{
|
||||
RendezvousConnectionState previousState = coordinator.State;
|
||||
while (!coordinator.IsCompleted)
|
||||
{
|
||||
traversal.Token.ThrowIfCancellationRequested();
|
||||
coordinator.Poll();
|
||||
if (coordinator.State != previousState)
|
||||
{
|
||||
previousState = coordinator.State;
|
||||
if (previousState == RendezvousConnectionState.Connecting)
|
||||
{
|
||||
output.Write(
|
||||
"join.direct-connect",
|
||||
"started",
|
||||
phase: "direct-connection");
|
||||
}
|
||||
}
|
||||
if (!coordinator.IsCompleted
|
||||
&& !await traversalPoll.WaitForNextTickAsync(traversal.Token).ConfigureAwait(false))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RendezvousConnectionOutcome outcome = coordinator.Outcome
|
||||
?? throw new InvalidOperationException("The completed coordinator had no typed outcome.");
|
||||
WriteOutcome(output, "join.traversal", outcome);
|
||||
if (!outcome.IsSuccess || coordinator.ConnectedPeer is null)
|
||||
{
|
||||
WriteFallback(output, outcome);
|
||||
await ReportOutcomeAsync(coordinator, joins, output, cancellationToken).ConfigureAwait(false);
|
||||
return TestClientExitCode.TraversalFailed;
|
||||
}
|
||||
|
||||
NetPeer peer = coordinator.ConnectedPeer;
|
||||
directConnected = true;
|
||||
string endpointType = EndpointType(peer.Address);
|
||||
output.Write(
|
||||
"join.connected",
|
||||
"connected",
|
||||
phase: "direct-connection",
|
||||
endpointType: endpointType,
|
||||
elapsedMilliseconds: ToMilliseconds(outcome.Elapsed));
|
||||
await ReportOutcomeAsync(coordinator, joins, output, cancellationToken).ConfigureAwait(false);
|
||||
echo.BeginJoin(peer);
|
||||
using (CancellationTokenSource traffic = CreateOperationTimeout(options, cancellationToken))
|
||||
using (PeriodicTimer trafficPoll = new(PollDelay))
|
||||
{
|
||||
while (!echo.Completion.IsCompleted)
|
||||
{
|
||||
traffic.Token.ThrowIfCancellationRequested();
|
||||
manager.PollEvents();
|
||||
if (!echo.Completion.IsCompleted
|
||||
&& !await trafficPoll.WaitForNextTickAsync(traffic.Token).ConfigureAwait(false))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
await echo.Completion.ConfigureAwait(false);
|
||||
output.Write(
|
||||
"join.direct-traffic",
|
||||
"verified",
|
||||
phase: "direct-traffic",
|
||||
endpointType: endpointType);
|
||||
peer.Disconnect();
|
||||
manager.PollEvents();
|
||||
return TestClientExitCode.Success;
|
||||
}
|
||||
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
output.WriteError(
|
||||
"join.timeout",
|
||||
"timed-out",
|
||||
"The bounded join operation timed out.",
|
||||
phase: "lifecycle",
|
||||
outcome: ConnectionOutcomeKind.TimedOut.ToString());
|
||||
if (coordinator is not null && !coordinator.IsCompleted)
|
||||
{
|
||||
coordinator.Poll();
|
||||
}
|
||||
if (coordinator is not null && !coordinator.IsCompleted)
|
||||
{
|
||||
coordinator.Cancel();
|
||||
coordinator.Poll();
|
||||
if (coordinator.Outcome is { } timeoutOutcome)
|
||||
{
|
||||
WriteOutcome(output, "join.traversal", timeoutOutcome);
|
||||
WriteFallback(output, timeoutOutcome, listing.DedicatedFallback);
|
||||
await ReportOutcomeAsync(
|
||||
coordinator,
|
||||
joins,
|
||||
output,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
return directConnected
|
||||
? TestClientExitCode.DirectTrafficFailed
|
||||
: TestClientExitCode.TraversalFailed;
|
||||
}
|
||||
finally
|
||||
{
|
||||
coordinator?.Dispose();
|
||||
manager.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<SessionSelection> SelectListingAsync(
|
||||
TestClientOptions options,
|
||||
TestClientOutput output,
|
||||
TextReader input,
|
||||
RendezvousSessionBrowserClient browser,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (options.ListingId.HasValue)
|
||||
{
|
||||
RendezvousClientResult<GetSessionResponse> exact = await browser.GetAsync(
|
||||
options.ListingId.Value,
|
||||
options.GameId,
|
||||
options.EnvironmentId,
|
||||
options.ProtocolVersion,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
if (!exact.IsSuccess || exact.Value is null)
|
||||
{
|
||||
WriteServiceFailure(output, "join.selection", "directory", exact);
|
||||
return new(null, TestClientExitCode.ServiceFailure);
|
||||
}
|
||||
return new(exact.Value.Session, TestClientExitCode.Success);
|
||||
}
|
||||
|
||||
RendezvousClientResult<IReadOnlyList<SessionListing>> result = await browser.BrowseAllAsync(
|
||||
BrowseRequest(options),
|
||||
maximumPages: 10,
|
||||
cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (!result.IsSuccess || result.Value is null)
|
||||
{
|
||||
WriteServiceFailure(output, "join.selection", "directory", result);
|
||||
return new(null, TestClientExitCode.ServiceFailure);
|
||||
}
|
||||
if (result.Value.Count == 0)
|
||||
{
|
||||
output.Write("join.selection", "empty", phase: "directory", count: 0);
|
||||
return new(null, TestClientExitCode.NoCompatibleSession);
|
||||
}
|
||||
WriteListings(output, result.Value);
|
||||
if (options.Script)
|
||||
{
|
||||
return new(result.Value[0], TestClientExitCode.Success);
|
||||
}
|
||||
|
||||
output.WritePrompt($"Select session [1-{result.Value.Count}]: ");
|
||||
string? selection = await input.ReadLineAsync(cancellationToken).ConfigureAwait(false);
|
||||
SessionListing? selected = int.TryParse(selection, out int index)
|
||||
&& index >= 1
|
||||
&& index <= result.Value.Count
|
||||
? result.Value[index - 1]
|
||||
: null;
|
||||
return selected is null
|
||||
? new(null, TestClientExitCode.NoCompatibleSession)
|
||||
: new(selected, TestClientExitCode.Success);
|
||||
}
|
||||
|
||||
private static void WriteListings(TestClientOutput output, IReadOnlyList<SessionListing> listings)
|
||||
{
|
||||
output.Write("browse.completed", "complete", phase: "directory", count: listings.Count);
|
||||
foreach (SessionListing listing in listings)
|
||||
{
|
||||
output.Write(
|
||||
"browse.session",
|
||||
"available",
|
||||
phase: "directory",
|
||||
listingId: listing.ListingId.ToString(),
|
||||
displayName: listing.DisplayName);
|
||||
}
|
||||
}
|
||||
|
||||
private static BrowseSessionsRequest BrowseRequest(TestClientOptions options) => new()
|
||||
{
|
||||
GameId = options.GameId,
|
||||
EnvironmentId = options.EnvironmentId,
|
||||
ProtocolVersion = options.ProtocolVersion,
|
||||
RegionId = options.RegionId,
|
||||
PageSize = options.PageSize,
|
||||
ExcludeFull = true,
|
||||
};
|
||||
|
||||
private static HttpClient CreateHttpClient(TestClientOptions options) => new()
|
||||
{
|
||||
BaseAddress = options.ServiceUri,
|
||||
Timeout = Timeout.InfiniteTimeSpan,
|
||||
};
|
||||
|
||||
private static RendezvousClientOptions ClientOptions(TestClientOptions options) => new()
|
||||
{
|
||||
RequestTimeout = TimeSpan.FromSeconds(Math.Min(30, options.OperationTimeout.TotalSeconds)),
|
||||
};
|
||||
|
||||
private static RendezvousCoordinatorOptions CoordinatorOptions(TestClientOptions options)
|
||||
{
|
||||
TimeSpan phaseTimeout = TimeSpan.FromSeconds(
|
||||
Math.Min(30, options.OperationTimeout.TotalSeconds * 0.45));
|
||||
return new RendezvousCoordinatorOptions
|
||||
{
|
||||
PunchTimeout = phaseTimeout,
|
||||
DirectConnectTimeout = phaseTimeout,
|
||||
};
|
||||
}
|
||||
|
||||
private static CancellationTokenSource CreateOperationTimeout(
|
||||
TestClientOptions options,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
CancellationTokenSource source = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
source.CancelAfter(options.OperationTimeout);
|
||||
return source;
|
||||
}
|
||||
|
||||
private static async Task ReportOutcomeAsync(
|
||||
RendezvousClientCoordinator coordinator,
|
||||
RendezvousJoinClient joins,
|
||||
TestClientOutput output,
|
||||
CancellationToken callerCancellationToken)
|
||||
{
|
||||
using CancellationTokenSource telemetry = CancellationTokenSource.CreateLinkedTokenSource(
|
||||
callerCancellationToken);
|
||||
telemetry.CancelAfter(TimeSpan.FromSeconds(5));
|
||||
try
|
||||
{
|
||||
RendezvousClientResult<ReportConnectionOutcomeResponse> report =
|
||||
await coordinator.ReportOutcomeAsync(joins, telemetry.Token).ConfigureAwait(false);
|
||||
output.Write(
|
||||
"join.outcome-report",
|
||||
report.IsSuccess ? "accepted" : "failed",
|
||||
phase: "telemetry",
|
||||
message: report.IsSuccess ? null : SafeServiceMessage(report));
|
||||
}
|
||||
catch (OperationCanceledException) when (!callerCancellationToken.IsCancellationRequested)
|
||||
{
|
||||
output.WriteError(
|
||||
"join.outcome-report",
|
||||
"cancelled",
|
||||
"Outcome reporting was cancelled within the operation budget.",
|
||||
phase: "telemetry");
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteOutcome(
|
||||
TestClientOutput output,
|
||||
string eventName,
|
||||
RendezvousConnectionOutcome outcome) => output.Write(
|
||||
eventName,
|
||||
outcome.IsSuccess ? "connected" : "failed",
|
||||
phase: outcome.Phase.ToString(),
|
||||
outcome: outcome.Kind.ToString(),
|
||||
elapsedMilliseconds: ToMilliseconds(outcome.Elapsed));
|
||||
|
||||
private static void WriteFallback(
|
||||
TestClientOutput output,
|
||||
RendezvousConnectionOutcome outcome,
|
||||
NetworkEndpoint? authoritativeFallback = null)
|
||||
{
|
||||
bool hasFallback = outcome.HasDedicatedFallback || authoritativeFallback is not null;
|
||||
output.Write(
|
||||
"join.fallback",
|
||||
hasFallback ? "available" : "unavailable",
|
||||
phase: "fallback",
|
||||
outcome: outcome.Kind.ToString(),
|
||||
endpointType: hasFallback ? "dedicated" : "none");
|
||||
}
|
||||
|
||||
private static void WriteServiceFailure<T>(
|
||||
TestClientOutput output,
|
||||
string eventName,
|
||||
string phase,
|
||||
RendezvousClientResult<T> result) => output.WriteError(
|
||||
eventName,
|
||||
"failed",
|
||||
SafeServiceMessage(result),
|
||||
phase,
|
||||
result.Error.ToString());
|
||||
|
||||
private static string SafeServiceMessage<T>(RendezvousClientResult<T> result) =>
|
||||
$"Rendezvous returned {result.Error}.";
|
||||
|
||||
private static async Task ObserveCancellationAsync<T>(Task<T>? task)
|
||||
{
|
||||
if (task is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
await task.ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private static string EndpointType(IPAddress address)
|
||||
{
|
||||
if (IPAddress.IsLoopback(address))
|
||||
{
|
||||
return "loopback";
|
||||
}
|
||||
if (address.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
byte[] ipv6 = address.GetAddressBytes();
|
||||
return address.IsIPv6LinkLocal || (ipv6[0] & 0xfe) == 0xfc
|
||||
? "private"
|
||||
: "public";
|
||||
}
|
||||
byte[] bytes = address.GetAddressBytes();
|
||||
bool privateAddress = bytes[0] == 10
|
||||
|| bytes[0] == 172 && bytes[1] is >= 16 and <= 31
|
||||
|| bytes[0] == 192 && bytes[1] == 168;
|
||||
return privateAddress ? "private" : "public";
|
||||
}
|
||||
|
||||
private static long ToMilliseconds(TimeSpan elapsed) =>
|
||||
(long)Math.Min(long.MaxValue, Math.Max(0, elapsed.TotalMilliseconds));
|
||||
|
||||
private sealed record SessionSelection(
|
||||
SessionListing? Listing,
|
||||
TestClientExitCode ExitCode);
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
namespace FinalFactory.Rendezvous.TestClient;
|
||||
|
||||
internal enum TestClientExitCode
|
||||
{
|
||||
Success = 0,
|
||||
Usage = 2,
|
||||
Configuration = 3,
|
||||
ServiceFailure = 10,
|
||||
NoCompatibleSession = 11,
|
||||
TraversalFailed = 12,
|
||||
DirectTrafficFailed = 13,
|
||||
Cancelled = 130,
|
||||
}
|
||||
|
||||
internal interface ITestClientCommandRunner
|
||||
{
|
||||
Task<TestClientExitCode> RunAsync(
|
||||
TestClientOptions options,
|
||||
TestClientOutput output,
|
||||
TextReader input,
|
||||
CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
internal sealed class TestClientApplication(ITestClientCommandRunner runner)
|
||||
{
|
||||
private readonly ITestClientCommandRunner _runner = runner ?? throw new ArgumentNullException(nameof(runner));
|
||||
|
||||
internal async Task<int> RunAsync(
|
||||
string[] args,
|
||||
TextReader input,
|
||||
TextWriter standardOutput,
|
||||
TextWriter standardError,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
bool jsonRequested = args.Contains("--json", StringComparer.Ordinal);
|
||||
TestClientParseResult parsed = TestClientOptionParser.Parse(args);
|
||||
TestClientOutput output = new(standardOutput, standardError, jsonRequested);
|
||||
if (parsed.ShowHelp)
|
||||
{
|
||||
if (jsonRequested)
|
||||
{
|
||||
output.Write(
|
||||
"cli.help",
|
||||
"complete",
|
||||
phase: "configuration",
|
||||
message: "Run without --json to read the full command reference.");
|
||||
}
|
||||
else
|
||||
{
|
||||
await standardOutput.WriteLineAsync(TestClientOptionParser.Usage).ConfigureAwait(false);
|
||||
}
|
||||
return (int)TestClientExitCode.Success;
|
||||
}
|
||||
if (!parsed.Succeeded || parsed.Options is null)
|
||||
{
|
||||
if (jsonRequested)
|
||||
{
|
||||
output.WriteError(
|
||||
"cli.usage",
|
||||
"failed",
|
||||
parsed.Error ?? "Invalid command line.",
|
||||
phase: "configuration");
|
||||
}
|
||||
else
|
||||
{
|
||||
await standardError.WriteLineAsync(parsed.Error ?? "Invalid command line.").ConfigureAwait(false);
|
||||
await standardError.WriteLineAsync("Use --help for documented options.").ConfigureAwait(false);
|
||||
}
|
||||
return (int)TestClientExitCode.Usage;
|
||||
}
|
||||
|
||||
output = new TestClientOutput(standardOutput, standardError, parsed.Options.Json);
|
||||
try
|
||||
{
|
||||
return (int)await _runner.RunAsync(
|
||||
parsed.Options,
|
||||
output,
|
||||
input,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
output.Write("lifecycle.cancelled", "cancelled", phase: "lifecycle");
|
||||
return (int)TestClientExitCode.Cancelled;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
output.WriteError(
|
||||
"lifecycle.failed",
|
||||
"failed",
|
||||
$"Unexpected {exception.GetType().Name}; credentials remain redacted.");
|
||||
return (int)TestClientExitCode.ServiceFailure;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,377 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using FinalFactory.Rendezvous.Contracts;
|
||||
|
||||
namespace FinalFactory.Rendezvous.TestClient;
|
||||
|
||||
internal enum TestClientMode
|
||||
{
|
||||
Host,
|
||||
Browse,
|
||||
Join,
|
||||
}
|
||||
|
||||
internal sealed class TestClientOptions
|
||||
{
|
||||
internal TestClientMode Mode { get; init; }
|
||||
internal Uri ServiceUri { get; init; } = new("http://127.0.0.1:5000/");
|
||||
internal IPEndPoint Mediator { get; init; } = new(IPAddress.Loopback, 9050);
|
||||
internal GameId GameId { get; init; } = new("space-game");
|
||||
internal EnvironmentId EnvironmentId { get; init; } = new("development");
|
||||
internal RegionId RegionId { get; init; } = new("local");
|
||||
internal uint ProtocolVersion { get; init; } = 1;
|
||||
internal string BuildVersion { get; init; } = "test-client";
|
||||
internal string DisplayName { get; init; } = "Rendezvous diagnostic host";
|
||||
internal string PublisherCredentialEnvironmentVariable { get; init; } =
|
||||
"RENDEZVOUS_PUBLISHER_CREDENTIAL";
|
||||
internal Dictionary<string, string> Metadata { get; init; } = new(StringComparer.Ordinal);
|
||||
internal NetworkEndpoint? DedicatedFallback { get; init; }
|
||||
internal SessionListingId? ListingId { get; init; }
|
||||
internal int LocalPort { get; init; }
|
||||
internal int PageSize { get; init; } = 20;
|
||||
internal TimeSpan OperationTimeout { get; init; } = TimeSpan.FromSeconds(20);
|
||||
internal TimeSpan? RunDuration { get; init; }
|
||||
internal bool Script { get; init; }
|
||||
internal bool Json { get; init; }
|
||||
internal bool ExitAfterEcho { get; init; }
|
||||
}
|
||||
|
||||
internal sealed class TestClientParseResult
|
||||
{
|
||||
private TestClientParseResult(TestClientOptions? options, string? error, bool showHelp)
|
||||
{
|
||||
Options = options;
|
||||
Error = error;
|
||||
ShowHelp = showHelp;
|
||||
}
|
||||
|
||||
internal TestClientOptions? Options { get; }
|
||||
internal string? Error { get; }
|
||||
internal bool ShowHelp { get; }
|
||||
internal bool Succeeded => Options is not null;
|
||||
|
||||
internal static TestClientParseResult Success(TestClientOptions options) => new(options, null, false);
|
||||
internal static TestClientParseResult Failure(string error) => new(null, error, false);
|
||||
internal static TestClientParseResult Help() => new(null, null, true);
|
||||
}
|
||||
|
||||
internal static class TestClientOptionParser
|
||||
{
|
||||
internal const string Usage = """
|
||||
Rendezvous diagnostic client
|
||||
|
||||
Usage:
|
||||
rendezvous-test-client host [options]
|
||||
rendezvous-test-client browse [options]
|
||||
rendezvous-test-client join [options]
|
||||
|
||||
Common options:
|
||||
--service URL HTTP(S) Rendezvous base URL
|
||||
--mediator IP:PORT UDP mediator endpoint
|
||||
--game ID Game scope (default: space-game)
|
||||
--environment ID Environment scope (default: development)
|
||||
--protocol NUMBER Exact gameplay protocol (default: 1)
|
||||
--region ID Region filter/publication (default: local)
|
||||
--timeout-seconds NUMBER Bounded startup/traversal stage, 1-300 (default: 20)
|
||||
--port NUMBER Caller-owned gameplay UDP port; 0 chooses one
|
||||
--page-size NUMBER Bounded browser page size, 1-100 (default: 20)
|
||||
--script Never prompt; select the first compatible listing
|
||||
--json Emit one versioned JSON event per line
|
||||
--help Show this help
|
||||
|
||||
Host options:
|
||||
--publisher-credential-env NAME Environment variable containing the credential
|
||||
--display-name TEXT Public listing name
|
||||
--build-version TEXT Public build version
|
||||
--metadata KEY=VALUE Bounded public metadata; may be repeated
|
||||
--fallback IP:PORT Optional policy-authorized dedicated fallback
|
||||
--run-seconds NUMBER Stop after 1-86400 seconds
|
||||
--exit-after-echo Stop after an authenticated ping/echo/ack exchange
|
||||
|
||||
Join options:
|
||||
--listing UUID Join an exact listing; otherwise browse/select
|
||||
|
||||
Credentials are accepted only through the named environment variable. They are never
|
||||
accepted on the command line and are never written to human or JSON output.
|
||||
""";
|
||||
|
||||
internal static TestClientParseResult Parse(string[] args)
|
||||
{
|
||||
if (args.Length == 0 || args.Length == 1 && IsHelp(args[0]))
|
||||
{
|
||||
return TestClientParseResult.Help();
|
||||
}
|
||||
if (args.Length > 64)
|
||||
{
|
||||
return TestClientParseResult.Failure("Too many command-line arguments.");
|
||||
}
|
||||
if (!TryMode(args[0], out TestClientMode mode))
|
||||
{
|
||||
return TestClientParseResult.Failure("The first argument must be host, browse, or join.");
|
||||
}
|
||||
|
||||
Uri serviceUri = new("http://127.0.0.1:5000/");
|
||||
IPEndPoint mediator = new(IPAddress.Loopback, 9050);
|
||||
string game = "space-game";
|
||||
string environment = "development";
|
||||
string region = "local";
|
||||
uint protocol = 1;
|
||||
string buildVersion = "test-client";
|
||||
string displayName = "Rendezvous diagnostic host";
|
||||
string credentialEnvironmentVariable = "RENDEZVOUS_PUBLISHER_CREDENTIAL";
|
||||
Dictionary<string, string> metadata = new(StringComparer.Ordinal);
|
||||
NetworkEndpoint? dedicatedFallback = null;
|
||||
SessionListingId? listingId = null;
|
||||
int localPort = 0;
|
||||
int pageSize = 20;
|
||||
int timeoutSeconds = 20;
|
||||
int? runSeconds = null;
|
||||
bool script = false;
|
||||
bool json = false;
|
||||
bool exitAfterEcho = false;
|
||||
HashSet<string> seen = new(StringComparer.Ordinal);
|
||||
|
||||
for (int index = 1; index < args.Length; index++)
|
||||
{
|
||||
string option = args[index];
|
||||
if (IsHelp(option))
|
||||
{
|
||||
return TestClientParseResult.Help();
|
||||
}
|
||||
if (option is "--script" or "--json" or "--exit-after-echo")
|
||||
{
|
||||
if (!seen.Add(option))
|
||||
{
|
||||
return TestClientParseResult.Failure($"Option {option} was specified more than once.");
|
||||
}
|
||||
script |= option == "--script";
|
||||
json |= option == "--json";
|
||||
exitAfterEcho |= option == "--exit-after-echo";
|
||||
continue;
|
||||
}
|
||||
if (!option.StartsWith("--", StringComparison.Ordinal)
|
||||
|| index + 1 >= args.Length)
|
||||
{
|
||||
return TestClientParseResult.Failure("Every option must use the form --name value.");
|
||||
}
|
||||
|
||||
string value = args[++index];
|
||||
if (value.Length is 0 or > 512)
|
||||
{
|
||||
return TestClientParseResult.Failure($"Option {option} has an invalid value length.");
|
||||
}
|
||||
if (option != "--metadata" && !seen.Add(option))
|
||||
{
|
||||
return TestClientParseResult.Failure($"Option {option} was specified more than once.");
|
||||
}
|
||||
|
||||
switch (option)
|
||||
{
|
||||
case "--service":
|
||||
if (!TryServiceUri(value, out serviceUri))
|
||||
{
|
||||
return TestClientParseResult.Failure("The service URL must be absolute HTTP(S), credential-free, and query-free.");
|
||||
}
|
||||
break;
|
||||
case "--mediator":
|
||||
if (!IPEndPoint.TryParse(value, out IPEndPoint? parsedMediator)
|
||||
|| parsedMediator.Port == 0)
|
||||
{
|
||||
return TestClientParseResult.Failure("The mediator must be an IP endpoint with a non-zero port.");
|
||||
}
|
||||
mediator = parsedMediator;
|
||||
break;
|
||||
case "--game":
|
||||
game = value;
|
||||
break;
|
||||
case "--environment":
|
||||
environment = value;
|
||||
break;
|
||||
case "--region":
|
||||
region = value;
|
||||
break;
|
||||
case "--protocol":
|
||||
if (!uint.TryParse(value, out protocol) || protocol == 0)
|
||||
{
|
||||
return TestClientParseResult.Failure("The protocol must be a positive integer.");
|
||||
}
|
||||
break;
|
||||
case "--build-version":
|
||||
buildVersion = value;
|
||||
break;
|
||||
case "--display-name":
|
||||
displayName = value;
|
||||
break;
|
||||
case "--publisher-credential-env":
|
||||
if (!IsEnvironmentVariableName(value))
|
||||
{
|
||||
return TestClientParseResult.Failure("The credential environment-variable name is invalid.");
|
||||
}
|
||||
credentialEnvironmentVariable = value;
|
||||
break;
|
||||
case "--metadata":
|
||||
if (!TryMetadata(value, metadata))
|
||||
{
|
||||
return TestClientParseResult.Failure("Metadata must be a unique KEY=VALUE pair with a non-empty key.");
|
||||
}
|
||||
break;
|
||||
case "--fallback":
|
||||
if (!IPEndPoint.TryParse(value, out IPEndPoint? fallbackEndpoint)
|
||||
|| fallbackEndpoint.Port == 0)
|
||||
{
|
||||
return TestClientParseResult.Failure("The fallback must be an IP endpoint with a non-zero port.");
|
||||
}
|
||||
dedicatedFallback = new NetworkEndpoint
|
||||
{
|
||||
AddressFamily = fallbackEndpoint.AddressFamily == AddressFamily.InterNetwork
|
||||
? AddressFamilyKind.Ipv4
|
||||
: AddressFamilyKind.Ipv6,
|
||||
Address = fallbackEndpoint.Address.ToString(),
|
||||
Port = fallbackEndpoint.Port,
|
||||
};
|
||||
break;
|
||||
case "--listing":
|
||||
if (!Guid.TryParse(value, out Guid parsedListing) || parsedListing == Guid.Empty)
|
||||
{
|
||||
return TestClientParseResult.Failure("The listing must be a non-empty UUID.");
|
||||
}
|
||||
listingId = new SessionListingId(parsedListing);
|
||||
break;
|
||||
case "--port":
|
||||
if (!int.TryParse(value, out localPort) || localPort is < 0 or > 65_535)
|
||||
{
|
||||
return TestClientParseResult.Failure("The local UDP port must be between 0 and 65535.");
|
||||
}
|
||||
break;
|
||||
case "--page-size":
|
||||
if (!int.TryParse(value, out pageSize)
|
||||
|| pageSize is < 1 or > ContractLimits.BrowserPageMaxItems)
|
||||
{
|
||||
return TestClientParseResult.Failure("The page size is outside the contract limit.");
|
||||
}
|
||||
break;
|
||||
case "--timeout-seconds":
|
||||
if (!int.TryParse(value, out timeoutSeconds) || timeoutSeconds is < 1 or > 300)
|
||||
{
|
||||
return TestClientParseResult.Failure("The timeout must be between 1 and 300 seconds.");
|
||||
}
|
||||
break;
|
||||
case "--run-seconds":
|
||||
if (!int.TryParse(value, out int parsedRunSeconds)
|
||||
|| parsedRunSeconds is < 1 or > 86_400)
|
||||
{
|
||||
return TestClientParseResult.Failure("The host run duration must be between 1 and 86400 seconds.");
|
||||
}
|
||||
runSeconds = parsedRunSeconds;
|
||||
break;
|
||||
default:
|
||||
return TestClientParseResult.Failure($"Unknown option {option}.");
|
||||
}
|
||||
}
|
||||
|
||||
if (!IsSlug(game, ContractLimits.GameIdMaxCharacters)
|
||||
|| !IsSlug(environment, ContractLimits.EnvironmentIdMaxCharacters)
|
||||
|| !IsSlug(region, ContractLimits.RegionIdMaxCharacters)
|
||||
|| !ContractValidation.IsBuildVersionValid(buildVersion)
|
||||
|| !ContractValidation.IsDisplayNameValid(displayName)
|
||||
|| !ContractValidation.IsMetadataValid(metadata))
|
||||
{
|
||||
return TestClientParseResult.Failure("One or more game, environment, region, build, or display values violate v1 limits.");
|
||||
}
|
||||
if (listingId.HasValue && mode != TestClientMode.Join
|
||||
|| runSeconds.HasValue && mode != TestClientMode.Host
|
||||
|| exitAfterEcho && mode != TestClientMode.Host
|
||||
|| metadata.Count > 0 && mode != TestClientMode.Host
|
||||
|| dedicatedFallback is not null && mode != TestClientMode.Host
|
||||
|| seen.Contains("--publisher-credential-env") && mode != TestClientMode.Host
|
||||
|| seen.Contains("--display-name") && mode != TestClientMode.Host
|
||||
|| seen.Contains("--build-version") && mode != TestClientMode.Host)
|
||||
{
|
||||
return TestClientParseResult.Failure("One or more options do not apply to the selected mode.");
|
||||
}
|
||||
|
||||
return TestClientParseResult.Success(new TestClientOptions
|
||||
{
|
||||
Mode = mode,
|
||||
ServiceUri = serviceUri,
|
||||
Mediator = mediator,
|
||||
GameId = new(game),
|
||||
EnvironmentId = new(environment),
|
||||
RegionId = new(region),
|
||||
ProtocolVersion = protocol,
|
||||
BuildVersion = buildVersion,
|
||||
DisplayName = displayName,
|
||||
PublisherCredentialEnvironmentVariable = credentialEnvironmentVariable,
|
||||
Metadata = metadata,
|
||||
DedicatedFallback = dedicatedFallback,
|
||||
ListingId = listingId,
|
||||
LocalPort = localPort,
|
||||
PageSize = pageSize,
|
||||
OperationTimeout = TimeSpan.FromSeconds(timeoutSeconds),
|
||||
RunDuration = runSeconds.HasValue
|
||||
? TimeSpan.FromSeconds(runSeconds.Value)
|
||||
: mode == TestClientMode.Host && script
|
||||
? TimeSpan.FromSeconds(timeoutSeconds)
|
||||
: null,
|
||||
Script = script,
|
||||
Json = json,
|
||||
ExitAfterEcho = exitAfterEcho,
|
||||
});
|
||||
}
|
||||
|
||||
private static bool TryMode(string value, out TestClientMode mode) =>
|
||||
Enum.TryParse(value, true, out mode) && Enum.IsDefined(mode);
|
||||
|
||||
private static bool IsHelp(string value) => value is "--help" or "-h" or "help";
|
||||
|
||||
private static bool TryServiceUri(string value, out Uri uri)
|
||||
{
|
||||
uri = null!;
|
||||
if (!Uri.TryCreate(value, UriKind.Absolute, out Uri? parsed)
|
||||
|| parsed.Scheme is not ("http" or "https")
|
||||
|| !string.IsNullOrEmpty(parsed.UserInfo)
|
||||
|| !string.IsNullOrEmpty(parsed.Query)
|
||||
|| !string.IsNullOrEmpty(parsed.Fragment))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
UriBuilder builder = new(parsed) { Path = parsed.AbsolutePath.TrimEnd('/') + "/" };
|
||||
uri = builder.Uri;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsEnvironmentVariableName(string value)
|
||||
{
|
||||
if (value.Length is 0 or > 64 || !(char.IsLetter(value[0]) || value[0] == '_'))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return value.All(static character =>
|
||||
char.IsAsciiLetterOrDigit(character) || character == '_');
|
||||
}
|
||||
|
||||
private static bool TryMetadata(string value, Dictionary<string, string> metadata)
|
||||
{
|
||||
int separator = value.IndexOf('=');
|
||||
if (separator is < 1 or > ContractLimits.MetadataKeyMaxBytes
|
||||
|| metadata.Count >= ContractLimits.MetadataMaxKeys)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string key = value[..separator];
|
||||
string metadataValue = value[(separator + 1)..];
|
||||
return !string.IsNullOrWhiteSpace(key)
|
||||
&& ContractValidation.IsUtf8LengthWithin(key, ContractLimits.MetadataKeyMaxBytes)
|
||||
&& ContractValidation.IsUtf8LengthWithin(metadataValue, ContractLimits.MetadataValueMaxBytes)
|
||||
&& metadata.TryAdd(key, metadataValue);
|
||||
}
|
||||
|
||||
private static bool IsSlug(string value, int maximumCharacters) =>
|
||||
value.Length is > 0
|
||||
&& value.Length <= maximumCharacters
|
||||
&& value[0] is >= 'a' and <= 'z'
|
||||
&& value.All(static character => character is >= 'a' and <= 'z'
|
||||
or >= '0' and <= '9'
|
||||
or '-');
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace FinalFactory.Rendezvous.TestClient;
|
||||
|
||||
internal sealed class TestClientOutput(TextWriter standardOutput, TextWriter standardError, bool json)
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
|
||||
{
|
||||
WriteIndented = false,
|
||||
};
|
||||
|
||||
private readonly object _gate = new();
|
||||
private readonly TextWriter _standardOutput = standardOutput ?? throw new ArgumentNullException(nameof(standardOutput));
|
||||
private readonly TextWriter _standardError = standardError ?? throw new ArgumentNullException(nameof(standardError));
|
||||
private readonly bool _json = json;
|
||||
|
||||
internal void Write(
|
||||
string eventName,
|
||||
string status,
|
||||
string? phase = null,
|
||||
string? listingId = null,
|
||||
string? displayName = null,
|
||||
string? outcome = null,
|
||||
string? endpointType = null,
|
||||
int? count = null,
|
||||
long? elapsedMilliseconds = null,
|
||||
string? message = null) => WriteCore(
|
||||
_standardOutput,
|
||||
new TestClientEvent
|
||||
{
|
||||
Event = SafeToken(eventName) ?? string.Empty,
|
||||
Status = SafeToken(status) ?? string.Empty,
|
||||
Phase = SafeToken(phase),
|
||||
ListingId = SafeToken(listingId),
|
||||
DisplayName = SafeText(displayName),
|
||||
Outcome = SafeToken(outcome),
|
||||
EndpointType = SafeToken(endpointType),
|
||||
Count = count,
|
||||
ElapsedMilliseconds = elapsedMilliseconds,
|
||||
Message = SafeText(message),
|
||||
});
|
||||
|
||||
internal void WriteError(
|
||||
string eventName,
|
||||
string status,
|
||||
string message,
|
||||
string? phase = null,
|
||||
string? outcome = null) => WriteCore(
|
||||
_standardError,
|
||||
new TestClientEvent
|
||||
{
|
||||
Event = SafeToken(eventName) ?? string.Empty,
|
||||
Status = SafeToken(status) ?? string.Empty,
|
||||
Phase = SafeToken(phase),
|
||||
Outcome = SafeToken(outcome),
|
||||
Message = SafeText(message),
|
||||
});
|
||||
|
||||
internal void WritePrompt(string prompt)
|
||||
{
|
||||
if (_json)
|
||||
{
|
||||
return;
|
||||
}
|
||||
lock (_gate)
|
||||
{
|
||||
_standardOutput.Write(SafeText(prompt));
|
||||
_standardOutput.Flush();
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteCore(TextWriter writer, TestClientEvent item)
|
||||
{
|
||||
string line = _json
|
||||
? JsonSerializer.Serialize(item, JsonOptions)
|
||||
: HumanLine(item);
|
||||
lock (_gate)
|
||||
{
|
||||
writer.WriteLine(line);
|
||||
writer.Flush();
|
||||
}
|
||||
}
|
||||
|
||||
private static string HumanLine(TestClientEvent item)
|
||||
{
|
||||
StringBuilder line = new();
|
||||
line.Append('[').Append(item.Status).Append("] ").Append(item.Event);
|
||||
Append(line, "phase", item.Phase);
|
||||
Append(line, "listing", item.ListingId);
|
||||
Append(line, "name", item.DisplayName, quote: true);
|
||||
Append(line, "outcome", item.Outcome);
|
||||
Append(line, "endpoint", item.EndpointType);
|
||||
if (item.Count.HasValue)
|
||||
{
|
||||
Append(line, "count", item.Count.Value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
if (item.ElapsedMilliseconds.HasValue)
|
||||
{
|
||||
Append(
|
||||
line,
|
||||
"elapsedMs",
|
||||
item.ElapsedMilliseconds.Value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
Append(line, "message", item.Message, quote: true);
|
||||
return line.ToString();
|
||||
}
|
||||
|
||||
private static void Append(
|
||||
StringBuilder builder,
|
||||
string name,
|
||||
string? value,
|
||||
bool quote = false)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
{
|
||||
builder.Append(' ').Append(name).Append('=');
|
||||
if (quote)
|
||||
{
|
||||
builder.Append('"').Append(value.Replace("\\", "\\\\", StringComparison.Ordinal)
|
||||
.Replace("\"", "\\\"", StringComparison.Ordinal)).Append('"');
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string? SafeToken(string? value)
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new string(value
|
||||
.Take(96)
|
||||
.Select(static character => char.IsAsciiLetterOrDigit(character)
|
||||
|| character is '.' or '-' or '_' or ':'
|
||||
? char.ToLowerInvariant(character)
|
||||
: '_')
|
||||
.ToArray());
|
||||
}
|
||||
|
||||
private static string? SafeText(string? value)
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new string(value
|
||||
.Take(160)
|
||||
.Select(static character => IsUnsafeHumanCharacter(character) ? '?' : character)
|
||||
.ToArray());
|
||||
}
|
||||
|
||||
private static bool IsUnsafeHumanCharacter(char character) =>
|
||||
char.GetUnicodeCategory(character) is
|
||||
UnicodeCategory.Control
|
||||
or UnicodeCategory.Format
|
||||
or UnicodeCategory.LineSeparator
|
||||
or UnicodeCategory.ParagraphSeparator
|
||||
or UnicodeCategory.Surrogate
|
||||
or UnicodeCategory.PrivateUse;
|
||||
|
||||
private sealed class TestClientEvent
|
||||
{
|
||||
public int Version { get; init; } = 1;
|
||||
public string Event { get; init; } = string.Empty;
|
||||
public string Status { get; init; } = string.Empty;
|
||||
public string? Phase { get; init; }
|
||||
public string? ListingId { get; init; }
|
||||
public string? DisplayName { get; init; }
|
||||
public string? Outcome { get; init; }
|
||||
public string? EndpointType { get; init; }
|
||||
public int? Count { get; init; }
|
||||
public long? ElapsedMilliseconds { get; init; }
|
||||
public string? Message { get; init; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user