Skip to content

Commit

Permalink
test[typescript]: ts protocol test
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 8, 2024
1 parent 231f010 commit 52b2bfb
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 46 deletions.
11 changes: 11 additions & 0 deletions protocol/src/test/typescript/zfoots/IProtocolRegistration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import IByteBuffer from "./IByteBuffer";

interface IProtocolRegistration<T> {
protocolId(): number;

write(buffer: IByteBuffer, packet: T | null): void;

read(buffer: IByteBuffer): T | null;
}

export default IProtocolRegistration;
46 changes: 35 additions & 11 deletions protocol/src/test/typescript/zfoots/ProtocolManager.ts
Original file line number Diff line number Diff line change
@@ -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<number, any>();
const protocols = new Map<number, IProtocolRegistration<unknown>>();
const protocolIdMap = new Map<any, number>();

// 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<unknown> {
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);
Expand Down
4 changes: 4 additions & 0 deletions protocol/src/test/typescript/zfoots/buffer/ByteBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions protocol/src/test/typescript/zfoots/buffer/Long.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down
12 changes: 7 additions & 5 deletions protocol/src/test/typescript/zfoots/packet/ComplexObject.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import IByteBuffer from '../IByteBuffer';
import IProtocolRegistration from '../IProtocolRegistration';
import ObjectA from './ObjectA';

// 复杂的对象,包括了各种复杂的结构,数组,List,Set,Map
class ComplexObject {
// byte类型,最简单的整形
Expand Down Expand Up @@ -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<ComplexObject> {
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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions protocol/src/test/typescript/zfoots/packet/EmptyObject.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import IByteBuffer from '../IByteBuffer';
import IProtocolRegistration from '../IProtocolRegistration';


class EmptyObject {

}

static PROTOCOL_ID: number = 0;

export class EmptyObjectRegistration implements IProtocolRegistration<EmptyObject> {
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;
}
buffer.writeInt(-1);
}

static read(buffer: IByteBuffer): EmptyObject | null {
read(buffer: IByteBuffer): EmptyObject | null {
const length = buffer.readInt();
if (length === 0) {
return null;
Expand Down
13 changes: 8 additions & 5 deletions protocol/src/test/typescript/zfoots/packet/NormalObject.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import IByteBuffer from '../IByteBuffer';
import IProtocolRegistration from '../IProtocolRegistration';
import ObjectA from './ObjectA';

// 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法
class NormalObject {
a: number = 0;
aaa: Array<number> = [];
b: number = 0;
// 整数类型
c: number = 0;
d: number = 0;
e: number = 0;
Expand All @@ -22,14 +25,14 @@ class NormalObject {
ssss: Set<string> = new Set();
outCompatibleValue: number = 0;
outCompatibleValue2: number = 0;
}

static PROTOCOL_ID: number = 101;

export class NormalObjectRegistration implements IProtocolRegistration<NormalObject> {
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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions protocol/src/test/typescript/zfoots/packet/ObjectA.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import IByteBuffer from '../IByteBuffer';
import IProtocolRegistration from '../IProtocolRegistration';
import ObjectB from './ObjectB';


class ObjectA {
a: number = 0;
m: Map<number, string> = new Map();
objectB: ObjectB | null = null;
innerCompatibleValue: number = 0;
}

static PROTOCOL_ID: number = 102;

export class ObjectARegistration implements IProtocolRegistration<ObjectA> {
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;
Expand All @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions protocol/src/test/typescript/zfoots/packet/ObjectB.ts
Original file line number Diff line number Diff line change
@@ -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<ObjectB> {
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;
Expand All @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions protocol/src/test/typescript/zfoots/packet/SimpleObject.ts
Original file line number Diff line number Diff line change
@@ -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<SimpleObject> {
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;
Expand All @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions protocol/src/test/typescript/zfoots/packet/VeryBigObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import IByteBuffer from '../IByteBuffer';
import IProtocolRegistration from '../IProtocolRegistration';
import ObjectA from './ObjectA';


class VeryBigObject {
a1: number = 0;
aa1: number = 0;
Expand Down Expand Up @@ -3346,14 +3348,14 @@ class VeryBigObject {
mm88: Map<number, ObjectA> = new Map();
s88: Set<number> = new Set();
ssss88: Set<string> = new Set();
}

static PROTOCOL_ID: number = 1;

export class VeryBigObjectRegistration implements IProtocolRegistration<VeryBigObject> {
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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 52b2bfb

Please sign in to comment.