Surface CultNet background transport failures

This commit is contained in:
Meta Vens 2026-07-12 10:33:02 +01:00
commit 6be0573ffc
3 changed files with 101 additions and 27 deletions

View file

@ -779,7 +779,8 @@ namespace GameCult.Mesh
completion.Task,
endpoint,
resolvedOptions,
messageId)
messageId,
(client as ICultNetSchemaClientHealth)?.BackgroundFailure)
.ConfigureAwait(false);
}
@ -886,10 +887,20 @@ namespace GameCult.Mesh
Task<CultNetSnapshotResponseRawMessage> responseTask,
string endpoint,
CultMeshSnapshotRequestOptions options,
string messageId)
string messageId,
Task<Exception>? backgroundFailure = null)
{
var timeoutTask = Task.Delay(options.ResponseTimeout);
var completed = await Task.WhenAny(responseTask, timeoutTask).ConfigureAwait(false);
var completed = backgroundFailure == null
? await Task.WhenAny(responseTask, timeoutTask).ConfigureAwait(false)
: await Task.WhenAny(responseTask, timeoutTask, backgroundFailure).ConfigureAwait(false);
if (completed == backgroundFailure)
{
var error = await backgroundFailure.ConfigureAwait(false);
throw new InvalidOperationException(
$"CultNet snapshot client failed while waiting for response '{messageId}' from {endpoint}.",
error);
}
if (completed != responseTask)
{
throw new TimeoutException(

View file

@ -6,6 +6,7 @@ using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace GameCult.Networking
{
@ -37,6 +38,17 @@ namespace GameCult.Networking
where T : ICultNetSchemaMessage;
}
/// <summary>
/// Exposes terminal failure of a schema client's background transport work.
/// </summary>
public interface ICultNetSchemaClientHealth
{
/// <summary>
/// Completes if the client's background transport loop terminates unexpectedly.
/// </summary>
Task<Exception> BackgroundFailure { get; }
}
/// <summary>
/// LiteNetLib-backed implementation of the CultNet schema client port.
/// </summary>
@ -91,7 +103,7 @@ namespace GameCult.Networking
/// <summary>
/// RUDP-backed implementation of the CultNet schema client port.
/// </summary>
public sealed class RudpCultNetSchemaClient : ICultNetSchemaClient
public sealed class RudpCultNetSchemaClient : ICultNetSchemaClient, ICultNetSchemaClientHealth
{
private readonly string _runtimeId;
private readonly uint _connectionId;
@ -100,6 +112,8 @@ namespace GameCult.Networking
private readonly long _resendDelayMs;
private readonly ConcurrentDictionary<Type, List<Delegate>> _handlers = new();
private readonly object _handlerLock = new();
private readonly TaskCompletionSource<Exception> _backgroundFailure = new(
TaskCreationOptions.RunContinuationsAsynchronously);
private CultNetRudpSocketTransportConnection? _transport;
private Thread? _pumpThread;
private volatile bool _disposed;
@ -125,6 +139,9 @@ namespace GameCult.Networking
/// <inheritdoc />
public bool Connected => _transport?.Connected == true;
/// <inheritdoc />
public Task<Exception> BackgroundFailure => _backgroundFailure.Task;
/// <inheritdoc />
public void Connect(string host, int port)
{
@ -188,35 +205,36 @@ namespace GameCult.Networking
private void Pump()
{
while (!_disposed)
try
{
var transport = _transport;
if (transport == null)
while (!_disposed)
{
return;
}
var transport = _transport;
if (transport == null)
{
return;
}
ICultNetSchemaMessage? message;
try
{
message = transport.ReceiveSchemaMessageOnce();
var message = transport.ReceiveSchemaMessageOnce();
transport.PollResends();
}
catch (ObjectDisposedException) when (_disposed)
{
return;
}
catch (SocketException) when (_disposed)
{
return;
}
if (message != null)
{
Dispatch(message);
}
if (message != null)
{
Dispatch(message);
}
Thread.Sleep(1);
Thread.Sleep(1);
}
}
catch (ObjectDisposedException) when (_disposed)
{
}
catch (SocketException) when (_disposed)
{
}
catch (Exception error)
{
_backgroundFailure.TrySetResult(error);
}
}

View file

@ -2654,6 +2654,26 @@ public sealed class CultMeshStreamingTests
request.MessageId.StartsWith("mesh-test-scoped-snapshot:", StringComparison.Ordinal));
}
[Test]
public async Task SnapshotHelpers_SurfaceBackgroundClientFailureBeforeTimeout()
{
var options = new CultMeshSnapshotRequestOptions
{
ResponseTimeout = TimeSpan.FromSeconds(30),
RecordKeys = new[] { "eve:surface:test" },
CreateClient = () => new FailedBackgroundSchemaClient(
new FormatException("malformed fragment"))
};
Func<Task> act = async () => await CultMesh.FetchSnapshotAsync(
"rudp://127.0.0.1:3075",
options);
var failure = await act.Should().ThrowAsync<InvalidOperationException>();
failure.Which.InnerException.Should().BeOfType<FormatException>();
failure.Which.Message.Should().Contain("127.0.0.1:3075");
}
[Test]
public async Task SnapshotHelpers_DecodeSameSchemaAliasFromForeignSchemaId()
{
@ -3815,3 +3835,28 @@ internal sealed class MeshSnapshotSchemaClient : ICultNetSchemaClient
{
}
}
internal sealed class FailedBackgroundSchemaClient : ICultNetSchemaClient, ICultNetSchemaClientHealth
{
public FailedBackgroundSchemaClient(Exception error)
{
BackgroundFailure = Task.FromResult(error);
}
public bool Connected { get; private set; }
public Task<Exception> BackgroundFailure { get; }
public void Connect(string host, int port) => Connected = true;
public void SendCultNet<T>(T message) where T : ICultNetSchemaMessage
{
}
public void OnCultNet<T>(Action<T> callback) where T : ICultNetSchemaMessage
{
}
public void Dispose()
{
}
}