Skip to content

C# - 协议类型

本页类型是 Nnrp.Core 提供的底层、与承载方式无关的模型。应用代码通常从 NnrpClientNnrpServer 开始。

csharp
using Nnrp.Core;

NnrpHeader

NnrpHeader 是不可变的 40 字节公共帧头。

属性类型含义
VersionMajorbyteNNRP 协议主版本
WireFormatbyte已选择线路格式
MessageTypeMessageType消息类型
HeaderLengthValuebyte编码帧头长度,必须为 40
FlagsHeaderFlags公共帧头标志
MetaLengthuintMetadata 字节数
BodyLengthuintBody 字节数
SessionIduint会话身份
FrameIduint帧身份
ViewIdushort视角身份
RouteIdushort路由身份
TraceIdulong追踪身份
csharp
public const int HeaderLength = 40;
public const byte CurrentVersionMajor = 1;
public const byte CurrentWireFormat = 0;

public void Write(Span<byte> destination);
public bool TryWrite(Span<byte> destination, out int bytesWritten);
public byte[] ToArray();

public static bool TryParse(ReadOnlySpan<byte> source, out NnrpHeader header);
public static bool TryParse(
    ReadOnlySpan<byte> source,
    NnrpHeaderParseOptions options,
    out NnrpHeader header,
    out NnrpParseError error);

处理不可信线路输入时必须使用 NnrpHeaderParseOptions.Strict。严格解析拒绝不支持的版本、未知 消息类型、保留标志、无效 wire format 和超限消息。

NnrpFramedMessage

NnrpFramedMessage 表示一个完整、与承载方式无关的帧。构造函数会验证帧头长度与传入内存区域一致。

csharp
public readonly struct NnrpFramedMessage
{
    public NnrpHeader Header { get; }
    public ReadOnlyMemory<byte> Metadata { get; }
    public ReadOnlyMemory<byte> Body { get; }
    public int Length { get; }

    public void CopyTo(Span<byte> destination);
    public bool TryCopyTo(Span<byte> destination, out int bytesWritten);
    public byte[] ToArray();

    public static bool TryParse(
        ReadOnlyMemory<byte> source,
        out NnrpFramedMessage message,
        out NnrpParseError error);
    public static bool TryParse(
        ReadOnlyMemory<byte> source,
        NnrpHeaderParseOptions options,
        out NnrpFramedMessage message,
        out NnrpParseError error);
}

NnrpProtocolFailure

NnrpProtocolFailure 保留 typed 协议错误、scope、诊断文本、fatal 标记,以及存在时的原始解析错误。

属性类型
IsFailurebool
ErrorCodeErrorCode
ScopeNnrpErrorScope
Messagestring
IsFatalbool
ParseErrorNnrpParseError
csharp
public static NnrpProtocolFailure None { get; }
public static NnrpProtocolFailure FromHeaderParseError(
    NnrpParseError parseError, string? message = null);
public static NnrpProtocolFailure FromBodyParseError(
    NnrpParseError parseError, string? message = null);
public static NnrpProtocolFailure InvalidState(
    NnrpErrorScope scope, string message, bool isFatal = false);
public static NnrpProtocolFailure UnsupportedCapability(
    string message, bool isFatal = true);
public static NnrpProtocolFailure LimitExceeded(
    NnrpErrorScope scope, string message, bool isFatal = false);

NnrpCapabilitySelection

NnrpCapabilitySelection 是协商后的协议能力集。传输选择属于 provider registry,不存放在此值中。

属性类型
CodecCodecId
DTypeDTypeId
TensorLayoutTensorLayoutId
PayloadKindBitmapuint
CacheObjectBitmapuint
DegradePoliciesBudgetPolicy
MaxViewsint
EnableCachebool
MaxCacheEntriesint
MaxConcurrentFramesint
MaxBodyBytesint
MaxSectionCountint
MaxTileCountint
TokenTtlSecondsint
AllowSessionRenewalbool

NnrpSessionStateMachine

底层 host 使用此状态机保证连接和会话顺序。

NnrpSessionState

含义
Init尚未开始协商
Negotiating正在握手
Active可以接受提交
Draining正在排空已有工作,拒绝新提交
Closed终态
csharp
public sealed class NnrpSessionStateMachine
{
    public NnrpSessionState State { get; }
    public NnrpProtocolFailure LastFailure { get; }

    public bool TryBeginNegotiation(out NnrpProtocolFailure failure);
    public bool TryActivate(out NnrpProtocolFailure failure);
    public bool TryFailNegotiation(
        NnrpProtocolFailure reason,
        out NnrpProtocolFailure failure);
    public bool TryBeginDraining(out NnrpProtocolFailure failure);
    public bool TryClose(out NnrpProtocolFailure failure);
    public bool TryAcceptFrameSubmit(out NnrpProtocolFailure failure);
    public void ApplyFailure(NnrpProtocolFailure failure);
}

缓存对象身份

Preview4 只使用 NnrpCacheObjectId 作为托管侧缓存身份。它包含 CacheNamespace、两个 64 位 key word 和 ObjectKind。缓存消息、lease、dependency、运行时对象引用 和本地 cache store 使用同一个值;公共 API 不再暴露第二套更窄的 key。

NnrpCacheStore

NnrpCacheStore 是应用侧、服务端本地缓存。它不能证明远端命中,也不能替代缓存引用协议消息。

csharp
public sealed class NnrpCacheStore
{
    public NnrpCacheStore(
        int maxEntries = 256,
        long maxObjectBytes = 16 * 1024 * 1024);

    public NnrpCacheResult TryGet(NnrpCacheObjectId objectId);
    public NnrpCacheResult TryPut(
        NnrpCacheObjectId objectId,
        ReadOnlyMemory<byte> objectBytes,
        uint ttlMilliseconds);
    public bool TryInvalidate(NnrpCacheObjectId objectId);
    public void Clear();
    public void EvictExpired();

    public int Count { get; }
    public int MaxEntries { get; set; }
    public long MaxObjectBytes { get; set; }
}

WARNING

客户端必须遵守 CacheAckCacheMiss、lease、version 和 invalidation 语义。本地字典查询不能证明 远端缓存命中。

NNRP Documentation