diff --git a/CHANGELOG.md b/CHANGELOG.md index b5e2d7c..f96d026 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ ## [Unreleased] +### Modern option semantics and lifecycle — 2026-11-13 + +- 按用户批准的兼容选择,默认 AddOption/codec 不再把 wire number 19 转成消息头 Token;旧 Token 名称保留并弃用,迁移到 Message.Token。QBlock1/2 仅登记现行名称/格式,默认未启用其处理器。 +- 显式 legacy codec 保留受测 Token 往返并拒绝 raw19 歧义,默认在所有受测旧符号构建下仍为 RFC7252;现代与 Draft18 端点缓存隔离。 +- 新增 endpoint 选项定义/注册,派发前按长度、方向、重复、UTF-8 和语义校验;critical 请求/响应按消息类型拒绝,非法 elective 被忽略,unknown elective 保留。默认不支持代理时返回5.05,真实代理宿主需显式启用 SupportsProxying。 +- 分块状态、计时器、事件登记在成功/错误/取消/超时及 endpoint Clear/Stop/Dispose 时回收,原调用方收到明确终态。终止通知保持锁外且异常清理继续执行;旧排队代次被拒绝,可重入清理不复活传输或丢失新请求。 +- 端点同时退休小请求和非分块观察,释放自有重传/重注册context,旧outbox绑定原始代次。Matcher按expected exchange原子删除Token/MID/URI,避免旧完成删掉replacement映射;远端未发response的清理不再空引用。 +- 四种终态事件(Cancelled/TimedOut/Rejected/Completed)即使较早用户回调抛出也通知后续清理,最后以AggregateException报告全部异常;非终态事件保持原合同。完整多播收集是既有缺口,单独跟踪#37,本批不宣称支持。 +- CoapClient 普通单播成功/失败共同仲裁一次终态,区分实际 Observe 通知、注销与正常 GET fallback。异步客户端回归改为等待实际响应信号,保留原断言并去除固定100ms协调假设。 +- 兼容影响、受测 legacy 范围及未完成的 Token 生成/无符号 getter/大 Max-Age 见[迁移合同](docs/coap-options-compatibility.md);新包发行、AOT运行、真实peer与现场资格独立验收。 + ### Fixed — UDP interoperability audit (2026-11-13) - 按 RFC 7252 严格拒绝截断头、Token、option、无 payload 的 marker 和非法 Empty/type/code 组合;修复版本自比较,格式失败后的 decoder 保持失败状态。可辨识的畸形 CON 使用匹配 MID 的 RST,ACK/RST、未知版本和短头不反射回复。 diff --git a/CoAP.NET/CoapClient.cs b/CoAP.NET/CoapClient.cs index 34a72bd..89da4cb 100644 --- a/CoAP.NET/CoapClient.cs +++ b/CoAP.NET/CoapClient.cs @@ -391,15 +391,25 @@ public Response Send(Request request) public void SendAsync(Request request, Action done = null, Action fail = null) { - Int32 cancellationReported = 0; - request.Respond += (o, e) => Deliver(done, e); - request.Rejected += (o, e) => Fail(fail, FailReason.Rejected); - request.TimedOut += (o, e) => Fail(fail, FailReason.TimedOut); - request.Cancelled += (o, e) => + Int32 terminalReported = 0; + Boolean multicast = request.Multicast; + Boolean observeRegistration = request.Method == Method.GET && request.Observe == 0; + Action reportFailure = reason => { - if (System.Threading.Interlocked.Exchange(ref cancellationReported, 1) == 0) - Fail(fail, FailReason.Cancelled); + if (System.Threading.Interlocked.Exchange(ref terminalReported, 1) == 0) + Fail(fail, reason); }; + request.Respond += (o, e) => + { + Boolean ongoing = multicast || (observeRegistration && e.Response.HasOption(OptionType.Observe)); + if (ongoing + ? System.Threading.Volatile.Read(ref terminalReported) == 0 + : System.Threading.Interlocked.Exchange(ref terminalReported, 1) == 0) + Deliver(done, e); + }; + request.Rejected += (o, e) => reportFailure(FailReason.Rejected); + request.TimedOut += (o, e) => reportFailure(FailReason.TimedOut); + request.Cancelled += (o, e) => reportFailure(FailReason.Cancelled); Prepare(request).Send(); } diff --git a/CoAP.NET/Message.cs b/CoAP.NET/Message.cs index 1767f12..5d229e8 100644 --- a/CoAP.NET/Message.cs +++ b/CoAP.NET/Message.cs @@ -395,7 +395,7 @@ protected virtual void OnAcknowledged() /// protected virtual void OnRejected() { - Fire(Rejected); + DispatchTerminalEvent(Rejected, this); } /// @@ -403,7 +403,7 @@ protected virtual void OnRejected() /// protected virtual void OnTimedOut() { - Fire(TimedOut); + DispatchTerminalEvent(TimedOut, this); } /// @@ -411,7 +411,33 @@ protected virtual void OnTimedOut() /// protected virtual void OnCanceled() { - Fire(Cancelled); + DispatchTerminalEvent(Cancelled, this); + } + + internal static void DispatchTerminalEvent(EventHandler handlers, Object sender) + { + if (handlers == null) + return; + + List failures = null; + // Preserve the event invocation snapshot and registration order. + // A user callback must not prevent later lifecycle owners from cleanup. + foreach (EventHandler callback in handlers.GetInvocationList()) + { + try + { + callback(sender, EventArgs.Empty); + } + catch (Exception error) + { + if (failures == null) + failures = new List(); + failures.Add(error); + } + } + + if (failures != null) + throw new AggregateException("A terminal event callback failed.", failures).Flatten(); } internal void FireRetransmitting() @@ -1063,21 +1089,14 @@ private IEnumerable SelectOptions(OptionType optionType, Func f } /// - /// Adds an option to the list of options of this CoAP message. + /// 按现行 CoAP option number 添加选项;报文头 Token 只能通过 设置。 /// - /// the option to add + /// 要添加的原始协议选项。 public Message AddOption(Option option) { if (option == null) throw new ArgumentNullException("opt"); - if (option.Type == OptionType.Token) - { - // be compatible with draft 13- - Token = option.RawValue; - return this; - } - LinkedList