typescript.ProtocolManagerTemplate.ts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protocol Show documentation
Show all versions of protocol Show documentation
zfoo protocol is binary serialization framework for Java/C++/js/ts/C#/Go/Lua/GDScript/Python
The newest version!
${protocol_imports}
import IByteBuffer from "./IByteBuffer";
import IProtocolRegistration from "./IProtocolRegistration";
const protocols = new Map>();
const protocolIdMap = new Map();
// initProtocol
${protocol_manager_registrations}
class ProtocolManager {
static getProtocolId(clazz: any): number {
const protocolId = protocolIdMap.get(clazz);
if (protocolId === null || protocolId === undefined) {
throw '[protocol:' + clazz + '] not exist';
}
return protocolId;
}
static getProtocol(protocolId: number): IProtocolRegistration {
const protocol = protocols.get(protocolId);
if (protocol === null || protocol === undefined) {
throw '[protocolId:' + protocolId + '] not exist';
}
return protocol;
}
static write(buffer: IByteBuffer, packet: any): void {
const protocolId = ProtocolManager.getProtocolId(packet.constructor);
buffer.writeShort(protocolId);
const protocol = ProtocolManager.getProtocol(protocolId);
protocol.write(buffer, packet);
}
static read(buffer: IByteBuffer): any {
const protocolId = buffer.readShort();
const protocol = ProtocolManager.getProtocol(protocolId);
const packet = protocol.read(buffer);
return packet;
}
}
export default ProtocolManager;