Skip to content

C# — Client API

The C# client API is centered on NnrpClient: connect, submit, receive session events, migrate when needed, and close. Low-level protocol objects remain available, but application code should start from the methods below.

Imports

csharp
using Nnrp.Client;
using Nnrp.Core;

Client Workflow

  1. Create a ClientProfile.
  2. Create an INnrpMessageTransport or let a bridge/bootstrap helper choose one.
  3. Construct NnrpClient and call ConnectAsync.
  4. Submit with SubmitAsync or send first and await later with SendSubmitAsync plus ReceiveResultAsync.
  5. Read control events with ReceiveNextEventAsync when your app needs flow updates or result hints.
  6. Close with CloseAsync.

NnrpClient

Top-level client over a selected message transport.

Constructor

ParameterTypeRequiredValues / RangeDescription
profileClientProfileYesNon-nullClient capabilities and preferences.
transportINnrpMessageTransportYesConnected transportTCP, QUIC bridge, or custom framed transport.
ReturnsRaises
NnrpClientArgumentNullException when required arguments are null.
csharp
var client = new NnrpClient(profile, transport);

NnrpClient.ConnectAsync

Sends CLIENT_HELLO, validates SERVER_HELLO_ACK, and activates the session state.

ParameterTypeRequiredValues / RangeDescription
requestedSessionIduintNo0 lets the server allocateRequested session id.
traceIdulongNoAny trace idTrace correlation value.
cancellationTokenCancellationTokenNoDefaults to defaultCancels transport I/O.
ReturnsRaises
NnrpClientConnectResultTransport exceptions; malformed ack is returned as a failed result when possible.
csharp
var connect = await client.ConnectAsync(requestedSessionId: 1, cancellationToken: ct);
if (!connect.IsConnected)
{
    throw new InvalidOperationException(connect.Failure.ToString());
}

NnrpClient.SubmitAsync

Submits one frame and waits for the matching RESULT_PUSH.

ParameterTypeRequiredValues / RangeDescription
submitRequestNnrpSubmitRequestYesFrameId must be unique while in flightStructured inline tensor submit request.
cancellationTokenCancellationTokenNoDefaults to defaultCancels send or receive.
ReturnsRaises
NnrpSubmitResultTransport exceptions, InvalidOperationException for drops or correlation errors.
csharp
var result = await client.SubmitAsync(new NnrpSubmitRequest(
    frameId: 1,
    sourceWidth: 1920,
    sourceHeight: 1080,
    tileWidth: 256,
    tileHeight: 256,
    cameraBlock: cameraBytes,
    tileIds: tileIds,
    sections: tensorSections), ct);

NnrpClient.SendSubmitAsync

Sends a frame and returns after the packet is written. Use this for multiple in-flight frames.

ParameterTypeRequiredValues / RangeDescription
submitRequestNnrpSubmitRequestYesFrameId must be unique while in flightRequest to serialize and send.
cancellationTokenCancellationTokenNoDefaults to defaultCancels send.
ReturnsRaises
NnrpSubmittedFrameSerialization, transport, or duplicate in-flight frame errors.
csharp
var submitted = await client.SendSubmitAsync(request, ct);

NnrpClient.ReceiveResultAsync

Waits for the result matching a previously submitted frame.

ParameterTypeRequiredValues / RangeDescription
expectedFrameIduintYesExisting in-flight frameFrame id to match.
expectedViewIdushortNoDefaults to 0View id to match.
cancellationTokenCancellationTokenNoDefaults to defaultCancels receive.
ReturnsRaises
ResultPushMessageDrop, malformed packet, session mismatch, or correlation errors.
csharp
var resultMessage = await client.ReceiveResultAsync(submitted.FrameId, submitted.ViewId, ct);

NnrpClient.ReceiveNextEventAsync

Reads the next session event, including result pushes, result drops, flow updates, and result hints.

ParameterTypeRequiredValues / RangeDescription
cancellationTokenCancellationTokenNoDefaults to defaultCancels receive.
ReturnsRaises
NnrpSessionEventTransport or parse errors.
csharp
var sessionEvent = await client.ReceiveNextEventAsync(ct);
if (sessionEvent.MessageType == MessageType.FlowUpdate)
{
    ApplyBackpressure(sessionEvent.FlowUpdate);
}

NnrpClient.CloseAsync

Sends CLOSE for active sessions and clears local in-flight state.

ParameterTypeRequiredValues / RangeDescription
reasonstringNoDefaults to ""Human-readable close reason.
traceIdulongNoAny trace idTrace correlation value.
cancellationTokenCancellationTokenNoDefaults to defaultCancels send.
ReturnsRaises
NnrpProtocolFailureTransport errors.
csharp
await client.CloseAsync("shutdown", cancellationToken: ct);

Native Runtime Bridge

