Table of Contents

Class LiveControlProtocol

Namespace
Typhon.Profiler
Assembly
Typhon.Profiler.dll

Encode / decode helpers for the live-attach control channel (ADR-078, design Runtime/17 §7.3). Frames use the LiveStreamProtocol envelope ([u8 type][u32 length][payload]), little-endian, strings as u16 length + UTF-8.

public static class LiveControlProtocol
Inheritance
LiveControlProtocol
Inherited Members

Remarks

Workbench→engine frames use the high-bit range of LiveFrameType so they can never be mistaken for an engine→Workbench frame. They are a separate protocol from the replication wire's Typhon.Protocol.MessageTypes: same byte values, different socket.

Decoders are strict: a payload whose length is not exactly the frame type's is malformed, as is a step count of 0 or above MaxValue, or a scale that is NaN or not positive. A receiver closes the connection on any inbound frame larger than MaxInboundFrameBytes before reading its payload, so the peer never chooses an allocation.

Fields

ControlHelloFrameBytes

Whole-frame size of ControlHello.

public const int ControlHelloFrameBytes = 39

Field Value

int

ControlHelloPayloadBytes

Payload size of ControlHello: u16 version, bytes32 mac.

public const int ControlHelloPayloadBytes = 34

Field Value

int

ControlStateFixedBytes

Fixed part of a ControlState payload, before the reason's UTF-8 bytes.

public const int ControlStateFixedBytes = 80

Field Value

int

Remarks

u8 runState, u8 flags, u64 tick, f64 scale, f64 achievedScale, u32 stepsPending, u32 leaseMs, u32 ackSeq, f64 maxTimeScale, bytes32 nonce, u16 reasonLength.

DefaultMaxTimeScale

The engine's default RuntimeOptions.MaxTimeScale: the time scale an engine accepts is [1/max, max], or +∞.

public const double DefaultMaxTimeScale = 64

Field Value

double

LeasePingFrameBytes

Whole-frame size of LeasePing.

public const int LeasePingFrameBytes = 5

Field Value

int

LeasePingPayloadBytes

Payload size of LeasePing: empty.

public const int LeasePingPayloadBytes = 0

Field Value

int

MaxInboundFrameBytes

The largest Workbench→engine frame a receiver accepts, header included. The largest legal one, ControlHello, is ControlHelloFrameBytes; anything above this is a malformed or hostile stream and closes the connection.

public const int MaxInboundFrameBytes = 64

Field Value

int

SeqFrameBytes

Whole-frame size of Pause / Resume.

public const int SeqFrameBytes = 9

Field Value

int

SeqPayloadBytes

Payload size of Pause and Resume: u32 seq.

public const int SeqPayloadBytes = 4

Field Value

int

SetScaleFrameBytes

Whole-frame size of SetScale.

public const int SetScaleFrameBytes = 17

Field Value

int

SetScalePayloadBytes

Payload size of SetScale: u32 seq, f64 scale.

public const int SetScalePayloadBytes = 12

Field Value

int

StepFrameBytes

Whole-frame size of Step.

public const int StepFrameBytes = 13

Field Value

int

StepPayloadBytes

Payload size of Step: u32 seq, u32 n.

public const int StepPayloadBytes = 8

Field Value

int

TokenBytes

Size of the control token: 256 bits.

public const int TokenBytes = 32

Field Value

int

Version

The control protocol version a ControlHello carries. 2: the hello carries HMAC-SHA256(token, nonce) instead of the token, and the state carries the nonce and the engine's max time scale. An engine refuses any other version with a reason.

public const ushort Version = 2

Field Value

ushort

Methods

CommandPayloadBytes(LiveFrameType)

The exact payload size of a control command, or -1 when type is not one.

public static int CommandPayloadBytes(LiveFrameType type)

Parameters

type LiveFrameType

Returns

int

ComputeHelloMac(in LiveControlToken, in LiveControlToken)

The hello's proof: HMAC-SHA256 keyed by the control token over the connection's nonce. Both sides compute it — the Workbench to send, the engine to compare in constant time — so the token never crosses the wire and a hello is worthless on any other connection. Allocation-free.

