mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-21 20:26:36 +00:00
ref[cpp]: refactor cpp protocol generate
This commit is contained in:
@@ -35,11 +35,11 @@ namespace zfoo {
|
||||
|
||||
class ByteBuffer;
|
||||
|
||||
class IPacket {
|
||||
class IProtocol {
|
||||
public:
|
||||
virtual int16_t protocolId() = 0;
|
||||
|
||||
virtual ~IPacket() {
|
||||
virtual ~IProtocol() {
|
||||
}
|
||||
};
|
||||
|
||||
@@ -47,9 +47,9 @@ namespace zfoo {
|
||||
public:
|
||||
virtual int16_t protocolId() = 0;
|
||||
|
||||
virtual void write(ByteBuffer &buffer, IPacket *packet) = 0;
|
||||
virtual void write(ByteBuffer &buffer, IProtocol *packet) = 0;
|
||||
|
||||
virtual IPacket *read(ByteBuffer &buffer) = 0;
|
||||
virtual IProtocol *read(ByteBuffer &buffer) = 0;
|
||||
};
|
||||
|
||||
IProtocolRegistration *getProtocol(int16_t protocolId);
|
||||
@@ -395,21 +395,21 @@ namespace zfoo {
|
||||
return readString()[0];
|
||||
}
|
||||
|
||||
inline bool writePacketFlag(const IPacket *packet) {
|
||||
inline bool writePacketFlag(const IProtocol *packet) {
|
||||
bool flag = packet == nullptr;
|
||||
writeBool(!flag);
|
||||
return flag;
|
||||
}
|
||||
|
||||
inline void writePacket(IPacket *packet, const int16_t &protocolId) {
|
||||
inline void writePacket(IProtocol *packet, const int16_t &protocolId) {
|
||||
IProtocolRegistration *protocolRegistration = getProtocol(protocolId);
|
||||
protocolRegistration->write(*this, packet);
|
||||
}
|
||||
|
||||
inline unique_ptr<IPacket> readPacket(const int16_t &protocolId) {
|
||||
inline unique_ptr<IProtocol> readPacket(const int16_t &protocolId) {
|
||||
IProtocolRegistration *protocolRegistration = getProtocol(protocolId);
|
||||
auto packet = protocolRegistration->read(*this);
|
||||
return unique_ptr<IPacket>(packet);
|
||||
return unique_ptr<IProtocol>(packet);
|
||||
}
|
||||
|
||||
|
||||
@@ -1069,7 +1069,7 @@ namespace zfoo {
|
||||
writeInt(map.size());
|
||||
for (const auto&[key, value] : map) {
|
||||
writeInt(key);
|
||||
writePacket((IPacket *) &value, protocolId);
|
||||
writePacket((IProtocol *) &value, protocolId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1164,7 +1164,7 @@ namespace zfoo {
|
||||
writeInt(map.size());
|
||||
for (const auto&[key, value] : map) {
|
||||
writeLong(key);
|
||||
writePacket((IPacket *) &value, protocolId);
|
||||
writePacket((IProtocol *) &value, protocolId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1259,7 +1259,7 @@ namespace zfoo {
|
||||
writeInt(map.size());
|
||||
for (const auto&[key, value] : map) {
|
||||
writeString(key);
|
||||
writePacket((IPacket *) &value, protocolId);
|
||||
writePacket((IProtocol *) &value, protocolId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1287,7 +1287,7 @@ namespace zfoo {
|
||||
int32_t length = array.size();
|
||||
writeInt(length);
|
||||
for (auto value : array) {
|
||||
writePacket((IPacket *) &value, protocolId);
|
||||
writePacket((IProtocol *) &value, protocolId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1312,7 +1312,7 @@ namespace zfoo {
|
||||
int32_t length = list.size();
|
||||
writeInt(length);
|
||||
for (auto value : list) {
|
||||
writePacket((IPacket *) &value, protocolId);
|
||||
writePacket((IProtocol *) &value, protocolId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1337,7 +1337,7 @@ namespace zfoo {
|
||||
int32_t length = set.size();
|
||||
writeInt(length);
|
||||
for (auto value : set) {
|
||||
writePacket((IPacket *) &value, protocolId);
|
||||
writePacket((IProtocol *) &value, protocolId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,17 +7,12 @@
|
||||
|
||||
namespace zfoo {
|
||||
|
||||
// 复杂的对象
|
||||
// 包括了各种复杂的结构,数组,List,Set,Map
|
||||
//
|
||||
// @author godotg
|
||||
// @version 3.0
|
||||
class ComplexObject : public IPacket {
|
||||
// 复杂的对象,包括了各种复杂的结构,数组,List,Set,Map
|
||||
class ComplexObject : public IProtocol {
|
||||
public:
|
||||
// byte类型,最简单的整形
|
||||
int8_t a;
|
||||
// byte的包装类型
|
||||
// 优先使用基础类型,包装类型会有装箱拆箱
|
||||
// byte的包装类型,优先使用基础类型,包装类型会有装箱拆箱
|
||||
int8_t aa;
|
||||
// 数组类型
|
||||
vector<int8_t> aaa;
|
||||
@@ -255,7 +250,7 @@ namespace zfoo {
|
||||
return 100;
|
||||
}
|
||||
|
||||
void write(ByteBuffer &buffer, IPacket *packet) override {
|
||||
void write(ByteBuffer &buffer, IProtocol *packet) override {
|
||||
if (buffer.writePacketFlag(packet)) {
|
||||
return;
|
||||
}
|
||||
@@ -317,7 +312,7 @@ namespace zfoo {
|
||||
buffer.writeIntPacketMap(message->mm, 102);
|
||||
buffer.writeInt(message->mmm.size());
|
||||
for (auto&[keyElement4, valueElement5] : message->mmm) {
|
||||
buffer.writePacket((IPacket *) &keyElement4, 102);
|
||||
buffer.writePacket((IProtocol *) &keyElement4, 102);
|
||||
buffer.writeIntList(valueElement5);
|
||||
}
|
||||
buffer.writeInt(message->mmmm.size());
|
||||
@@ -366,7 +361,7 @@ namespace zfoo {
|
||||
buffer.writePacket(&message->myObject, 102);
|
||||
}
|
||||
|
||||
IPacket *read(ByteBuffer &buffer) override {
|
||||
IProtocol *read(ByteBuffer &buffer) override {
|
||||
auto *packet = new ComplexObject();
|
||||
if (!buffer.readBool()) {
|
||||
return packet;
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#ifndef ZFOO_EMPTYOBJECT_H
|
||||
#define ZFOO_EMPTYOBJECT_H
|
||||
|
||||
#include "cppProtocol/ByteBuffer.h"
|
||||
|
||||
namespace zfoo {
|
||||
|
||||
|
||||
class EmptyObject : public IProtocol {
|
||||
public:
|
||||
|
||||
|
||||
~EmptyObject() override = default;
|
||||
|
||||
static EmptyObject valueOf() {
|
||||
auto packet = EmptyObject();
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
int16_t protocolId() override {
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool operator<(const EmptyObject &_) const {
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class EmptyObjectRegistration : public IProtocolRegistration {
|
||||
public:
|
||||
int16_t protocolId() override {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void write(ByteBuffer &buffer, IProtocol *packet) override {
|
||||
if (buffer.writePacketFlag(packet)) {
|
||||
return;
|
||||
}
|
||||
auto *message = (EmptyObject *) packet;
|
||||
|
||||
}
|
||||
|
||||
IProtocol *read(ByteBuffer &buffer) override {
|
||||
auto *packet = new EmptyObject();
|
||||
if (!buffer.readBool()) {
|
||||
return packet;
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -7,9 +7,8 @@
|
||||
|
||||
namespace zfoo {
|
||||
|
||||
// @author godotg
|
||||
// @version 3.0
|
||||
class NormalObject : public IPacket {
|
||||
|
||||
class NormalObject : public IProtocol {
|
||||
public:
|
||||
int8_t a;
|
||||
vector<int8_t> aaa;
|
||||
@@ -107,7 +106,7 @@ namespace zfoo {
|
||||
return 101;
|
||||
}
|
||||
|
||||
void write(ByteBuffer &buffer, IPacket *packet) override {
|
||||
void write(ByteBuffer &buffer, IProtocol *packet) override {
|
||||
if (buffer.writePacketFlag(packet)) {
|
||||
return;
|
||||
}
|
||||
@@ -132,7 +131,7 @@ namespace zfoo {
|
||||
buffer.writeStringSet(message->ssss);
|
||||
}
|
||||
|
||||
IPacket *read(ByteBuffer &buffer) override {
|
||||
IProtocol *read(ByteBuffer &buffer) override {
|
||||
auto *packet = new NormalObject();
|
||||
if (!buffer.readBool()) {
|
||||
return packet;
|
||||
|
||||
@@ -6,9 +6,8 @@
|
||||
|
||||
namespace zfoo {
|
||||
|
||||
// @author godotg
|
||||
// @version 3.0
|
||||
class ObjectA : public IPacket {
|
||||
|
||||
class ObjectA : public IProtocol {
|
||||
public:
|
||||
int32_t a;
|
||||
map<int32_t, string> m;
|
||||
@@ -46,7 +45,7 @@ namespace zfoo {
|
||||
return 102;
|
||||
}
|
||||
|
||||
void write(ByteBuffer &buffer, IPacket *packet) override {
|
||||
void write(ByteBuffer &buffer, IProtocol *packet) override {
|
||||
if (buffer.writePacketFlag(packet)) {
|
||||
return;
|
||||
}
|
||||
@@ -56,7 +55,7 @@ namespace zfoo {
|
||||
buffer.writePacket(&message->objectB, 103);
|
||||
}
|
||||
|
||||
IPacket *read(ByteBuffer &buffer) override {
|
||||
IProtocol *read(ByteBuffer &buffer) override {
|
||||
auto *packet = new ObjectA();
|
||||
if (!buffer.readBool()) {
|
||||
return packet;
|
||||
|
||||
@@ -5,9 +5,8 @@
|
||||
|
||||
namespace zfoo {
|
||||
|
||||
// @author godotg
|
||||
// @version 3.0
|
||||
class ObjectB : public IPacket {
|
||||
|
||||
class ObjectB : public IProtocol {
|
||||
public:
|
||||
bool flag;
|
||||
|
||||
@@ -37,7 +36,7 @@ namespace zfoo {
|
||||
return 103;
|
||||
}
|
||||
|
||||
void write(ByteBuffer &buffer, IPacket *packet) override {
|
||||
void write(ByteBuffer &buffer, IProtocol *packet) override {
|
||||
if (buffer.writePacketFlag(packet)) {
|
||||
return;
|
||||
}
|
||||
@@ -45,7 +44,7 @@ namespace zfoo {
|
||||
buffer.writeBool(message->flag);
|
||||
}
|
||||
|
||||
IPacket *read(ByteBuffer &buffer) override {
|
||||
IProtocol *read(ByteBuffer &buffer) override {
|
||||
auto *packet = new ObjectB();
|
||||
if (!buffer.readBool()) {
|
||||
return packet;
|
||||
|
||||
@@ -5,9 +5,8 @@
|
||||
|
||||
namespace zfoo {
|
||||
|
||||
// @author godotg
|
||||
// @version 3.0
|
||||
class SimpleObject : public IPacket {
|
||||
|
||||
class SimpleObject : public IProtocol {
|
||||
public:
|
||||
int32_t c;
|
||||
bool g;
|
||||
@@ -41,7 +40,7 @@ namespace zfoo {
|
||||
return 104;
|
||||
}
|
||||
|
||||
void write(ByteBuffer &buffer, IPacket *packet) override {
|
||||
void write(ByteBuffer &buffer, IProtocol *packet) override {
|
||||
if (buffer.writePacketFlag(packet)) {
|
||||
return;
|
||||
}
|
||||
@@ -50,7 +49,7 @@ namespace zfoo {
|
||||
buffer.writeBool(message->g);
|
||||
}
|
||||
|
||||
IPacket *read(ByteBuffer &buffer) override {
|
||||
IProtocol *read(ByteBuffer &buffer) override {
|
||||
auto *packet = new SimpleObject();
|
||||
if (!buffer.readBool()) {
|
||||
return packet;
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -2,6 +2,8 @@
|
||||
#define ZFOO_PROTOCOLMANAGER_H
|
||||
|
||||
#include "ByteBuffer.h"
|
||||
#include "cppProtocol/packet/EmptyObject.h"
|
||||
#include "cppProtocol/packet/VeryBigObject.h"
|
||||
#include "cppProtocol/packet/ComplexObject.h"
|
||||
#include "cppProtocol/packet/NormalObject.h"
|
||||
#include "cppProtocol/packet/ObjectA.h"
|
||||
@@ -14,6 +16,8 @@ namespace zfoo {
|
||||
const IProtocolRegistration *protocols[MAX_PROTOCOL_NUM];
|
||||
|
||||
void initProtocol() {
|
||||
protocols[0] = new EmptyObjectRegistration();
|
||||
protocols[1] = new VeryBigObjectRegistration();
|
||||
protocols[100] = new ComplexObjectRegistration();
|
||||
protocols[101] = new NormalObjectRegistration();
|
||||
protocols[102] = new ObjectARegistration();
|
||||
@@ -25,7 +29,7 @@ namespace zfoo {
|
||||
return const_cast<IProtocolRegistration *>(protocols[protocolId]);
|
||||
}
|
||||
|
||||
void write(ByteBuffer &buffer, IPacket *packet) {
|
||||
void write(ByteBuffer &buffer, IProtocol *packet) {
|
||||
auto protocolId = packet->protocolId();
|
||||
// 写入协议号
|
||||
buffer.writeShort(protocolId);
|
||||
@@ -33,7 +37,7 @@ namespace zfoo {
|
||||
getProtocol(protocolId)->write(buffer, packet);
|
||||
}
|
||||
|
||||
IPacket *read(ByteBuffer &buffer) {
|
||||
IProtocol *read(ByteBuffer &buffer) {
|
||||
auto protocolId = buffer.readShort();
|
||||
return getProtocol(protocolId)->read(buffer);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user