diff --git a/protocol/src/test/typescript/main.ts b/protocol/src/test/typescript/main.ts index 372a1859..d46c3436 100644 --- a/protocol/src/test/typescript/main.ts +++ b/protocol/src/test/typescript/main.ts @@ -7,15 +7,22 @@ function assert(flag: boolean): void { } } +// 如果是在idea中运行,需要先安装插件:Run Configuration for TypeScript +// 如果是在idea中运行,需要先安装插件:Run Configuration for TypeScript +// 如果是在idea中运行,需要先安装插件:Run Configuration for TypeScript +// 如果是在idea中运行,需要先安装插件:Run Configuration for TypeScript +// 如果是在idea中运行,需要先安装插件:Run Configuration for TypeScript +// 如果是在idea中运行,需要先安装插件:Run Configuration for TypeScript +// 如果是在idea中运行,需要先安装插件:Run Configuration for TypeScript // 如果是在idea中运行,需要先安装插件:Run Configuration for TypeScript console.log("Hello world"); -// 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 data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-no-compatible.bytes'); +// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes'); +// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes'); +// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes'); +const data = fs.readFileSync('D:\\Project\\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); @@ -32,7 +39,8 @@ ProtocolManager.write(newByteBuffer, packet); const newPacket = ProtocolManager.read(newByteBuffer); console.log(newPacket); - +console.log("source size " + byteBuffer.getWriteOffset()); +console.log("target size " + newByteBuffer.getWriteOffset()); // assert(byteBuffer.readOffset <= newByteBuffer.writeOffset); // // // set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的 diff --git a/protocol/src/test/typescript/zfoots/buffer/ByteBuffer.ts b/protocol/src/test/typescript/zfoots/buffer/ByteBuffer.ts index 2ecc7f6b..6fddbc1a 100644 --- a/protocol/src/test/typescript/zfoots/buffer/ByteBuffer.ts +++ b/protocol/src/test/typescript/zfoots/buffer/ByteBuffer.ts @@ -13,11 +13,13 @@ const maxInt = 2147483647; const minInt = -2147483648; // UTF-8编码与解码 +const encoder = new TextEncoder(); +const decoder = new TextDecoder(); // nodejs的测试环境需要用以下方式特殊处理 -const util = require('util'); -const encoder = new util.TextEncoder('utf-8'); -const decoder = new util.TextDecoder('utf-8'); +// const util = require('util'); +// const encoder = new util.TextEncoder('utf-8'); +// const decoder = new util.TextDecoder('utf-8'); // 在js中long可以支持的最大值 // const maxLong = 9007199254740992; @@ -73,6 +75,10 @@ class ByteBuffer { } } + compatibleRead(beforeReadIndex: number, length: number): boolean { + return length !== -1 && this.getReadOffset() < length + beforeReadIndex; + } + setWriteOffset(writeOffset: number): void { if (writeOffset > this.buffer.byteLength) { throw new Error('index out of bounds exception:readerIndex:' + this.readOffset + @@ -237,6 +243,7 @@ class ByteBuffer { if (!(minInt <= value && value <= maxInt)) { throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647'); } + value = encodeZigzagInt(value); if (value >>> 7 === 0) { return 1; } diff --git a/protocol/src/test/typescript/zfoots/buffer/Long.ts b/protocol/src/test/typescript/zfoots/buffer/Long.ts index 7acaf35f..dac9a00b 100644 --- a/protocol/src/test/typescript/zfoots/buffer/Long.ts +++ b/protocol/src/test/typescript/zfoots/buffer/Long.ts @@ -12,6 +12,35 @@ 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. diff --git a/protocol/src/test/typescript/zfoots/packet/NormalObject.ts b/protocol/src/test/typescript/zfoots/packet/NormalObject.ts index 8980c1c5..0286e9bd 100644 --- a/protocol/src/test/typescript/zfoots/packet/NormalObject.ts +++ b/protocol/src/test/typescript/zfoots/packet/NormalObject.ts @@ -22,7 +22,6 @@ class NormalObject { s: Set = new Set(); ssss: Set = new Set(); outCompatibleValue: number = 0; - outCompatibleValue2: number = 0; static PROTOCOL_ID: number = 101; @@ -36,7 +35,7 @@ class NormalObject { return; } const beforeWriteIndex = buffer.getWriteOffset(); - buffer.writeInt(857); + buffer.writeInt(854); buffer.writeByte(packet.a); buffer.writeByteArray(packet.aaa); buffer.writeShort(packet.b); @@ -56,8 +55,7 @@ class NormalObject { buffer.writeIntSet(packet.s); buffer.writeStringSet(packet.ssss); buffer.writeInt(packet.outCompatibleValue); - buffer.writeInt(packet.outCompatibleValue2); - buffer.adjustPadding(857, beforeWriteIndex); + buffer.adjustPadding(854, beforeWriteIndex); } static read(buffer: any): NormalObject | null { @@ -107,10 +105,6 @@ class NormalObject { const result18 = buffer.readInt(); packet.outCompatibleValue = result18; } - if (buffer.compatibleRead(beforeReadIndex, length)) { - const result19 = buffer.readInt(); - packet.outCompatibleValue2 = result19; - } if (length > 0) { buffer.setReadOffset(beforeReadIndex + length); } diff --git a/protocol/src/test/typescript/zfoots/packet/ObjectA.ts b/protocol/src/test/typescript/zfoots/packet/ObjectA.ts index 30efe545..98f15386 100644 --- a/protocol/src/test/typescript/zfoots/packet/ObjectA.ts +++ b/protocol/src/test/typescript/zfoots/packet/ObjectA.ts @@ -6,6 +6,7 @@ class ObjectA { a: number = 0; m: Map = 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(201); buffer.writeInt(packet.a); buffer.writeIntStringMap(packet.m); buffer.writePacket(packet.objectB, 103); + buffer.writeInt(packet.innerCompatibleValue); + buffer.adjustPadding(201, 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 (buffer.compatibleRead(beforeReadIndex, length)) { + const result3 = buffer.readInt(); + packet.innerCompatibleValue = result3; + } if (length > 0) { buffer.setReadOffset(beforeReadIndex + length); } diff --git a/protocol/src/test/typescript/zfoots/packet/ObjectB.ts b/protocol/src/test/typescript/zfoots/packet/ObjectB.ts index 12170816..32ba3574 100644 --- a/protocol/src/test/typescript/zfoots/packet/ObjectB.ts +++ b/protocol/src/test/typescript/zfoots/packet/ObjectB.ts @@ -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(4); buffer.writeBoolean(packet.flag); + buffer.writeInt(packet.innerCompatibleValue); + buffer.adjustPadding(4, 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 (buffer.compatibleRead(beforeReadIndex, length)) { + const result1 = buffer.readInt(); + packet.innerCompatibleValue = result1; + } if (length > 0) { buffer.setReadOffset(beforeReadIndex + length); }