Skip to content

C# — Client API

C# client API 以 NnrpClient 为入口:连接、提交、接收事件、迁移、关闭。低层协议对象保留,但应用代码应优先从这些方法开始。

导入

csharp
using Nnrp.Client;
using Nnrp.Core;

Client 使用流程

  1. 构造 ClientProfile
  2. 创建 INnrpMessageTransport,或交给 bridge/bootstrap helper 选择。
  3. 构造 NnrpClient,调用 ConnectAsync
  4. 简单请求用 SubmitAsync;多帧并发用 SendSubmitAsync + ReceiveResultAsync
  5. 需要 flow update / result hint 时调用 ReceiveNextEventAsync
  6. CloseAsync 关闭。

NnrpClient

构造函数

参数类型必填取值 / 范围说明
profileClientProfile非空客户端 capability 和偏好。
transportINnrpMessageTransport已连接 transportTCP、QUIC bridge 或自定义 framed transport。
返回可能抛出
NnrpClientArgumentNullException
csharp
var client = new NnrpClient(profile, transport);

NnrpClient.ConnectAsync

发送 CLIENT_HELLO,校验 SERVER_HELLO_ACK,激活 session state。

参数类型必填取值 / 范围说明
requestedSessionIduint0 表示服务端分配请求的 session id。
traceIdulong任意 trace id追踪关联值。
cancellationTokenCancellationToken默认 default取消 transport I/O。
返回可能抛出
NnrpClientConnectResulttransport 异常;部分握手错误会体现在失败结果里。
csharp
var connect = await client.ConnectAsync(requestedSessionId: 1, cancellationToken: ct);
if (!connect.IsConnected)
{
    throw new InvalidOperationException(connect.Failure.ToString());
}

NnrpClient.SubmitAsync

提交一帧并等待匹配的 RESULT_PUSH

参数类型必填取值 / 范围说明
submitRequestNnrpSubmitRequestFrameId 在 in-flight 中唯一结构化 inline tensor 请求。
cancellationTokenCancellationToken默认 default取消发送或接收。
返回可能抛出
NnrpSubmitResulttransport、drop 或关联错误。
csharp
var result = await client.SubmitAsync(request, ct);

NnrpClient.SendSubmitAsync

只发送,不等待结果。

参数类型必填取值 / 范围说明
submitRequestNnrpSubmitRequestFrameId 在 in-flight 中唯一要序列化并发送的请求。
cancellationTokenCancellationToken默认 default取消发送。
返回可能抛出
NnrpSubmittedFrame序列化、transport 或重复 frame 错误。
csharp
var submitted = await client.SendSubmitAsync(request, ct);

NnrpClient.ReceiveResultAsync

等待之前提交帧的结果。

参数类型必填取值 / 范围说明
expectedFrameIduint已 in-flight 的 frame目标 frame id。
expectedViewIdushort默认 0目标 view id。
cancellationTokenCancellationToken默认 default取消接收。
返回可能抛出
ResultPushMessagedrop、包格式错误、session mismatch 或关联错误。
csharp
var resultMessage = await client.ReceiveResultAsync(submitted.FrameId, submitted.ViewId, ct);

NnrpClient.ReceiveNextEventAsync

接收下一条 session event,包括 result、drop、flow update、result hint。

参数类型必填取值 / 范围说明
cancellationTokenCancellationToken默认 default取消接收。
返回可能抛出
NnrpSessionEventtransport 或解析错误。
csharp
var sessionEvent = await client.ReceiveNextEventAsync(ct);

NnrpClient.CloseAsync

发送 CLOSE 并清理本地 in-flight 状态。

参数类型必填取值 / 范围说明
reasonstring默认 ""关闭原因。
traceIdulong任意 trace id追踪关联值。
cancellationTokenCancellationToken默认 default取消发送。
返回可能抛出
NnrpProtocolFailuretransport 错误。
csharp
await client.CloseAsync("shutdown", cancellationToken: ct);

核心类型

ClientProfile

属性类型默认值说明
TransportPolicyTransportPolicy仓库默认值transport 偏好。
SessionLossToleranceLossTolerance仓库默认值可接受 loss 策略。
MaxViewsint1最大并发 view。
EnableCachebooltrue是否请求 cache。
MaxCacheEntriesint256请求的 cache 条目数。
SupportedCodecsCodecId[]标准集合codec capability。
SupportedDTypesDTypeId[]标准集合dtype capability。
SupportedTensorLayoutsTensorLayoutId[]标准集合layout capability。

NnrpSubmitRequest

属性类型必填说明
FrameIduintin-flight 内唯一。
SourceWidth / SourceHeightushort源尺寸。
TileWidth / TileHeightushorttile 尺寸。
CameraBlockReadOnlyMemory<byte>camera metadata。
TileIdsReadOnlyMemory<ushort>tile id。
SectionsReadOnlyMemory<TensorSectionBlock>tensor sections。
ViewIdushort默认 0
TraceIdulong默认 0
FrameClassFrameClass默认 Keyframe
InputProfileInputProfile默认 DenseLumaFrame
TileIndexModeTileIndexMode默认 RawUInt16
LatencyBudgetMillisecondsushort默认 16

NnrpSubmitResult

属性类型说明
SessionIduint协商后的 session id。
FrameIduint结果 frame id。
ViewIdushort结果 view id。
StatusCodeResultStatusCode结果状态。
ResultClassResultClass完整性分类。
ResultFlagsResultFlags结果 flags。
InferenceMillisecondsushort推理耗时。
TileIdsReadOnlyMemory<ushort>结果 tile id。
SectionsReadOnlyMemory<TensorSectionBlock>结果 tensor sections。

NnrpClientConnectResult

属性类型说明
IsConnectedboolcapability negotiation 成功时为 true
NegotiationResultNnrpCapabilityNegotiationResultcapability negotiation 细节。
FailureNnrpProtocolFailure未连接时的失败信息。

NnrpSubmittedFrame

属性类型说明
SessionIduintsubmit 使用的 session id。
FrameIduint已提交 frame id。
ViewIdushortview id。
TraceIdulongtrace id。
WireFormatbyte当前 NNRP wire format。

常见坑

WARNING

  1. NnrpClient 不负责创建任意 transport;先选择或构造 transport。
  2. FrameId + ViewId 在 in-flight 内必须唯一。
  3. 服务端可能插入 flow update / result hint 时,使用 ReceiveNextEventAsync
  4. 关闭 client 后也要释放底层 transport 或 bridge。

NNRP Documentation