test[ecmascript]: es protocol test

This commit is contained in:
godotg
2024-07-08 17:07:15 +08:00
parent 6651154869
commit 5358b804ff
8 changed files with 74 additions and 44 deletions
@@ -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);
@@ -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;
@@ -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;
@@ -1,8 +1,9 @@
// 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法
class NormalObject {
a = 0; // number
aaa = []; // Array<number>
b = 0; // number
// 整数类型
c = 0; // number
d = 0; // number
e = 0; // number
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;