fix[js]: javascript support compatible field

This commit is contained in:
sun committed 2023-10-16 12:21:14 +08:00
1 parent 1475e640c6
commit 6e9a0c53fc
9 files changed
+13435 -12

No files matched your search

@@ -235,6 +235,7 @@ const ByteBuffer = function() {
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;
}
@@ -243,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;
}
+4 -2
View File
@@ -8,11 +8,11 @@ function assert(flag) {
}
}
// 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-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 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);
@@ -28,6 +28,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());
// set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
// assert(byteBuffer.readOffset == newByteBuffer.writeOffset)
@@ -1,4 +1,5 @@
import EmptyObject from './packet/EmptyObject.js';
import VeryBigObject from './packet/VeryBigObject.js';
import ComplexObject from './packet/ComplexObject.js';
import NormalObject from './packet/NormalObject.js';
import ObjectA from './packet/ObjectA.js';
@@ -11,6 +12,7 @@ const ProtocolManager = {};
// initProtocol
protocols.set(0, EmptyObject);
protocols.set(1, VeryBigObject);
protocols.set(100, ComplexObject);
protocols.set(101, NormalObject);
protocols.set(102, ObjectA);
@@ -235,6 +235,7 @@ const ByteBuffer = function() {
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;
}
@@ -19,7 +19,6 @@ const NormalObject = function() {
this.s = new Set(); // Set<number>
this.ssss = new Set(); // Set<string>
this.outCompatibleValue = 0; // number
this.outCompatibleValue2 = 0; // number
};
NormalObject.prototype.protocolId = function() {
@@ -32,7 +31,7 @@ NormalObject.write = function(buffer, packet) {
return;
}
const beforeWriteIndex = buffer.getWriteOffset();
buffer.writeInt(857);
buffer.writeInt(854);
buffer.writeByte(packet.a);
buffer.writeByteArray(packet.aaa);
buffer.writeShort(packet.b);
@@ -52,8 +51,7 @@ NormalObject.write = function(buffer, packet) {
buffer.writeIntSet(packet.s);
buffer.writeStringSet(packet.ssss);
buffer.writeInt(packet.outCompatibleValue);
buffer.writeInt(packet.outCompatibleValue2);
buffer.adjustPadding(857, beforeWriteIndex);
buffer.adjustPadding(854, beforeWriteIndex);
};
NormalObject.read = function(buffer) {
@@ -103,10 +101,6 @@ NormalObject.read = function(buffer) {
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);
}
@@ -3,6 +3,7 @@ const ObjectA = function() {
this.a = 0; // number
this.m = new Map(); // Map<number, string>
this.objectB = null; // ObjectB | null
this.innerCompatibleValue = 0; // number
};
ObjectA.prototype.protocolId = function() {
@@ -14,10 +15,13 @@ ObjectA.write = function(buffer, packet) {
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);
};
ObjectA.read = function(buffer) {
@@ -33,6 +37,10 @@ ObjectA.read = function(buffer) {
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);
}
@@ -1,6 +1,7 @@
const ObjectB = function() {
this.flag = false; // boolean
this.innerCompatibleValue = 0; // number
};
ObjectB.prototype.protocolId = function() {
@@ -12,8 +13,11 @@ ObjectB.write = function(buffer, packet) {
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);
};
ObjectB.read = function(buffer) {
@@ -25,6 +29,10 @@ ObjectB.read = function(buffer) {
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);
}
File diff suppressed because it is too large. Load diff