From 52b2bfbd8271b026b83f642f8ddb9e8fd8ff5f7d Mon Sep 17 00:00:00 2001 From: godotg Date: Mon, 8 Jul 2024 16:20:29 +0800 Subject: [PATCH] test[typescript]: ts protocol test --- .../zfoots/IProtocolRegistration.ts | 11 +++++ .../test/typescript/zfoots/ProtocolManager.ts | 46 ++++++++++++++----- .../typescript/zfoots/buffer/ByteBuffer.ts | 4 ++ .../src/test/typescript/zfoots/buffer/Long.ts | 29 ++++++++++++ .../typescript/zfoots/packet/ComplexObject.ts | 12 +++-- .../typescript/zfoots/packet/EmptyObject.ts | 12 +++-- .../typescript/zfoots/packet/NormalObject.ts | 13 ++++-- .../test/typescript/zfoots/packet/ObjectA.ts | 12 +++-- .../test/typescript/zfoots/packet/ObjectB.ts | 12 +++-- .../typescript/zfoots/packet/SimpleObject.ts | 12 +++-- .../typescript/zfoots/packet/VeryBigObject.ts | 12 +++-- 11 files changed, 129 insertions(+), 46 deletions(-) create mode 100644 protocol/src/test/typescript/zfoots/IProtocolRegistration.ts diff --git a/protocol/src/test/typescript/zfoots/IProtocolRegistration.ts b/protocol/src/test/typescript/zfoots/IProtocolRegistration.ts new file mode 100644 index 000000000..ef4256d3b --- /dev/null +++ b/protocol/src/test/typescript/zfoots/IProtocolRegistration.ts @@ -0,0 +1,11 @@ +import IByteBuffer from "./IByteBuffer"; + +interface IProtocolRegistration { + protocolId(): number; + + write(buffer: IByteBuffer, packet: T | null): void; + + read(buffer: IByteBuffer): T | null; +} + +export default IProtocolRegistration; \ No newline at end of file diff --git a/protocol/src/test/typescript/zfoots/ProtocolManager.ts b/protocol/src/test/typescript/zfoots/ProtocolManager.ts index 101630c17..f8f3f2c4b 100644 --- a/protocol/src/test/typescript/zfoots/ProtocolManager.ts +++ b/protocol/src/test/typescript/zfoots/ProtocolManager.ts @@ -1,34 +1,58 @@ import EmptyObject from './packet/EmptyObject'; +import { EmptyObjectRegistration } from './packet/EmptyObject'; import VeryBigObject from './packet/VeryBigObject'; +import { VeryBigObjectRegistration } from './packet/VeryBigObject'; import ComplexObject from './packet/ComplexObject'; +import { ComplexObjectRegistration } from './packet/ComplexObject'; import NormalObject from './packet/NormalObject'; +import { NormalObjectRegistration } from './packet/NormalObject'; import ObjectA from './packet/ObjectA'; +import { ObjectARegistration } from './packet/ObjectA'; import ObjectB from './packet/ObjectB'; +import { ObjectBRegistration } from './packet/ObjectB'; import SimpleObject from './packet/SimpleObject'; +import { SimpleObjectRegistration } from './packet/SimpleObject'; import IByteBuffer from "./IByteBuffer"; +import IProtocolRegistration from "./IProtocolRegistration"; -const protocols = new Map(); +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 getProtocol(protocolId: number): any { + 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) { + if (protocol === null || protocol === undefined) { throw '[protocolId:' + protocolId + '] not exist'; } return protocol; } static write(buffer: IByteBuffer, packet: any): void { - 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/typescript/zfoots/buffer/ByteBuffer.ts b/protocol/src/test/typescript/zfoots/buffer/ByteBuffer.ts index f93aecd58..48edd1752 100644 --- a/protocol/src/test/typescript/zfoots/buffer/ByteBuffer.ts +++ b/protocol/src/test/typescript/zfoots/buffer/ByteBuffer.ts @@ -22,6 +22,10 @@ const util = require('util'); const encoder = new util.TextEncoder('utf-8'); const decoder = new util.TextDecoder('utf-8'); +// 现在所有主流浏览器都支持TextDecoder,只有微信小程序不支持TextDecoder(微信浏览器也支持,微信的小程序和浏览器不是同一个js环境) +// https://developers.weixin.qq.com/community/develop/doc/000ca85023ce78c8484e0d1d256400 +// 如果在微信小程序中使用,需要按照上面的链接全局引入TextEncoder相关依赖 + // 在js中long可以支持的最大值 // const maxLong = 9007199254740992; // const minLong = -9007199254740992; diff --git a/protocol/src/test/typescript/zfoots/buffer/Long.ts b/protocol/src/test/typescript/zfoots/buffer/Long.ts index 54d8f479e..86e76ee2f 100644 --- a/protocol/src/test/typescript/zfoots/buffer/Long.ts +++ b/protocol/src/test/typescript/zfoots/buffer/Long.ts @@ -12,6 +12,35 @@ type WasmExports = { let wasm: WasmExports; +try { + // nodejs环境无法使用 + // wasm = new WebAssembly.Instance( + // new WebAssembly.Module( + // new Uint8Array([ + // 0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, + // 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, + // 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, + // 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, + // 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, + // 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, + // 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, + // 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, + // 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, + // 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, + // 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, + // 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, + // 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, + // 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, + // 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, + // 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, + // 11, + // ]) + // ), + // {} + // ).exports as WasmExports; +} catch { + // no wasm support +} export class Long { /** diff --git a/protocol/src/test/typescript/zfoots/packet/ComplexObject.ts b/protocol/src/test/typescript/zfoots/packet/ComplexObject.ts index 361e67b64..02efdec88 100644 --- a/protocol/src/test/typescript/zfoots/packet/ComplexObject.ts +++ b/protocol/src/test/typescript/zfoots/packet/ComplexObject.ts @@ -1,5 +1,7 @@ import IByteBuffer from '../IByteBuffer'; +import IProtocolRegistration from '../IProtocolRegistration'; import ObjectA from './ObjectA'; + // 复杂的对象,包括了各种复杂的结构,数组,List,Set,Map class ComplexObject { // byte类型,最简单的整形 @@ -55,14 +57,14 @@ class ComplexObject { // 如果要修改协议并且兼容老协议,需要加上Compatible注解,保持Compatible注解的value自增 myCompatible: number = 0; myObject: ObjectA | null = null; +} - static PROTOCOL_ID: number = 100; - +export class ComplexObjectRegistration implements IProtocolRegistration { protocolId(): number { - return ComplexObject.PROTOCOL_ID; + return 100; } - static write(buffer: IByteBuffer, packet: ComplexObject | null) { + write(buffer: IByteBuffer, packet: ComplexObject | null) { if (packet === null) { buffer.writeInt(0); return; @@ -236,7 +238,7 @@ class ComplexObject { buffer.adjustPadding(36962, beforeWriteIndex); } - static read(buffer: IByteBuffer): ComplexObject | null { + read(buffer: IByteBuffer): ComplexObject | null { const length = buffer.readInt(); if (length === 0) { return null; diff --git a/protocol/src/test/typescript/zfoots/packet/EmptyObject.ts b/protocol/src/test/typescript/zfoots/packet/EmptyObject.ts index 83288c31a..944166889 100644 --- a/protocol/src/test/typescript/zfoots/packet/EmptyObject.ts +++ b/protocol/src/test/typescript/zfoots/packet/EmptyObject.ts @@ -1,15 +1,17 @@ import IByteBuffer from '../IByteBuffer'; +import IProtocolRegistration from '../IProtocolRegistration'; + class EmptyObject { +} - static PROTOCOL_ID: number = 0; - +export class EmptyObjectRegistration implements IProtocolRegistration { protocolId(): number { - return EmptyObject.PROTOCOL_ID; + return 0; } - static write(buffer: IByteBuffer, packet: EmptyObject | null) { + write(buffer: IByteBuffer, packet: EmptyObject | null) { if (packet === null) { buffer.writeInt(0); return; @@ -17,7 +19,7 @@ class EmptyObject { buffer.writeInt(-1); } - static read(buffer: IByteBuffer): EmptyObject | null { + read(buffer: IByteBuffer): EmptyObject | null { const length = buffer.readInt(); if (length === 0) { return null; diff --git a/protocol/src/test/typescript/zfoots/packet/NormalObject.ts b/protocol/src/test/typescript/zfoots/packet/NormalObject.ts index d4e54be41..25ed1bc32 100644 --- a/protocol/src/test/typescript/zfoots/packet/NormalObject.ts +++ b/protocol/src/test/typescript/zfoots/packet/NormalObject.ts @@ -1,10 +1,13 @@ import IByteBuffer from '../IByteBuffer'; +import IProtocolRegistration from '../IProtocolRegistration'; import ObjectA from './ObjectA'; +// 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法 class NormalObject { a: number = 0; aaa: Array = []; b: number = 0; + // 整数类型 c: number = 0; d: number = 0; e: number = 0; @@ -22,14 +25,14 @@ class NormalObject { ssss: Set = new Set(); outCompatibleValue: number = 0; outCompatibleValue2: number = 0; +} - static PROTOCOL_ID: number = 101; - +export class NormalObjectRegistration implements IProtocolRegistration { protocolId(): number { - return NormalObject.PROTOCOL_ID; + return 101; } - static write(buffer: IByteBuffer, packet: NormalObject | null) { + write(buffer: IByteBuffer, packet: NormalObject | null) { if (packet === null) { buffer.writeInt(0); return; @@ -59,7 +62,7 @@ class NormalObject { buffer.adjustPadding(857, beforeWriteIndex); } - static read(buffer: IByteBuffer): NormalObject | null { + read(buffer: IByteBuffer): NormalObject | null { const length = buffer.readInt(); if (length === 0) { return null; diff --git a/protocol/src/test/typescript/zfoots/packet/ObjectA.ts b/protocol/src/test/typescript/zfoots/packet/ObjectA.ts index d914a497c..c2c056bb0 100644 --- a/protocol/src/test/typescript/zfoots/packet/ObjectA.ts +++ b/protocol/src/test/typescript/zfoots/packet/ObjectA.ts @@ -1,19 +1,21 @@ import IByteBuffer from '../IByteBuffer'; +import IProtocolRegistration from '../IProtocolRegistration'; import ObjectB from './ObjectB'; + class ObjectA { a: number = 0; m: Map = new Map(); objectB: ObjectB | null = null; innerCompatibleValue: number = 0; +} - static PROTOCOL_ID: number = 102; - +export class ObjectARegistration implements IProtocolRegistration { protocolId(): number { - return ObjectA.PROTOCOL_ID; + return 102; } - static write(buffer: IByteBuffer, packet: ObjectA | null) { + write(buffer: IByteBuffer, packet: ObjectA | null) { if (packet === null) { buffer.writeInt(0); return; @@ -27,7 +29,7 @@ class ObjectA { buffer.adjustPadding(201, beforeWriteIndex); } - static read(buffer: IByteBuffer): ObjectA | null { + read(buffer: IByteBuffer): ObjectA | null { const length = buffer.readInt(); if (length === 0) { return null; diff --git a/protocol/src/test/typescript/zfoots/packet/ObjectB.ts b/protocol/src/test/typescript/zfoots/packet/ObjectB.ts index 04d5eb71a..dc4daf20b 100644 --- a/protocol/src/test/typescript/zfoots/packet/ObjectB.ts +++ b/protocol/src/test/typescript/zfoots/packet/ObjectB.ts @@ -1,16 +1,18 @@ import IByteBuffer from '../IByteBuffer'; +import IProtocolRegistration from '../IProtocolRegistration'; + class ObjectB { flag: boolean = false; innerCompatibleValue: number = 0; +} - static PROTOCOL_ID: number = 103; - +export class ObjectBRegistration implements IProtocolRegistration { protocolId(): number { - return ObjectB.PROTOCOL_ID; + return 103; } - static write(buffer: IByteBuffer, packet: ObjectB | null) { + write(buffer: IByteBuffer, packet: ObjectB | null) { if (packet === null) { buffer.writeInt(0); return; @@ -22,7 +24,7 @@ class ObjectB { buffer.adjustPadding(4, beforeWriteIndex); } - static read(buffer: IByteBuffer): ObjectB | null { + read(buffer: IByteBuffer): ObjectB | null { const length = buffer.readInt(); if (length === 0) { return null; diff --git a/protocol/src/test/typescript/zfoots/packet/SimpleObject.ts b/protocol/src/test/typescript/zfoots/packet/SimpleObject.ts index 1a2f4326e..db01b7671 100644 --- a/protocol/src/test/typescript/zfoots/packet/SimpleObject.ts +++ b/protocol/src/test/typescript/zfoots/packet/SimpleObject.ts @@ -1,16 +1,18 @@ import IByteBuffer from '../IByteBuffer'; +import IProtocolRegistration from '../IProtocolRegistration'; + class SimpleObject { c: number = 0; g: boolean = false; +} - static PROTOCOL_ID: number = 104; - +export class SimpleObjectRegistration implements IProtocolRegistration { protocolId(): number { - return SimpleObject.PROTOCOL_ID; + return 104; } - static write(buffer: IByteBuffer, packet: SimpleObject | null) { + write(buffer: IByteBuffer, packet: SimpleObject | null) { if (packet === null) { buffer.writeInt(0); return; @@ -20,7 +22,7 @@ class SimpleObject { buffer.writeBoolean(packet.g); } - static read(buffer: IByteBuffer): SimpleObject | null { + read(buffer: IByteBuffer): SimpleObject | null { const length = buffer.readInt(); if (length === 0) { return null; diff --git a/protocol/src/test/typescript/zfoots/packet/VeryBigObject.ts b/protocol/src/test/typescript/zfoots/packet/VeryBigObject.ts index 1a2754b88..9fe2dcda0 100644 --- a/protocol/src/test/typescript/zfoots/packet/VeryBigObject.ts +++ b/protocol/src/test/typescript/zfoots/packet/VeryBigObject.ts @@ -1,6 +1,8 @@ import IByteBuffer from '../IByteBuffer'; +import IProtocolRegistration from '../IProtocolRegistration'; import ObjectA from './ObjectA'; + class VeryBigObject { a1: number = 0; aa1: number = 0; @@ -3346,14 +3348,14 @@ class VeryBigObject { mm88: Map = new Map(); s88: Set = new Set(); ssss88: Set = new Set(); +} - static PROTOCOL_ID: number = 1; - +export class VeryBigObjectRegistration implements IProtocolRegistration { protocolId(): number { - return VeryBigObject.PROTOCOL_ID; + return 1; } - static write(buffer: IByteBuffer, packet: VeryBigObject | null) { + write(buffer: IByteBuffer, packet: VeryBigObject | null) { if (packet === null) { buffer.writeInt(0); return; @@ -6705,7 +6707,7 @@ class VeryBigObject { buffer.writeStringSet(packet.ssss9); } - static read(buffer: IByteBuffer): VeryBigObject | null { + read(buffer: IByteBuffer): VeryBigObject | null { const length = buffer.readInt(); if (length === 0) { return null;