Table of Contents

Interface IVisitor

Namespace
sofab
Assembly
SofaBuffers.dll

Receives decoded fields pushed by an IStream.

public interface IVisitor

Remarks

The decoder follows the visitor pattern: rather than binding a destination buffer per field (as the C API does), it calls back into an IVisitor as each field is decoded. Every method has a default no-op implementation (a C# default-interface method), so an implementor overrides only the field kinds it cares about; unhandled fields are simply dropped (the equivalent of "not interested" / skip in the C API). This keeps generated message classes small: a generated visitor is typically one switch on the field id.

Streaming contract. Scalars and floats are delivered whole. String and blob payloads are delivered in one or more chunks so they can exceed the input chunk size (and even RAM); each chunk reports the field total length and the byte offset of the chunk within the field. Array elements are announced once via ArrayBegin(int, ArrayKind, int) and then delivered through the scalar / float callbacks with the same id.

Header hooks. A field that declares a size is announced on the word that declares it, ahead of any payload: ArrayBegin(int, ArrayKind, int) for an array's element count, and FixlenBegin(int, FixlenType, int) for a fixlen field's byte length. A receiver holding a schema count / maxlen bound judges it there, so the verdict cannot depend on where the input was chunked (CORELIB_PLAN §5.2, §6.4).

Buffer ownership. The data array handed to String(int, int, int, byte[], int, int) and Blob(int, int, int, byte[], int, int) is the caller's input buffer; it is only valid for the duration of the call. A visitor that needs to retain bytes must copy the [chunkOffset, chunkOffset + chunkLength) range.

Methods

ArrayBegin(int, ArrayKind, int)

Start of an array field. The count elements follow through the scalar / float callbacks with the same id.

void ArrayBegin(int id, ArrayKind kind, int count)

Parameters

id int

field id

kind ArrayKind

element category; for a fixlen array, its element subtype

count int

number of elements

Remarks

Called exactly once per array field, never per element, and always before the first element. When it is called depends on the wire type:

  • An integer array (ARRAY_UNSIGNED / ARRAY_SIGNED) is announced immediately after its element-count varint — the wire type already fixes the element kind.
  • A fixlen array (ARRAY_FIXLEN) is announced only after its fixlen_word has been read and validated, so kind is the true element subtype, Fp32 or Fp64. CORELIB_PLAN §4.8 fixes this order: a receiver must be able to decide the field is not its array's value (MESSAGE_SPEC §7.3, a wrong subtype ⇒ skip) before it applies any schema bound to count. A zero-count fixlen array still carries its word, and is still announced exactly once.

The format ceiling on count is enforced on the count varint, ahead of this call, so an absurd count is rejected without any allocation whatever the subtype turns out to be.

Blob(int, int, int, byte[], int, int)

A chunk of a blob field. See String(int, int, int, byte[], int, int) for the chunking model.

void Blob(int id, int total, int offset, byte[] data, int chunkOffset, int chunkLength)

Parameters

id int

field id

total int

full field length in bytes

offset int

byte position of this chunk within the field

data byte[]

backing array containing the chunk

chunkOffset int

start of the chunk within data

chunkLength int

number of bytes in the chunk

FixlenBegin(int, FixlenType, int)

Start of a fixed-length field, announced once the fixlen_word has been read and validated and before any payload byte is delivered.

void FixlenBegin(int id, FixlenType subtype, int total)

Parameters

id int

field id

subtype FixlenType

the resolved fixlen subtype that arrived on the wire

total int

declared payload length in bytes (4 / 8 for a float)

Remarks

Called exactly once per fixlen field — fp32, fp64, string and blob alike, total == 0 included — always before the matching Fp32(int, float) / Fp64(int, double) / String(int, int, int, byte[], int, int) / Blob(int, int, int, byte[], int, int) call, and never per payload chunk. This is the ArrayBegin(int, ArrayKind, int) of the fixlen world, and it exists for the same reason.

Why the decoder has to be the one to say it. A schema maxlen bound is fully established by the length word: the number that exceeds it is already on the wire, and no later byte can make it legal. CORELIB_PLAN §5.2 makes INVALID dominate INCOMPLETE, so a message ending exactly at that word is malformed — but the payload callbacks carry total only once payload bytes exist, so such a message would deliver no event at all and degrade to INCOMPLETE. The verdict would then depend on where the input happened to be chunked, which §6.4 and §7.2 forbid outright. Raising from this callback is what turns the field into INVALID at the only point where that decision is chunk-independent.

subtype is the subtype that arrived, not the one that was declared — the corelib is schema-agnostic. A receiver whose schema names a different subtype must treat the field as a MESSAGE_SPEC §7.3 skip and not measure it against this field's bound, exactly as it does for a mistyped ArrayBegin(int, ArrayKind, int). What the format itself rejects — a reserved subtype, or an fp32/ fp64 of the wrong width (§4.6) — is INVALID and is judged before this call, so a subtype seen here is always one of the four.

A fixlen array is announced by ArrayBegin(int, ArrayKind, int) instead, whose ArrayKind already carries the element subtype; its shared fixlen_word does not raise this hook.

Fp32(int, float)

A 32-bit float field, or an fp32 array element.

void Fp32(int id, float value)

Parameters

id int

field id

value float

the value

Fp64(int, double)

A 64-bit float field, or an fp64 array element.

void Fp64(int id, double value)

Parameters

id int

field id

value double

the value

SequenceBegin(int)

Start of a nested sequence (a new id scope).

void SequenceBegin(int id)

Parameters

id int

field id of the sequence

SequenceEnd()

End of the current nested sequence.

void SequenceEnd()

Signed(int, long)

A signed-integer field, or a signed array element.

void Signed(int id, long value)

Parameters

id int

field id

value long

the value

String(int, int, int, byte[], int, int)

A chunk of a string field (raw UTF-8 bytes, no NUL terminator).

void String(int id, int total, int offset, byte[] data, int chunkOffset, int chunkLength)

Parameters

id int

field id

total int

full field length in bytes

offset int

byte position of this chunk within the field

data byte[]

backing array containing the chunk

chunkOffset int

start of the chunk within data

chunkLength int

number of bytes in the chunk

Remarks

For an empty string this is called once with total == 0 and chunkLength == 0.

Unsigned(int, ulong)

An unsigned-integer field, or an unsigned array element.

void Unsigned(int id, ulong value)

Parameters

id int

field id

value ulong

the unsigned 64-bit value