All Downloads are FREE. Search and download functionalities are using the official Maven repository.

typescript.ProtocolManagerTemplate.ts Maven / Gradle / Ivy

Go to download

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;




© 2015 - 2024 Weber Informatics LLC | Privacy Policy