Class LiveControlProtocol
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
ControlHelloPayloadBytes
Payload size of ControlHello: u16 version, bytes32 mac.
public const int ControlHelloPayloadBytes = 34
Field Value
ControlStateFixedBytes
Fixed part of a ControlState payload, before the reason's UTF-8 bytes.
public const int ControlStateFixedBytes = 80
Field Value
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
LeasePingFrameBytes
Whole-frame size of LeasePing.
public const int LeasePingFrameBytes = 5
Field Value
LeasePingPayloadBytes
Payload size of LeasePing: empty.
public const int LeasePingPayloadBytes = 0
Field Value
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
SeqFrameBytes
public const int SeqFrameBytes = 9
Field Value
SeqPayloadBytes
public const int SeqPayloadBytes = 4
Field Value
SetScaleFrameBytes
Whole-frame size of SetScale.
public const int SetScaleFrameBytes = 17
Field Value
SetScalePayloadBytes
Payload size of SetScale: u32 seq, f64 scale.
public const int SetScalePayloadBytes = 12
Field Value
StepFrameBytes
Whole-frame size of Step.
public const int StepFrameBytes = 13
Field Value
StepPayloadBytes
Payload size of Step: u32 seq, u32 n.
public const int StepPayloadBytes = 8
Field Value
TokenBytes
Size of the control token: 256 bits.
public const int TokenBytes = 32
Field Value
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
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
typeLiveFrameType
Returns
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
tokenLiveControlTokennonceLiveControlToken
Returns
ControlStateFrameBytes(string)
Whole-frame size of a ControlState carrying reason.
public static int ControlStateFrameBytes(string reason)
Parameters
reasonstring
Returns
IsControlCommand(LiveFrameType)
Whether type is a Workbench→engine control frame.
public static bool IsControlCommand(LiveFrameType type)
Parameters
typeLiveFrameType
Returns
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
Returns
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
scaledouble
Returns
NewNonce()
A fresh random nonce from the OS's cryptographic generator.
public static LiveControlToken NewNonce()
Returns
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
typeLiveFrameTypepayloadReadOnlySpan<byte>commandLiveControlCommand
Returns
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
payloadReadOnlySpan<byte>stateLiveControlState
Returns
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
destinationSpan<byte>macLiveControlTokenversionushort
Returns
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
destinationSpan<byte>stateLiveControlState
Returns
WriteLeasePing(Span<byte>)
Writes a LeasePing frame. Returns the bytes written.
public static int WriteLeasePing(Span<byte> destination)
Parameters
Returns
WritePause(Span<byte>, uint)
Writes a Pause frame. Returns the bytes written.
public static int WritePause(Span<byte> destination, uint seq)
Parameters
Returns
WriteResume(Span<byte>, uint)
Writes a Resume frame. Returns the bytes written.
public static int WriteResume(Span<byte> destination, uint seq)
Parameters
Returns
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
Returns
Exceptions
- ArgumentOutOfRangeException
scaleis 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
Returns
Exceptions
- ArgumentOutOfRangeException
stepsis 0 or above MaxValue.