Skip to content

Commit

Permalink
test[ecmascript]: es protocol test
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 8, 2024
1 parent 6651154 commit 5358b80
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 44 deletions.
38 changes: 30 additions & 8 deletions protocol/src/test/ecmascript/zfooes/ProtocolManager.mjs
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions protocol/src/test/ecmascript/zfooes/packet/ComplexObject.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -234,7 +234,7 @@ class ComplexObject {
buffer.adjustPadding(36962, beforeWriteIndex);
}

static read(buffer) {
read(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
Expand Down Expand Up @@ -464,4 +464,5 @@ class ComplexObject {
return packet;
}
}

export default ComplexObject;
11 changes: 6 additions & 5 deletions protocol/src/test/ecmascript/zfooes/packet/EmptyObject.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@

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;
}
buffer.writeInt(-1);
}

static read(buffer) {
read(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
Expand All @@ -30,4 +30,5 @@ class EmptyObject {
return packet;
}
}

export default EmptyObject;
14 changes: 8 additions & 6 deletions protocol/src/test/ecmascript/zfooes/packet/NormalObject.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

// 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法
class NormalObject {
a = 0; // number
aaa = []; // Array<number>
b = 0; // number
// 整数类型
c = 0; // number
d = 0; // number
e = 0; // number
Expand All @@ -20,14 +21,14 @@ class NormalObject {
ssss = new Set(); // Set<string>
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;
Expand Down Expand Up @@ -57,7 +58,7 @@ class NormalObject {
buffer.adjustPadding(857, beforeWriteIndex);
}

static read(buffer) {
read(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
Expand Down Expand Up @@ -114,4 +115,5 @@ class NormalObject {
return packet;
}
}

export default NormalObject;
11 changes: 6 additions & 5 deletions protocol/src/test/ecmascript/zfooes/packet/ObjectA.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class ObjectA {
m = new Map(); // Map<number, string>
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;
Expand All @@ -25,7 +25,7 @@ class ObjectA {
buffer.adjustPadding(201, beforeWriteIndex);
}

static read(buffer) {
read(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
Expand All @@ -48,4 +48,5 @@ class ObjectA {
return packet;
}
}

export default ObjectA;
11 changes: 6 additions & 5 deletions protocol/src/test/ecmascript/zfooes/packet/ObjectB.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +21,7 @@ class ObjectB {
buffer.adjustPadding(4, beforeWriteIndex);
}

static read(buffer) {
read(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
Expand All @@ -40,4 +40,5 @@ class ObjectB {
return packet;
}
}

export default ObjectB;
11 changes: 6 additions & 5 deletions protocol/src/test/ecmascript/zfooes/packet/SimpleObject.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +19,7 @@ class SimpleObject {
buffer.writeBoolean(packet.g);
}

static read(buffer) {
read(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
Expand All @@ -36,4 +36,5 @@ class SimpleObject {
return packet;
}
}

export default SimpleObject;
11 changes: 6 additions & 5 deletions protocol/src/test/ecmascript/zfooes/packet/VeryBigObject.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3344,14 +3344,14 @@ class VeryBigObject {
mm88 = new Map(); // Map<number, ObjectA>
s88 = new Set(); // Set<number>
ssss88 = new Set(); // Set<string>
}

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;
Expand Down Expand Up @@ -6703,7 +6703,7 @@ class VeryBigObject {
buffer.writeStringSet(packet.ssss9);
}

static read(buffer) {
read(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
Expand Down Expand Up @@ -13404,4 +13404,5 @@ class VeryBigObject {
return packet;
}
}

export default VeryBigObject;

0 comments on commit 5358b80

Please sign in to comment.