using System.Runtime.CompilerServices; namespace UCVL.VPNPlus; internal sealed record PlannedTransport(ConnectionAttempt Attempt, ConfigResponse? Cached = null, bool TailnetOnly = false, bool Dispatched = false, bool DispatchPass = false) { // Preserve a known dispatched node even if confirmation fails before it // returns a new config; that is one candidate failure, not an empty AI pool. internal NodeInfo? KnownCandidate => Attempt.Auxiliary ?? Cached?.Node; } internal static class TransportPlan { internal static IReadOnlyList Create(ConnectionSelection selection, IReadOnlyList rankedPrivate, string routeMode, int publicNodeCount) { var legacy = ConnectionAttemptPlanner.CreateRanked(selection, rankedPrivate, publicNodeCount); if (selection.Manual || routeMode == "direct") return legacy.Select(a => new PlannedTransport(a)).ToArray(); // This marker does no API work. Its public pass is expanded only when // iteration reaches it after the private inner attempts have failed. return rankedPrivate.Where(n => n.ProbeIpv4 != PrivateAuxiliaryRouting.PublicPhoneAddress) .Select(n => new PlannedTransport(new(n.Slug, n))) .Concat([new PlannedTransport(new("auto", null), TailnetOnly: true, DispatchPass: true)]) .Concat(rankedPrivate.Where(n => n.ProbeIpv4 == PrivateAuxiliaryRouting.PublicPhoneAddress) .Select(n => new PlannedTransport(new(n.Slug, n)))) .Concat(legacy.Where(a => !a.IsPrivate).Select(a => new PlannedTransport(a))).ToArray(); } internal static async IAsyncEnumerable ExpandAsync(IEnumerable plans, Func>> dispatch, [EnumeratorCancellation] CancellationToken token) { foreach (var plan in plans) { token.ThrowIfCancellationRequested(); if (!plan.DispatchPass) { yield return plan; continue; } var dispatched = await dispatch(token).WaitAsync(token).ConfigureAwait(false); token.ThrowIfCancellationRequested(); foreach (var candidate in dispatched) { token.ThrowIfCancellationRequested(); yield return candidate; } } } }