Table of Contents

Struct WireReader

Namespace
Typhon.Protocol
Assembly
Typhon.Protocol.dll

Reads the wire's primitives (03-wire-protocol § 2) from a span: little-endian fixed-width integers, LEB128 varints, half and single floats, and length-prefixed strings and blobs.

public ref struct WireReader
Inherited Members

Remarks

Every read validates against what remains and throws WireFormatException with close code 1007 when the input is short or out of range, so a decoder built on it never indexes past its message. It never allocates except in ReadStr(int), which returns a managed string.

Varints are read leniently. An over-long encoding (0x80 0x00 for zero) is accepted as long as the value fits 32 bits in at most five bytes; encoders always write the minimal form, so golden vectors stay canonical while a decoder does not have to police it.

Constructors

WireReader(ReadOnlySpan<byte>)

Creates a reader over data, positioned at its first byte.

public WireReader(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

The bytes to read.

Properties

IsAtEnd

Whether every byte has been consumed.

public readonly bool IsAtEnd { get; }

Property Value

bool

Position

Bytes consumed so far.

public readonly int Position { get; }

Property Value

int

Remaining

Bytes left to read.

public readonly int Remaining { get; }

Property Value

int

Methods

ExpectEnd(string)

Throws unless every byte has been consumed: a block whose content is shorter than its declared length is malformed, not padded.

public readonly void ExpectEnd(string what)

Parameters

what string

What was being read, for the error message.

ReadBlob(int)

Reads a blob: a varu length, at most maxBytes, then that many bytes.

public ReadOnlySpan<byte> ReadBlob(int maxBytes)

Parameters

maxBytes int

The cap the catalog or protocol declares.

Returns

ReadOnlySpan<byte>

A view of the bytes.

ReadBytes(int)

Reads count raw bytes.

public ReadOnlySpan<byte> ReadBytes(int count)

Parameters

count int

How many.

Returns

ReadOnlySpan<byte>

A view of the bytes; valid as long as the underlying buffer is.

ReadF16()

Reads a little-endian IEEE half.

public double ReadF16()

Returns

double

The value, widened exactly to a double; every NaN pattern decodes as the canonical NaN.

ReadF32()

Reads a little-endian IEEE single.

public double ReadF32()

Returns

double

The value, widened exactly to a double; every NaN pattern decodes as the canonical NaN (CanonicalNaN).

ReadI16()

Reads a little-endian i16.

public short ReadI16()

Returns

short

The value.

ReadI24()

Reads a little-endian i24: a u24 sign-extended from bit 23.

public int ReadI24()

Returns

int

The value, in [−2²³, 2²³).

ReadI32()

Reads a little-endian i32.

public int ReadI32()

Returns

int

The value.

ReadI8()

Reads an i8.

public sbyte ReadI8()

Returns

sbyte

The value.

ReadSigned(int)

Reads a two's-complement integer of bits ∈ {8, 16, 24, 32} bits, sign-extended.

public int ReadSigned(int bits)

Parameters

bits int

The width.

Returns

int

The value.

ReadStr(int)

Reads a str as a managed string. Allocates; decoders on a zero-allocation path use ReadStrUtf8(int).

public string ReadStr(int maxBytes)

Parameters

maxBytes int

The cap the catalog or protocol declares.

Returns

string

The string.

ReadStrUtf8(int)

Reads a str: a varu byte length, at most maxBytes, then that many bytes of valid UTF-8.

public ReadOnlySpan<byte> ReadStrUtf8(int maxBytes)

Parameters

maxBytes int

The cap the catalog or protocol declares.

Returns

ReadOnlySpan<byte>

A view of the UTF-8 bytes, validated.

ReadU16()

Reads a little-endian u16.

public ushort ReadU16()

Returns

ushort

The value.

ReadU24()

Reads a little-endian u24: b0 | b1 << 8 | b2 << 16.

public uint ReadU24()

Returns

uint

The value, in [0, 2²⁴).

ReadU32()

Reads a little-endian u32.

public uint ReadU32()

Returns

uint

The value.

ReadU8()

Reads a u8.

public byte ReadU8()

Returns

byte

The value.

ReadUnsigned(int)

Reads an unsigned integer of bits ∈ {8, 16, 24, 32} bits.

public uint ReadUnsigned(int bits)

Parameters

bits int

The width.

Returns

uint

The value.

ReadVari()

Reads a vari: a zigzag-mapped varu.

public int ReadVari()

Returns

int

The value.

ReadVaru()

Reads a varu: unsigned LEB128, at most five bytes, fitting 32 bits.

public uint ReadVaru()

Returns

uint

The value.

ReadVaruAtMost(int, string)

Reads a varu that must not exceed max, as an int ready to use as a count or index.

public int ReadVaruAtMost(int max, string what)

Parameters

max int

The largest acceptable value.

what string

What the value is, for the error message.

Returns

int

The value.

Skip(int)

Skips count bytes.

public void Skip(int count)

Parameters

count int

How many.

Slice(int)

Returns a reader over the next length bytes and advances past them: the way a length-prefixed block is isolated.

public WireReader Slice(int length)

Parameters

length int

The sub-reader's length.

Returns

WireReader

A reader over exactly those bytes.