diff --git a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolRegistration.java b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolRegistration.java index 78c54831..7540ea6f 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolRegistration.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolRegistration.java @@ -77,14 +77,14 @@ public class ProtocolRegistration implements IProtocolRegistration { return; } + var beforeWriteIndex = byteBuf.writerIndex(); + if (compatible) { - byteBuf.markWriterIndex(); ByteBufUtils.writeInt(byteBuf, predictionLength); } else { ByteBufUtils.writeInt(byteBuf, -1); } - var beforeWriteIndex = byteBuf.writerIndex(); for (int i = 0, length = fields.length; i < length; i++) { Field field = fields[i]; IFieldRegistration packetFieldRegistration = fieldRegistrations[i]; @@ -96,23 +96,24 @@ public class ProtocolRegistration implements IProtocolRegistration { if (compatible) { // 因为写入的是可变长的int,如果预留的位置过多,则清除多余的位置 var currentWriteIndex = byteBuf.writerIndex(); - var length = currentWriteIndex - beforeWriteIndex; + var predictionCount = ByteBufUtils.writeIntCount(predictionLength); + var length = currentWriteIndex - beforeWriteIndex - predictionCount; var lengthCount = ByteBufUtils.writeIntCount(length); - var padding = lengthCount - ByteBufUtils.writeIntCount(predictionLength); + var padding = lengthCount - predictionCount; if (padding == 0) { - byteBuf.resetWriterIndex(); + byteBuf.writerIndex(beforeWriteIndex); ByteBufUtils.writeInt(byteBuf, length); byteBuf.writerIndex(currentWriteIndex); } else if (padding < 0) { var retainedByteBuf = byteBuf.retainedSlice(currentWriteIndex - length, length); - byteBuf.resetWriterIndex(); + byteBuf.writerIndex(beforeWriteIndex); ByteBufUtils.writeInt(byteBuf, length); byteBuf.writeBytes(retainedByteBuf); ReferenceCountUtil.release(retainedByteBuf); } else { var retainedByteBuf = byteBuf.retainedSlice(currentWriteIndex - length, length); var bytes = ByteBufUtils.readAllBytes(retainedByteBuf); - byteBuf.resetWriterIndex(); + byteBuf.writerIndex(beforeWriteIndex); ByteBufUtils.writeInt(byteBuf, length); byteBuf.writeBytes(bytes); ReferenceCountUtil.release(retainedByteBuf); diff --git a/protocol/src/test/java/com/zfoo/protocol/BenchmarkTesting.java b/protocol/src/test/java/com/zfoo/protocol/BenchmarkTesting.java index ee7581f4..365244b3 100644 --- a/protocol/src/test/java/com/zfoo/protocol/BenchmarkTesting.java +++ b/protocol/src/test/java/com/zfoo/protocol/BenchmarkTesting.java @@ -338,7 +338,9 @@ public class BenchmarkTesting { public static final Map mapWithInteger = new HashMap<>(Map.of(Integer.MIN_VALUE, "a", -99, "b", 0, "c", 99, "d", Integer.MAX_VALUE, "e")); public static final ObjectB objectB = new ObjectB(true); +// public static final ObjectB objectB = new ObjectB(true, 44); public static final ObjectA objectA = new ObjectA(Integer.MAX_VALUE, mapWithInteger, objectB); +// public static final ObjectA objectA = new ObjectA(Integer.MAX_VALUE, mapWithInteger, objectB, 66); public static final List listWithInteger = new ArrayList<>(ArrayUtils.toList(intArray)); public static final List listWithInteger1 = new ArrayList<>(ArrayUtils.toList(intArray1)); public static final List listWithInteger2 = new ArrayList<>(ArrayUtils.toList(intArray2)); diff --git a/protocol/src/test/java/com/zfoo/protocol/compatible/CompatibleTesting.java b/protocol/src/test/java/com/zfoo/protocol/compatible/CompatibleTesting.java index ed0a5fc1..e175e20d 100644 --- a/protocol/src/test/java/com/zfoo/protocol/compatible/CompatibleTesting.java +++ b/protocol/src/test/java/com/zfoo/protocol/compatible/CompatibleTesting.java @@ -95,7 +95,8 @@ public class CompatibleTesting { public void normalTest() { var buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 1_0000); ProtocolManager.write(buffer, normalObject); -// FileUtils.writeInputStreamToFile(new File("normal-no-compatible.bytes"), new ByteArrayInputStream(ByteBufUtils.readAllBytes(buffer))); + // normalObject.outCompatibleValue = 88; + // FileUtils.writeInputStreamToFile(new File("normal-no-compatible.bytes"), new ByteArrayInputStream(ByteBufUtils.readAllBytes(buffer))); var packet = ProtocolManager.read(buffer); diff --git a/protocol/src/test/java/com/zfoo/protocol/packet/NormalObject.java b/protocol/src/test/java/com/zfoo/protocol/packet/NormalObject.java index 15b23691..ffe08833 100644 --- a/protocol/src/test/java/com/zfoo/protocol/packet/NormalObject.java +++ b/protocol/src/test/java/com/zfoo/protocol/packet/NormalObject.java @@ -14,6 +14,7 @@ package com.zfoo.protocol.packet; +import com.zfoo.protocol.anno.Compatible; import com.zfoo.protocol.anno.Protocol; import java.util.List; @@ -57,6 +58,9 @@ public class NormalObject { private Set s; private Set ssss; +// @Compatible(1) +// public int outCompatibleValue; + public byte getA() { return a; } diff --git a/protocol/src/test/java/com/zfoo/protocol/packet/ObjectA.java b/protocol/src/test/java/com/zfoo/protocol/packet/ObjectA.java index 1f2f82f3..c6ce017f 100644 --- a/protocol/src/test/java/com/zfoo/protocol/packet/ObjectA.java +++ b/protocol/src/test/java/com/zfoo/protocol/packet/ObjectA.java @@ -14,6 +14,7 @@ package com.zfoo.protocol.packet; +import com.zfoo.protocol.anno.Compatible; import com.zfoo.protocol.anno.Protocol; import java.util.Map; @@ -29,5 +30,8 @@ public record ObjectA( Map m, ObjectB objectB + +// @Compatible(1) +// int innerCompatibleValue, ) { } diff --git a/protocol/src/test/java/com/zfoo/protocol/packet/ObjectB.java b/protocol/src/test/java/com/zfoo/protocol/packet/ObjectB.java index 6e4c6bef..56635e1d 100644 --- a/protocol/src/test/java/com/zfoo/protocol/packet/ObjectB.java +++ b/protocol/src/test/java/com/zfoo/protocol/packet/ObjectB.java @@ -14,6 +14,7 @@ package com.zfoo.protocol.packet; +import com.zfoo.protocol.anno.Compatible; import com.zfoo.protocol.anno.Protocol; /** @@ -24,6 +25,9 @@ public record ObjectB( boolean flag +// @Compatible(1) +// int innerCompatibleValue + ) { } diff --git a/protocol/src/test/resources/compatible/normal-inner-compatible.bytes b/protocol/src/test/resources/compatible/normal-inner-compatible.bytes new file mode 100644 index 00000000..4c4ac50a Binary files /dev/null and b/protocol/src/test/resources/compatible/normal-inner-compatible.bytes differ diff --git a/protocol/src/test/resources/compatible/normal-no-compatible.bytes b/protocol/src/test/resources/compatible/normal-no-compatible.bytes new file mode 100644 index 00000000..cb3184fa Binary files /dev/null and b/protocol/src/test/resources/compatible/normal-no-compatible.bytes differ diff --git a/protocol/src/test/resources/compatible/normal-out-compatible.bytes b/protocol/src/test/resources/compatible/normal-out-compatible.bytes new file mode 100644 index 00000000..c2d390da Binary files /dev/null and b/protocol/src/test/resources/compatible/normal-out-compatible.bytes differ diff --git a/protocol/src/test/resources/compatible/normal-out-inner-compatible.bytes b/protocol/src/test/resources/compatible/normal-out-inner-compatible.bytes new file mode 100644 index 00000000..ca9c3d85 Binary files /dev/null and b/protocol/src/test/resources/compatible/normal-out-inner-compatible.bytes differ diff --git a/protocol/src/test/resources/compatible/normal-out-inner-inner-compatible.bytes b/protocol/src/test/resources/compatible/normal-out-inner-inner-compatible.bytes new file mode 100644 index 00000000..c4feb2ac Binary files /dev/null and b/protocol/src/test/resources/compatible/normal-out-inner-inner-compatible.bytes differ