feat[cpp]: compatible field of inside protocol class

This commit is contained in:
godotg
2023-10-15 16:30:36 +08:00
parent 08f198ef4f
commit 6f7dc49775
4 changed files with 47 additions and 14 deletions
@@ -245,8 +245,13 @@ public abstract class GenerateCppUtils {
private static String writeObject(ProtocolRegistration registration) {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var cppBuilder = new StringBuilder();
if (registration.isCompatible()) {
cppBuilder.append(TAB + TAB + TAB).append("auto beforeWriteIndex = buffer.writerIndex();").append(LS);
cppBuilder.append(TAB + TAB + TAB).append(StringUtils.format("buffer.writeInt({});", registration.getPredictionLength())).append(LS);
} else {
cppBuilder.append(TAB + TAB + TAB).append("buffer.writeInt(-1);").append(LS);
}
for (int i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
@@ -273,6 +278,17 @@ public abstract class GenerateCppUtils {
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);
if (ProtocolManager.isProtocolClass(field.getType())) {
cppBuilder.append(StringUtils.format("packet->{} = *{};", field.getName(), compatibleReadObject));
} else {
cppBuilder.append(StringUtils.format("packet->{} = {};", field.getName(), compatibleReadObject));
}
continue;
}
var readObject = cppSerializer(fieldRegistration.serializer()).readObject(cppBuilder, 3, field, fieldRegistration);
cppBuilder.append(TAB + TAB + TAB);
@@ -205,7 +205,6 @@ public abstract class GenerateTsUtils {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
if (field.isAnnotationPresent(Compatible.class)) {
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);
+28 -11
View File
@@ -77,6 +77,29 @@ namespace zfoo {
ByteBuffer &operator=(const ByteBuffer &buffer) = delete;
void adjustPadding(int32_t predictionLength, int32_t beforeWriteIndex) {
int32_t currentWriteIndex = writerIndex();
int32_t predictionCount = writeIntCount(predictionLength);
int32_t length = currentWriteIndex - beforeWriteIndex - predictionCount;
int32_t lengthCount = writeIntCount(length);
int32_t padding = lengthCount - predictionCount;
if (padding == 0) {
writerIndex(beforeWriteIndex);
writeInt(length);
writerIndex(currentWriteIndex);
} else {
int8_t *targetPtr = (int8_t *) calloc(length, sizeof(int8_t));
memcpy(targetPtr, &m_buffer[currentWriteIndex - length], length);
writerIndex(beforeWriteIndex);
writeInt(length);
writeBytes(targetPtr, length);
free(targetPtr);
}
}
bool compatibleRead(int32_t beforeReadIndex, int32_t length) {
return length != -1 && readerIndex() < length + beforeReadIndex;
}
void clear() {
m_writerIndex = 0;
@@ -229,17 +252,17 @@ namespace zfoo {
}
inline int32_t writeIntCount(const int32_t &intValue) {
uint32_t v = (uint32_t) ((intValue << 1) ^ (intValue >> 31));
if (v >> 7 == 0) {
uint32_t value = (uint32_t) ((intValue << 1) ^ (intValue >> 31));
if (value >> 7 == 0) {
return 1;
}
if (v >> 14 == 0) {
if (value >> 14 == 0) {
return 2;
}
if (v >> 21 == 0) {
if (value >> 21 == 0) {
return 3;
}
if (v >> 28 == 0) {
if (value >> 28 == 0) {
return 4;
}
return 5;
@@ -401,12 +424,6 @@ namespace zfoo {
return str;
}
inline bool writePacketFlag(const IProtocol *packet) {
bool flag = packet == nullptr;
writeBool(!flag);
return flag;
}
inline void writePacket(IProtocol *packet, const int16_t &protocolId) {
IProtocolRegistration *protocolRegistration = getProtocol(protocolId);
protocolRegistration->write(*this, packet);
@@ -36,7 +36,8 @@ namespace zfoo {
}
void write(ByteBuffer &buffer, IProtocol *packet) override {
if (buffer.writePacketFlag(packet)) {
if (packet == nullptr) {
buffer.writeInt(0);
return;
}
auto *message = ({} *) packet;