From 5358b804fff73c8d5e69eae76ac231ea8a8df687 Mon Sep 17 00:00:00 2001 From: godotg Date: Mon, 8 Jul 2024 17:07:15 +0800 Subject: [PATCH] test[ecmascript]: es protocol test --- .../ecmascript/zfooes/ProtocolManager.mjs | 38 +++++++++++++++---- .../zfooes/packet/ComplexObject.mjs | 11 +++--- .../ecmascript/zfooes/packet/EmptyObject.mjs | 11 +++--- .../ecmascript/zfooes/packet/NormalObject.mjs | 14 ++++--- .../test/ecmascript/zfooes/packet/ObjectA.mjs | 11 +++--- .../test/ecmascript/zfooes/packet/ObjectB.mjs | 11 +++--- .../ecmascript/zfooes/packet/SimpleObject.mjs | 11 +++--- .../zfooes/packet/VeryBigObject.mjs | 11 +++--- 8 files changed, 74 insertions(+), 44 deletions(-) diff --git a/protocol/src/test/ecmascript/zfooes/ProtocolManager.mjs b/protocol/src/test/ecmascript/zfooes/ProtocolManager.mjs index 1a6af9c18..616ebf17a 100644 --- a/protocol/src/test/ecmascript/zfooes/ProtocolManager.mjs +++ b/protocol/src/test/ecmascript/zfooes/ProtocolManager.mjs @@ -1,23 +1,45 @@ import EmptyObject from './packet/EmptyObject.mjs'; +import { EmptyObjectRegistration } from './packet/EmptyObject.mjs'; import VeryBigObject from './packet/VeryBigObject.mjs'; +import { VeryBigObjectRegistration } from './packet/VeryBigObject.mjs'; import ComplexObject from './packet/ComplexObject.mjs'; +import { ComplexObjectRegistration } from './packet/ComplexObject.mjs'; import NormalObject from './packet/NormalObject.mjs'; +import { NormalObjectRegistration } from './packet/NormalObject.mjs'; import ObjectA from './packet/ObjectA.mjs'; +import { ObjectARegistration } from './packet/ObjectA.mjs'; import ObjectB from './packet/ObjectB.mjs'; +import { ObjectBRegistration } from './packet/ObjectB.mjs'; import SimpleObject from './packet/SimpleObject.mjs'; +import { SimpleObjectRegistration } from './packet/SimpleObject.mjs'; const protocols = new Map(); +const protocolIdMap = new Map(); // initProtocol -protocols.set(0, EmptyObject); -protocols.set(1, VeryBigObject); -protocols.set(100, ComplexObject); -protocols.set(101, NormalObject); -protocols.set(102, ObjectA); -protocols.set(103, ObjectB); -protocols.set(104, SimpleObject); +protocols.set(0, new EmptyObjectRegistration()); +protocolIdMap.set(EmptyObject, 0); +protocols.set(1, new VeryBigObjectRegistration()); +protocolIdMap.set(VeryBigObject, 1); +protocols.set(100, new ComplexObjectRegistration()); +protocolIdMap.set(ComplexObject, 100); +protocols.set(101, new NormalObjectRegistration()); +protocolIdMap.set(NormalObject, 101); +protocols.set(102, new ObjectARegistration()); +protocolIdMap.set(ObjectA, 102); +protocols.set(103, new ObjectBRegistration()); +protocolIdMap.set(ObjectB, 103); +protocols.set(104, new SimpleObjectRegistration()); +protocolIdMap.set(SimpleObject, 104); class ProtocolManager { + static getProtocolId(clazz) { + const protocolId = protocolIdMap.get(clazz); + if (protocolId === null || protocolId === undefined) { + throw '[protocol:' + clazz + '] not exist'; + } + return protocolId; + } static getProtocol(protocolId) { const protocol = protocols.get(protocolId); if (protocol === null) { @@ -27,7 +49,7 @@ class ProtocolManager { } static write(buffer, packet) { - const protocolId = packet.protocolId(); + const protocolId = ProtocolManager.getProtocolId(packet.constructor); buffer.writeShort(protocolId); const protocol = ProtocolManager.getProtocol(protocolId); protocol.write(buffer, packet); diff --git a/protocol/src/test/ecmascript/zfooes/packet/ComplexObject.mjs b/protocol/src/test/ecmascript/zfooes/packet/ComplexObject.mjs index b2a36c950..804a46bde 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/ComplexObject.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/ComplexObject.mjs @@ -53,14 +53,14 @@ class ComplexObject { // 如果要修改协议并且兼容老协议,需要加上Compatible注解,保持Compatible注解的value自增 myCompatible = 0; // number myObject = null; // ObjectA | null +} - static PROTOCOL_ID = 100; - +export class ComplexObjectRegistration { protocolId() { - return ComplexObject.PROTOCOL_ID; + return 100; } - static write(buffer, packet) { + write(buffer, packet) { if (packet === null) { buffer.writeInt(0); return; @@ -234,7 +234,7 @@ class ComplexObject { buffer.adjustPadding(36962, beforeWriteIndex); } - static read(buffer) { + read(buffer) { const length = buffer.readInt(); if (length === 0) { return null; @@ -464,4 +464,5 @@ class ComplexObject { return packet; } } + export default ComplexObject; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/packet/EmptyObject.mjs b/protocol/src/test/ecmascript/zfooes/packet/EmptyObject.mjs index 15c74f4ba..00e1515d2 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/EmptyObject.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/EmptyObject.mjs @@ -1,14 +1,14 @@ class EmptyObject { +} - static PROTOCOL_ID = 0; - +export class EmptyObjectRegistration { protocolId() { - return EmptyObject.PROTOCOL_ID; + return 0; } - static write(buffer, packet) { + write(buffer, packet) { if (packet === null) { buffer.writeInt(0); return; @@ -16,7 +16,7 @@ class EmptyObject { buffer.writeInt(-1); } - static read(buffer) { + read(buffer) { const length = buffer.readInt(); if (length === 0) { return null; @@ -30,4 +30,5 @@ class EmptyObject { return packet; } } + export default EmptyObject; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/packet/NormalObject.mjs b/protocol/src/test/ecmascript/zfooes/packet/NormalObject.mjs index 7dd096944..52c474195 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/NormalObject.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/NormalObject.mjs @@ -1,8 +1,9 @@ - +// 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法 class NormalObject { a = 0; // number aaa = []; // Array b = 0; // number + // 整数类型 c = 0; // number d = 0; // number e = 0; // number @@ -20,14 +21,14 @@ class NormalObject { ssss = new Set(); // Set outCompatibleValue = 0; // number outCompatibleValue2 = 0; // number +} - static PROTOCOL_ID = 101; - +export class NormalObjectRegistration { protocolId() { - return NormalObject.PROTOCOL_ID; + return 101; } - static write(buffer, packet) { + write(buffer, packet) { if (packet === null) { buffer.writeInt(0); return; @@ -57,7 +58,7 @@ class NormalObject { buffer.adjustPadding(857, beforeWriteIndex); } - static read(buffer) { + read(buffer) { const length = buffer.readInt(); if (length === 0) { return null; @@ -114,4 +115,5 @@ class NormalObject { return packet; } } + export default NormalObject; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/packet/ObjectA.mjs b/protocol/src/test/ecmascript/zfooes/packet/ObjectA.mjs index b3cc59e80..51de8babe 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/ObjectA.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/ObjectA.mjs @@ -4,14 +4,14 @@ class ObjectA { m = new Map(); // Map objectB = null; // ObjectB | null innerCompatibleValue = 0; // number +} - static PROTOCOL_ID = 102; - +export class ObjectARegistration { protocolId() { - return ObjectA.PROTOCOL_ID; + return 102; } - static write(buffer, packet) { + write(buffer, packet) { if (packet === null) { buffer.writeInt(0); return; @@ -25,7 +25,7 @@ class ObjectA { buffer.adjustPadding(201, beforeWriteIndex); } - static read(buffer) { + read(buffer) { const length = buffer.readInt(); if (length === 0) { return null; @@ -48,4 +48,5 @@ class ObjectA { return packet; } } + export default ObjectA; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/packet/ObjectB.mjs b/protocol/src/test/ecmascript/zfooes/packet/ObjectB.mjs index 7590cf339..b8e271082 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/ObjectB.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/ObjectB.mjs @@ -2,14 +2,14 @@ class ObjectB { flag = false; // boolean innerCompatibleValue = 0; // number +} - static PROTOCOL_ID = 103; - +export class ObjectBRegistration { protocolId() { - return ObjectB.PROTOCOL_ID; + return 103; } - static write(buffer, packet) { + write(buffer, packet) { if (packet === null) { buffer.writeInt(0); return; @@ -21,7 +21,7 @@ class ObjectB { buffer.adjustPadding(4, beforeWriteIndex); } - static read(buffer) { + read(buffer) { const length = buffer.readInt(); if (length === 0) { return null; @@ -40,4 +40,5 @@ class ObjectB { return packet; } } + export default ObjectB; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/packet/SimpleObject.mjs b/protocol/src/test/ecmascript/zfooes/packet/SimpleObject.mjs index 7c9d7549a..7d6a0cf2d 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/SimpleObject.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/SimpleObject.mjs @@ -2,14 +2,14 @@ class SimpleObject { c = 0; // number g = false; // boolean +} - static PROTOCOL_ID = 104; - +export class SimpleObjectRegistration { protocolId() { - return SimpleObject.PROTOCOL_ID; + return 104; } - static write(buffer, packet) { + write(buffer, packet) { if (packet === null) { buffer.writeInt(0); return; @@ -19,7 +19,7 @@ class SimpleObject { buffer.writeBoolean(packet.g); } - static read(buffer) { + read(buffer) { const length = buffer.readInt(); if (length === 0) { return null; @@ -36,4 +36,5 @@ class SimpleObject { return packet; } } + export default SimpleObject; \ No newline at end of file diff --git a/protocol/src/test/ecmascript/zfooes/packet/VeryBigObject.mjs b/protocol/src/test/ecmascript/zfooes/packet/VeryBigObject.mjs index b2e0704ac..d97414085 100644 --- a/protocol/src/test/ecmascript/zfooes/packet/VeryBigObject.mjs +++ b/protocol/src/test/ecmascript/zfooes/packet/VeryBigObject.mjs @@ -3344,14 +3344,14 @@ class VeryBigObject { mm88 = new Map(); // Map s88 = new Set(); // Set ssss88 = new Set(); // Set +} - static PROTOCOL_ID = 1; - +export class VeryBigObjectRegistration { protocolId() { - return VeryBigObject.PROTOCOL_ID; + return 1; } - static write(buffer, packet) { + write(buffer, packet) { if (packet === null) { buffer.writeInt(0); return; @@ -6703,7 +6703,7 @@ class VeryBigObject { buffer.writeStringSet(packet.ssss9); } - static read(buffer) { + read(buffer) { const length = buffer.readInt(); if (length === 0) { return null; @@ -13404,4 +13404,5 @@ class VeryBigObject { return packet; } } + export default VeryBigObject; \ No newline at end of file