AlexStocks commented on code in PR #21:
URL: https://github.com/apache/dubbo-net/pull/21#discussion_r4463444922


##########
DubboService/Native/NativeDubboCodec.cs:
##########
@@ -101,11 +100,14 @@ internal static byte[] EncodeGenericRequest(
         internal static byte[] EncodeHeartbeat(long requestId)
         {
             byte[] frame = new byte[HeaderLength + 1];
-            frame[0] = 0xda;
-            frame[1] = 0xbb;
-            frame[2] = RequestFlag | TwoWayFlag | EventFlag | 
Hessian2SerializationId;
-            BinaryPrimitives.WriteInt64BigEndian(frame.AsSpan(4, 8), 
requestId);
-            BinaryPrimitives.WriteInt32BigEndian(frame.AsSpan(12, 4), 1);
+            frame[Frame.MagicHighByteOffset] = Frame.MagicHighByte;
+            frame[Frame.MagicLowByteOffset] = Frame.MagicLowByte;
+            frame[Frame.FlagsOffset] = Frame.RequestFlag | Frame.TwoWayFlag

Review Comment:
   💭 **偏移 3 现在有两个含义,建议在常量上点明**
   
   `StatusOffset = 3` 这个名字只在**响应**帧里成立。Dubbo2 的头部第 4 
个字节是复用的:请求帧里它是保留位(`DubboCodec` 写入 0),响应帧里才是 status(`RESPONSE_OK = 20` 等)。
   
   于是这次抽取之后出现了不对称:
   
   - `EncodeRequest`(第 88 行)显式写了 `frame[Frame.StatusOffset] = 0;`
   - `EncodeHeartbeat`(本处往上几行)**完全不写**这个字节,靠 `new byte[]` 的零初始化
   
   改之前两边都是裸字面量,没人会追问;改之后有了名字,读代码的人就会问「为什么 request 要清零、heartbeat 
不用」。这属于「命名暴露了原来藏起来的歧义」,值得在源头消掉。
   
   **Suggestion:** 把常量名或注释写明双重语义,并让三个编码器行为一致:
   
   ```csharp
   // 请求帧此字节为保留位且必须为 0;响应帧才是 status(OkStatus 等)。
   internal const int StatusOrReservedOffset = 3;
   ```
   
   然后在 `EncodeHeartbeat` 里也补一行 `frame[Frame.StatusOrReservedOffset] = 
0;`。虽然结果等价,但「靠零初始化」是一种需要读者自己推理才能确认的隐含契约——留给下一个改这段代码的人不合适。



##########
DubboService/Native/NativeDubboCodec.cs:
##########
@@ -35,21 +37,16 @@ namespace DubboNet.DubboService.Native
     /// </summary>
     internal static class NativeDubboCodec
     {
-        internal const int HeaderLength = 16;
+        internal const int HeaderLength = Frame.HeaderLength;

Review Comment:
   💭 **这个别名现在只服务测试了,定位建议写清楚(或直接删掉)**
   
   PR 描述说「Keep `NativeDubboCodec`'s existing internal aliases for current 
callers」,但实际调用方现在是:
   
   - `HeaderLength`:只剩本文件和 
`Tests/UnitTestForDubboNet/NativeDubboConnectionTest.cs`(3 处);**唯一的跨文件生产调用方 
`NativeDubboConnection` 已经改成 `Frame.HeaderLength` 了**(见 
`NativeDubboConnection.cs:212`)。
   - `OkStatus` / `Hessian2SerializationId`:`NativeDubboActuatorSuite.cs:202` 
和测试在用,这两个确实还需要留。
   
   也就是说 `HeaderLength` 这条别名已经没有生产调用方了。两个名字指同一个常量,正是这个 PR 在别处消除的漂移源——将来有人只改 
`Frame.HeaderLength`,本文件的 `HeaderLength` 因为 `const` 
链会自动跟随(同程序集编译期求值),所以目前没有实际风险;但它会让「到底该用哪个」变成一个需要考古的问题。
   
   **Suggestion(二选一):**
   
   1. 让 `NativeDubboConnectionTest` 改用 `Frame.HeaderLength`,然后删掉第 40 行——最干净;
   2. 保留,但把注释写成 `// 保留给已有测试引用;生产代码请直接用 DubboFrameConstants`。
   
   顺带:`HeaderLength` 是 `const int`,`Frame.HeaderLength` 
也是,所以这条赋值是**编译期折叠**的,不存在运行时开销,两条路都可以。



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to