namespace XFESystem.NativeRuntime.Application.Web;
/// <summary>One idle, same-origin wire connection. No response bytes or cookies are cached.</summary>
internal sealed class NativeHttpConnectionPool
{
internal sealed class Connection(Uri origin, Stream wire, Action<long> setDeadline, Action close,
bool certificateValidated, string protocol)
{
private int closed;
public Uri Origin { get; } = origin;
public Stream Wire { get; } = wire;
public bool CertificateValidated { get; } = certificateValidated;
public string Protocol { get; } = protocol;
public void SetDeadline(long deadline) => setDeadline(deadline);
public void Close() { if (Interlocked.Exchange(ref closed, 1) == 0) close(); }
}
private Connection? idle;
private long returnedAt;
// Called only under NativeWebTransport's single ownership gate. The
// network has one active TCP stream, so close before opening another.
internal Connection Take(Uri target, long now, long deadline, Func<Connection> open, out bool reused)
{
var candidate = idle; idle = null;
reused = candidate is not null && now >= returnedAt && now - returnedAt <= 10_000 &&
target.Scheme == candidate.Origin.Scheme && target.IdnHost.Equals(candidate.Origin.IdnHost, StringComparison.OrdinalIgnoreCase) &&
target.Port == candidate.Origin.Port && (target.Scheme != "https" || candidate.CertificateValidated);
if (!reused) { candidate?.Close(); candidate = open(); }
try { candidate!.SetDeadline(deadline); return candidate; }
catch { candidate!.Close(); throw; }
}
internal void Return(Connection connection, bool canReuse, long now)
{
idle?.Close(); idle = null;
if (canReuse) { idle = connection; returnedAt = now; }
else connection.Close();
}
}
namespace XFESystem.NativeRuntime.Application.Web;
/// <summary>One idle, same-origin wire connection. No response bytes or cookies are cached.</summary>
internal sealed class NativeHttpConnectionPool
{
internal sealed class Connection(Uri origin, Stream wire, Action<long> setDeadline, Action close,
bool certificateValidated, string protocol)
{
private int closed;
public Uri Origin { get; } = origin;
public Stream Wire { get; } = wire;
public bool CertificateValidated { get; } = certificateValidated;
public string Protocol { get; } = protocol;
public void SetDeadline(long deadline) => setDeadline(deadline);
public void Close() { if (Interlocked.Exchange(ref closed, 1) == 0) close(); }
}
private Connection? idle;
private long returnedAt;
// Called only under NativeWebTransport's single ownership gate. The
// network has one active TCP stream, so close before opening another.
internal Connection Take(Uri target, long now, long deadline, Func<Connection> open, out bool reused)
{
var candidate = idle; idle = null;
reused = candidate is not null && now >= returnedAt && now - returnedAt <= 10_000 &&
target.Scheme == candidate.Origin.Scheme && target.IdnHost.Equals(candidate.Origin.IdnHost, StringComparison.OrdinalIgnoreCase) &&
target.Port == candidate.Origin.Port && (target.Scheme != "https" || candidate.CertificateValidated);
if (!reused) { candidate?.Close(); candidate = open(); }
try { candidate!.SetDeadline(deadline); return candidate; }
catch { candidate!.Close(); throw; }
}
internal void Return(Connection connection, bool canReuse, long now)
{
idle?.Close(); idle = null;
if (canReuse) { idle = connection; returnedAt = now; }
else connection.Close();
}
}