Skip to content

Commit

Permalink
feat[cpp]: compatible field of inside protocol class
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Oct 15, 2023
1 parent 1c87c12 commit 98504cb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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;
}

Expand Down
7 changes: 6 additions & 1 deletion protocol/src/main/resources/cpp/ProtocolTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down

0 comments on commit 98504cb

Please sign in to comment.