mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-25 09:38:07 +00:00
feat[protocol]: 支持C++序列化协议
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,57 @@
|
||||
#ifndef ZFOO_{}_H
|
||||
#define ZFOO_{}_H
|
||||
|
||||
#include "zfoocpp/ByteBuffer.h"
|
||||
{}
|
||||
namespace zfoo {
|
||||
|
||||
{}
|
||||
class {} : public IPacket {
|
||||
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, IPacket *packet) override {
|
||||
if (buffer.writePacketFlag(packet)) {
|
||||
return;
|
||||
}
|
||||
auto *message = ({} *) packet;
|
||||
{}
|
||||
}
|
||||
|
||||
IPacket *read(ByteBuffer &buffer) override {
|
||||
auto *packet = new {}();
|
||||
if (!buffer.readBool()) {
|
||||
return packet;
|
||||
}
|
||||
{}
|
||||
return packet;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef ZFOO_PROTOCOLMANAGER_H
|
||||
#define ZFOO_PROTOCOLMANAGER_H
|
||||
|
||||
#include "ByteBuffer.h"
|
||||
{}
|
||||
namespace zfoo {
|
||||
|
||||
const int16_t MAX_PROTOCOL_NUM = 32767;
|
||||
const IProtocolRegistration *protocols[MAX_PROTOCOL_NUM];
|
||||
|
||||
void initProtocol() {
|
||||
{}
|
||||
}
|
||||
|
||||
inline IProtocolRegistration *getProtocol(int16_t protocolId) {
|
||||
return const_cast<IProtocolRegistration *>(protocols[protocolId]);
|
||||
}
|
||||
|
||||
void write(ByteBuffer &buffer, IPacket *packet) {
|
||||
auto protocolId = packet->protocolId();
|
||||
// 写入协议号
|
||||
buffer.writeShort(protocolId);
|
||||
// 写入包体
|
||||
getProtocol(protocolId)->write(buffer, packet);
|
||||
}
|
||||
|
||||
IPacket *read(ByteBuffer &buffer) {
|
||||
auto protocolId = buffer.readShort();
|
||||
return getProtocol(protocolId)->read(buffer);
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user