mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-14 10:26:26 +00:00
feat[ts]: compatible typescript inside class
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import ByteBuffer from './zfoo/buffer/ByteBuffer';
|
||||
import ProtocolManager from './zfoo/ProtocolManager';
|
||||
|
||||
import * as fs from "fs";
|
||||
function assert(flag: boolean): void {
|
||||
if (!flag) {
|
||||
throw "exception happen";
|
||||
@@ -11,7 +11,11 @@ function assert(flag: boolean): void {
|
||||
console.log("Hello world");
|
||||
|
||||
|
||||
const data = fs.readFileSync('C:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes');
|
||||
// const data = fs.readFileSync('D:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-no-compatible.bytes');
|
||||
// const data = fs.readFileSync('D:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes');
|
||||
// const data = fs.readFileSync('D:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes');
|
||||
// const data = fs.readFileSync('D:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes');
|
||||
const data = fs.readFileSync('D:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes');
|
||||
|
||||
const arrayBytes = new Uint8Array(data.length);
|
||||
data.copy(arrayBytes, 0, 0, data.length);
|
||||
@@ -21,7 +25,6 @@ byteBuffer.writeBytes(arrayBytes);
|
||||
|
||||
const packet = ProtocolManager.read(byteBuffer);
|
||||
// complexObjec是老的协议,所以序列化回来myCompatible是nil,所以要重新赋值
|
||||
packet.myCompatible = 0
|
||||
console.log(packet);
|
||||
|
||||
const newByteBuffer = new ByteBuffer();
|
||||
@@ -30,17 +33,17 @@ ProtocolManager.write(newByteBuffer, packet);
|
||||
const newPacket = ProtocolManager.read(newByteBuffer);
|
||||
console.log(newPacket);
|
||||
|
||||
assert(byteBuffer.readOffset <= newByteBuffer.writeOffset);
|
||||
|
||||
// set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
|
||||
const length = newByteBuffer.writeOffset;
|
||||
byteBuffer.setReadOffset(0);
|
||||
newByteBuffer.setReadOffset(0);
|
||||
for (let i = 0; i < length; i++) {
|
||||
assert(byteBuffer.readByte() == newByteBuffer.readByte());
|
||||
}
|
||||
|
||||
|
||||
// assert(byteBuffer.readOffset <= newByteBuffer.writeOffset);
|
||||
//
|
||||
// // set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
|
||||
// const length = newByteBuffer.writeOffset;
|
||||
// byteBuffer.setReadOffset(0);
|
||||
// newByteBuffer.setReadOffset(0);
|
||||
// for (let i = 0; i < length; i++) {
|
||||
// if (byteBuffer.readByte() !== newByteBuffer.readByte()) {
|
||||
// console.log(i);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// ByteBuffer test
|
||||
@@ -106,9 +109,3 @@ buffer = new ByteBuffer();
|
||||
const testString = 'hello world!';
|
||||
buffer.writeString(testString);
|
||||
assert(buffer.readString() == testString);
|
||||
|
||||
// char
|
||||
buffer = new ByteBuffer();
|
||||
const testChar = 'h';
|
||||
buffer.writeChar(testString);
|
||||
assert(buffer.readChar() == testChar);
|
||||
|
||||
@@ -13,8 +13,6 @@ const maxInt = 2147483647;
|
||||
const minInt = -2147483648;
|
||||
|
||||
// UTF-8编码与解码
|
||||
// const encoder = new TextEncoder();
|
||||
// const decoder = new TextDecoder();
|
||||
|
||||
// nodejs的测试环境需要用以下方式特殊处理
|
||||
const util = require('util');
|
||||
|
||||
@@ -12,35 +12,6 @@ type WasmExports = {
|
||||
|
||||
let wasm: WasmExports;
|
||||
|
||||
try {
|
||||
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 {
|
||||
/**
|
||||
* Signed zero.
|
||||
|
||||
@@ -69,7 +69,7 @@ class ComplexObject {
|
||||
return;
|
||||
}
|
||||
const beforeWriteIndex = buffer.getWriteOffset();
|
||||
buffer.writeInt(128);
|
||||
buffer.writeInt(1);
|
||||
buffer.writeByte(packet.a);
|
||||
buffer.writeByte(packet.aa);
|
||||
buffer.writeByteArray(packet.aaa);
|
||||
@@ -234,7 +234,7 @@ class ComplexObject {
|
||||
}
|
||||
buffer.writeInt(packet.myCompatible);
|
||||
buffer.writePacket(packet.myObject, 102);
|
||||
buffer.adjustPadding(128, beforeWriteIndex);
|
||||
buffer.adjustPadding(1, beforeWriteIndex);
|
||||
}
|
||||
|
||||
static read(buffer: any): ComplexObject | null {
|
||||
|
||||
@@ -21,6 +21,8 @@ class NormalObject {
|
||||
mm: Map<number, ObjectA> = new Map();
|
||||
s: Set<number> = new Set();
|
||||
ssss: Set<string> = new Set();
|
||||
outCompatibleValue: number = 0;
|
||||
outCompatibleValue2: number = 0;
|
||||
|
||||
static PROTOCOL_ID: number = 101;
|
||||
|
||||
@@ -33,7 +35,8 @@ class NormalObject {
|
||||
buffer.writeInt(0);
|
||||
return;
|
||||
}
|
||||
buffer.writeInt(-1);
|
||||
const beforeWriteIndex = buffer.getWriteOffset();
|
||||
buffer.writeInt(1);
|
||||
buffer.writeByte(packet.a);
|
||||
buffer.writeByteArray(packet.aaa);
|
||||
buffer.writeShort(packet.b);
|
||||
@@ -52,6 +55,9 @@ class NormalObject {
|
||||
buffer.writeIntPacketMap(packet.mm, 102);
|
||||
buffer.writeIntSet(packet.s);
|
||||
buffer.writeStringSet(packet.ssss);
|
||||
buffer.writeInt(packet.outCompatibleValue);
|
||||
buffer.writeInt(packet.outCompatibleValue2);
|
||||
buffer.adjustPadding(1, beforeWriteIndex);
|
||||
}
|
||||
|
||||
static read(buffer: any): NormalObject | null {
|
||||
@@ -97,6 +103,14 @@ class NormalObject {
|
||||
packet.s = set16;
|
||||
const set17 = buffer.readStringSet();
|
||||
packet.ssss = set17;
|
||||
if (length !== -1 && buffer.getReadOffset() - readIndex < length) {
|
||||
const result18 = buffer.readInt();
|
||||
packet.outCompatibleValue = result18;
|
||||
}
|
||||
if (length !== -1 && buffer.getReadOffset() - readIndex < length) {
|
||||
const result19 = buffer.readInt();
|
||||
packet.outCompatibleValue2 = result19;
|
||||
}
|
||||
if (length > 0) {
|
||||
buffer.setReadOffset(readIndex + length);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ class ObjectA {
|
||||
a: number = 0;
|
||||
m: Map<number, string> = new Map();
|
||||
objectB: ObjectB | null = null;
|
||||
innerCompatibleValue: number = 0;
|
||||
|
||||
static PROTOCOL_ID: number = 102;
|
||||
|
||||
@@ -18,10 +19,13 @@ class ObjectA {
|
||||
buffer.writeInt(0);
|
||||
return;
|
||||
}
|
||||
buffer.writeInt(-1);
|
||||
const beforeWriteIndex = buffer.getWriteOffset();
|
||||
buffer.writeInt(1);
|
||||
buffer.writeInt(packet.a);
|
||||
buffer.writeIntStringMap(packet.m);
|
||||
buffer.writePacket(packet.objectB, 103);
|
||||
buffer.writeInt(packet.innerCompatibleValue);
|
||||
buffer.adjustPadding(1, beforeWriteIndex);
|
||||
}
|
||||
|
||||
static read(buffer: any): ObjectA | null {
|
||||
@@ -37,6 +41,10 @@ class ObjectA {
|
||||
packet.m = map1;
|
||||
const result2 = buffer.readPacket(103);
|
||||
packet.objectB = result2;
|
||||
if (length !== -1 && buffer.getReadOffset() - readIndex < length) {
|
||||
const result3 = buffer.readInt();
|
||||
packet.innerCompatibleValue = result3;
|
||||
}
|
||||
if (length > 0) {
|
||||
buffer.setReadOffset(readIndex + length);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
class ObjectB {
|
||||
|
||||
flag: boolean = false;
|
||||
innerCompatibleValue: number = 0;
|
||||
|
||||
static PROTOCOL_ID: number = 103;
|
||||
|
||||
@@ -15,8 +16,11 @@ class ObjectB {
|
||||
buffer.writeInt(0);
|
||||
return;
|
||||
}
|
||||
buffer.writeInt(-1);
|
||||
const beforeWriteIndex = buffer.getWriteOffset();
|
||||
buffer.writeInt(1);
|
||||
buffer.writeBoolean(packet.flag);
|
||||
buffer.writeInt(packet.innerCompatibleValue);
|
||||
buffer.adjustPadding(1, beforeWriteIndex);
|
||||
}
|
||||
|
||||
static read(buffer: any): ObjectB | null {
|
||||
@@ -28,6 +32,10 @@ class ObjectB {
|
||||
const packet = new ObjectB();
|
||||
const result0 = buffer.readBoolean();
|
||||
packet.flag = result0;
|
||||
if (length !== -1 && buffer.getReadOffset() - readIndex < length) {
|
||||
const result1 = buffer.readInt();
|
||||
packet.innerCompatibleValue = result1;
|
||||
}
|
||||
if (length > 0) {
|
||||
buffer.setReadOffset(readIndex + length);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user