mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2026-09-08 10:10:49 +00:00
Limits
This commit is contained in:
@@ -15,6 +15,16 @@ public sealed class ParserLimitException : Exception
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised before transmission when a composed value exceeds a budget advertised by the peer.
|
||||
/// </summary>
|
||||
public sealed class RemoteParserLimitException : Exception
|
||||
{
|
||||
public RemoteParserLimitException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal static class ParserGuard
|
||||
{
|
||||
internal static Warehouse? GetWarehouse(EpConnection? connection)
|
||||
@@ -70,4 +80,64 @@ internal static class ParserGuard
|
||||
|
||||
internal static ulong MultiplySaturated(ulong value, ulong multiplier)
|
||||
=> value > ulong.MaxValue / multiplier ? ulong.MaxValue : value * multiplier;
|
||||
|
||||
internal static void EnsureRemotePacketSize(EpConnection? connection, ulong size)
|
||||
{
|
||||
var limit = connection?.Session?.RemoteMaximumPacketSize ?? 0;
|
||||
if (limit > 0 && size > limit)
|
||||
throw new RemoteParserLimitException(
|
||||
$"Composed packet payload of {size} bytes exceeds the peer's advertised {limit}-byte limit.");
|
||||
}
|
||||
|
||||
internal static void EnsureRemoteAllocation(
|
||||
EpConnection? connection,
|
||||
ulong size,
|
||||
string kind)
|
||||
{
|
||||
var limit = connection?.Session?.RemoteMaximumAllocationSize ?? 0;
|
||||
if (limit > 0 && size > limit)
|
||||
throw new RemoteParserLimitException(
|
||||
$"Composed {kind} would allocate {size} bytes at the peer, exceeding its advertised {limit}-byte limit.");
|
||||
}
|
||||
|
||||
internal static void EnsureRemoteCollectionCount(
|
||||
EpConnection? connection,
|
||||
int count,
|
||||
int estimatedBytesPerItem = 0)
|
||||
{
|
||||
var limit = connection?.Session?.RemoteMaximumCollectionItems ?? 0;
|
||||
if (limit > 0 && count > limit)
|
||||
throw new RemoteParserLimitException(
|
||||
$"Composed collection has {count} items, exceeding the peer's advertised {limit}-item limit.");
|
||||
|
||||
if (estimatedBytesPerItem > 0)
|
||||
EnsureRemoteAllocation(
|
||||
connection,
|
||||
MultiplySaturated((ulong)count, (ulong)estimatedBytesPerItem),
|
||||
"collection");
|
||||
}
|
||||
|
||||
internal static void EnsureRemoteTypeMetadataDepth(EpConnection? connection, Tru? tru)
|
||||
{
|
||||
var limit = connection?.Session?.RemoteMaximumTypeMetadataDepth ?? 0;
|
||||
if (limit <= 0 || tru == null)
|
||||
return;
|
||||
|
||||
var depth = GetTypeMetadataDepth(tru);
|
||||
if (depth > limit)
|
||||
throw new RemoteParserLimitException(
|
||||
$"Composed TRU type metadata depth of {depth} exceeds the peer's advertised limit of {limit}.");
|
||||
}
|
||||
|
||||
static int GetTypeMetadataDepth(Tru tru)
|
||||
{
|
||||
if (tru is not TruComposite composite || composite.SubTypes == null || composite.SubTypes.Length == 0)
|
||||
return 1;
|
||||
|
||||
var maximumChildDepth = 0;
|
||||
foreach (var child in composite.SubTypes)
|
||||
maximumChildDepth = Math.Max(maximumChildDepth, GetTypeMetadataDepth(child));
|
||||
|
||||
return 1 + maximumChildDepth;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user