NeverENG commented on code in PR #18:
URL: https://github.com/apache/dubbo-net/pull/18#discussion_r4463615313


##########
DubboService/Native/Hessian2/Hessian2Tags.cs:
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace DubboNet.DubboService.Native.Hessian2
+{
+    // Wire-format values used by the dynamic Hessian2 response reader.
+    internal static class Hessian2Tags
+    {
+        internal const byte Null = (byte)'N';
+        internal const byte True = (byte)'T';
+        internal const byte False = (byte)'F';
+        internal const byte End = (byte)'Z';
+        internal const byte Reference = 0x51;
+
+        internal const byte Int = (byte)'I';
+        internal const byte CompactIntFirstTag = 0x80;
+        internal const byte CompactIntLastTag = 0xbf;
+        internal const byte CompactIntZeroTag = 0x90;
+        internal const byte CompactIntTwoByteFirstTag = 0xc0;
+        internal const byte CompactIntTwoByteLastTag = 0xcf;
+        internal const byte CompactIntTwoByteZeroTag = 0xc8;
+        internal const byte CompactIntThreeByteFirstTag = 0xd0;
+        internal const byte CompactIntThreeByteLastTag = 0xd7;
+        internal const byte CompactIntThreeByteZeroTag = 0xd4;
+
+        internal const byte Long = (byte)'L';
+        internal const byte LongAsInt = 0x59;
+        internal const byte CompactLongFirstTag = 0xd8;
+        internal const byte CompactLongLastTag = 0xef;
+        internal const byte CompactLongZeroTag = 0xe0;
+        internal const byte CompactLongTwoByteFirstTag = 0xf0;
+        internal const byte CompactLongTwoByteLastTag = 0xff;
+        internal const byte CompactLongTwoByteZeroTag = 0xf8;
+        internal const byte CompactLongThreeByteFirstTag = 0x38;
+        internal const byte CompactLongThreeByteLastTag = 0x3f;
+        internal const byte CompactLongThreeByteZeroTag = 0x3c;
+
+        internal const byte Double = (byte)'D'; // 0x44
+        internal const byte DoubleZero = 0x5b;
+        internal const byte DoubleOne = 0x5c;
+        internal const byte DoubleAsByte = 0x5d;
+        internal const byte DoubleAsShort = 0x5e;
+        internal const byte DoubleAsMillisecond = 0x5f;

Review Comment:
   已在 `97c88b0` 给 `DoubleAsMillisecond` 加上 Dubbo hessian-lite `BC_DOUBLE_MILL` 
与 Caucho bytecode map 的差异说明;先在 `bddbbd8` 加了 2.5、0.25 的独立 wire-byte 用例。新 head 
等维护者批准 CI。



##########
DubboService/Native/Hessian2/Hessian2ByteCursor.cs:
##########
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Buffers.Binary;
+using System.IO;
+
+namespace DubboNet.DubboService.Native.Hessian2
+{
+    // Owns the input buffer and cursor; Hessian2 tags and decoded values 
belong to the caller.
+    internal sealed class Hessian2ByteCursor
+    {
+        private readonly byte[] _buffer;
+        private int _position;
+
+        internal Hessian2ByteCursor(byte[] buffer)
+        {
+            _buffer = buffer ?? throw new 
ArgumentNullException(nameof(buffer));
+        }
+
+        internal int Remaining => _buffer.Length - _position;
+
+        internal byte PeekByte()
+        {
+            EnsureAvailable(1);
+            return _buffer[_position];
+        }
+
+        internal byte ReadByte()
+        {
+            EnsureAvailable(1);
+            return _buffer[_position++];
+        }
+
+        internal int ReadUInt16()
+        {
+            EnsureAvailable(2);
+            int value = 
BinaryPrimitives.ReadUInt16BigEndian(_buffer.AsSpan(_position, 2));
+            _position += 2;
+            return value;
+        }
+
+        internal int ReadInt32()
+        {
+            EnsureAvailable(4);
+            int value = 
BinaryPrimitives.ReadInt32BigEndian(_buffer.AsSpan(_position, 4));
+            _position += 4;
+            return value;
+        }
+
+        internal long ReadInt64()
+        {
+            EnsureAvailable(8);
+            long value = 
BinaryPrimitives.ReadInt64BigEndian(_buffer.AsSpan(_position, 8));
+            _position += 8;
+            return value;
+        }
+
+        internal byte[] ReadBytes(int count)
+        {
+            EnsureAvailable(count);
+            byte[] result = new byte[count];
+            Buffer.BlockCopy(_buffer, _position, result, 0, count);
+            _position += count;
+            return result;
+        }
+
+        internal void CopyTo(Stream destination, int count)

Review Comment:
   采纳:`97c88b0` 将游标的 `CopyTo(Stream, int)` 改为 `ReadSpan(int)`,由 reader 决定写入 
`MemoryStream`;`ReadBytes` 保持返回拷贝。游标仍用 `System.IO.EndOfStreamException` 
表达原有越界契约,但不再依赖 Stream 目的地。



##########
Tests/UnitTestForDubboNet/Hessian2ByteCursorTest.cs:
##########
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using DubboNet.DubboService.Native.Hessian2;
+
+namespace UnitTestForDubboNet;
+
+public class Hessian2ByteCursorTest
+{
+    [Fact]
+    public void PeekDoesNotAdvanceAndSequentialReadsConsumeExactlyOneByte()
+    {
+        var input = new Hessian2ByteCursor(new byte[] { 0x80, 0x01 });
+        Assert.Equal(2, input.Remaining);
+        Assert.Equal(0x80, input.PeekByte());
+        Assert.Equal(0x80, input.PeekByte());
+        Assert.Equal(2, input.Remaining);
+        Assert.Equal(0x80, input.ReadByte());
+        Assert.Equal(1, input.Remaining);
+        Assert.Equal(0x01, input.ReadByte());
+        Assert.Equal(0, input.Remaining);
+    }
+
+    [Fact]
+    public void FixedWidthIntegersUseBigEndianOrderAndAdvanceOnce()
+    {
+        var input = new Hessian2ByteCursor(Convert.FromHexString(
+            "1234010203040102030405060708FF"));
+        Assert.Equal(0x1234, input.ReadUInt16());
+        Assert.Equal(0x01020304, input.ReadInt32());
+        Assert.Equal(0x0102030405060708L, input.ReadInt64());
+        Assert.Equal(0xff, input.PeekByte());
+        Assert.Equal(1, input.Remaining);
+    }
+
+    [Fact]
+    public void ReadBytesCopiesWithoutChangingTheSourceBuffer()
+    {
+        byte[] source = { 0x11, 0x22, 0x33 };
+        var input = new Hessian2ByteCursor(source);
+        byte[] copy = input.ReadBytes(2);
+        source[0] = 0xff;
+        Assert.Equal(new byte[] { 0x11, 0x22 }, copy);
+        Assert.Equal(0x33, input.ReadByte());
+        Assert.Equal(0, input.Remaining);
+    }
+
+    [Fact]
+    public void CopyToWritesOnlyRequestedBytesAndLeavesNextTagUnread()
+    {
+        var input = new Hessian2ByteCursor(new byte[] { 0xab, 0xcd, 0x54 });
+        using var destination = new MemoryStream();
+        input.CopyTo(destination, 2);
+        Assert.Equal(new byte[] { 0xab, 0xcd }, destination.ToArray());
+        Assert.Equal(0x54, input.PeekByte());
+        Assert.Equal(1, input.Remaining);
+        input.CopyTo(destination, 0);
+        Assert.Equal(1, input.Remaining);
+    }
+
+    [Fact]
+    public void ConstructorPreservesTheExistingNullArgumentName()
+    {
+        var error = Assert.Throws<ArgumentNullException>(() => new 
Hessian2ByteCursor(null!));
+        Assert.Equal("buffer", error.ParamName);
+    }
+
+    [Theory]
+    [InlineData(0)] // Peek
+    [InlineData(1)] // Single byte
+    [InlineData(2)] // Two-byte integer
+    [InlineData(4)] // Four-byte integer
+    [InlineData(8)] // Eight-byte integer
+    public void TruncatedReadsDoNotConsumeInput(int width)
+    {
+        var input = new Hessian2ByteCursor(new byte[] { 0x12 });
+        Assert.Equal(0x12, input.PeekByte());
+        Assert.Equal(0x12, input.ReadByte());
+        Assert.Equal(0, input.Remaining);
+        Action read = width switch
+        {
+            0 => () => input.PeekByte(),
+            1 => () => input.ReadByte(),
+            2 => () => input.ReadUInt16(),
+            4 => () => input.ReadInt32(),
+            _ => () => input.ReadInt64()
+        };
+        var error = Assert.Throws<EndOfStreamException>(read);
+        Assert.Equal("Unexpected end of Hessian2 payload.", error.Message);
+        Assert.Equal(0, input.Remaining);
+    }
+
+    [Theory]
+    [InlineData(2)]
+    [InlineData(4)]
+    [InlineData(8)]
+    public void PartialFixedWidthReadLeavesExistingBytesUnread(int width)
+    {
+        // Consume one byte first so a failed read cannot silently reset the 
cursor.
+        byte[] wire = new byte[] { 0xaa }
+            .Concat(Enumerable.Range(1, width - 1).Select(value => 
(byte)value))
+            .ToArray();
+        var input = new Hessian2ByteCursor(wire);
+        Assert.Equal(0xaa, input.ReadByte());
+        int remaining = width - 1;
+        Action read = width switch
+        {
+            2 => () => input.ReadUInt16(),
+            4 => () => input.ReadInt32(),
+            _ => () => input.ReadInt64()
+        };
+
+        var error = Assert.Throws<EndOfStreamException>(read);
+        Assert.Equal("Unexpected end of Hessian2 payload.", error.Message);
+        Assert.Equal(remaining, input.Remaining);
+        Assert.Equal(0x01, input.PeekByte());
+        Assert.Equal(0x01, input.ReadByte());
+        Assert.Equal(remaining - 1, input.Remaining);
+    }
+
+    [Theory]
+    [InlineData(-1)]
+    [InlineData(2)]
+    public void InvalidCopyOrReadLengthDoesNotConsumeInput(int length)

Review Comment:
   已在测试先行提交 `bddbbd8` 增加 `int.MinValue` / `int.MaxValue`,分别检查 `ReadBytes` 与新的 
`ReadSpan` 失败后游标不推进。新 head 的 CI 待批准。



-- 
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