public static LiveControlToken ComputeHelloMac(in LiveControlToken token, in LiveControlToken nonce)

Parameters

token LiveControlToken
nonce LiveControlToken

Returns

LiveControlToken

ControlStateFrameBytes(string)

Whole-frame size of a ControlState carrying reason.

public static int ControlStateFrameBytes(string reason)

Parameters

reason string

Returns

int

IsControlCommand(LiveFrameType)

Whether type is a Workbench→engine control frame.

public static bool IsControlCommand(LiveFrameType type)

Parameters

type LiveFrameType

Returns

bool

IsInEngineRange(double, double)

Whether scale is inside an engine's accepted range for maxTimeScale: [1/max, max], or +∞.

public static bool IsInEngineRange(double scale, double maxTimeScale = 64)

Parameters

scale double
maxTimeScale double

Returns

bool

IsValidScale(double)

A scale that can travel: positive and not NaN, or PositiveInfinity (unpaced). The engine narrows it further.

public static bool IsValidScale(double scale)

Parameters

scale double

Returns

bool

NewNonce()

A fresh random nonce from the OS's cryptographic generator.

public static LiveControlToken NewNonce()

Returns

LiveControlToken

TryReadCommand(LiveFrameType, ReadOnlySpan<byte>, out LiveControlCommand)

Decodes a control command payload (the bytes after the frame header). Returns false when the type is not a control command, the payload length is not the type's exact length, or a field is out of range.

public static bool TryReadCommand(LiveFrameType type, ReadOnlySpan<byte> payload, out LiveControlCommand command)

Parameters

type LiveFrameType
payload ReadOnlySpan<byte>
command LiveControlCommand

Returns

bool

TryReadControlState(ReadOnlySpan<byte>, out LiveControlState)

Decodes a ControlState payload. Returns false when it is shorter than its fixed part, its reason length disagrees with the payload length, or its run state is not a known value.

public static bool TryReadControlState(ReadOnlySpan<byte> payload, out LiveControlState state)

Parameters

payload ReadOnlySpan<byte>
state LiveControlState

Returns

bool

WriteControlHello(Span<byte>, in LiveControlToken, ushort)

Writes a ControlHello frame carrying mac — ComputeHelloMac(in LiveControlToken, in LiveControlToken) of the token over the nonce the engine sent in this connection's state. Returns the bytes written (ControlHelloFrameBytes).

public static int WriteControlHello(Span<byte> destination, in LiveControlToken mac, ushort version = 2)

Parameters

destination Span<byte>
mac LiveControlToken
version ushort

Returns

int

WriteControlState(Span<byte>, in LiveControlState)

Writes a ControlState frame. Returns the bytes written (ControlStateFrameBytes(string)).

public static int WriteControlState(Span<byte> destination, in LiveControlState state)

Parameters

destination Span<byte>
state LiveControlState

Returns

int

WriteLeasePing(Span<byte>)

Writes a LeasePing frame. Returns the bytes written.

public static int WriteLeasePing(Span<byte> destination)

Parameters

destination Span<byte>

Returns

int

WritePause(Span<byte>, uint)

Writes a Pause frame. Returns the bytes written.

public static int WritePause(Span<byte> destination, uint seq)

Parameters

destination Span<byte>
seq uint

Returns

int

WriteResume(Span<byte>, uint)

Writes a Resume frame. Returns the bytes written.

public static int WriteResume(Span<byte> destination, uint seq)

Parameters

destination Span<byte>
seq uint

Returns

int

WriteSetScale(Span<byte>, uint, double)

Writes a SetScale frame (PositiveInfinity = unpaced). Returns the bytes written.

public static int WriteSetScale(Span<byte> destination, uint seq, double scale)

Parameters

destination Span<byte>
seq uint
scale double

Returns

int

Exceptions

ArgumentOutOfRangeException

scale is NaN or not positive.

WriteStep(Span<byte>, uint, uint)

Writes a Step frame. Returns the bytes written.

public static int WriteStep(Span<byte> destination, uint seq, uint steps)

Parameters

destination Span<byte>
seq uint
steps uint

Returns

int

Exceptions

ArgumentOutOfRangeException

steps is 0 or above MaxValue.