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