From d17c81528b6cdb53685e880748cb79d7680dda0f Mon Sep 17 00:00:00 2001 From: sun Date: Tue, 26 Sep 2023 14:27:28 +0800 Subject: [PATCH] fix[protocol]: compatible field of inside protocol class --- .../registration/ProtocolRegistration.java | 15 ++++++++------- .../java/com/zfoo/protocol/BenchmarkTesting.java | 2 ++ .../protocol/compatible/CompatibleTesting.java | 3 ++- .../com/zfoo/protocol/packet/NormalObject.java | 4 ++++ .../java/com/zfoo/protocol/packet/ObjectA.java | 4 ++++ .../java/com/zfoo/protocol/packet/ObjectB.java | 4 ++++ .../compatible/normal-inner-compatible.bytes | Bin 0 -> 444 bytes .../compatible/normal-no-compatible.bytes | Bin 0 -> 430 bytes .../compatible/normal-out-compatible.bytes | Bin 0 -> 433 bytes .../compatible/normal-out-inner-compatible.bytes | Bin 0 -> 447 bytes .../normal-out-inner-inner-compatible.bytes | Bin 0 -> 454 bytes 11 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 protocol/src/test/resources/compatible/normal-inner-compatible.bytes create mode 100644 protocol/src/test/resources/compatible/normal-no-compatible.bytes create mode 100644 protocol/src/test/resources/compatible/normal-out-compatible.bytes create mode 100644 protocol/src/test/resources/compatible/normal-out-inner-compatible.bytes create mode 100644 protocol/src/test/resources/compatible/normal-out-inner-inner-compatible.bytes 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 0000000000000000000000000000000000000000..4c4ac50ab0bfee5b71f1a8112690152d4c17be37 GIT binary patch literal 444 zcmZQbWlZL3n9Gn{ug?Fkd42r9|LuRy?m4XNvu}@reql^s47$N2X)d+#a!Qd~{cr-N- zJ3%Ic0UKefsG)+3DUm6ODVZsSDHXS`*q8`g#X?vK5nh6X9s@Xhk1|4n4-`&F&IAC7 CRn$rV literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..c2d390dac9694c13ef74025947a969aa72224558 GIT binary patch literal 433 zcmZQby~UQy)i9SKxn7voBz+8Ig61iBQ+-{pYh-S z|Nr^9fW&`(rbGs&WH6s8^(Z4#(lJJ+6h=lqu-spuF-I90jxj>a1F9ARiG#sksPSlO zAa;Vx0|Pd~R#8I*7gHir5>ql$3R5a>U$HR}wu*(Y5+b|=2|WgIs2*j61Rp4zkes=J F5df-L)!G06 literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..ca9c3d853f22714ffa9cb3effee14c33859bd9cb GIT binary patch literal 447 zcmZQb{l=Ee)i9SKxn7voBz+8Ig61iBQ+-{-|gT3 z|Nr^9fW&`(rbGs&WH6s8^(Z4#(lJJ+6h_7tMn16YU!Xxp85xc-LQDjz7Xpcc!C$Ba zXlfv~f=mPhHexKJmKrXmM5ZLBWTq6RRNTH}Vd z|Ns14V3H}7fhift{m;*oc$AST=@=tZ3JYUI3nL#`@GsD$ql^s47$HUil?j2w!Qd~{ z1~fGgYe9yC0UJ@aQBM&UQzBCmQ!-NuQz~vBvM~{5Aq!Eei1Q&NJQ=`&ev}asprDXL Ia_