Harden CultNet snapshot fetches

This commit is contained in:
Meta Vens 2026-06-27 14:35:54 +01:00
commit 81b01ec41e
2 changed files with 54 additions and 1 deletions

View file

@ -516,6 +516,16 @@ namespace GameCult.Networking
/// </summary>
public TimeSpan ResponseTimeout { get; set; } = TimeSpan.FromSeconds(5);
/// <summary>
/// Gets or sets schema ids to request. Null or empty requests every schema allowed by the shard.
/// </summary>
public IReadOnlyList<string>? SchemaIds { get; set; }
/// <summary>
/// Gets or sets record keys to request. Null or empty requests every record allowed by the shard/schema filter.
/// </summary>
public IReadOnlyList<string>? RecordKeys { get; set; }
/// <summary>
/// Gets or sets a callback used to customize each ephemeral fetch client.
/// </summary>
@ -569,7 +579,8 @@ namespace GameCult.Networking
client.SendCultNet(new CultNetSnapshotRequestMessage
{
MessageId = messageId,
SchemaIds = shard.SchemaIds.Count == 0 ? null : shard.SchemaIds.ToArray(),
SchemaIds = ResolveRequestedSchemaIds(shard),
RecordKeys = ResolveRequestedRecordKeys(),
ShardId = shard.ShardId,
ShardEpoch = shard.Epoch
});
@ -583,6 +594,33 @@ namespace GameCult.Networking
?? CultNetSchemaClients.CreateForEndpoint(endpoint, _options.Security, _options.ConfigureClient);
}
private string[]? ResolveRequestedSchemaIds(CultNetShardDescriptor shard)
{
if (_options.SchemaIds is { Count: > 0 })
{
return _options.SchemaIds
.Where(value => !string.IsNullOrWhiteSpace(value))
.Distinct(StringComparer.Ordinal)
.ToArray();
}
return shard.SchemaIds.Count == 0 ? null : shard.SchemaIds.ToArray();
}
private string[]? ResolveRequestedRecordKeys()
{
if (_options.RecordKeys is not { Count: > 0 })
{
return null;
}
var recordKeys = _options.RecordKeys
.Where(value => !string.IsNullOrWhiteSpace(value))
.Distinct(StringComparer.Ordinal)
.ToArray();
return recordKeys.Length == 0 ? null : recordKeys;
}
private async Task WaitForConnectionAsync(ICultNetSchemaClient client, string endpoint)
{
var deadline = DateTimeOffset.UtcNow + _options.ConnectTimeout;

View file

@ -1837,6 +1837,14 @@ namespace GameCult.Networking
{
return null;
}
catch (SocketException error) when (
error.SocketErrorCode == SocketError.ConnectionReset ||
error.SocketErrorCode == SocketError.ConnectionAborted ||
error.SocketErrorCode == SocketError.Shutdown)
{
DisconnectReason = Array.Empty<byte>();
return null;
}
_stats.BytesReceived += received;
if (_remoteEndPoint == null)
@ -2163,6 +2171,13 @@ namespace GameCult.Networking
{
return null;
}
catch (SocketException error) when (
error.SocketErrorCode == SocketError.ConnectionReset ||
error.SocketErrorCode == SocketError.ConnectionAborted ||
error.SocketErrorCode == SocketError.Shutdown)
{
return null;
}
_stats.BytesReceived += received;
var wire = new byte[received];