Skip to content

C# — Server API

The C# server API is session-oriented: accept the handshake, receive submits, send results or drops, and close. This page documents the application-facing methods first and keeps message types as linked references.

Imports

csharp
using Nnrp.Server;
using Nnrp.Core;

Server Workflow

  1. Create a ServerProfile.
  2. Create an INnrpMessageTransport for an accepted connection.
  3. Construct NnrpServerSession and call AcceptAsync.
  4. Loop on ReceiveSubmitAsync.
  5. Respond with SendResultAsync or SendResultDropAsync.
  6. Close with CloseAsync.

NnrpServerSession

Default server session implementation.

Constructor

ParameterTypeRequiredValues / RangeDescription
profileServerProfileYesNon-nullServer capabilities and limits.
transportINnrpMessageTransportYesAccepted connectionFramed transport for this peer.
sessionIdAllocatorFunc<uint, uint>?NoDefaults to echo-or-oneMaps requested ids to server session ids.
cacheStoreNnrpCacheStore?NoOptionalEnables cache message handling.
ReturnsRaises
NnrpServerSessionArgumentNullException for required arguments.
csharp
var session = new NnrpServerSession(profile, transport);

NnrpServerSession.AcceptAsync

Receives CLIENT_HELLO, negotiates capabilities, sends SERVER_HELLO_ACK, and activates the session.

ParameterTypeRequiredValues / RangeDescription
cancellationTokenCancellationTokenYesAny tokenCancels receive or send.
ReturnsRaises
NnrpProtocolFailureTransport exceptions; negotiation failures are returned.
csharp
var failure = await session.AcceptAsync(ct);
if (failure.IsFailure)
{
    return;
}

NnrpServerSession.ReceiveSubmitAsync

Receives and parses the next frame submission.

ParameterTypeRequiredValues / RangeDescription
cancellationTokenCancellationTokenYesAny tokenCancels receive.
ReturnsRaises
NnrpFrameSubmitClose, malformed submit, session mismatch, lifecycle errors.
csharp
var submit = await session.ReceiveSubmitAsync(ct);

NnrpServerSession.SendResultAsync

Sends a result for a submitted frame.

ParameterTypeRequiredValues / RangeDescription
resultNnrpResultYesFrameId must match a submitted frameStructured result to serialize as RESULT_PUSH.
cancellationTokenCancellationTokenYesAny tokenCancels send.
ReturnsRaises
ValueTaskLifecycle, correlation, serialization, or transport errors.
csharp
await session.SendResultAsync(new NnrpResult(
    frameId: submit.FrameId,
    viewId: submit.ViewId,
    traceId: submit.TraceId,
    tileIds: submit.TileIds,
    sections: outputSections), ct);

NnrpServerSession.SendResultDropAsync

Sends RESULT_DROP for a frame that will not produce a result.

ParameterTypeRequiredValues / RangeDescription
dropMessageResultDropMessageYesMust match the active sessionDrop message to send.
cancellationTokenCancellationTokenYesAny tokenCancels send.
ReturnsRaises
ValueTaskLifecycle, correlation, or transport errors.
csharp
await session.SendResultDropAsync(ResultDropMessage.Create(session.SessionId, submit.FrameId), ct);

NnrpServerSession.CloseAsync

Gracefully closes an active session.

ParameterTypeRequiredValues / RangeDescription
reasonstringYesEmpty string allowedClose reason.
traceIdulongYesAny trace idTrace correlation value.
cancellationTokenCancellationTokenYesAny tokenCancels send.
ReturnsRaises
NnrpProtocolFailureTransport errors.
csharp
await session.CloseAsync("shutdown", traceId: 0, ct);

Core Types

ServerProfile

Server capability and limit configuration.

PropertyTypeDefaultDescription
MaxConcurrentFramesint1Advertised in-flight frame limit.
EnableCachebooltrueEnables cache negotiation.
MaxSectionsint16Maximum sections per frame.
MaxBodyBytesint33554432Maximum request body size.
ModelNamestring""Model name returned in the handshake when configured.

NnrpFrameSubmit

Structured frame submission returned by ReceiveSubmitAsync.

PropertyTypeDescription
SessionIduintSession id.
FrameIduintSubmitted frame id.
ViewIdushortSubmitted view id.
TraceIdulongTrace id.
SourceWidth / SourceHeightushortSource dimensions.
TileWidth / TileHeightushortTile dimensions.
CameraBlockReadOnlyMemory<byte>Camera metadata block.
TileIdsReadOnlyMemory<ushort>Submitted tile ids.
SectionsReadOnlyMemory<TensorSectionBlock>Tensor sections.
FrameClassFrameClassFrame class.
InputProfileInputProfileInput profile.

NnrpResult

Structured result accepted by SendResultAsync.

PropertyTypeRequiredDescription
FrameIduintYesFrame id being answered.
ViewIdushortYesView id being answered.
TraceIdulongNoTrace id.
TileIdsReadOnlyMemory<ushort>NoResult tile ids.
SectionsReadOnlyMemory<TensorSectionBlock>NoResult tensor sections.
ResultClassResultClassNoCompleteness class.
ResultFlagsResultFlagsNoResult flags.
AppliedBudgetPolicyBudgetPolicyNoDegradation actually used.
InferenceMillisecondsushortNoModel execution time.
QueueMillisecondsushortNoQueue wait time.
ServerTotalMillisecondsushortNoTotal server-side time.

Example

csharp
async Task HandleAsync(INnrpMessageTransport transport, CancellationToken ct)
{
    var session = new NnrpServerSession(new ServerProfile { MaxConcurrentFrames = 4 }, transport);
    var failure = await session.AcceptAsync(ct);
    if (failure.IsFailure)
    {
        return;
    }

    try
    {
        while (true)
        {
            var submit = await session.ReceiveSubmitAsync(ct);
            var output = await RunInferenceAsync(submit, ct);
            await session.SendResultAsync(output, ct);
        }
    }
    finally
    {
        await session.CloseAsync("server shutdown", 0, ct);
    }
}

Common Pitfalls

WARNING

  1. Every received frame needs SendResultAsync or SendResultDropAsync.
  2. Do not block the I/O loop while running inference; move CPU/GPU work out of the receive path.
  3. AcceptAsync returns protocol rejection information; check it before entering the submit loop.
  4. Cache helpers require a configured NnrpCacheStore.

NNRP Documentation