From 98504cb1243f86bde9097e291c22b647ed925341 Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 15 Oct 2023 17:35:25 +0800 Subject: [PATCH] feat[cpp]: compatible field of inside protocol class --- .../zfoo/protocol/serializer/cpp/GenerateCppUtils.java | 9 +++++---- protocol/src/main/resources/cpp/ProtocolTemplate.h | 7 ++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java index 4614a50e7..a16228c85 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java @@ -262,6 +262,9 @@ private static String writeObject(ProtocolRegistration registration) { serializer.writeObject(cppBuilder, "message->" + field.getName(), 3, field, fieldRegistration); } } + if (registration.isCompatible()) { + cppBuilder.append(TAB + TAB + TAB).append(StringUtils.format("buffer.adjustPadding({}, beforeWriteIndex);", registration.getPredictionLength())).append(LS); + } return cppBuilder.toString(); } @@ -275,18 +278,16 @@ private static String readObject(ProtocolRegistration registration) { var field = fields[i]; var fieldRegistration = fieldRegistrations[i]; - if (field.isAnnotationPresent(Compatible.class)) { - cppBuilder.append(TAB + TAB + TAB).append(StringUtils.format("if (!buffer.isReadable()) { return packet; }")).append(LS); - } if (field.isAnnotationPresent(Compatible.class)) { cppBuilder.append(TAB + TAB + TAB).append("if (buffer.compatibleRead(beforeReadIndex, length)) {").append(LS); var compatibleReadObject = cppSerializer(fieldRegistration.serializer()).readObject(cppBuilder, 4, field, fieldRegistration); - cppBuilder.append(TAB + TAB + TAB); + cppBuilder.append(TAB + TAB + TAB + TAB); if (ProtocolManager.isProtocolClass(field.getType())) { cppBuilder.append(StringUtils.format("packet->{} = *{};", field.getName(), compatibleReadObject)); } else { cppBuilder.append(StringUtils.format("packet->{} = {};", field.getName(), compatibleReadObject)); } + cppBuilder.append(LS).append(TAB + TAB + TAB).append("}").append(LS); continue; } diff --git a/protocol/src/main/resources/cpp/ProtocolTemplate.h b/protocol/src/main/resources/cpp/ProtocolTemplate.h index 5a4daa761..26201396a 100644 --- a/protocol/src/main/resources/cpp/ProtocolTemplate.h +++ b/protocol/src/main/resources/cpp/ProtocolTemplate.h @@ -46,10 +46,15 @@ namespace zfoo { IProtocol *read(ByteBuffer &buffer) override { auto *packet = new {}(); - if (!buffer.readBool()) { + auto length = buffer.readInt(); + if (length == 0) { return packet; } + auto beforeReadIndex = buffer.readerIndex(); {} + if (length > 0) { + buffer.readerIndex(beforeReadIndex + length); + } return packet; } };