Skip to content

C# — Server API

C# server API 以 session 为中心:接受握手、接收提交、发送结果或 drop、关闭。

导入

csharp
using Nnrp.Server;
using Nnrp.Core;

Server 使用流程

  1. 构造 ServerProfile
  2. 为已接受连接创建 INnrpMessageTransport
  3. 构造 NnrpServerSession,调用 AcceptAsync
  4. 循环调用 ReceiveSubmitAsync
  5. SendResultAsyncSendResultDropAsync 回答。
  6. CloseAsync 关闭。

NnrpServerSession

构造函数

参数类型必填取值 / 范围说明
profileServerProfile非空服务端 capability 和限制。
transportINnrpMessageTransport已接受连接当前 peer 的 framed transport。
sessionIdAllocatorFunc<uint, uint>?默认 echo-or-onesession id 分配函数。
cacheStoreNnrpCacheStore?可选启用 cache 消息处理。
返回可能抛出
NnrpServerSessionArgumentNullException
csharp
var session = new NnrpServerSession(profile, transport);

NnrpServerSession.AcceptAsync

接收 CLIENT_HELLO,协商 capability,发送 SERVER_HELLO_ACK

参数类型必填取值 / 范围说明
cancellationTokenCancellationToken任意 token取消接收或发送。
返回可能抛出
NnrpProtocolFailuretransport 异常;协商失败通过返回值表达。
csharp
var failure = await session.AcceptAsync(ct);
if (failure.IsFailure) return;

NnrpServerSession.ReceiveSubmitAsync

接收并解析下一帧提交。

参数类型必填取值 / 范围说明
cancellationTokenCancellationToken任意 token取消接收。
返回可能抛出
NnrpFrameSubmitclose、submit 格式错误、session mismatch、生命周期错误。
csharp
var submit = await session.ReceiveSubmitAsync(ct);

NnrpServerSession.SendResultAsync

发送提交帧的结果。

参数类型必填取值 / 范围说明
resultNnrpResultFrameId 匹配已提交帧要序列化为 RESULT_PUSH 的结果。
cancellationTokenCancellationToken任意 token取消发送。
返回可能抛出
ValueTask生命周期、关联、序列化或 transport 错误。
csharp
await session.SendResultAsync(result, ct);

NnrpServerSession.SendResultDropAsync

发送 RESULT_DROP

参数类型必填取值 / 范围说明
dropMessageResultDropMessage必须匹配当前 sessiondrop 消息。
cancellationTokenCancellationToken任意 token取消发送。
返回可能抛出
ValueTask生命周期、关联或 transport 错误。
csharp
await session.SendResultDropAsync(ResultDropMessage.Create(session.SessionId, submit.FrameId), ct);

核心类型

ServerProfile

属性类型默认值说明
MaxConcurrentFramesint1协议层 in-flight 限制。
EnableCachebooltrue是否启用 cache 协商。
MaxSectionsint16每帧最大 section 数。
MaxBodyBytesint33554432最大请求 body 字节数。
ModelNamestring""握手返回的模型名。

NnrpFrameSubmit

属性类型说明
SessionIduintsession id。
FrameIduint提交 frame id。
ViewIdushortview id。
TraceIdulongtrace id。
CameraBlockReadOnlyMemory<byte>camera metadata。
TileIdsReadOnlyMemory<ushort>tile id。
SectionsReadOnlyMemory<TensorSectionBlock>tensor sections。
FrameClassFrameClassframe class。
InputProfileInputProfileinput profile。

NnrpResult

属性类型必填说明
FrameIduint要回答的 frame。
TileIdsReadOnlyMemory<ushort>结果 tile id。
SectionsReadOnlyMemory<TensorSectionBlock>结果 tensor sections。
ViewIdushort默认 0
TraceIdulong默认 0
ResultClassResultClass完整性分类。
ResultFlagsResultFlags结果 flags。
AppliedBudgetPolicyBudgetPolicy实际使用的降级策略。

示例

csharp
var session = new NnrpServerSession(profile, transport);
var failure = await session.AcceptAsync(ct);
if (!failure.IsFailure)
{
    var submit = await session.ReceiveSubmitAsync(ct);
    var result = await RunInferenceAsync(submit, ct);
    await session.SendResultAsync(result, ct);
}

常见坑

WARNING

  1. 每个收到的 frame 都需要 result 或 drop。
  2. 不要在 I/O loop 里阻塞推理。
  3. 进入 submit loop 前检查 AcceptAsync 返回值。
  4. Cache helper 需要配置 NnrpCacheStore

NNRP Documentation