mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-14 12:17:06 +00:00
test[ecmascript]: es protocol test
This commit is contained in:
1 parent
6651154869
commit
5358b804ff
8 files changed
+74
-44
No files matched your search
@@ -1,23 +1,45 @@
|
|||||||
import EmptyObject from './packet/EmptyObject.mjs';
|
import EmptyObject from './packet/EmptyObject.mjs';
|
||||||
|
import { EmptyObjectRegistration } from './packet/EmptyObject.mjs';
|
||||||
import VeryBigObject from './packet/VeryBigObject.mjs';
|
import VeryBigObject from './packet/VeryBigObject.mjs';
|
||||||
|
import { VeryBigObjectRegistration } from './packet/VeryBigObject.mjs';
|
||||||
import ComplexObject from './packet/ComplexObject.mjs';
|
import ComplexObject from './packet/ComplexObject.mjs';
|
||||||
|
import { ComplexObjectRegistration } from './packet/ComplexObject.mjs';
|
||||||
import NormalObject from './packet/NormalObject.mjs';
|
import NormalObject from './packet/NormalObject.mjs';
|
||||||
|
import { NormalObjectRegistration } from './packet/NormalObject.mjs';
|
||||||
import ObjectA from './packet/ObjectA.mjs';
|
import ObjectA from './packet/ObjectA.mjs';
|
||||||
|
import { ObjectARegistration } from './packet/ObjectA.mjs';
|
||||||
import ObjectB from './packet/ObjectB.mjs';
|
import ObjectB from './packet/ObjectB.mjs';
|
||||||
|
import { ObjectBRegistration } from './packet/ObjectB.mjs';
|
||||||
import SimpleObject from './packet/SimpleObject.mjs';
|
import SimpleObject from './packet/SimpleObject.mjs';
|
||||||
|
import { SimpleObjectRegistration } from './packet/SimpleObject.mjs';
|
||||||
|
|
||||||
const protocols = new Map();
|
const protocols = new Map();
|
||||||
|
const protocolIdMap = new Map();
|
||||||
|
|
||||||
// initProtocol
|
// initProtocol
|
||||||
protocols.set(0, EmptyObject);
|
protocols.set(0, new EmptyObjectRegistration());
|
||||||
protocols.set(1, VeryBigObject);
|
protocolIdMap.set(EmptyObject, 0);
|
||||||
protocols.set(100, ComplexObject);
|
protocols.set(1, new VeryBigObjectRegistration());
|
||||||
protocols.set(101, NormalObject);
|
protocolIdMap.set(VeryBigObject, 1);
|
||||||
protocols.set(102, ObjectA);
|
protocols.set(100, new ComplexObjectRegistration());
|
||||||
protocols.set(103, ObjectB);
|
protocolIdMap.set(ComplexObject, 100);
|
||||||
protocols.set(104, SimpleObject);
|
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 {
|
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) {
|
static getProtocol(protocolId) {
|
||||||
const protocol = protocols.get(protocolId);
|
const protocol = protocols.get(protocolId);
|
||||||
if (protocol === null) {
|
if (protocol === null) {
|
||||||
@@ -27,7 +49,7 @@ class ProtocolManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static write(buffer, packet) {
|
static write(buffer, packet) {
|
||||||
const protocolId = packet.protocolId();
|
const protocolId = ProtocolManager.getProtocolId(packet.constructor);
|
||||||
buffer.writeShort(protocolId);
|
buffer.writeShort(protocolId);
|
||||||
const protocol = ProtocolManager.getProtocol(protocolId);
|
const protocol = ProtocolManager.getProtocol(protocolId);
|
||||||
protocol.write(buffer, packet);
|
protocol.write(buffer, packet);
|
||||||
|
|||||||
@@ -53,14 +53,14 @@ class ComplexObject {
|
|||||||
// 如果要修改协议并且兼容老协议,需要加上Compatible注解,保持Compatible注解的value自增
|
// 如果要修改协议并且兼容老协议,需要加上Compatible注解,保持Compatible注解的value自增
|
||||||
myCompatible = 0; // number
|
myCompatible = 0; // number
|
||||||
myObject = null; // ObjectA | null
|
myObject = null; // ObjectA | null
|
||||||
|
}
|
||||||
|
|
||||||
static PROTOCOL_ID = 100;
|
export class ComplexObjectRegistration {
|
||||||
|
|
||||||
protocolId() {
|
protocolId() {
|
||||||
return ComplexObject.PROTOCOL_ID;
|
return 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
static write(buffer, packet) {
|
write(buffer, packet) {
|
||||||
if (packet === null) {
|
if (packet === null) {
|
||||||
buffer.writeInt(0);
|
buffer.writeInt(0);
|
||||||
return;
|
return;
|
||||||
@@ -234,7 +234,7 @@ class ComplexObject {
|
|||||||
buffer.adjustPadding(36962, beforeWriteIndex);
|
buffer.adjustPadding(36962, beforeWriteIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static read(buffer) {
|
read(buffer) {
|
||||||
const length = buffer.readInt();
|
const length = buffer.readInt();
|
||||||
if (length === 0) {
|
if (length === 0) {
|
||||||
return null;
|
return null;
|
||||||
@@ -464,4 +464,5 @@ class ComplexObject {
|
|||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ComplexObject;
|
export default ComplexObject;
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
|
|
||||||
class EmptyObject {
|
class EmptyObject {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
static PROTOCOL_ID = 0;
|
export class EmptyObjectRegistration {
|
||||||
|
|
||||||
protocolId() {
|
protocolId() {
|
||||||
return EmptyObject.PROTOCOL_ID;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static write(buffer, packet) {
|
write(buffer, packet) {
|
||||||
if (packet === null) {
|
if (packet === null) {
|
||||||
buffer.writeInt(0);
|
buffer.writeInt(0);
|
||||||
return;
|
return;
|
||||||
@@ -16,7 +16,7 @@ class EmptyObject {
|
|||||||
buffer.writeInt(-1);
|
buffer.writeInt(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static read(buffer) {
|
read(buffer) {
|
||||||
const length = buffer.readInt();
|
const length = buffer.readInt();
|
||||||
if (length === 0) {
|
if (length === 0) {
|
||||||
return null;
|
return null;
|
||||||
@@ -30,4 +30,5 @@ class EmptyObject {
|
|||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default EmptyObject;
|
export default EmptyObject;
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
|
// 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法
|
||||||
class NormalObject {
|
class NormalObject {
|
||||||
a = 0; // number
|
a = 0; // number
|
||||||
aaa = []; // Array<number>
|
aaa = []; // Array<number>
|
||||||
b = 0; // number
|
b = 0; // number
|
||||||
|
// 整数类型
|
||||||
c = 0; // number
|
c = 0; // number
|
||||||
d = 0; // number
|
d = 0; // number
|
||||||
e = 0; // number
|
e = 0; // number
|
||||||
@@ -20,14 +21,14 @@ class NormalObject {
|
|||||||
ssss = new Set(); // Set<string>
|
ssss = new Set(); // Set<string>
|
||||||
outCompatibleValue = 0; // number
|
outCompatibleValue = 0; // number
|
||||||
outCompatibleValue2 = 0; // number
|
outCompatibleValue2 = 0; // number
|
||||||
|
}
|
||||||
|
|
||||||
static PROTOCOL_ID = 101;
|
export class NormalObjectRegistration {
|
||||||
|
|
||||||
protocolId() {
|
protocolId() {
|
||||||
return NormalObject.PROTOCOL_ID;
|
return 101;
|
||||||
}
|
}
|
||||||
|
|
||||||
static write(buffer, packet) {
|
write(buffer, packet) {
|
||||||
if (packet === null) {
|
if (packet === null) {
|
||||||
buffer.writeInt(0);
|
buffer.writeInt(0);
|
||||||
return;
|
return;
|
||||||
@@ -57,7 +58,7 @@ class NormalObject {
|
|||||||
buffer.adjustPadding(857, beforeWriteIndex);
|
buffer.adjustPadding(857, beforeWriteIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static read(buffer) {
|
read(buffer) {
|
||||||
const length = buffer.readInt();
|
const length = buffer.readInt();
|
||||||
if (length === 0) {
|
if (length === 0) {
|
||||||
return null;
|
return null;
|
||||||
@@ -114,4 +115,5 @@ class NormalObject {
|
|||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default NormalObject;
|
export default NormalObject;
|
||||||
@@ -4,14 +4,14 @@ class ObjectA {
|
|||||||
m = new Map(); // Map<number, string>
|
m = new Map(); // Map<number, string>
|
||||||
objectB = null; // ObjectB | null
|
objectB = null; // ObjectB | null
|
||||||
innerCompatibleValue = 0; // number
|
innerCompatibleValue = 0; // number
|
||||||
|
}
|
||||||
|
|
||||||
static PROTOCOL_ID = 102;
|
export class ObjectARegistration {
|
||||||
|
|
||||||
protocolId() {
|
protocolId() {
|
||||||
return ObjectA.PROTOCOL_ID;
|
return 102;
|
||||||
}
|
}
|
||||||
|
|
||||||
static write(buffer, packet) {
|
write(buffer, packet) {
|
||||||
if (packet === null) {
|
if (packet === null) {
|
||||||
buffer.writeInt(0);
|
buffer.writeInt(0);
|
||||||
return;
|
return;
|
||||||
@@ -25,7 +25,7 @@ class ObjectA {
|
|||||||
buffer.adjustPadding(201, beforeWriteIndex);
|
buffer.adjustPadding(201, beforeWriteIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static read(buffer) {
|
read(buffer) {
|
||||||
const length = buffer.readInt();
|
const length = buffer.readInt();
|
||||||
if (length === 0) {
|
if (length === 0) {
|
||||||
return null;
|
return null;
|
||||||
@@ -48,4 +48,5 @@ class ObjectA {
|
|||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ObjectA;
|
export default ObjectA;
|
||||||
@@ -2,14 +2,14 @@
|
|||||||
class ObjectB {
|
class ObjectB {
|
||||||
flag = false; // boolean
|
flag = false; // boolean
|
||||||
innerCompatibleValue = 0; // number
|
innerCompatibleValue = 0; // number
|
||||||
|
}
|
||||||
|
|
||||||
static PROTOCOL_ID = 103;
|
export class ObjectBRegistration {
|
||||||
|
|
||||||
protocolId() {
|
protocolId() {
|
||||||
return ObjectB.PROTOCOL_ID;
|
return 103;
|
||||||
}
|
}
|
||||||
|
|
||||||
static write(buffer, packet) {
|
write(buffer, packet) {
|
||||||
if (packet === null) {
|
if (packet === null) {
|
||||||
buffer.writeInt(0);
|
buffer.writeInt(0);
|
||||||
return;
|
return;
|
||||||
@@ -21,7 +21,7 @@ class ObjectB {
|
|||||||
buffer.adjustPadding(4, beforeWriteIndex);
|
buffer.adjustPadding(4, beforeWriteIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static read(buffer) {
|
read(buffer) {
|
||||||
const length = buffer.readInt();
|
const length = buffer.readInt();
|
||||||
if (length === 0) {
|
if (length === 0) {
|
||||||
return null;
|
return null;
|
||||||
@@ -40,4 +40,5 @@ class ObjectB {
|
|||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ObjectB;
|
export default ObjectB;
|
||||||
@@ -2,14 +2,14 @@
|
|||||||
class SimpleObject {
|
class SimpleObject {
|
||||||
c = 0; // number
|
c = 0; // number
|
||||||
g = false; // boolean
|
g = false; // boolean
|
||||||
|
}
|
||||||
|
|
||||||
static PROTOCOL_ID = 104;
|
export class SimpleObjectRegistration {
|
||||||
|
|
||||||
protocolId() {
|
protocolId() {
|
||||||
return SimpleObject.PROTOCOL_ID;
|
return 104;
|
||||||
}
|
}
|
||||||
|
|
||||||
static write(buffer, packet) {
|
write(buffer, packet) {
|
||||||
if (packet === null) {
|
if (packet === null) {
|
||||||
buffer.writeInt(0);
|
buffer.writeInt(0);
|
||||||
return;
|
return;
|
||||||
@@ -19,7 +19,7 @@ class SimpleObject {
|
|||||||
buffer.writeBoolean(packet.g);
|
buffer.writeBoolean(packet.g);
|
||||||
}
|
}
|
||||||
|
|
||||||
static read(buffer) {
|
read(buffer) {
|
||||||
const length = buffer.readInt();
|
const length = buffer.readInt();
|
||||||
if (length === 0) {
|
if (length === 0) {
|
||||||
return null;
|
return null;
|
||||||
@@ -36,4 +36,5 @@ class SimpleObject {
|
|||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default SimpleObject;
|
export default SimpleObject;
|
||||||
@@ -3344,14 +3344,14 @@ class VeryBigObject {
|
|||||||
mm88 = new Map(); // Map<number, ObjectA>
|
mm88 = new Map(); // Map<number, ObjectA>
|
||||||
s88 = new Set(); // Set<number>
|
s88 = new Set(); // Set<number>
|
||||||
ssss88 = new Set(); // Set<string>
|
ssss88 = new Set(); // Set<string>
|
||||||
|
}
|
||||||
|
|
||||||
static PROTOCOL_ID = 1;
|
export class VeryBigObjectRegistration {
|
||||||
|
|
||||||
protocolId() {
|
protocolId() {
|
||||||
return VeryBigObject.PROTOCOL_ID;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static write(buffer, packet) {
|
write(buffer, packet) {
|
||||||
if (packet === null) {
|
if (packet === null) {
|
||||||
buffer.writeInt(0);
|
buffer.writeInt(0);
|
||||||
return;
|
return;
|
||||||
@@ -6703,7 +6703,7 @@ class VeryBigObject {
|
|||||||
buffer.writeStringSet(packet.ssss9);
|
buffer.writeStringSet(packet.ssss9);
|
||||||
}
|
}
|
||||||
|
|
||||||
static read(buffer) {
|
read(buffer) {
|
||||||
const length = buffer.readInt();
|
const length = buffer.readInt();
|
||||||
if (length === 0) {
|
if (length === 0) {
|
||||||
return null;
|
return null;
|
||||||
@@ -13404,4 +13404,5 @@ class VeryBigObject {
|
|||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default VeryBigObject;
|
export default VeryBigObject;
|
||||||
Reference in new issue
Block a user