Skip to content

Commit

Permalink
perf[protocol]: typescript generate
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Sep 27, 2023
1 parent 456fc9c commit d1b6c2d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void adjustPadding(ByteBuf byteBuf, int predictionLength, int befo
}

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--------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private static String readMethodBody(ProtocolRegistration registration) {
// 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("}");
Expand All @@ -254,18 +254,18 @@ private static String readMethodBody(ProtocolRegistration registration) {
// 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);
var compatibleReadObject = enhanceSerializer(fieldRegistration.serializer()).readObject(builder, field, fieldRegistration);
if (Modifier.isPublic(field.getModifiers())) {
builder.append(StringUtils.format("packet.{}={};", field.getName(), defaultReadObject));
builder.append(StringUtils.format("packet.{}={};", field.getName(), compatibleReadObject));
} else {
builder.append(StringUtils.format("packet.{}({});", FieldUtils.fieldToSetMethod(packetClazz, field), defaultReadObject));
builder.append(StringUtils.format("packet.{}({});", FieldUtils.fieldToSetMethod(packetClazz, field), compatibleReadObject));
}
builder.append("} else {");
var compatibleReadObject = enhanceSerializer(fieldRegistration.serializer()).readObject(builder, field, fieldRegistration);
var defaultReadObject = enhanceSerializer(fieldRegistration.serializer()).defaultValue(builder, field, fieldRegistration);
if (Modifier.isPublic(field.getModifiers())) {
builder.append(StringUtils.format("packet.{}={};", field.getName(), compatibleReadObject));
builder.append(StringUtils.format("packet.{}={};", field.getName(), defaultReadObject));
} else {
builder.append(StringUtils.format("packet.{}({});", FieldUtils.fieldToSetMethod(packetClazz, field), compatibleReadObject));
builder.append(StringUtils.format("packet.{}({});", FieldUtils.fieldToSetMethod(packetClazz, field), defaultReadObject));
}
builder.append("}");
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public Object read(ByteBuf byteBuf) {

// 协议向后兼容
if (field.isAnnotationPresent(Compatible.class)) {
if (ByteBufUtils.compatibleRead(byteBuf, beforeReadIndex, length)) {
if (!ByteBufUtils.compatibleRead(byteBuf, beforeReadIndex, length)) {
constructorParams[index] = packetFieldRegistration.defaultValue();
continue;
}
Expand All @@ -140,7 +140,7 @@ public Object read(ByteBuf byteBuf) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ private static String readObject(ProtocolRegistration registration) {
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);
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/main/resources/typescript/ProtocolTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions protocol/src/main/resources/typescript/buffer/ByteBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand Down

0 comments on commit d1b6c2d

Please sign in to comment.