Class PayloadAcc
- Namespace
- sofab
- Assembly
- SofaBuffers.dll
Reassembles a string or blob payload that arrives in more than
one piece — the support layer for generated code, not part of the codec.
public sealed class PayloadAcc
- Inheritance
-
PayloadAcc
- Inherited Members
Remarks
String(int, int, int, byte[], int, int) and Blob(int, int, int, byte[], int, int) deliver a payload
in one or more chunks, split wherever the input happened to be split, and the
data array they hand over is only valid for the duration of the call
(that is what lets a payload exceed the input buffer, and RAM). A consumer that
wants the whole value therefore has to buffer the pieces. That is all this is,
and its code has the same shape for every schema, so it lives here rather than
being emitted into every generated source tree (generator#345).
Hold one per visitor and pass the callback's arguments straight through; the
value comes back on the chunk that completes it, and null before that:
public void String(int id, int total, int offset, byte[] data, int co, int cl)
{
string? s = _acc.String(total, offset, data, co, cl, MaxDynStringLen);
if (s is null)
{
return; // more chunks to come
}
... // route s to its field
}
A payload that arrives whole never touches the buffer. The common case — one chunk carrying the entire field — is answered straight out of the caller's input array, so an accumulator that is never needed never allocates one byte.
total is not an allocation. The announced length is the wire's
claim, so the buffer grows by doubling against bytes that have actually
arrived: even a payload nothing bounds cannot make this class allocate more
than the peer really sent.
The cap is an argument, never a possession (CORELIB_PLAN §6.2.1). Both
methods take the max_dyn_string_len / max_dyn_blob_len the
receiver configured and compare total against it before a byte is
taken, at the length header, which is where §6.2.1 requires the check to run
— ahead of the allocation it exists to prevent. The number stays
generated code's: it is used for that one comparison and not retained, and
this class holds no limit of its own, defaults none, reads no omitted
argument as unlimited and clamps to nothing. Which is why the parameter is
required: there is no unset state and no unlimited mode.
...and at the LENGTH WORD, not only at the first chunk. String(int, int, byte[], int, int, long)
and Blob(int, int, byte[], int, int, long) cannot be the enforcement point on their own: they fire
only once a payload byte exists, so a message that ends immediately after the
length word reaches neither, and a decode whose verdict was already decided
answers Incomplete instead — losing the category (§6.3 makes the refusal
terminal) and inviting a caller to keep feeding a stream this receiver has
already refused. So the comparison is reachable on its own, as
CheckStringLength(int, long) / CheckBlobLength(int, long), for a caller to
make from FixlenBegin(int, FixlenType, int). The payload methods call the same
two, so the rule has one implementation applied at two points.
Behind the tag test, and only for a field that is read. Generated code resolves the destination first (MESSAGE_SPEC §7.3: a field whose wire type contradicts the declared one is skipped, and a skipped field is never capped) and calls only for a payload it is actually materializing, so the check here already sits behind both conditions.
A schema-bounded field is not capped here. Where the schema declares a
maxlen, that bound governs and exceeding it is InvalidMessage,
not LimitExceeded (MESSAGE_SPEC §7.1, CORELIB_PLAN §6.3). Generated
code rejects it at the same header before calling and passes that same bound
on, where it can no longer fire: this class is never the one to decide which
of the two a field has.
No re-arming step. Every payload's first chunk is reported at offset 0, and that is where the buffer is emptied — so an accumulator still holding the remains of a payload that never completed (a stream that ended mid-field) is correct again the moment the next one starts, whether or not the visitor around it was reused.
The split must not be observable. CORELIB_PLAN §6.4 forbids an outcome that depends on where a chunk boundary fell: for the same bytes the value — and for a string the UTF-8 verdict — is the same whether they arrive in one piece or one byte at a time. Which is why a string is validated once, on the reassembled payload, and never per chunk: a multi-byte sequence split across a feed is a well-formed prefix, not a defect.
This class is not thread-safe; decode one message on one thread.
Methods
Blob(int, int, byte[], int, int, long)
Offer a chunk of a blob payload; returns the payload once the last
chunk has arrived, null while more are expected.
public byte[]? Blob(int total, int offset, byte[] data, int chunkOffset, int chunkLength, long cap)
Parameters
totalintfull payload length in bytes, as Blob(int, int, int, byte[], int, int) reports it
offsetintbyte position of this chunk within the payload
databyte[]backing array containing the chunk
chunkOffsetintstart of the chunk within
datachunkLengthintnumber of bytes in the chunk
caplongthe receiver's
max_dyn_blob_lenfor this field, in bytes — the caller's number, used for this one comparison and not retained (§6.2.1); for a field the schema bounds, the schemamaxlenthe caller has already enforced
Returns
- byte[]
the completed payload, or
nullwhile it is incomplete
Remarks
The returned array is the caller's to keep: it is a copy, never a view into the decoder's input buffer or into this accumulator.
Exceptions
- SofabException
(LimitExceeded) when
totalexceedscap— checked before a byte is taken; (Argument) when no cap was stated (capis negative).
CheckBlobLength(int, long)
Refuse an announced blob length the receiver's cap does not admit —
the blob twin of CheckStringLength(int, long), down to why it
exists. A blob and a string are separate limits.
public static void CheckBlobLength(int total, long cap)
Parameters
totalintthe announced payload length, as the
fixlen_wordgives itcaplongthe receiver's
max_dyn_blob_lenfor this field
Exceptions
- SofabException
(LimitExceeded) when
totalexceedscap; (Argument) when no cap was stated.
CheckStringLength(int, long)
Refuse an announced string length the receiver's cap does not admit
— the §6.2.1 comparison on its own, for a caller to make at the
length word.
public static void CheckStringLength(int total, long cap)
Parameters
totalintthe announced payload length, as the
fixlen_wordgives itcaplongthe receiver's
max_dyn_string_lenfor this field — the caller's number, used for this one comparison and not retained; for a field the schema bounds, the schemamaxlenthe caller has already enforced
Remarks
String(int, int, byte[], int, int, long) calls this first, so a caller that only routes payload
chunks is already covered. Call it in addition from
FixlenBegin(int, FixlenType, int) to close the case no payload callback can
see: a message whose length word declares more than the cap and then
ends. There is no chunk, so there is no String(int, int, byte[], int, int, long) call,
and the decode reports Incomplete for bytes this receiver has already
refused — the wrong category (§6.3 makes the refusal terminal) and an active
invitation to feed more of a stream that will never be accepted. Three bytes
claiming a hundred, or five claiming a megabyte, would hold a connection
open: the amplification the caps exist to close.
Calling it at both points is not two implementations of the rule. It is this one, applied where §6.2.1 requires it ("at the count/length header — before the allocation it is meant to prevent") and again where an accumulator driven by hand would otherwise slip past it.
Only for a field this message actually reads. §6.2.1: "a skipped field is never capped" — a limit bounds an allocation, and a field walked over allocates nothing. The caller resolves the destination, and the MESSAGE_SPEC §7.3 subtype test, before it gets here.
Exceptions
- SofabException
(LimitExceeded) when
totalexceedscap; (Argument) when no cap was stated (capis negative).
String(int, int, byte[], int, int, long)
Offer a chunk of a string payload; returns the decoded string once
the last chunk has arrived, null while more are expected.
public string? String(int total, int offset, byte[] data, int chunkOffset, int chunkLength, long cap)
Parameters
totalintfull payload length in bytes, as String(int, int, int, byte[], int, int) reports it
offsetintbyte position of this chunk within the payload
databyte[]backing array containing the chunk
chunkOffsetintstart of the chunk within
datachunkLengthintnumber of bytes in the chunk
caplongthe receiver's
max_dyn_string_lenfor this field, in bytes — the caller's number, used for this one comparison and not retained (§6.2.1); for a field the schema bounds, the schemamaxlenthe caller has already enforced
Returns
- string
the completed string, or
nullwhile the payload is incomplete
Remarks
Validation happens on the reassembled payload, once, at the point it is complete (Decode(byte[], int, int)).
Exceptions
- SofabException
(LimitExceeded) when
totalexceedscap— checked before a byte is taken; (Argument) when no cap was stated (capis negative), a caller defect and never LimitExceeded, which would promise a limit to raise that was never configured (§6.3); (InvalidMessage) when the completed payload is not valid UTF-8.