diff --git a/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java b/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java index 784e7d76..c755aa55 100644 --- a/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/buffer/ByteBufUtils.java @@ -35,10 +35,10 @@ import java.util.Set; */ public abstract class ByteBufUtils { + //---------------------------------compatible-------------------------------------- public static final Double ZERO_DOUBLE = Double.valueOf(0D); public static final Float ZERO_FLOAT = Float.valueOf(0F); - public static void adjustPadding(ByteBuf byteBuf, int predictionLength, int beforeWriteIndex) { // 因为写入的是可变长的int,如果预留的位置过多,则清除多余的位置 var currentWriteIndex = byteBuf.writerIndex(); @@ -66,6 +66,10 @@ public abstract class ByteBufUtils { } } + public static boolean compatibleRead(ByteBuf byteBuf, int beforeReadIndex, int length) { + return length == -1 || byteBuf.readerIndex() >= length + beforeReadIndex; + } + //---------------------------------boolean-------------------------------------- public static void writeBoolean(ByteBuf byteBuf, boolean value) { byteBuf.writeBoolean(value); diff --git a/protocol/src/main/java/com/zfoo/protocol/registration/EnhanceUtils.java b/protocol/src/main/java/com/zfoo/protocol/registration/EnhanceUtils.java index 9c9d1fb5..155ac6a1 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/EnhanceUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/EnhanceUtils.java @@ -218,7 +218,7 @@ public abstract class EnhanceUtils { builder.append("{"); builder.append(StringUtils.format("int length = {}.readInt($1);", byteBufUtils)); builder.append("if(length==0){return null;}"); - builder.append("int readIndex = $1.readerIndex();"); + builder.append("int beforeReadIndex = $1.readerIndex();"); var packetClazz = constructor.getDeclaringClass(); if (packetClazz.isRecord()) { var fields = registration.getFields(); @@ -232,7 +232,7 @@ public abstract class EnhanceUtils { // protocol backwards compatibility,协议向后兼容 if (field.isAnnotationPresent(Compatible.class)) { var defaultReadObject = enhanceSerializer(fieldRegistration.serializer()).defaultValue(builder, field, fieldRegistration); - builder.append("if (length != -1 && $1.readerIndex() < length + readIndex) {"); + builder.append(StringUtils.format("if (!{}.compatibleRead($1, beforeReadIndex, length)) {", byteBufUtils)); var compatibleReadObject = enhanceSerializer(fieldRegistration.serializer()).readObject(builder, field, fieldRegistration); builder.append(StringUtils.format("{} = {};", defaultReadObject, compatibleReadObject)); builder.append("}"); @@ -253,7 +253,7 @@ public abstract class EnhanceUtils { var fieldRegistration = fieldRegistrations[i]; // protocol backwards compatibility,协议向后兼容 if (field.isAnnotationPresent(Compatible.class)) { - builder.append("if (length == -1 || $1.readerIndex() >= length + readIndex) {"); + builder.append(StringUtils.format("if ({}.compatibleRead($1, beforeReadIndex, length)) {", byteBufUtils)); var defaultReadObject = enhanceSerializer(fieldRegistration.serializer()).defaultValue(builder, field, fieldRegistration); if (Modifier.isPublic(field.getModifiers())) { builder.append(StringUtils.format("packet.{}={};", field.getName(), defaultReadObject)); @@ -279,7 +279,7 @@ public abstract class EnhanceUtils { } } - builder.append("if (length > 0) { $1.readerIndex(readIndex + length); }"); + builder.append("if (length > 0) { $1.readerIndex(beforeReadIndex + length); }"); builder.append("return packet;}"); return builder.toString(); 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 0aeab779..bfb09f84 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolRegistration.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolRegistration.java @@ -103,13 +103,14 @@ public class ProtocolRegistration implements IProtocolRegistration { @Override public Object read(ByteBuf byteBuf) { + // length为-1代表协议没有可兼容的部分,0代表为空对象,正数代表需要兼容的协议长度 var length = ByteBufUtils.readInt(byteBuf); if (length == 0) { return null; } Object object = null; - var readIndex = byteBuf.readerIndex(); + var beforeReadIndex = byteBuf.readerIndex(); var packetClazz = constructor.getDeclaringClass(); if (packetClazz.isRecord()) { var originFields = ProtocolAnalysis.getFields(packetClazz); @@ -121,7 +122,7 @@ public class ProtocolRegistration implements IProtocolRegistration { // 协议向后兼容 if (field.isAnnotationPresent(Compatible.class)) { - if (length == -1 || byteBuf.readerIndex() >= length + readIndex) { + if (ByteBufUtils.compatibleRead(byteBuf, beforeReadIndex, length)) { constructorParams[index] = packetFieldRegistration.defaultValue(); continue; } @@ -139,7 +140,7 @@ public class ProtocolRegistration implements IProtocolRegistration { ISerializer serializer = packetFieldRegistration.serializer(); // 协议向后兼容 if (field.isAnnotationPresent(Compatible.class)) { - if (length == -1 || byteBuf.readerIndex() - readIndex >= length) { + if (ByteBufUtils.compatibleRead(byteBuf, beforeReadIndex, length)) { ReflectionUtils.setField(field, object, packetFieldRegistration.defaultValue()); continue; } @@ -150,7 +151,7 @@ public class ProtocolRegistration implements IProtocolRegistration { } if (length > 0) { - byteBuf.readerIndex(readIndex + length); + byteBuf.readerIndex(beforeReadIndex + length); } return object; diff --git a/protocol/src/test/resources/speed.proto b/protocol/src/test/resources/speed.proto index db78d39a..26479cd1 100644 --- a/protocol/src/test/resources/speed.proto +++ b/protocol/src/test/resources/speed.proto @@ -4,6 +4,9 @@ option java_package = "com.zfoo.protocol.packet"; option java_outer_classname = "ProtobufObject"; // protoc -I=D:\zfoo\protocol\src\test\resources --java_out=D:\zfoo\protocol\src\test\java D:\zfoo\protocol\src\test\resources\speed.proto +// protoc -I=D:\Project\zfoo\protocol\src\test\resources --js_out=D:\Project\zfoo\protocol\src\test D:\Project\zfoo\protocol\src\test\resources\speed.proto +// npm install -g ts-protoc-gen +// protoc -I=D:\Project\zfoo\protocol\src\test\resources --ts_out=D:\Project\zfoo\protocol\src\test D:\Project\zfoo\protocol\src\test\resources\speed.proto message ObjectB { bool flag = 1;