Vixen
02b45cc4
csharp
public ref struct PacketReader

Reads a packet out of a span, and refuses to be surprised by it.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

Nothing here throws, ever. Every read returns a Boolean, and the first one that fails sets Failed so that every read after it fails too. A decoder can therefore read a whole message as straight-line code and check once at the end, and a hostile packet takes the same path as a truncated one.

That is a security property, not a convenience. Inbound bytes come from a machine we do not control; an exception escaping a decoder is a denial of service if it unwinds a frame and a crash if it does not, and a try/catch around the whole of the receive path is how a parser bug becomes exploitable. The reader is the closed door: it never allocates on a length it was told (only TryReadString allocates at all, and only up to a cap the caller states), never indexes outside its span, and never believes a length field.

Fields and properties (5)

  • public bool Failed

    Whether any read has failed. Once set, every later read fails too.

  • public readonly int Position

    How many bytes have been read.

  • public readonly int Remaining

    How many bytes are left.

  • public readonly bool IsComplete

    Whether everything was read and nothing failed — what a well-formed packet looks like.

  • public readonly ReadOnlySpan<byte> RemainingBytes

    Everything not yet read, for handing the rest of a packet to whoever owns that part of it.

Methods (14)

  • public PacketReader(ReadOnlySpan<byte> buffer)

    Creates a reader over a packet.

  • public bool TryReadByte(out byte value)

    Reads one byte.

  • public bool TryReadBool(out bool value)

    Reads a boolean.

  • public bool TryReadUInt16(out ushort value)

    Reads an unsigned 16-bit value, little-endian.

  • public bool TryReadUInt32(out uint value)

    Reads an unsigned 32-bit value, little-endian.

  • public bool TryReadInt32(out int value)

    Reads a signed 32-bit value, little-endian.

  • public bool TryReadUInt64(out ulong value)

    Reads an unsigned 64-bit value, little-endian.

  • public bool TryReadSingle(out float value)

    Reads a 32-bit float by its bits.

  • public bool TryReadTick(out Tick value)

    Reads a tick.

  • public bool TryReadVariable(out uint value)

    Reads a value written by WriteVariable.

  • public bool TryReadRaw(int count, out ReadOnlySpan<byte> bytes)

    Reads a fixed number of bytes.

  • public bool TryReadBlob(int maxBytes, out ReadOnlySpan<byte> bytes)

    Reads bytes written by WriteBlob.

  • public bool TryReadString(int maxBytes, out string value)

    Reads a string written by WriteString.

  • public bool Reject()

    Marks the read as failed, for a decoder that found something it does not accept.

Used by (4)

  • MatchProtocolMultiplayer
  • NetworkSessionVixen.Net
  • PacketCodecTestsVixen.Net.Tests
  • PacketReaderTargetVixen.Net.Fuzz