Nnrp.NativeBridge exposes Rust-backed host facades for client sessions, shared client connections, and server sessions. Use the TCP or QUIC runtime package to bind the host facade to a specific transport slot.

NnrpNativeRuntimeConnectionHost.OpenSession

ParameterTypeRequiredValues / RangeDescription
optionsNnrpNativeRuntimeSessionOptionsYesSession id, generation, profile id, schema id, schema versionOpens a native-backed session on an existing native connection host.
ReturnsRaises
NnrpNativeRuntimeSessionNative artifact load, connection, session, or disposal errors.
csharp
using var connection = NnrpNativeQuicRuntime.OpenConnectionHost(
    new NnrpNativeQuicRuntimeConnectionHostOptions(connectionId: 1, connectionGeneration: 1));

using var session = connection.OpenSession(
    new NnrpNativeRuntimeSessionOptions(sessionId: 1, sessionGeneration: 1, profileId: 1, schemaId: 1, schemaVersion: 1));

Core Types

ClientProfile

Client capabilities sent during handshake.

PropertyTypeDefaultDescription
TransportPolicyTransportPolicyPreferQuic or repo defaultTransport preference.
SessionLossToleranceLossToleranceRepo defaultAccepted loss policy.
MaxViewsint1Maximum concurrent views.
EnableCachebooltrueWhether cache support is requested.
MaxCacheEntriesint256Requested cache entry count.
SupportedCodecsCodecId[]Standard setCodec capability bitmap.
SupportedDTypesDTypeId[]Standard setTensor dtype capability bitmap.
SupportedTensorLayoutsTensorLayoutId[]Standard setTensor layout capability bitmap.

NnrpSubmitRequest

Inline tensor submit request.

PropertyTypeRequiredDescription
FrameIduintYesUnique frame id while in flight.
SourceWidth / SourceHeightushortYesSource dimensions.
TileWidth / TileHeightushortYesTile dimensions.
CameraBlockReadOnlyMemory<byte>YesCamera metadata block.
TileIdsReadOnlyMemory<ushort>YesTile ids encoded according to TileIndexMode.
SectionsReadOnlyMemory<TensorSectionBlock>YesTensor payload sections.
ViewIdushortNoDefaults to 0.
TraceIdulongNoDefaults to 0.
FrameClassFrameClassNoDefaults to Keyframe.
InputProfileInputProfileNoDefaults to DenseLumaFrame.
TileIndexModeTileIndexModeNoDefaults to RawUInt16.
LatencyBudgetMillisecondsushortNoDefaults to 16.
CadenceHintX100ushortNoFPS times 100; 0 means unspecified.
DependencyFrameIduintNoDefaults to 0.
TileBaseIduintNoDefaults to 0.

NnrpSubmitResult

Structured result returned by SubmitAsync.

PropertyTypeDescription
SessionIduintNegotiated session id.
FrameIduintResult frame id.
ViewIdushortResult view id.
StatusCodeResultStatusCodeResult status.
ResultClassResultClassCompleteness class.
ResultFlagsResultFlagsResult flags.
InferenceMillisecondsushortModel execution time.
QueueMillisecondsushortQueue wait time.
ServerTotalMillisecondsushortTotal server-side time.
TileIdsReadOnlyMemory<ushort>Result tile ids.
SectionsReadOnlyMemory<TensorSectionBlock>Result tensor sections.
TypedPayloadFramesReadOnlyMemory<TypedPayloadFrameView>Non-tensor result payloads.

NnrpClientConnectResult

PropertyTypeDescription
IsConnectedbooltrue when negotiation succeeded.
NegotiationResultNnrpCapabilityNegotiationResultAccepted or rejected capability negotiation details.
FailureNnrpProtocolFailureFailure details when not connected.

NnrpSubmittedFrame

PropertyTypeDescription
SessionIduintSession id used for the submit.
FrameIduintSubmitted frame id.
ViewIdushortSubmitted view id.
TraceIdulongTrace id.
WireFormatbyteCurrent NNRP wire format.

NnrpSessionEvent

Event returned by ReceiveNextEventAsync.

PropertyTypeDescription
MessageTypeMessageTypeEvent packet type.
ResultPushResultPushMessageValid when IsResultPush is true.
ResultDropResultDropMessageValid when IsResultDrop is true.
FlowUpdateFlowUpdateMessageValid when IsFlowUpdate is true.
ResultHintResultHintMessageValid when IsResultHint is true.

Common Pitfalls

WARNING

  1. NnrpClient does not own arbitrary transport creation; construct or select the transport first.
  2. FrameId plus ViewId must be unique while in flight.
  3. Use ReceiveNextEventAsync when the server may send flow updates or result hints between results.
  4. Always call CloseAsync and dispose the underlying transport or bridge. :::

NNRP Documentation