fix[protocol]: compatible field of inside protocol class

This commit is contained in:
sun
2023-09-26 14:27:28 +08:00
parent ec9ade6186
commit d17c81528b
11 changed files with 24 additions and 8 deletions
@@ -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);
@@ -338,7 +338,9 @@ public class BenchmarkTesting {
public static final Map<Integer, String> 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<Integer> listWithInteger = new ArrayList<>(ArrayUtils.toList(intArray));
public static final List<Integer> listWithInteger1 = new ArrayList<>(ArrayUtils.toList(intArray1));
public static final List<Integer> listWithInteger2 = new ArrayList<>(ArrayUtils.toList(intArray2));
@@ -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);
@@ -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<Integer> s;
private Set<String> ssss;
// @Compatible(1)
// public int outCompatibleValue;
public byte getA() {
return a;
}
@@ -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<Integer, String> m,
ObjectB objectB
// @Compatible(1)
// int innerCompatibleValue,
) {
}
@@ -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
) {
}