mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-22 08:25:24 +00:00
perf[protocol]: 使用模板生成协议
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
#ifndef ZFOO_PROTOCOLMANAGER_H
|
||||
#define ZFOO_PROTOCOLMANAGER_H
|
||||
|
||||
#include "ByteBuffer.h"
|
||||
#include "cppProtocol/packet/ComplexObject.h"
|
||||
#include "cppProtocol/packet/NormalObject.h"
|
||||
#include "cppProtocol/packet/ObjectA.h"
|
||||
#include "cppProtocol/packet/ObjectB.h"
|
||||
#include "cppProtocol/packet/SimpleObject.h"
|
||||
|
||||
namespace zfoo {
|
||||
|
||||
const int16_t MAX_PROTOCOL_NUM = 32767;
|
||||
const IProtocolRegistration *protocols[MAX_PROTOCOL_NUM];
|
||||
|
||||
void initProtocol() {
|
||||
protocols[100] = new ComplexObjectRegistration();
|
||||
protocols[101] = new NormalObjectRegistration();
|
||||
protocols[102] = new ObjectARegistration();
|
||||
protocols[103] = new ObjectBRegistration();
|
||||
protocols[104] = new SimpleObjectRegistration();
|
||||
}
|
||||
|
||||
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