perf[protocol]: typescript generate

This commit is contained in:
sun
2023-09-27 16:11:51 +08:00
parent 456fc9c652
commit d1b6c2dcff
6 changed files with 20 additions and 14 deletions
@@ -67,7 +67,7 @@ public abstract class ByteBufUtils {
}
public static boolean compatibleRead(ByteBuf byteBuf, int beforeReadIndex, int length) {
return length == -1 || byteBuf.readerIndex() >= length + beforeReadIndex;
return length != -1 && byteBuf.readerIndex() < length + beforeReadIndex;
}
//---------------------------------boolean--------------------------------------
@@ -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(StringUtils.format("if (!{}.compatibleRead($1, beforeReadIndex, length)) {", byteBufUtils));
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("}");
@@ -254,19 +254,19 @@ public abstract class EnhanceUtils {
// protocol backwards compatibility,协议向后兼容
if (field.isAnnotationPresent(Compatible.class)) {
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));
} else {
builder.append(StringUtils.format("packet.{}({});", FieldUtils.fieldToSetMethod(packetClazz, field), defaultReadObject));
}
builder.append("} else {");
var compatibleReadObject = enhanceSerializer(fieldRegistration.serializer()).readObject(builder, field, fieldRegistration);
if (Modifier.isPublic(field.getModifiers())) {
builder.append(StringUtils.format("packet.{}={};", field.getName(), compatibleReadObject));
} else {
builder.append(StringUtils.format("packet.{}({});", FieldUtils.fieldToSetMethod(packetClazz, field), compatibleReadObject));
}
builder.append("} else {");
var defaultReadObject = enhanceSerializer(fieldRegistration.serializer()).defaultValue(builder, field, fieldRegistration);
if (Modifier.isPublic(field.getModifiers())) {
builder.append(StringUtils.format("packet.{}={};", field.getName(), defaultReadObject));
} else {
builder.append(StringUtils.format("packet.{}({});", FieldUtils.fieldToSetMethod(packetClazz, field), defaultReadObject));
}
builder.append("}");
continue;
}
@@ -122,7 +122,7 @@ public class ProtocolRegistration implements IProtocolRegistration {
// 协议向后兼容
if (field.isAnnotationPresent(Compatible.class)) {
if (ByteBufUtils.compatibleRead(byteBuf, beforeReadIndex, length)) {
if (!ByteBufUtils.compatibleRead(byteBuf, beforeReadIndex, length)) {
constructorParams[index] = packetFieldRegistration.defaultValue();
continue;
}
@@ -140,7 +140,7 @@ public class ProtocolRegistration implements IProtocolRegistration {
ISerializer serializer = packetFieldRegistration.serializer();
// 协议向后兼容
if (field.isAnnotationPresent(Compatible.class)) {
if (ByteBufUtils.compatibleRead(byteBuf, beforeReadIndex, length)) {
if (!ByteBufUtils.compatibleRead(byteBuf, beforeReadIndex, length)) {
ReflectionUtils.setField(field, object, packetFieldRegistration.defaultValue());
continue;
}
@@ -205,7 +205,9 @@ public abstract class GenerateTsUtils {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
if (field.isAnnotationPresent(Compatible.class)) {
tsBuilder.append(TAB + TAB).append("if (length !== -1 && buffer.getReadOffset() - readIndex < length) {").append(LS);
tsBuilder.append(TAB + TAB).append("if (length !== -1 && buffer.getReadOffset() - beforeReadIndex < length) {").append(LS);
tsBuilder.append(TAB + TAB).append("if (buffer.compatibleRead(beforeReadIndex, length)) {").append(LS);
var compatibleReadObject = tsSerializer(fieldRegistration.serializer()).readObject(tsBuilder, 3, field, fieldRegistration);
tsBuilder.append(TAB + TAB+ TAB).append(StringUtils.format("packet.{} = {};", field.getName(), compatibleReadObject)).append(LS);
tsBuilder.append(TAB + TAB).append("}").append(LS);
@@ -23,11 +23,11 @@ class {} {
if (length === 0) {
return null;
}
const readIndex = buffer.getReadOffset();
const beforeReadIndex = buffer.getReadOffset();
const packet = new {}();
{}
if (length > 0) {
buffer.setReadOffset(readIndex + length);
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
@@ -75,6 +75,10 @@ class ByteBuffer {
}
}
compatibleRead(beforeReadIndex: number, length: number): boolean {
return length !== -1 && this.getReadOffset() < length + beforeReadIndex;
}
setWriteOffset(writeOffset: number): void {
if (writeOffset > this.buffer.byteLength) {
throw new Error('index out of bounds exception:readerIndex:' + this.readOffset +