ref[cpp]: refactor generate C++ protocol

This commit is contained in:
godotg
2024-05-24 17:26:48 +08:00
parent 102be61edd
commit 26fcf1af85
26 changed files with 629 additions and 567 deletions
@@ -0,0 +1,11 @@
${protocol_note}
class ${protocol_name} : public IProtocol {
public:
${protocol_field_definition}
~${protocol_name}() override = default;
int16_t protocolId() override {
return ${protocol_id};
}
};
@@ -2,14 +2,14 @@
#define ZFOO_PROTOCOLMANAGER_H
#include "ByteBuffer.h"
{}
${protocol_imports}
namespace zfoo {
const int16_t MAX_PROTOCOL_NUM = 32767;
const IProtocolRegistration *protocols[MAX_PROTOCOL_NUM];
void initProtocol() {
{}
${protocol_manager_registrations}
}
inline IProtocolRegistration *getProtocol(int16_t protocolId) {
@@ -0,0 +1,29 @@
class ${protocol_name}Registration : public IProtocolRegistration {
public:
int16_t protocolId() override {
return ${protocol_id};
}
void write(ByteBuffer &buffer, IProtocol *packet) override {
if (packet == nullptr) {
buffer.writeInt(0);
return;
}
auto *message = (${protocol_name} *) packet;
${protocol_write_serialization}
}
IProtocol *read(ByteBuffer &buffer) override {
auto *packet = new ${protocol_name}();
auto length = buffer.readInt();
if (length == 0) {
return packet;
}
auto beforeReadIndex = buffer.readerIndex();
${protocol_read_deserialization}
if (length > 0) {
buffer.readerIndex(beforeReadIndex + length);
}
return packet;
}
};
@@ -1,63 +1,12 @@
#ifndef ZFOO_{}_H
#define ZFOO_{}_H
#ifndef ZFOO_${protocol_name}
#define ZFOO_${protocol_name}
#include "{}/ByteBuffer.h"
{}
#include "${protocol_root_path}/ByteBuffer.h"
${protocol_imports}
namespace zfoo {
${protocol_class}
{}
class {} : public IProtocol {
public:
{}
~{}() override = default;
static {} valueOf({}) {
auto packet = {}();
{}
return packet;
}
int16_t protocolId() override {
return {};
}
bool operator<(const {} &_) const {
{}
return false;
}
};
class {}Registration : public IProtocolRegistration {
public:
int16_t protocolId() override {
return {};
}
void write(ByteBuffer &buffer, IProtocol *packet) override {
if (packet == nullptr) {
buffer.writeInt(0);
return;
}
auto *message = ({} *) packet;
{}
}
IProtocol *read(ByteBuffer &buffer) override {
auto *packet = new {}();
auto length = buffer.readInt();
if (length == 0) {
return packet;
}
auto beforeReadIndex = buffer.readerIndex();
{}
if (length > 0) {
buffer.readerIndex(beforeReadIndex + length);
}
return packet;
}
};
${protocol_registration}
}
#endif
@@ -0,0 +1,12 @@
#ifndef ZFOO_Protocols
#define ZFOO_Protocols
#include "${protocol_root_path}/ByteBuffer.h"
namespace zfoo {
${protocol_class}
${protocol_registration}
}
#endif