perf[protocol]: 使用模板生成协议

This commit is contained in:
jaysunxiao
2022-05-16 20:54:14 +08:00
parent 6a8aa811a0
commit 29ee2b0d90
30 changed files with 307 additions and 294 deletions
@@ -0,0 +1,5 @@
module.exports = {
env: {
jest: true
}
};
@@ -0,0 +1,5 @@
module.exports = {
presets: [
'@vue/app'
]
};
+107
View File
@@ -0,0 +1,107 @@
import ByteBuffer from './zfoojs/buffer/ByteBuffer.js';
import ProtocolManager from './zfoojs/ProtocolManager.js';
const fs = require('fs');
describe('jsProtocolTest', () => {
it('complexObjectTest', () => {
const data = fs.readFileSync('resources\\ComplexObject.bytes');
ProtocolManager.initProtocol();
const arrayBytes = new Uint8Array(data.length);
data.copy(arrayBytes, 0, 0, data.length);
const byteBuffer = new ByteBuffer();
byteBuffer.writeBytes(arrayBytes);
const packet = ProtocolManager.read(byteBuffer);
console.log(packet);
const newByteBuffer = new ByteBuffer();
ProtocolManager.write(newByteBuffer, packet);
const newPacket = ProtocolManager.read(newByteBuffer);
console.log(newPacket);
expect(byteBuffer.readOffset).toBe(newByteBuffer.writeOffset);
// set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
const length = newByteBuffer.writeOffset;
byteBuffer.setReadOffset(0);
newByteBuffer.setReadOffset(0);
for (let i = 0; i < length; i++) {
expect(byteBuffer.readByte()).toBe(newByteBuffer.readByte());
}
});
it('byteBufferTest', () => {
let buffer = new ByteBuffer();
expect(buffer.getCapacity()).toBe(128);
// boolean
const testBoolean = [false, true];
testBoolean.forEach((value) => {
buffer.writeBoolean(value);
expect(buffer.readBoolean()).toBe(value);
});
expect(buffer.writeOffset).toBe(testBoolean.length);
expect(buffer.readOffset).toBe(testBoolean.length);
// byte
buffer = new ByteBuffer();
const testByte = [-128, -99, 0, 99, 127];
testByte.forEach((value) => {
buffer.writeByte(value);
expect(buffer.readByte()).toBe(value);
});
expect(buffer.writeOffset).toBe(testByte.length);
expect(buffer.readOffset).toBe(testByte.length);
// short
buffer = new ByteBuffer();
const testShort = [-32768, -99, 0, 99, 32767];
testShort.forEach((value) => {
buffer.writeShort(value);
expect(buffer.readShort()).toBe(value);
});
expect(buffer.writeOffset).toBe(testShort.length * 2);
expect(buffer.readOffset).toBe(testShort.length * 2);
// int
buffer = new ByteBuffer();
const testInt = [-2147483648, -99, 0, 99, 2147483647];
testInt.forEach((value) => {
buffer.writeInt(value);
expect(buffer.readInt()).toBe(value);
});
// float
buffer = new ByteBuffer();
const testFloat = [-999.5, -99.5, 0, 99.5, 999.5];
testFloat.forEach((value) => {
buffer.writeFloat(value);
expect(buffer.readFloat()).toBe(value);
});
// double
buffer = new ByteBuffer();
const testDouble = [-999.5, -99.5, 0, 99.5, 999.5];
testDouble.forEach((value) => {
buffer.writeDouble(value);
expect(buffer.readDouble()).toBe(value);
});
// string
buffer = new ByteBuffer();
const testString = 'hello world!';
buffer.writeString(testString);
expect(buffer.readString()).toBe(testString);
// char
buffer = new ByteBuffer();
const testChar = 'h';
buffer.writeChar(testString);
expect(buffer.readChar()).toBe(testChar);
});
});
+51
View File
@@ -0,0 +1,51 @@
{
"name": "zfoo",
"version": "0.0.0",
"description": "This is a common config for all projects",
"author": "jaysunxiao@gmail.com",
"license": "MIT",
"scripts": {
},
"dependencies": {
"vue": "2.6.11",
"vue-i18n": "8.12.0",
"vue-meta": "^2.2.2",
"vue-router": "3.1.6",
"vuelidate": "^0.7.5",
"vuetify": "^2.3.3",
"vuex": "3.1.3",
"vuex-router-sync": "^5.0.0"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.3.0",
"@vue/cli-plugin-eslint": "~4.3.0",
"@vue/cli-plugin-pwa": "^4.3.1",
"@vue/cli-service": "~4.3.0",
"@vue/cli-plugin-unit-jest": "~4.3.0",
"@vue/eslint-config-standard": "^4.0.0",
"@vue/test-utils": "1.0.0-beta.33",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "23.6.0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"node-sass": "^4.13.0",
"sass": "^1.19.0",
"sass-loader": "^8.0.0",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.1",
"mockjs": "1.0.0",
"vue-template-compiler": "2.6.11",
"vue-cli-plugin-vuetify": "^2.0.6",
"vuetify-loader": "^1.5.0"
},
"engines": {
"node": ">=8.9",
"npm": ">= 3.0.0"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
@@ -0,0 +1,42 @@
import ComplexObject from './packet/ComplexObject.js';
import NormalObject from './packet/NormalObject.js';
import ObjectA from './packet/ObjectA.js';
import ObjectB from './packet/ObjectB.js';
import SimpleObject from './packet/SimpleObject.js';
const protocols = new Map();
const ProtocolManager = protocols.set(100, ComplexObject);
protocols.set(101, NormalObject);
protocols.set(102, ObjectA);
protocols.set(103, ObjectB);
protocols.set(104, SimpleObject);
ProtocolManager.getProtocol = function getProtocol(protocolId) {
const protocol = protocols.get(protocolId);
if (protocol === null) {
throw new Error('[protocolId:' + protocolId + ']协议不存在');
}
return protocol;
};
ProtocolManager.write = function write(buffer, packet) {
const protocolId = packet.protocolId();
buffer.writeShort(protocolId);
const protocol = ProtocolManager.getProtocol(protocolId);
protocol.write(buffer, packet);
};
ProtocolManager.read = function read(buffer) {
const protocolId = buffer.readShort();
const protocol = ProtocolManager.getProtocol(protocolId);
const packet = protocol.read(buffer);
return packet;
};
ProtocolManager.initProtocol = function initProtocol() {
{
}
};
export default ProtocolManager;
@@ -0,0 +1,886 @@
import {readInt64, writeInt64} from './longbits.js';
import ProtocolManager from '../ProtocolManager.js';
const empty_str = '';
const initSize = 128;
const maxSize = 655537;
const maxShort = 32767;
const minShort = -32768;
const maxInt = 2147483647;
const minInt = -2147483648;
// UTF-8编码与解码
// const encoder = new TextEncoder('utf-8');
// const decoder = new TextDecoder('utf-8');
// nodejs的测试环境需要用以下方式特殊处理
const util = require('util');
const encoder = new util.TextEncoder('utf-8');
const decoder = new util.TextDecoder('utf-8');
// 在js中long可以支持的最大值
// const maxLong = 9007199254740992;
// const minLong = -9007199254740992;
const copy = function copy(original, newLength) {
if (original.byteLength > newLength) {
throw new Error('newLength is too small');
}
const dst = new ArrayBuffer(newLength);
new Uint8Array(dst).set(new Uint8Array(original));
return dst;
};
function encodeZigzagInt(n) {
// 有效位左移一位+符号位右移31位
return (n << 1) ^ (n >> 31);
}
function decodeZigzagInt(n) {
return (n >>> 1) ^ -(n & 1);
}
const ByteBuffer = function() {
this.writeOffset = 0;
this.readOffset = 0;
this.buffer = new ArrayBuffer(initSize);
this.bufferView = new DataView(this.buffer, 0, this.buffer.byteLength);
this.setWriteOffset = function(writeOffset) {
if (writeOffset > this.buffer.byteLength) {
throw new Error('index out of bounds exception: readerIndex: ' + this.readOffset +
', writerIndex: ' + this.writeOffset +
'(expected: 0 <= readerIndex <= writerIndex <= capacity:' + this.buffer.byteLength);
}
this.writeOffset = writeOffset;
};
this.setReadOffset = function(readOffset) {
if (readOffset > this.writeOffset) {
throw new Error('index out of bounds exception: readerIndex: ' + this.readOffset +
', writerIndex: ' + this.writeOffset +
'(expected: 0 <= readerIndex <= writerIndex <= capacity:' + this.buffer.byteLength);
}
this.readOffset = readOffset;
};
this.getCapacity = function() {
return this.buffer.byteLength - this.writeOffset;
};
this.ensureCapacity = function(minCapacity) {
while (minCapacity - this.getCapacity() > 0) {
const newSize = this.buffer.byteLength * 2;
if (newSize > maxSize) {
throw new Error('out of memory error');
}
this.buffer = copy(this.buffer, newSize);
this.bufferView = new DataView(this.buffer, 0, this.buffer.byteLength);
}
};
this.writeBoolean = function(value) {
if (!(value === true || value === false)) {
throw new Error('value must be true of false');
}
this.ensureCapacity(1);
if (value === true) {
this.bufferView.setInt8(this.writeOffset, 1);
} else {
this.bufferView.setInt8(this.writeOffset, 0);
}
this.writeOffset++;
};
this.readBoolean = function() {
const value = this.bufferView.getInt8(this.readOffset);
this.readOffset++;
return (value === 1);
};
this.writeBytes = function(byteArray) {
const length = byteArray.byteLength;
this.ensureCapacity(length);
new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset);
this.writeOffset += length;
};
this.writeByte = function(value) {
this.ensureCapacity(1);
this.bufferView.setInt8(this.writeOffset, value);
this.writeOffset++;
};
this.readByte = function() {
const value = this.bufferView.getInt8(this.readOffset);
this.readOffset++;
return value;
};
this.writeShort = function(value) {
if (!(minShort <= value && value <= maxShort)) {
throw new Error('value must range between minShort:-32768 and maxShort:32767');
}
this.ensureCapacity(2);
this.bufferView.setInt16(this.writeOffset, value);
this.writeOffset += 2;
};
this.readShort = function() {
const value = this.bufferView.getInt16(this.readOffset);
this.readOffset += 2;
return value;
};
this.writeRawInt = function(value) {
if (!(minInt <= value && value <= maxInt)) {
throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647');
}
this.ensureCapacity(4);
this.bufferView.setInt32(this.writeOffset, value);
this.writeOffset += 4;
};
this.readRawInt = function() {
const value = this.bufferView.getInt32(this.readOffset);
this.readOffset += 4;
return value;
};
this.writeInt = function(value) {
if (!(minInt <= value && value <= maxInt)) {
throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647');
}
this.ensureCapacity(5);
value = encodeZigzagInt(value);
if (value >>> 7 === 0) {
this.writeByte(value);
return;
}
if (value >>> 14 === 0) {
this.writeByte((value & 0x7F) | 0x80);
this.writeByte((value >>> 7));
return;
}
if (value >>> 21 === 0) {
this.writeByte((value & 0x7F) | 0x80);
this.writeByte((value >>> 7 | 0x80));
this.writeByte(value >>> 14);
return;
}
if (value >>> 28 === 0) {
this.writeByte((value & 0x7F) | 0x80);
this.writeByte((value >>> 7 | 0x80));
this.writeByte((value >>> 14 | 0x80));
this.writeByte(value >>> 21);
return;
}
this.writeByte((value & 0x7F) | 0x80);
this.writeByte((value >>> 7 | 0x80));
this.writeByte((value >>> 14 | 0x80));
this.writeByte((value >>> 21 | 0x80));
this.writeByte(value >>> 28);
};
this.readInt = function() {
let b = this.readByte();
let value = b & 0x7F;
if ((b & 0x80) !== 0) {
b = this.readByte();
value |= (b & 0x7F) << 7;
if ((b & 0x80) !== 0) {
b = this.readByte();
value |= (b & 0x7F) << 14;
if ((b & 0x80) !== 0) {
b = this.readByte();
value |= (b & 0x7F) << 21;
if ((b & 0x80) !== 0) {
b = this.readByte();
value |= (b & 0x7F) << 28;
}
}
}
}
return decodeZigzagInt(value);
};
this.writeLong = function(value) {
if (value === null || value === undefined) {
throw new Error('value must not be null');
}
this.ensureCapacity(9);
writeInt64(this, value);
};
this.readLong = function() {
const buffer = new ArrayBuffer(9);
const bufferView = new DataView(buffer, 0, buffer.byteLength);
let count = 0;
let b = this.readByte();
bufferView.setUint8(count++, b);
if ((b & 0x80) !== 0) {
b = this.readByte();
bufferView.setUint8(count++, b);
if ((b & 0x80) !== 0) {
b = this.readByte();
bufferView.setUint8(count++, b);
if ((b & 0x80) !== 0) {
b = this.readByte();
bufferView.setUint8(count++, b);
if ((b & 0x80) !== 0) {
b = this.readByte();
bufferView.setUint8(count++, b);
if ((b & 0x80) !== 0) {
b = this.readByte();
bufferView.setUint8(count++, b);
if ((b & 0x80) !== 0) {
b = this.readByte();
bufferView.setUint8(count++, b);
if ((b & 0x80) !== 0) {
b = this.readByte();
bufferView.setUint8(count++, b);
if ((b & 0x80) !== 0) {
b = this.readByte();
bufferView.setUint8(count++, b);
}
}
}
}
}
}
}
}
return readInt64(new Uint8Array(buffer.slice(0, count))).toString();
};
this.writeFloat = function(value) {
if (value === null || value === undefined) {
throw new Error('value must not be null');
}
this.ensureCapacity(4);
this.bufferView.setFloat32(this.writeOffset, value);
this.writeOffset += 4;
};
this.readFloat = function() {
const value = this.bufferView.getFloat32(this.readOffset);
this.readOffset += 4;
return value;
};
this.writeDouble = function(value) {
if (value === null || value === undefined) {
throw new Error('value must not be null');
}
this.ensureCapacity(8);
this.bufferView.setFloat64(this.writeOffset, value);
this.writeOffset += 8;
};
this.readDouble = function() {
const value = this.bufferView.getFloat64(this.readOffset);
this.readOffset += 8;
return value;
};
this.writeChar = function(value) {
if (value === null || value === undefined || value.length === 0) {
this.writeString(empty_str);
return;
}
this.writeString(value.charAt(0));
};
this.readChar = function() {
return this.readString();
};
this.writeString = function(value) {
if (value === null || value === undefined || value.trim().length === 0) {
this.writeInt(0);
return;
}
const uint8Array = encoder.encode(value);
this.ensureCapacity(5 + uint8Array.length);
this.writeInt(uint8Array.length);
uint8Array.forEach((value) => this.writeByte(value));
};
this.readString = function() {
const length = this.readInt();
if (length <= 0) {
return empty_str;
}
const uint8Array = new Uint8Array(this.buffer.slice(this.readOffset, this.readOffset + length));
const value = decoder.decode(uint8Array);
this.readOffset += length;
return value;
};
this.toBytes = function() {
const result = new ArrayBuffer(this.writeOffset);
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
return result;
};
this.writePacketFlag = function(value) {
const flag = value === null;
this.writeBoolean(!flag);
return flag;
};
this.writePacket = function (packet, protocolId) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
protocolRegistration.write(this, packet);
};
this.readPacket = function (protocolId) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
return protocolRegistration.read(this);
};
this.writeBooleanArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeBoolean(element);
});
}
};
this.readBooleanArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readBoolean());
}
}
return array;
};
this.writeByteArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeByte(element);
});
}
};
this.readByteArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readByte());
}
}
return array;
};
this.writeShortArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeShort(element);
});
}
};
this.readShortArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readShort());
}
}
return array;
};
this.writeIntArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeInt(element);
});
}
};
this.readIntArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readInt());
}
}
return array;
};
this.writeLongArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeLong(element);
});
}
};
this.readLongArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readLong());
}
}
return array;
};
this.writeFloatArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeFloat(element);
});
}
};
this.readFloatArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readFloat());
}
}
return array;
};
this.writeDoubleArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeDouble(element);
});
}
};
this.readDoubleArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readDouble());
}
}
return array;
};
this.writeStringArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeString(element);
});
}
};
this.readStringArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readString());
}
}
return array;
};
this.writeCharArray = function (array) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeChar(element);
});
}
};
this.readCharArray = function () {
const array = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readChar());
}
}
return array;
};
this.writePacketArray = function (array, protocolId) {
if (array === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(array.length);
array.forEach(element => {
protocolRegistration.write(this, element);
});
}
};
this.readPacketArray = function(protocolId) {
const array = [];
const length = this.readInt();
if (length > 0) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
for (let index = 0; index < length; index++) {
array.push(protocolRegistration.read(this));
}
}
return array;
};
this.writeIntIntMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeInt(key);
this.writeInt(value);
});
}
};
this.readIntIntMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readInt();
const value = this.readInt();
map.set(key, value);
}
}
return map;
};
this.writeIntLongMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeInt(key);
this.writeLong(value);
});
}
};
this.readIntLongMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readInt();
const value = this.readLong();
map.set(key, value);
}
}
return map;
};
this.writeIntStringMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeInt(key);
this.writeString(value);
});
}
};
this.readIntStringMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readInt();
const value = this.readString();
map.set(key, value);
}
}
return map;
};
this.writeIntPacketMap = function (map, protocolId) {
if (map === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeInt(key);
protocolRegistration.write(this, value);
});
}
};
this.readIntPacketMap = function(protocolId) {
const map = new Map();
const size = this.readInt();
if (size > 0) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
for (let index = 0; index < size; index++) {
const key = this.readInt();
const value = protocolRegistration.read(this);
map.set(key, value);
}
}
return map;
};
this.writeLongIntMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeLong(key);
this.writeInt(value);
});
}
};
this.readLongIntMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readLong();
const value = this.readInt();
map.set(key, value);
}
}
return map;
};
this.writeLongLongMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeLong(key);
this.writeLong(value);
});
}
};
this.readLongLongMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readLong();
const value = this.readLong();
map.set(key, value);
}
}
return map;
};
this.writeLongStringMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeLong(key);
this.writeString(value);
});
}
};
this.readLongStringMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readLong();
const value = this.readString();
map.set(key, value);
}
}
return map;
};
this.writeLongPacketMap = function (map, protocolId) {
if (map === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeLong(key);
protocolRegistration.write(this, value);
});
}
};
this.readLongPacketMap = function(protocolId) {
const map = new Map();
const size = this.readInt();
if (size > 0) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
for (let index = 0; index < size; index++) {
const key = this.readLong();
const value = protocolRegistration.read(this);
map.set(key, value);
}
}
return map;
};
this.writeStringIntMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeString(key);
this.writeInt(value);
});
}
};
this.readStringIntMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readString();
const value = this.readInt();
map.set(key, value);
}
}
return map;
};
this.writeStringLongMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeString(key);
this.writeLong(value);
});
}
};
this.readStringLongMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readString();
const value = this.readLong();
map.set(key, value);
}
}
return map;
};
this.writeStringStringMap = function (map) {
if (map === null) {
this.writeInt(0);
} else {
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeString(key);
this.writeString(value);
});
}
};
this.readStringStringMap = function() {
const map = new Map();
const size = this.readInt();
if (size > 0) {
for (let index = 0; index < size; index++) {
const key = this.readString();
const value = this.readString();
map.set(key, value);
}
}
return map;
};
this.writeStringPacketMap = function (map, protocolId) {
if (map === null) {
this.writeInt(0);
} else {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
this.writeInt(map.size);
map.forEach((value, key) => {
this.writeString(key);
protocolRegistration.write(this, value);
});
}
};
this.readStringPacketMap = function(protocolId) {
const map = new Map();
const size = this.readInt();
if (size > 0) {
const protocolRegistration = ProtocolManager.getProtocol(protocolId);
for (let index = 0; index < size; index++) {
const key = this.readString();
const value = protocolRegistration.read(this);
map.set(key, value);
}
}
return map;
};
};
export default ByteBuffer;
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,184 @@
// from protobuf
import Long from './long.js';
/**
* Constructs new long bits.
* @classdesc Helper class for working with the low and high bits of a 64 bit value.
* @memberof util
* @constructor
* @param {number} lo Low 32 bits, unsigned
* @param {number} hi High 32 bits, unsigned
*/
function LongBits(lo, hi) {
// note that the casts below are theoretically unnecessary as of today, but older statically
// generated converter code might still call the ctor with signed 32bits. kept for compat.
/**
* Low bits.
* @type {number}
*/
this.lo = lo >>> 0;
/**
* High bits.
* @type {number}
*/
this.hi = hi >>> 0;
}
/**
* Zig-zag encodes this long bits.
* @returns {util.LongBits} `this`
*/
LongBits.prototype.zzEncode = function zzEncode() {
const mask = this.hi >> 31;
this.hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0;
this.lo = (this.lo << 1 ^ mask) >>> 0;
return this;
};
/**
* Zig-zag decodes this long bits.
* @returns {util.LongBits} `this`
*/
LongBits.prototype.zzDecode = function zzDecode() {
const mask = -(this.lo & 1);
this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0;
this.hi = (this.hi >>> 1 ^ mask) >>> 0;
return this;
};
/**
* Converts this long bits to a long.
* @param {boolean} [unsigned=false] Whether unsigned or not
* @returns {Long} Long
*/
LongBits.prototype.toLong = function toLong(unsigned) {
return new Long(this.lo | 0, this.hi | 0, Boolean(unsigned));
};
/**
* Zero bits.
* @memberof util.LongBits
* @type {util.LongBits}
*/
const zero = LongBits.zero = new LongBits(0, 0);
function from(value) {
if (typeof value === 'number') {
return fromNumber(value);
}
if (typeof value === 'string' || value instanceof String) {
value = Long.fromString(value);
}
return value.low || value.high ? new LongBits(value.low >>> 0, value.high >>> 0) : zero;
}
/**
* Constructs new long bits from the specified number.
* @param {number} value Value
* @returns {util.LongBits} Instance
*/
function fromNumber(value) {
if (value === 0) {
return zero;
}
const sign = value < 0;
if (sign) {
value = -value;
}
let lo = value >>> 0;
let hi = (value - lo) / 4294967296 >>> 0;
if (sign) {
hi = ~hi >>> 0;
lo = ~lo >>> 0;
if (++lo > 4294967295) {
lo = 0;
if (++hi > 4294967295) {
hi = 0;
}
}
}
return new LongBits(lo, hi);
}
function writeVarint64(byteBuffer, value) {
let count = 0;
while (value.hi) {
byteBuffer.writeByte(value.lo & 127 | 128);
value.lo = (value.lo >>> 7 | value.hi << 25) >>> 0;
value.hi >>>= 7;
count = count + 7;
}
while (value.lo > 127) {
if (count >= 56) {
byteBuffer.writeByte(value.lo);
return;
}
byteBuffer.writeByte(value.lo & 127 | 128);
value.lo = value.lo >>> 7;
count = count + 7;
}
byteBuffer.writeByte(value.lo);
}
function readLongVarint(buffer) {
// tends to deopt with local vars for octet etc.
const bits = new LongBits(0, 0);
let i = 0;
const len = buffer.length;
let pos = 0;
if (len - pos > 4) { // fast route (lo)
for (; i < 4; ++i) {
// 1st..4th
bits.lo = (bits.lo | (buffer[pos] & 127) << i * 7) >>> 0;
if (buffer[pos++] < 128) {
return bits;
}
}
// 5th
bits.lo = (bits.lo | (buffer[pos] & 127) << 28) >>> 0;
bits.hi = (bits.hi | (buffer[pos] & 127) >> 4) >>> 0;
if (buffer[pos++] < 128) {
return bits;
}
i = 0;
} else {
for (; i < 3; ++i) {
// 1st..3th
bits.lo = (bits.lo | (buffer[pos] & 127) << i * 7) >>> 0;
if (buffer[pos++] < 128) {
return bits;
}
}
// 4th
bits.lo = (bits.lo | (buffer[pos++] & 127) << i * 7) >>> 0;
return bits;
}
// 6th..9th
for (; i < 4; ++i) {
// 最后一位直接写入
if (pos === 8) {
bits.hi = (bits.hi | buffer[pos] << i * 7 + 3) >>> 0;
return bits;
}
bits.hi = (bits.hi | (buffer[pos] & 127) << i * 7 + 3) >>> 0;
if (buffer[pos++] < 128) {
return bits;
}
}
return bits;
}
export function writeInt64(byteBuffer, value) {
const bits = from(value).zzEncode();
writeVarint64(byteBuffer, bits);
}
export function readInt64(buffer) {
return readLongVarint(buffer).zzDecode().toLong(false);
}
@@ -0,0 +1,465 @@
// 复杂的对象
// 包括了各种复杂的结构,数组,List,Set,Map
//
// @author jaysunxiao
// @version 3.0
const ComplexObject = function(a, aa, aaa, aaaa, b, bb, bbb, bbbb, c, cc, ccc, cccc, d, dd, ddd, dddd, e, ee, eee, eeee, f, ff, fff, ffff, g, gg, ggg, gggg, h, hh, hhh, hhhh, jj, jjj, kk, kkk, l, ll, lll, llll, lllll, m, mm, mmm, mmmm, mmmmm, s, ss, sss, ssss, sssss) {
// byte类型,最简单的整形
this.a = a; // byte
// byte的包装类型
// 优先使用基础类型,包装类型会有装箱拆箱
this.aa = aa; // java.lang.Byte
// 数组类型
this.aaa = aaa; // byte[]
this.aaaa = aaaa; // java.lang.Byte[]
this.b = b; // short
this.bb = bb; // java.lang.Short
this.bbb = bbb; // short[]
this.bbbb = bbbb; // java.lang.Short[]
this.c = c; // int
this.cc = cc; // java.lang.Integer
this.ccc = ccc; // int[]
this.cccc = cccc; // java.lang.Integer[]
this.d = d; // long
this.dd = dd; // java.lang.Long
this.ddd = ddd; // long[]
this.dddd = dddd; // java.lang.Long[]
this.e = e; // float
this.ee = ee; // java.lang.Float
this.eee = eee; // float[]
this.eeee = eeee; // java.lang.Float[]
this.f = f; // double
this.ff = ff; // java.lang.Double
this.fff = fff; // double[]
this.ffff = ffff; // java.lang.Double[]
this.g = g; // boolean
this.gg = gg; // java.lang.Boolean
this.ggg = ggg; // boolean[]
this.gggg = gggg; // java.lang.Boolean[]
this.h = h; // char
this.hh = hh; // java.lang.Character
this.hhh = hhh; // char[]
this.hhhh = hhhh; // java.lang.Character[]
this.jj = jj; // java.lang.String
this.jjj = jjj; // java.lang.String[]
this.kk = kk; // com.zfoo.protocol.packet.ObjectA
this.kkk = kkk; // com.zfoo.protocol.packet.ObjectA[]
this.l = l; // java.util.List<java.lang.Integer>
this.ll = ll; // java.util.List<java.util.List<java.util.List<java.lang.Integer>>>
this.lll = lll; // java.util.List<java.util.List<com.zfoo.protocol.packet.ObjectA>>
this.llll = llll; // java.util.List<java.lang.String>
this.lllll = lllll; // java.util.List<java.util.Map<java.lang.Integer, java.lang.String>>
this.m = m; // java.util.Map<java.lang.Integer, java.lang.String>
this.mm = mm; // java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA>
this.mmm = mmm; // java.util.Map<com.zfoo.protocol.packet.ObjectA, java.util.List<java.lang.Integer>>
this.mmmm = mmmm; // java.util.Map<java.util.List<java.util.List<com.zfoo.protocol.packet.ObjectA>>, java.util.List<java.util.List<java.util.List<java.lang.Integer>>>>
this.mmmmm = mmmmm; // java.util.Map<java.util.List<java.util.Map<java.lang.Integer, java.lang.String>>, java.util.Set<java.util.Map<java.lang.Integer, java.lang.String>>>
this.s = s; // java.util.Set<java.lang.Integer>
this.ss = ss; // java.util.Set<java.util.Set<java.util.List<java.lang.Integer>>>
this.sss = sss; // java.util.Set<java.util.Set<com.zfoo.protocol.packet.ObjectA>>
this.ssss = ssss; // java.util.Set<java.lang.String>
this.sssss = sssss; // java.util.Set<java.util.Map<java.lang.Integer, java.lang.String>>
};
ComplexObject.prototype.protocolId = function() {
return 100;
};
ComplexObject.write = function(buffer, packet) {
if (buffer.writePacketFlag(packet)) {
return;
}
buffer.writeByte(packet.a);
buffer.writeByte(packet.aa);
buffer.writeByteArray(packet.aaa);
buffer.writeByteArray(packet.aaaa);
buffer.writeShort(packet.b);
buffer.writeShort(packet.bb);
buffer.writeShortArray(packet.bbb);
buffer.writeShortArray(packet.bbbb);
buffer.writeInt(packet.c);
buffer.writeInt(packet.cc);
buffer.writeIntArray(packet.ccc);
buffer.writeIntArray(packet.cccc);
buffer.writeLong(packet.d);
buffer.writeLong(packet.dd);
buffer.writeLongArray(packet.ddd);
buffer.writeLongArray(packet.dddd);
buffer.writeFloat(packet.e);
buffer.writeFloat(packet.ee);
buffer.writeFloatArray(packet.eee);
buffer.writeFloatArray(packet.eeee);
buffer.writeDouble(packet.f);
buffer.writeDouble(packet.ff);
buffer.writeDoubleArray(packet.fff);
buffer.writeDoubleArray(packet.ffff);
buffer.writeBoolean(packet.g);
buffer.writeBoolean(packet.gg);
buffer.writeBooleanArray(packet.ggg);
buffer.writeBooleanArray(packet.gggg);
buffer.writeChar(packet.h);
buffer.writeChar(packet.hh);
buffer.writeCharArray(packet.hhh);
buffer.writeCharArray(packet.hhhh);
buffer.writeString(packet.jj);
buffer.writeStringArray(packet.jjj);
buffer.writePacket(packet.kk, 102);
buffer.writePacketArray(packet.kkk, 102);
buffer.writeIntArray(packet.l);
if (packet.ll === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.ll.length);
packet.ll.forEach(element0 => {
if (element0 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(element0.length);
element0.forEach(element1 => {
buffer.writeIntArray(element1);
});
}
});
}
if (packet.lll === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.lll.length);
packet.lll.forEach(element2 => {
buffer.writePacketArray(element2, 102);
});
}
buffer.writeStringArray(packet.llll);
if (packet.lllll === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.lllll.length);
packet.lllll.forEach(element3 => {
buffer.writeIntStringMap(element3);
});
}
buffer.writeIntStringMap(packet.m);
buffer.writeIntPacketMap(packet.mm, 102);
if (packet.mmm === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.mmm.size);
packet.mmm.forEach((value5, key4) => {
buffer.writePacket(key4, 102);
buffer.writeIntArray(value5);
});
}
if (packet.mmmm === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.mmmm.size);
packet.mmmm.forEach((value7, key6) => {
if (key6 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(key6.length);
key6.forEach(element8 => {
buffer.writePacketArray(element8, 102);
});
}
if (value7 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(value7.length);
value7.forEach(element9 => {
if (element9 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(element9.length);
element9.forEach(element10 => {
buffer.writeIntArray(element10);
});
}
});
}
});
}
if (packet.mmmmm === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.mmmmm.size);
packet.mmmmm.forEach((value12, key11) => {
if (key11 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(key11.length);
key11.forEach(element13 => {
buffer.writeIntStringMap(element13);
});
}
if (value12 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(value12.size);
value12.forEach(element14 => {
buffer.writeIntStringMap(element14);
});
}
});
}
buffer.writeIntArray(packet.s);
if (packet.ss === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.ss.size);
packet.ss.forEach(element15 => {
if (element15 === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(element15.size);
element15.forEach(element16 => {
buffer.writeIntArray(element16);
});
}
});
}
if (packet.sss === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.sss.size);
packet.sss.forEach(element17 => {
buffer.writePacketArray(element17, 102);
});
}
buffer.writeStringArray(packet.ssss);
if (packet.sssss === null) {
buffer.writeInt(0);
} else {
buffer.writeInt(packet.sssss.size);
packet.sssss.forEach(element18 => {
buffer.writeIntStringMap(element18);
});
}
};
ComplexObject.read = function(buffer) {
if (!buffer.readBoolean()) {
return null;
}
const packet = new ComplexObject();
const result19 = buffer.readByte();
packet.a = result19;
const result20 = buffer.readByte();
packet.aa = result20;
const array21 = buffer.readByteArray();
packet.aaa = array21;
const array22 = buffer.readByteArray();
packet.aaaa = array22;
const result23 = buffer.readShort();
packet.b = result23;
const result24 = buffer.readShort();
packet.bb = result24;
const array25 = buffer.readShortArray();
packet.bbb = array25;
const array26 = buffer.readShortArray();
packet.bbbb = array26;
const result27 = buffer.readInt();
packet.c = result27;
const result28 = buffer.readInt();
packet.cc = result28;
const array29 = buffer.readIntArray();
packet.ccc = array29;
const array30 = buffer.readIntArray();
packet.cccc = array30;
const result31 = buffer.readLong();
packet.d = result31;
const result32 = buffer.readLong();
packet.dd = result32;
const array33 = buffer.readLongArray();
packet.ddd = array33;
const array34 = buffer.readLongArray();
packet.dddd = array34;
const result35 = buffer.readFloat();
packet.e = result35;
const result36 = buffer.readFloat();
packet.ee = result36;
const array37 = buffer.readFloatArray();
packet.eee = array37;
const array38 = buffer.readFloatArray();
packet.eeee = array38;
const result39 = buffer.readDouble();
packet.f = result39;
const result40 = buffer.readDouble();
packet.ff = result40;
const array41 = buffer.readDoubleArray();
packet.fff = array41;
const array42 = buffer.readDoubleArray();
packet.ffff = array42;
const result43 = buffer.readBoolean();
packet.g = result43;
const result44 = buffer.readBoolean();
packet.gg = result44;
const array45 = buffer.readBooleanArray();
packet.ggg = array45;
const array46 = buffer.readBooleanArray();
packet.gggg = array46;
const result47 = buffer.readChar();
packet.h = result47;
const result48 = buffer.readChar();
packet.hh = result48;
const array49 = buffer.readCharArray();
packet.hhh = array49;
const array50 = buffer.readCharArray();
packet.hhhh = array50;
const result51 = buffer.readString();
packet.jj = result51;
const array52 = buffer.readStringArray();
packet.jjj = array52;
const result53 = buffer.readPacket(102);
packet.kk = result53;
const array54 = buffer.readPacketArray(102);
packet.kkk = array54;
const list55 = buffer.readIntArray();
packet.l = list55;
const result56 = [];
const size57 = buffer.readInt();
if (size57 > 0) {
for (let index58 = 0; index58 < size57; index58++) {
const result59 = [];
const size60 = buffer.readInt();
if (size60 > 0) {
for (let index61 = 0; index61 < size60; index61++) {
const list62 = buffer.readIntArray();
result59.push(list62);
}
}
result56.push(result59);
}
}
packet.ll = result56;
const result63 = [];
const size64 = buffer.readInt();
if (size64 > 0) {
for (let index65 = 0; index65 < size64; index65++) {
const list66 = buffer.readPacketArray(102);
result63.push(list66);
}
}
packet.lll = result63;
const list67 = buffer.readStringArray();
packet.llll = list67;
const result68 = [];
const size69 = buffer.readInt();
if (size69 > 0) {
for (let index70 = 0; index70 < size69; index70++) {
const map71 = buffer.readIntStringMap();
result68.push(map71);
}
}
packet.lllll = result68;
const map72 = buffer.readIntStringMap();
packet.m = map72;
const map73 = buffer.readIntPacketMap(102);
packet.mm = map73;
const result74 = new Map();
const size75 = buffer.readInt();
if (size75 > 0) {
for (let index76 = 0; index76 < size75; index76++) {
const result77 = buffer.readPacket(102);
const list78 = buffer.readIntArray();
result74.set(result77, list78);
}
}
packet.mmm = result74;
const result79 = new Map();
const size80 = buffer.readInt();
if (size80 > 0) {
for (let index81 = 0; index81 < size80; index81++) {
const result82 = [];
const size83 = buffer.readInt();
if (size83 > 0) {
for (let index84 = 0; index84 < size83; index84++) {
const list85 = buffer.readPacketArray(102);
result82.push(list85);
}
}
const result86 = [];
const size87 = buffer.readInt();
if (size87 > 0) {
for (let index88 = 0; index88 < size87; index88++) {
const result89 = [];
const size90 = buffer.readInt();
if (size90 > 0) {
for (let index91 = 0; index91 < size90; index91++) {
const list92 = buffer.readIntArray();
result89.push(list92);
}
}
result86.push(result89);
}
}
result79.set(result82, result86);
}
}
packet.mmmm = result79;
const result93 = new Map();
const size94 = buffer.readInt();
if (size94 > 0) {
for (let index95 = 0; index95 < size94; index95++) {
const result96 = [];
const size97 = buffer.readInt();
if (size97 > 0) {
for (let index98 = 0; index98 < size97; index98++) {
const map99 = buffer.readIntStringMap();
result96.push(map99);
}
}
const result100 = new Set();
const size101 = buffer.readInt();
if (size101 > 0) {
for (let index102 = 0; index102 < size101; index102++) {
const map103 = buffer.readIntStringMap();
result100.add(map103);
}
}
result93.set(result96, result100);
}
}
packet.mmmmm = result93;
const set104 = buffer.readIntArray();
packet.s = set104;
const result105 = new Set();
const size106 = buffer.readInt();
if (size106 > 0) {
for (let index107 = 0; index107 < size106; index107++) {
const result108 = new Set();
const size109 = buffer.readInt();
if (size109 > 0) {
for (let index110 = 0; index110 < size109; index110++) {
const list111 = buffer.readIntArray();
result108.add(list111);
}
}
result105.add(result108);
}
}
packet.ss = result105;
const result112 = new Set();
const size113 = buffer.readInt();
if (size113 > 0) {
for (let index114 = 0; index114 < size113; index114++) {
const set115 = buffer.readPacketArray(102);
result112.add(set115);
}
}
packet.sss = result112;
const set116 = buffer.readStringArray();
packet.ssss = set116;
const result117 = new Set();
const size118 = buffer.readInt();
if (size118 > 0) {
for (let index119 = 0; index119 < size118; index119++) {
const map120 = buffer.readIntStringMap();
result117.add(map120);
}
}
packet.sssss = result117;
return packet;
};
export default ComplexObject;
@@ -0,0 +1,96 @@
// @author jaysunxiao
// @version 3.0
const NormalObject = function(a, aaa, b, c, d, e, f, g, jj, kk, l, ll, lll, llll, m, mm, s, ssss) {
this.a = a; // byte
this.aaa = aaa; // byte[]
this.b = b; // short
this.c = c; // int
this.d = d; // long
this.e = e; // float
this.f = f; // double
this.g = g; // boolean
this.jj = jj; // java.lang.String
this.kk = kk; // com.zfoo.protocol.packet.ObjectA
this.l = l; // java.util.List<java.lang.Integer>
this.ll = ll; // java.util.List<java.lang.Long>
this.lll = lll; // java.util.List<com.zfoo.protocol.packet.ObjectA>
this.llll = llll; // java.util.List<java.lang.String>
this.m = m; // java.util.Map<java.lang.Integer, java.lang.String>
this.mm = mm; // java.util.Map<java.lang.Integer, com.zfoo.protocol.packet.ObjectA>
this.s = s; // java.util.Set<java.lang.Integer>
this.ssss = ssss; // java.util.Set<java.lang.String>
};
NormalObject.prototype.protocolId = function() {
return 101;
};
NormalObject.write = function(buffer, packet) {
if (buffer.writePacketFlag(packet)) {
return;
}
buffer.writeByte(packet.a);
buffer.writeByteArray(packet.aaa);
buffer.writeShort(packet.b);
buffer.writeInt(packet.c);
buffer.writeLong(packet.d);
buffer.writeFloat(packet.e);
buffer.writeDouble(packet.f);
buffer.writeBoolean(packet.g);
buffer.writeString(packet.jj);
buffer.writePacket(packet.kk, 102);
buffer.writeIntArray(packet.l);
buffer.writeLongArray(packet.ll);
buffer.writePacketArray(packet.lll, 102);
buffer.writeStringArray(packet.llll);
buffer.writeIntStringMap(packet.m);
buffer.writeIntPacketMap(packet.mm, 102);
buffer.writeIntArray(packet.s);
buffer.writeStringArray(packet.ssss);
};
NormalObject.read = function(buffer) {
if (!buffer.readBoolean()) {
return null;
}
const packet = new NormalObject();
const result0 = buffer.readByte();
packet.a = result0;
const array1 = buffer.readByteArray();
packet.aaa = array1;
const result2 = buffer.readShort();
packet.b = result2;
const result3 = buffer.readInt();
packet.c = result3;
const result4 = buffer.readLong();
packet.d = result4;
const result5 = buffer.readFloat();
packet.e = result5;
const result6 = buffer.readDouble();
packet.f = result6;
const result7 = buffer.readBoolean();
packet.g = result7;
const result8 = buffer.readString();
packet.jj = result8;
const result9 = buffer.readPacket(102);
packet.kk = result9;
const list10 = buffer.readIntArray();
packet.l = list10;
const list11 = buffer.readLongArray();
packet.ll = list11;
const list12 = buffer.readPacketArray(102);
packet.lll = list12;
const list13 = buffer.readStringArray();
packet.llll = list13;
const map14 = buffer.readIntStringMap();
packet.m = map14;
const map15 = buffer.readIntPacketMap(102);
packet.mm = map15;
const set16 = buffer.readIntArray();
packet.s = set16;
const set17 = buffer.readStringArray();
packet.ssss = set17;
return packet;
};
export default NormalObject;
@@ -0,0 +1,36 @@
// @author jaysunxiao
// @version 3.0
const ObjectA = function(a, m, objectB) {
this.a = a; // int
this.m = m; // java.util.Map<java.lang.Integer, java.lang.String>
this.objectB = objectB; // com.zfoo.protocol.packet.ObjectB
};
ObjectA.prototype.protocolId = function() {
return 102;
};
ObjectA.write = function(buffer, packet) {
if (buffer.writePacketFlag(packet)) {
return;
}
buffer.writeInt(packet.a);
buffer.writeIntStringMap(packet.m);
buffer.writePacket(packet.objectB, 103);
};
ObjectA.read = function(buffer) {
if (!buffer.readBoolean()) {
return null;
}
const packet = new ObjectA();
const result0 = buffer.readInt();
packet.a = result0;
const map1 = buffer.readIntStringMap();
packet.m = map1;
const result2 = buffer.readPacket(103);
packet.objectB = result2;
return packet;
};
export default ObjectA;
@@ -0,0 +1,28 @@
// @author jaysunxiao
// @version 3.0
const ObjectB = function(flag) {
this.flag = flag; // boolean
};
ObjectB.prototype.protocolId = function() {
return 103;
};
ObjectB.write = function(buffer, packet) {
if (buffer.writePacketFlag(packet)) {
return;
}
buffer.writeBoolean(packet.flag);
};
ObjectB.read = function(buffer) {
if (!buffer.readBoolean()) {
return null;
}
const packet = new ObjectB();
const result0 = buffer.readBoolean();
packet.flag = result0;
return packet;
};
export default ObjectB;
@@ -0,0 +1,32 @@
// @author jaysunxiao
// @version 3.0
const SimpleObject = function(c, g) {
this.c = c; // int
this.g = g; // boolean
};
SimpleObject.prototype.protocolId = function() {
return 104;
};
SimpleObject.write = function(buffer, packet) {
if (buffer.writePacketFlag(packet)) {
return;
}
buffer.writeInt(packet.c);
buffer.writeBoolean(packet.g);
};
SimpleObject.read = function(buffer) {
if (!buffer.readBoolean()) {
return null;
}
const packet = new SimpleObject();
const result0 = buffer.readInt();
packet.c = result0;
const result1 = buffer.readBoolean();
packet.g = result1;
return packet;
};
export default SimpleObject;