Vixen
02b45cc4
csharp
public ref struct PacketWriter

Writes a packet into a caller-owned buffer.

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

Remarks

Little-endian, always, on every platform. The wire format is fixed by this file rather than by the machine that happens to be running, which is what makes the bit-exactness gate — the same payload bytes from the same values on Windows, Linux and macOS — a thing that can be asserted in CI.

Running out of room is not an exception. The writer sets Overflowed and stops writing, and TryFinish refuses to hand over a truncated packet. A bandwidth spike is an ordinary event in a frame loop and the right response to it is to shed the packet, not to unwind the stack — that is what the bandwidth budget and its priority shedding are built on.

It is a ref struct over a span the caller owns: no allocation, and no way to accidentally keep it past the buffer it writes into.

Fields and properties (5)

  • public readonly int Capacity

    How many bytes the buffer holds.

  • public readonly int Position

    How many bytes have been written.

  • public readonly int Remaining

    How many bytes are left.

  • public bool Overflowed

    Whether a write did not fit. Once set, nothing more is written.

  • public readonly ReadOnlySpan<byte> Written

    What has been written so far, whether or not it is complete.

Methods (14)

  • public PacketWriter(Span<byte> buffer)

    Creates a writer over a buffer.

  • public readonly bool TryFinish(out ReadOnlySpan<byte> packet)

    Hands over the packet, if all of it fits.

  • public void WriteByte(byte value)

    Writes one byte.

  • public void WriteBool(bool value)

    Writes a boolean as one byte.

  • public void WriteUInt16(ushort value)

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

  • public void WriteUInt32(uint value)

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

  • public void WriteInt32(int value)

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

  • public void WriteUInt64(ulong value)

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

  • public void WriteSingle(float value)

    Writes a 32-bit float by its bits, so the bytes do not depend on the platform.

  • public void WriteTick(Tick value)

    Writes a tick.

  • public void WriteVariable(uint value)

    Writes an unsigned value in one to five bytes, seven bits at a time.

  • public void WriteRaw(ReadOnlySpan<byte> bytes)

    Writes bytes with no length in front of them.

  • public void WriteBlob(ReadOnlySpan<byte> bytes)

    Writes bytes with their length in front of them.

  • public void WriteString(string? value)

    Writes a string as length-prefixed UTF-8.

Used by (8)

  • MatchProtocolMultiplayer
  • BitExactnessTestsVixen.Net.Tests
  • HandshakeTargetVixen.Net.Fuzz
  • NetworkSessionVixen.Net
  • PacketCodecTestsVixen.Net.Tests
  • PacketReaderTargetVixen.Net.Fuzz
  • SessionClientTargetVixen.Net.Fuzz
  • SessionTestsVixen.Net.Tests