Table of Contents

Struct WireWriter

Namespace
Typhon.Protocol
Assembly
Typhon.Protocol.dll

Writes the wire's primitives (03-wire-protocol § 2) into a caller-owned span.

public ref struct WireWriter
Inherited Members

Remarks

Encoders always write the canonical form: minimal varints, little-endian integers, NaN as 0x7E00 in a half. Two encoders fed the same values therefore produce the same bytes, which is what lets a golden vector be a byte comparison.

Running out of span throws InvalidOperationException: the caller sized the buffer, so overflow is a bug on this side, never input from a peer. Values that cannot be represented (a string over its cap, a negative count) throw ArgumentException for the same reason.

Constructors

WireWriter(Span<byte>)

Creates a writer over buffer, positioned at its first byte.

public WireWriter(Span<byte> buffer)

Parameters

buffer Span<byte>

Where to write.

Properties

Position

Bytes written so far.

public readonly int Position { get; }

Property Value

int

Written

The bytes written so far.

public readonly ReadOnlySpan<byte> Written { get; }

Property Value

ReadOnlySpan<byte>

Methods

BeginLengthPrefixed()

Reserves room for a varu length prefix and returns a mark for EndLengthPrefixed(int). The content is written next, and the prefix is patched in minimal form afterwards, so the caller never has to know a block's size before encoding it.

public int BeginLengthPrefixed()

Returns

int

The mark.

Remarks

The reservation is five bytes whatever the final prefix, so a buffer must have up to four bytes of slack beyond the encoded size per open prefix. Prefixes nest, and must be ended innermost first.

EndLengthPrefixed(int)

Writes the minimal varu length of everything written since mark and moves the content down behind it.

public void EndLengthPrefixed(int mark)

Parameters

mark int

The value BeginLengthPrefixed() returned.

VaruSize(uint)

The number of bytes WriteVaru(uint) writes for value.

public static int VaruSize(uint value)

Parameters

value uint

The value.

Returns

int

1 to 5.

WriteBits(uint, int)

Writes the low bits ∈ {8, 16, 24, 32} bits of value: an unsigned code, or a signed code's two's complement.

public void WriteBits(uint value, int bits)

Parameters

value uint

The value.

bits int

The width.

WriteBlob(scoped ReadOnlySpan<byte>, int)

Writes a blob: a varu length, then the bytes.

public void WriteBlob(scoped ReadOnlySpan<byte> bytes, int maxBytes)

Parameters

bytes ReadOnlySpan<byte>

The bytes.

maxBytes int

The declared cap.

WriteBytes(scoped ReadOnlySpan<byte>)

Writes raw bytes.

public void WriteBytes(scoped ReadOnlySpan<byte> bytes)

Parameters

bytes ReadOnlySpan<byte>

The bytes.

WriteF16(double)

Writes a little-endian IEEE half converted straight from value (W10): ties to even, overflow to ∞, NaN as 0x7E00.

public void WriteF16(double value)

Parameters

value double

The value, in its source precision — never narrowed through a float first.

WriteF32(double)

Writes a little-endian IEEE single, narrowing value with ties to even; NaN as 0x7FC00000.

public void WriteF32(double value)

Parameters

value double

The value.

WriteI16(short)

Writes a little-endian i16.

public void WriteI16(short value)

Parameters

value short

The value.

WriteI32(int)

Writes a little-endian i32.

public void WriteI32(int value)

Parameters

value int

The value.

WriteI8(sbyte)

Writes an i8.

public void WriteI8(sbyte value)

Parameters

value sbyte

The value.

WriteStr(string, int)

Writes a str: a varu byte length, then the UTF-8 bytes.

public void WriteStr(string value, int maxBytes)

Parameters

value string

The string; null writes the empty string.

maxBytes int

The declared cap.

WriteU16(ushort)

Writes a little-endian u16.

public void WriteU16(ushort value)

Parameters

value ushort

The value.

WriteU24(uint)

Writes the low 24 bits of value, little-endian.

public void WriteU24(uint value)

Parameters

value uint

The value; bits above 23 are dropped, so a negative int writes its two's complement.

WriteU32(uint)

Writes a little-endian u32.

public void WriteU32(uint value)

Parameters

value uint

The value.

WriteU8(byte)

Writes a u8.

public void WriteU8(byte value)

Parameters

value byte

The value.

WriteVari(int)

Writes a vari: zigzag, then varu.

public void WriteVari(int value)

Parameters

value int

The value.

WriteVaru(uint)

Writes a varu: minimal unsigned LEB128.

public void WriteVaru(uint value)

Parameters

value uint

The value.