From fbba746574be0937239e0ca72615cc97dd2a6c5f Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Sun, 5 Jun 2022 20:10:34 +0800 Subject: [PATCH] =?UTF-8?q?perf[protocol]:=20javascript=E4=BD=BF=E7=94=A8s?= =?UTF-8?q?et=E4=BB=A3=E6=9B=BF=E7=AE=80=E5=8C=96=E7=9A=84array=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../serializer/CutDownListSerializer.java | 54 ++-- .../serializer/CutDownSetSerializer.java | 54 ++-- .../resources/javascript/buffer/ByteBuffer.js | 246 ++++++++++++++++- .../javascript/zfoojs/buffer/ByteBuffer.js | 256 +++++++++++++++++- .../javascript/zfoojs/packet/ComplexObject.js | 44 +-- .../javascript/zfoojs/packet/NormalObject.js | 24 +- 6 files changed, 555 insertions(+), 123 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java index 0f74b6b3..42e11010 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java @@ -60,10 +60,8 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteBooleanList({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeBooleanList({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeBooleanList({});", objectStr)).append(LS); break; default: flag = false; @@ -84,10 +82,8 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteByteList({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeByteList({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeByteList({});", objectStr)).append(LS); break; default: flag = false; @@ -108,10 +104,8 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteShortList({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeShortList({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeShortList({});", objectStr)).append(LS); break; default: flag = false; @@ -132,10 +126,8 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteIntList({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeIntList({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeIntList({});", objectStr)).append(LS); break; default: flag = false; @@ -156,10 +148,8 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteLongList({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeLongList({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeLongList({});", objectStr)).append(LS); break; default: flag = false; @@ -181,10 +171,8 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteFloatList({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeFloatList({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeFloatList({});", objectStr)).append(LS); break; default: flag = false; @@ -206,10 +194,8 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteDoubleList({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeDoubleList({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeDoubleList({});", objectStr)).append(LS); break; default: flag = false; @@ -231,10 +217,8 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteStringList({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeStringList({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeStringList({});", objectStr)).append(LS); break; default: flag = false; @@ -258,10 +242,8 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WritePacketList({}, {});", objectStr, protocolId)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writePacketList({}, {});", objectStr, protocolId)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, protocolId)).append(LS); + builder.append(StringUtils.format("buffer.writePacketList({}, {});", objectStr, protocolId)).append(LS); break; default: flag = false; @@ -300,7 +282,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readBooleanList();", list)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", list)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readBooleanList();", list)).append(LS); break; default: flag = false; @@ -324,7 +306,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readByteList();", list)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readByteArray();", list)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readByteList();", list)).append(LS); break; default: flag = false; @@ -348,7 +330,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readShortList();", list)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readShortArray();", list)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readShortList();", list)).append(LS); break; default: flag = false; @@ -372,7 +354,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readIntList();", list)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readIntArray();", list)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readIntList();", list)).append(LS); break; default: flag = false; @@ -396,7 +378,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readLongList();", list)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readLongArray();", list)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readLongList();", list)).append(LS); break; default: flag = false; @@ -420,7 +402,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readFloatList();", list)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readFloatArray();", list)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readFloatList();", list)).append(LS); break; default: flag = false; @@ -444,7 +426,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readDoubleList();", list)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", list)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readDoubleList();", list)).append(LS); break; default: flag = false; @@ -468,7 +450,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readStringList();", list)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readStringArray();", list)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readStringList();", list)).append(LS); break; default: flag = false; @@ -494,7 +476,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readPacketList<{}>({});", list, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", list, protocolId)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readPacketList({});", list, protocolId)).append(LS); break; default: flag = false; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java index a7b28e4c..6420c688 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java @@ -61,10 +61,8 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteBooleanSet({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeBooleanSet({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeBooleanSet({});", objectStr)).append(LS); break; default: flag = false; @@ -85,10 +83,8 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteByteSet({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeByteSet({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeByteSet({});", objectStr)).append(LS); break; default: flag = false; @@ -109,10 +105,8 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteShortSet({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeShortSet({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeShortSet({});", objectStr)).append(LS); break; default: flag = false; @@ -133,10 +127,8 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteIntSet({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeIntSet({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeIntSet({});", objectStr)).append(LS); break; default: flag = false; @@ -157,10 +149,8 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteLongSet({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeLongSet({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeLongSet({});", objectStr)).append(LS); break; default: flag = false; @@ -181,10 +171,8 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteFloatSet({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeFloatSet({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeFloatSet({});", objectStr)).append(LS); break; default: flag = false; @@ -205,10 +193,8 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteDoubleSet({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeDoubleSet({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeDoubleSet({});", objectStr)).append(LS); break; default: flag = false; @@ -229,10 +215,8 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WriteStringSet({});", objectStr)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writeStringSet({});", objectStr)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); + builder.append(StringUtils.format("buffer.writeStringSet({});", objectStr)).append(LS); break; default: flag = false; @@ -256,10 +240,8 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.WritePacketSet({}, {});", objectStr, protocolId)).append(LS); break; case Cpp: - builder.append(StringUtils.format("buffer.writePacketSet({}, {});", objectStr, protocolId)).append(LS); - break; case JavaScript: - builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, protocolId)).append(LS); + builder.append(StringUtils.format("buffer.writePacketSet({}, {});", objectStr, protocolId)).append(LS); break; default: flag = false; @@ -298,7 +280,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readBooleanSet();", set)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", set)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readBooleanSet();", set)).append(LS); break; default: flag = false; @@ -322,7 +304,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readByteSet();", set)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readByteArray();", set)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readByteSet();", set)).append(LS); break; default: flag = false; @@ -346,7 +328,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readShortSet();", set)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readShortArray();", set)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readShortSet();", set)).append(LS); break; default: flag = false; @@ -370,7 +352,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readIntSet();", set)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readIntArray();", set)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readIntSet();", set)).append(LS); break; default: flag = false; @@ -394,7 +376,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readLongSet();", set)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readLongArray();", set)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readLongSet();", set)).append(LS); break; default: flag = false; @@ -418,7 +400,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readFloatSet();", set)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readFloatArray();", set)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readFloatSet();", set)).append(LS); break; default: flag = false; @@ -442,7 +424,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readDoubleSet();", set)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", set)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readDoubleSet();", set)).append(LS); break; default: flag = false; @@ -466,7 +448,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readStringSet();", set)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readStringArray();", set)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readStringSet();", set)).append(LS); break; default: flag = false; @@ -492,7 +474,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readPacketSet<{}>({});", set, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); break; case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", set, protocolId)).append(LS); + builder.append(StringUtils.format("const {} = buffer.readPacketSet({});", set, protocolId)).append(LS); break; default: flag = false; diff --git a/protocol/src/main/resources/javascript/buffer/ByteBuffer.js b/protocol/src/main/resources/javascript/buffer/ByteBuffer.js index dda8c512..0e55e7f0 100644 --- a/protocol/src/main/resources/javascript/buffer/ByteBuffer.js +++ b/protocol/src/main/resources/javascript/buffer/ByteBuffer.js @@ -112,6 +112,12 @@ const ByteBuffer = function() { this.writeOffset += length; }; + this.toBytes = function() { + const result = new ArrayBuffer(this.writeOffset); + new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset))); + return result; + }; + this.writeByte = function(value) { this.ensureCapacity(1); this.bufferView.setInt8(this.writeOffset, value); @@ -336,12 +342,6 @@ const ByteBuffer = function() { 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) || (value === undefined); this.writeBoolean(!flag); @@ -580,6 +580,240 @@ const ByteBuffer = function() { return array; }; + // ---------------------------------------------list------------------------------------------- + this.writeBooleanList = function(list) { + this.writeBooleanArray(list); + }; + + this.readBooleanList = function() { + return this.readBooleanArray(); + }; + + this.writeByteList = function(list) { + this.writeByteArray(list); + }; + + this.readByteList = function() { + return this.readByteArray(); + }; + + this.writeShortList = function(list) { + this.writeShortArray(list); + }; + + this.readShortList = function() { + return this.readShortArray(); + }; + + this.writeIntList = function(list) { + this.writeIntArray(list); + }; + + this.readIntList = function() { + return this.readIntArray(); + }; + + this.writeLongList = function(list) { + this.writeLongArray(list); + }; + + this.readLongList = function() { + return this.readLongArray(); + }; + + this.writeFloatList = function(list) { + this.writeFloatArray(list); + }; + + this.readFloatList = function() { + return this.readFloatArray(); + }; + + this.writeDoubleList = function(list) { + this.writeDoubleArray(list); + }; + + this.readDoubleList = function() { + return this.readDoubleArray(); + }; + + this.writeStringList = function(list) { + this.writeStringArray(list); + }; + + this.readStringList = function() { + return this.readStringArray(); + }; + + this.writeCharList = function(list) { + this.writeCharArray(list); + }; + + this.readCharList = function() { + return this.readCharArray(); + }; + + this.writePacketList = function(list, protocolId) { + this.writePacketArray(list, protocolId); + }; + + this.readPacketList = function(protocolId) { + return this.readPacketArray(protocolId); + }; + + // ---------------------------------------------set------------------------------------------- + this.writeBooleanSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeBoolean(element); + }); + } + }; + + this.readBooleanSet = function() { + return new Set(this.readBooleanArray()); + }; + + this.writeByteSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeByte(element); + }); + } + }; + + this.readByteSet = function() { + return new Set(this.readByteArray()); + }; + + this.writeShortSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeShort(element); + }); + } + }; + + this.readShortSet = function() { + return new Set(this.readShortArray()); + }; + + this.writeIntSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeInt(element); + }); + } + }; + + this.readIntSet = function() { + return new Set(this.readIntArray()); + }; + + this.writeLongSet = function(set) { + if (array === null) { + set.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeLong(element); + }); + } + }; + + this.readLongSet = function() { + return new Set(this.readLongArray()); + }; + + this.writeFloatSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeFloat(element); + }); + } + }; + + this.readFloatSet = function() { + return new Set(this.readFloatArray()); + }; + + this.writeDoubleSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeDouble(element); + }); + } + }; + + this.readDoubleSet = function() { + return new Set(this.readDoubleArray()); + }; + + this.writeStringSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeString(element); + }); + } + }; + + this.readStringSet = function() { + return new Set(this.readStringArray()); + }; + + this.writeCharSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeChar(element); + }); + } + }; + + this.readCharSet = function() { + return new Set(this.readCharArray()); + }; + + this.writePacketSet = function(set, protocolId) { + if (set === null) { + this.writeInt(0); + } else { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + this.writeInt(set.size); + set.forEach(element => { + protocolRegistration.write(this, element); + }); + } + }; + + this.readPacketSet = function(protocolId) { + return new Set(this.readPacketArray(protocolId)); + }; + + // ---------------------------------------------map------------------------------------------- this.writeIntIntMap = function(map) { if (map === null) { this.writeInt(0); diff --git a/protocol/src/test/javascript/zfoojs/buffer/ByteBuffer.js b/protocol/src/test/javascript/zfoojs/buffer/ByteBuffer.js index bf44d124..0e55e7f0 100644 --- a/protocol/src/test/javascript/zfoojs/buffer/ByteBuffer.js +++ b/protocol/src/test/javascript/zfoojs/buffer/ByteBuffer.js @@ -12,13 +12,13 @@ const maxInt = 2147483647; const minInt = -2147483648; // UTF-8编码与解码 -// const encoder = new TextEncoder('utf-8'); -// const decoder = new TextDecoder('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'); +// const util = require('util'); +// const encoder = new util.TextEncoder('utf-8'); +// const decoder = new util.TextDecoder('utf-8'); // 在js中long可以支持的最大值 // const maxLong = 9007199254740992; @@ -112,6 +112,12 @@ const ByteBuffer = function() { this.writeOffset += length; }; + this.toBytes = function() { + const result = new ArrayBuffer(this.writeOffset); + new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset))); + return result; + }; + this.writeByte = function(value) { this.ensureCapacity(1); this.bufferView.setInt8(this.writeOffset, value); @@ -336,12 +342,6 @@ const ByteBuffer = function() { 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) || (value === undefined); this.writeBoolean(!flag); @@ -580,6 +580,240 @@ const ByteBuffer = function() { return array; }; + // ---------------------------------------------list------------------------------------------- + this.writeBooleanList = function(list) { + this.writeBooleanArray(list); + }; + + this.readBooleanList = function() { + return this.readBooleanArray(); + }; + + this.writeByteList = function(list) { + this.writeByteArray(list); + }; + + this.readByteList = function() { + return this.readByteArray(); + }; + + this.writeShortList = function(list) { + this.writeShortArray(list); + }; + + this.readShortList = function() { + return this.readShortArray(); + }; + + this.writeIntList = function(list) { + this.writeIntArray(list); + }; + + this.readIntList = function() { + return this.readIntArray(); + }; + + this.writeLongList = function(list) { + this.writeLongArray(list); + }; + + this.readLongList = function() { + return this.readLongArray(); + }; + + this.writeFloatList = function(list) { + this.writeFloatArray(list); + }; + + this.readFloatList = function() { + return this.readFloatArray(); + }; + + this.writeDoubleList = function(list) { + this.writeDoubleArray(list); + }; + + this.readDoubleList = function() { + return this.readDoubleArray(); + }; + + this.writeStringList = function(list) { + this.writeStringArray(list); + }; + + this.readStringList = function() { + return this.readStringArray(); + }; + + this.writeCharList = function(list) { + this.writeCharArray(list); + }; + + this.readCharList = function() { + return this.readCharArray(); + }; + + this.writePacketList = function(list, protocolId) { + this.writePacketArray(list, protocolId); + }; + + this.readPacketList = function(protocolId) { + return this.readPacketArray(protocolId); + }; + + // ---------------------------------------------set------------------------------------------- + this.writeBooleanSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeBoolean(element); + }); + } + }; + + this.readBooleanSet = function() { + return new Set(this.readBooleanArray()); + }; + + this.writeByteSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeByte(element); + }); + } + }; + + this.readByteSet = function() { + return new Set(this.readByteArray()); + }; + + this.writeShortSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeShort(element); + }); + } + }; + + this.readShortSet = function() { + return new Set(this.readShortArray()); + }; + + this.writeIntSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeInt(element); + }); + } + }; + + this.readIntSet = function() { + return new Set(this.readIntArray()); + }; + + this.writeLongSet = function(set) { + if (array === null) { + set.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeLong(element); + }); + } + }; + + this.readLongSet = function() { + return new Set(this.readLongArray()); + }; + + this.writeFloatSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeFloat(element); + }); + } + }; + + this.readFloatSet = function() { + return new Set(this.readFloatArray()); + }; + + this.writeDoubleSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeDouble(element); + }); + } + }; + + this.readDoubleSet = function() { + return new Set(this.readDoubleArray()); + }; + + this.writeStringSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeString(element); + }); + } + }; + + this.readStringSet = function() { + return new Set(this.readStringArray()); + }; + + this.writeCharSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeChar(element); + }); + } + }; + + this.readCharSet = function() { + return new Set(this.readCharArray()); + }; + + this.writePacketSet = function(set, protocolId) { + if (set === null) { + this.writeInt(0); + } else { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + this.writeInt(set.size); + set.forEach(element => { + protocolRegistration.write(this, element); + }); + } + }; + + this.readPacketSet = function(protocolId) { + return new Set(this.readPacketArray(protocolId)); + }; + + // ---------------------------------------------map------------------------------------------- this.writeIntIntMap = function(map) { if (map === null) { this.writeInt(0); diff --git a/protocol/src/test/javascript/zfoojs/packet/ComplexObject.js b/protocol/src/test/javascript/zfoojs/packet/ComplexObject.js index ca794c9a..fd9b58a2 100644 --- a/protocol/src/test/javascript/zfoojs/packet/ComplexObject.js +++ b/protocol/src/test/javascript/zfoojs/packet/ComplexObject.js @@ -108,7 +108,7 @@ ComplexObject.write = function(buffer, packet) { buffer.writeStringArray(packet.jjj); buffer.writePacket(packet.kk, 102); buffer.writePacketArray(packet.kkk, 102); - buffer.writeIntArray(packet.l); + buffer.writeIntList(packet.l); if (packet.ll === null) { buffer.writeInt(0); } else { @@ -119,7 +119,7 @@ ComplexObject.write = function(buffer, packet) { } else { buffer.writeInt(element0.length); element0.forEach(element1 => { - buffer.writeIntArray(element1); + buffer.writeIntList(element1); }); } }); @@ -129,10 +129,10 @@ ComplexObject.write = function(buffer, packet) { } else { buffer.writeInt(packet.lll.length); packet.lll.forEach(element2 => { - buffer.writePacketArray(element2, 102); + buffer.writePacketList(element2, 102); }); } - buffer.writeStringArray(packet.llll); + buffer.writeStringList(packet.llll); if (packet.lllll === null) { buffer.writeInt(0); } else { @@ -149,7 +149,7 @@ ComplexObject.write = function(buffer, packet) { buffer.writeInt(packet.mmm.size); packet.mmm.forEach((value5, key4) => { buffer.writePacket(key4, 102); - buffer.writeIntArray(value5); + buffer.writeIntList(value5); }); } if (packet.mmmm === null) { @@ -162,7 +162,7 @@ ComplexObject.write = function(buffer, packet) { } else { buffer.writeInt(key6.length); key6.forEach(element8 => { - buffer.writePacketArray(element8, 102); + buffer.writePacketList(element8, 102); }); } if (value7 === null) { @@ -175,7 +175,7 @@ ComplexObject.write = function(buffer, packet) { } else { buffer.writeInt(element9.length); element9.forEach(element10 => { - buffer.writeIntArray(element10); + buffer.writeIntList(element10); }); } }); @@ -205,7 +205,7 @@ ComplexObject.write = function(buffer, packet) { } }); } - buffer.writeIntArray(packet.s); + buffer.writeIntSet(packet.s); if (packet.ss === null) { buffer.writeInt(0); } else { @@ -216,7 +216,7 @@ ComplexObject.write = function(buffer, packet) { } else { buffer.writeInt(element15.size); element15.forEach(element16 => { - buffer.writeIntArray(element16); + buffer.writeIntList(element16); }); } }); @@ -226,10 +226,10 @@ ComplexObject.write = function(buffer, packet) { } else { buffer.writeInt(packet.sss.size); packet.sss.forEach(element17 => { - buffer.writePacketArray(element17, 102); + buffer.writePacketSet(element17, 102); }); } - buffer.writeStringArray(packet.ssss); + buffer.writeStringSet(packet.ssss); if (packet.sssss === null) { buffer.writeInt(0); } else { @@ -319,7 +319,7 @@ ComplexObject.read = function(buffer) { packet.kk = result53; const array54 = buffer.readPacketArray(102); packet.kkk = array54; - const list55 = buffer.readIntArray(); + const list55 = buffer.readIntList(); packet.l = list55; const result56 = []; const size57 = buffer.readInt(); @@ -329,7 +329,7 @@ ComplexObject.read = function(buffer) { const size60 = buffer.readInt(); if (size60 > 0) { for (let index61 = 0; index61 < size60; index61++) { - const list62 = buffer.readIntArray(); + const list62 = buffer.readIntList(); result59.push(list62); } } @@ -341,12 +341,12 @@ ComplexObject.read = function(buffer) { const size64 = buffer.readInt(); if (size64 > 0) { for (let index65 = 0; index65 < size64; index65++) { - const list66 = buffer.readPacketArray(102); + const list66 = buffer.readPacketList(102); result63.push(list66); } } packet.lll = result63; - const list67 = buffer.readStringArray(); + const list67 = buffer.readStringList(); packet.llll = list67; const result68 = []; const size69 = buffer.readInt(); @@ -366,7 +366,7 @@ ComplexObject.read = function(buffer) { if (size75 > 0) { for (let index76 = 0; index76 < size75; index76++) { const result77 = buffer.readPacket(102); - const list78 = buffer.readIntArray(); + const list78 = buffer.readIntList(); result74.set(result77, list78); } } @@ -379,7 +379,7 @@ ComplexObject.read = function(buffer) { const size83 = buffer.readInt(); if (size83 > 0) { for (let index84 = 0; index84 < size83; index84++) { - const list85 = buffer.readPacketArray(102); + const list85 = buffer.readPacketList(102); result82.push(list85); } } @@ -391,7 +391,7 @@ ComplexObject.read = function(buffer) { const size90 = buffer.readInt(); if (size90 > 0) { for (let index91 = 0; index91 < size90; index91++) { - const list92 = buffer.readIntArray(); + const list92 = buffer.readIntList(); result89.push(list92); } } @@ -426,7 +426,7 @@ ComplexObject.read = function(buffer) { } } packet.mmmmm = result93; - const set104 = buffer.readIntArray(); + const set104 = buffer.readIntSet(); packet.s = set104; const result105 = new Set(); const size106 = buffer.readInt(); @@ -436,7 +436,7 @@ ComplexObject.read = function(buffer) { const size109 = buffer.readInt(); if (size109 > 0) { for (let index110 = 0; index110 < size109; index110++) { - const list111 = buffer.readIntArray(); + const list111 = buffer.readIntList(); result108.add(list111); } } @@ -448,12 +448,12 @@ ComplexObject.read = function(buffer) { const size113 = buffer.readInt(); if (size113 > 0) { for (let index114 = 0; index114 < size113; index114++) { - const set115 = buffer.readPacketArray(102); + const set115 = buffer.readPacketSet(102); result112.add(set115); } } packet.sss = result112; - const set116 = buffer.readStringArray(); + const set116 = buffer.readStringSet(); packet.ssss = set116; const result117 = new Set(); const size118 = buffer.readInt(); diff --git a/protocol/src/test/javascript/zfoojs/packet/NormalObject.js b/protocol/src/test/javascript/zfoojs/packet/NormalObject.js index 96861deb..286ddd48 100644 --- a/protocol/src/test/javascript/zfoojs/packet/NormalObject.js +++ b/protocol/src/test/javascript/zfoojs/packet/NormalObject.js @@ -39,14 +39,14 @@ NormalObject.write = function(buffer, packet) { 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.writeIntList(packet.l); + buffer.writeLongList(packet.ll); + buffer.writePacketList(packet.lll, 102); + buffer.writeStringList(packet.llll); buffer.writeIntStringMap(packet.m); buffer.writeIntPacketMap(packet.mm, 102); - buffer.writeIntArray(packet.s); - buffer.writeStringArray(packet.ssss); + buffer.writeIntSet(packet.s); + buffer.writeStringSet(packet.ssss); }; NormalObject.read = function(buffer) { @@ -74,21 +74,21 @@ NormalObject.read = function(buffer) { packet.jj = result8; const result9 = buffer.readPacket(102); packet.kk = result9; - const list10 = buffer.readIntArray(); + const list10 = buffer.readIntList(); packet.l = list10; - const list11 = buffer.readLongArray(); + const list11 = buffer.readLongList(); packet.ll = list11; - const list12 = buffer.readPacketArray(102); + const list12 = buffer.readPacketList(102); packet.lll = list12; - const list13 = buffer.readStringArray(); + const list13 = buffer.readStringList(); packet.llll = list13; const map14 = buffer.readIntStringMap(); packet.m = map14; const map15 = buffer.readIntPacketMap(102); packet.mm = map15; - const set16 = buffer.readIntArray(); + const set16 = buffer.readIntSet(); packet.s = set16; - const set17 = buffer.readStringArray(); + const set17 = buffer.readStringSet(); packet.ssss = set17; return packet; };