From c8d159def59acf46ef52188f27dd4c2089dde901 Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Sun, 17 Oct 2021 22:50:01 +0800 Subject: [PATCH] =?UTF-8?q?ref[protocol]:=20=E9=87=8D=E6=9E=84protocol?= =?UTF-8?q?=E7=9A=84CSharp=E7=9A=84=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../serializer/CutDownArraySerializer.java | 63 +- .../serializer/CutDownListSerializer.java | 60 ++ .../serializer/cs/CsArraySerializer.java | 16 +- .../serializer/cs/CsListSerializer.java | 16 +- .../serializer/cs/GenerateCsUtils.java | 4 +- .../main/resources/cs/Buffer/ByteBuffer.cs | 633 +++++++++++++++++- .../java/com/zfoo/protocol/SpeedTest.java | 3 +- 7 files changed, 783 insertions(+), 12 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java index 59713bd0..52ee952f 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java @@ -52,6 +52,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteBooleanArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -64,6 +67,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteBooleanArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -76,6 +82,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteByteArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -88,6 +97,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteByteArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -100,6 +112,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteShortArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -112,6 +127,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteShortArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -124,6 +142,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteIntArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -136,6 +157,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteIntArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -148,6 +172,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteLongArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -160,6 +187,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteLongArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -172,6 +202,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteFloatArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -184,6 +217,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteFloatArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -196,6 +232,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteDoubleArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -208,6 +247,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteDoubleArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -220,6 +262,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteStringArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -232,6 +277,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeCharArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteCharArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -244,6 +292,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeCharArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteCharArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -258,6 +309,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WritePacketArray<{}>({}, {});", getArrayClassSimpleName(arrayField), objectStr, objectProtocolField.getProtocolId())).append(LS); + break; default: flag = false; } @@ -486,10 +540,13 @@ public class CutDownArraySerializer implements ICutDownSerializer { if (arrayField.getArrayElementRegistration() instanceof ObjectProtocolField) { var objectProtocolField = (ObjectProtocolField) arrayField.getArrayElementRegistration(); switch (language) { - // Java不支持泛型的数组初始化,这边就不做任何操作 + // Java不支持泛型的数组初始化,这里不做任何操作 case JavaScript: builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", array, objectProtocolField.getProtocolId())).append(LS); break; + case CSharp: + builder.append(StringUtils.format("var {} = buffer.ReadPacketArray<{}>({});", array, getArrayClassSimpleName(arrayField), objectProtocolField.getProtocolId())).append(LS); + break; default: flag = false; } @@ -511,4 +568,8 @@ public class CutDownArraySerializer implements ICutDownSerializer { return arrayField.getField().getType().getComponentType().getCanonicalName().replaceFirst("java.lang.", StringUtils.EMPTY); } + public String getArrayClassSimpleName(ArrayField arrayField) { + return arrayField.getField().getType().getComponentType().getSimpleName(); + } + } 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 f46d2be6..0c938881 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java @@ -20,6 +20,7 @@ import com.zfoo.protocol.registration.field.ObjectProtocolField; import com.zfoo.protocol.util.StringUtils; import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; import static com.zfoo.protocol.util.FileUtils.LS; @@ -49,6 +50,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteBooleanList({});", objectStr)).append(LS); + break; default: flag = false; } @@ -61,6 +65,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteByteList({});", objectStr)).append(LS); + break; default: flag = false; } @@ -73,6 +80,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteShortList({});", objectStr)).append(LS); + break; default: flag = false; } @@ -85,6 +95,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteIntList({});", objectStr)).append(LS); + break; default: flag = false; } @@ -97,6 +110,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteLongList({});", objectStr)).append(LS); + break; default: flag = false; } @@ -110,6 +126,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteFloatList({});", objectStr)).append(LS); + break; default: flag = false; } @@ -123,6 +142,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteDoubleList({});", objectStr)).append(LS); + break; default: flag = false; } @@ -136,6 +158,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WriteStringList({});", objectStr)).append(LS); + break; default: flag = false; } @@ -151,6 +176,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); break; + case CSharp: + builder.append(StringUtils.format("buffer.WritePacketList<{}>({}, {});", getListClassSimpleName(listField), objectStr, objectProtocolField.getProtocolId())).append(LS); + break; default: flag = false; } @@ -177,6 +205,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", list)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("var {} = buffer.ReadBooleanList();", list)).append(LS); + break; default: flag = false; } @@ -189,6 +220,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readByteArray();", list)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("var {} = buffer.ReadByteList();", list)).append(LS); + break; default: flag = false; } @@ -201,6 +235,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readShortArray();", list)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("var {} = buffer.ReadShortList();", list)).append(LS); + break; default: flag = false; } @@ -213,6 +250,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readIntArray();", list)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("var {} = buffer.ReadIntList();", list)).append(LS); + break; default: flag = false; } @@ -225,6 +265,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readLongArray();", list)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("var {} = buffer.ReadLongList();", list)).append(LS); + break; default: flag = false; } @@ -237,6 +280,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readFloatArray();", list)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("var {} = buffer.ReadFloatList();", list)).append(LS); + break; default: flag = false; } @@ -249,6 +295,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", list)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("var {} = buffer.ReadDoubleList();", list)).append(LS); + break; default: flag = false; } @@ -261,6 +310,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readStringArray();", list)).append(LS); break; + case CSharp: + builder.append(StringUtils.format("var {} = buffer.ReadStringList();", list)).append(LS); + break; default: flag = false; } @@ -275,6 +327,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case JavaScript: builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", list, objectProtocolField.getProtocolId())).append(LS); break; + case CSharp: + builder.append(StringUtils.format("var {} = buffer.ReadPacketList<{}>({});", list, getListClassSimpleName(listField), objectProtocolField.getProtocolId())).append(LS); + break; default: flag = false; } @@ -291,4 +346,9 @@ public class CutDownListSerializer implements ICutDownSerializer { return null; } } + + public String getListClassSimpleName(ListField listField) { + return ((Class) ((ParameterizedType) listField.getType()).getActualTypeArguments()[0]).getSimpleName(); + } + } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsArraySerializer.java index 3e79e4aa..5a87d06d 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsArraySerializer.java @@ -16,6 +16,8 @@ package com.zfoo.protocol.serializer.cs; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.ArrayField; import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownArraySerializer; import com.zfoo.protocol.util.StringUtils; import java.lang.reflect.Field; @@ -30,9 +32,13 @@ public class CsArraySerializer implements ICsSerializer { @Override public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + if (CutDownArraySerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.CSharp)) { + return; + } + ArrayField arrayField = (ArrayField) fieldRegistration; - GenerateProtocolFile.addTab(builder, deep); builder.append(StringUtils.format("if (({} == null) || ({}.Length == 0))", objectStr, objectStr)).append(LS); GenerateProtocolFile.addTab(builder, deep); builder.append("{").append(LS); @@ -72,6 +78,13 @@ public class CsArraySerializer implements ICsSerializer { @Override public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + var cutDown = CutDownArraySerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.CSharp); + if (cutDown != null) { + return cutDown; + } + + var arrayField = (ArrayField) fieldRegistration; var result = "result" + GenerateProtocolFile.index.getAndIncrement(); @@ -79,7 +92,6 @@ public class CsArraySerializer implements ICsSerializer { var i = "index" + GenerateProtocolFile.index.getAndIncrement(); var size = "size" + GenerateProtocolFile.index.getAndIncrement(); - GenerateProtocolFile.addTab(builder, deep); builder.append(StringUtils.format("int {} = buffer.ReadInt();", size)).append(LS); GenerateProtocolFile.addTab(builder, deep); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsListSerializer.java index b6e95af6..dc7447ff 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cs/CsListSerializer.java @@ -16,6 +16,8 @@ package com.zfoo.protocol.serializer.cs; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; import com.zfoo.protocol.registration.field.ListField; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownListSerializer; import com.zfoo.protocol.util.StringUtils; import java.lang.reflect.Field; @@ -30,9 +32,12 @@ public class CsListSerializer implements ICsSerializer { @Override public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { - ListField listField = (ListField) fieldRegistration; - GenerateProtocolFile.addTab(builder, deep); + if (CutDownListSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.CSharp)) { + return; + } + + ListField listField = (ListField) fieldRegistration; builder.append(StringUtils.format("if ({} == null)", objectStr)).append(LS); GenerateProtocolFile.addTab(builder, deep); builder.append("{").append(LS); @@ -74,6 +79,12 @@ public class CsListSerializer implements ICsSerializer { @Override public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + var cutDown = CutDownListSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.CSharp); + if (cutDown != null) { + return cutDown; + } + var listField = (ListField) fieldRegistration; var result = "result" + GenerateProtocolFile.index.getAndIncrement(); @@ -81,7 +92,6 @@ public class CsListSerializer implements ICsSerializer { var i = "index" + GenerateProtocolFile.index.getAndIncrement(); var size = "size" + GenerateProtocolFile.index.getAndIncrement(); - GenerateProtocolFile.addTab(builder, deep); builder.append(StringUtils.format("int {} = buffer.ReadInt();", size)).append(LS); GenerateProtocolFile.addTab(builder, deep); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cs/GenerateCsUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cs/GenerateCsUtils.java index 41789f5b..bfa43f00 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cs/GenerateCsUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cs/GenerateCsUtils.java @@ -325,12 +325,10 @@ public abstract class GenerateCsUtils { var csBuilder = new StringBuilder(); csBuilder.append(TAB + TAB).append("public void Write(ByteBuffer buffer, IPacket packet)").append(LS); csBuilder.append(TAB + TAB).append("{").append(LS); - csBuilder.append(TAB + TAB + TAB).append("if (packet == null)").append(LS); + csBuilder.append(TAB + TAB + TAB).append("if (buffer.WritePacketFlag(packet))").append(LS); csBuilder.append(TAB + TAB + TAB).append("{").append(LS); - csBuilder.append(TAB + TAB + TAB + TAB).append("buffer.WriteBool(false);").append(LS); csBuilder.append(TAB + TAB + TAB + TAB).append("return;").append(LS); csBuilder.append(TAB + TAB + TAB + "}").append(LS); - csBuilder.append(TAB + TAB + TAB).append("buffer.WriteBool(true);").append(LS); csBuilder.append(TAB + TAB + TAB) .append(StringUtils.format("{} message = ({}) packet;", protocolClazzName, protocolClazzName)) diff --git a/protocol/src/main/resources/cs/Buffer/ByteBuffer.cs b/protocol/src/main/resources/cs/Buffer/ByteBuffer.cs index cfd6f428..40b99921 100644 --- a/protocol/src/main/resources/cs/Buffer/ByteBuffer.cs +++ b/protocol/src/main/resources/cs/Buffer/ByteBuffer.cs @@ -557,5 +557,636 @@ namespace CsProtocol.Buffer return Encoding.UTF8.GetString(value, 0, value.Length); } + + public bool WritePacketFlag(IPacket packet) + { + bool flag = packet == null; + WriteBool(!flag); + return flag; + } + + public void WriteBooleanArray(bool[] array) + { + if ((array == null) || (array.Length == 0)) + { + WriteInt(0); + } + else + { + WriteInt(array.Length); + int length = array.Length; + for (int index = 0; index < length; index++) + { + WriteBool(array[index]); + } + } + } + + public bool[] ReadBooleanArray() + { + int size = ReadInt(); + bool[] array = new bool[size]; + if (size > 0) + { + for (int index = 0; index < size; index++) + { + array[index] = ReadBool(); + } + } + return array; + } + + public void WriteByteArray(byte[] array) + { + if ((array == null) || (array.Length == 0)) + { + WriteInt(0); + } + else + { + WriteInt(array.Length); + int length = array.Length; + for (int index = 0; index < length; index++) + { + WriteByte(array[index]); + } + } + } + + public byte[] ReadByteArray() + { + int size = ReadInt(); + byte[] array = new byte[size]; + if (size > 0) + { + for (int index = 0; index < size; index++) + { + array[index] = ReadByte(); + } + } + return array; + } + + public void WriteShortArray(short[] array) + { + if ((array == null) || (array.Length == 0)) + { + WriteInt(0); + } + else + { + WriteInt(array.Length); + int length = array.Length; + for (int index = 0; index < length; index++) + { + WriteShort(array[index]); + } + } + } + + public short[] ReadShortArray() + { + int size = ReadInt(); + short[] array = new short[size]; + if (size > 0) + { + for (int index = 0; index < size; index++) + { + array[index] = ReadShort(); + } + } + return array; + } + + public void WriteIntArray(int[] array) + { + if ((array == null) || (array.Length == 0)) + { + WriteInt(0); + } + else + { + WriteInt(array.Length); + int length = array.Length; + for (int index = 0; index < length; index++) + { + WriteInt(array[index]); + } + } + } + + public int[] ReadIntArray() + { + int size = ReadInt(); + int[] array = new int[size]; + if (size > 0) + { + for (int index = 0; index < size; index++) + { + array[index] = ReadInt(); + } + } + return array; + } + + public void WriteLongArray(long[] array) + { + if ((array == null) || (array.Length == 0)) + { + WriteInt(0); + } + else + { + WriteInt(array.Length); + int length = array.Length; + for (int index = 0; index < length; index++) + { + WriteLong(array[index]); + } + } + } + + public long[] ReadLongArray() + { + int size = ReadInt(); + long[] array = new long[size]; + if (size > 0) + { + for (int index = 0; index < size; index++) + { + array[index] = ReadLong(); + } + } + return array; + } + + public void WriteFloatArray(float[] array) + { + if ((array == null) || (array.Length == 0)) + { + WriteInt(0); + } + else + { + WriteInt(array.Length); + int length = array.Length; + for (int index = 0; index < length; index++) + { + WriteFloat(array[index]); + } + } + } + + public float[] ReadFloatArray() + { + int size = ReadInt(); + float[] array = new float[size]; + if (size > 0) + { + for (int index = 0; index < size; index++) + { + array[index] = ReadLong(); + } + } + return array; + } + + public void WriteDoubleArray(double[] array) + { + if ((array == null) || (array.Length == 0)) + { + WriteInt(0); + } + else + { + WriteInt(array.Length); + int length = array.Length; + for (int index = 0; index < length; index++) + { + WriteDouble(array[index]); + } + } + } + + public double[] ReadDoubleArray() + { + int size = ReadInt(); + double[] array = new double[size]; + if (size > 0) + { + for (int index = 0; index < size; index++) + { + array[index] = ReadLong(); + } + } + return array; + } + + public void WriteCharArray(char[] array) + { + if ((array == null) || (array.Length == 0)) + { + WriteInt(0); + } + else + { + WriteInt(array.Length); + int length = array.Length; + for (int index = 0; index < length; index++) + { + WriteChar(array[index]); + } + } + } + + public char[] ReadCharArray() + { + int size = ReadInt(); + char[] array = new char[size]; + if (size > 0) + { + for (int index = 0; index < size; index++) + { + array[index] = ReadChar(); + } + } + return array; + } + + public void WriteStringArray(string[] array) + { + if ((array == null) || (array.Length == 0)) + { + WriteInt(0); + } + else + { + WriteInt(array.Length); + int length = array.Length; + for (int index = 0; index < length; index++) + { + WriteString(array[index]); + } + } + } + + public string[] ReadStringArray() + { + int size = ReadInt(); + string[] array = new string[size]; + if (size > 0) + { + for (int index = 0; index < size; index++) + { + array[index] = ReadString(); + } + } + return array; + } + + public void WritePacketArray(T[] array, short protocolId) + { + if ((array == null) || (array.Length == 0)) + { + WriteInt(0); + } + else + { + IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); + WriteInt(array.Length); + int length = array.Length; + for (int index = 0; index < length; index++) + { + protocolRegistration.Write(this, (IPacket) array[index]); + } + } + } + + public T[] ReadPacketArray(short protocolId) + { + int size = ReadInt(); + T[] array = new T[size]; + if (size > 0) + { + IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); + for (int index = 0; index < size; index++) + { + array[index] = (T) protocolRegistration.Read(this); + } + } + return array; + } + + public void WriteBooleanList(List list) + { + if ((list == null) || (list.Count == 0)) + { + WriteInt(0); + } + else + { + WriteInt(list.Count); + int length = list.Count; + for (int index = 0; index < length; index++) + { + WriteBool(list[index]); + } + } + } + + public List ReadBooleanList() + { + int size = ReadInt(); + List list = new List(size); + if (size > 0) + { + for (int index = 0; index < size; index++) + { + list.Add(ReadBool()); + } + } + return list; + } + + public void WriteByteList(List list) + { + if ((list == null) || (list.Count == 0)) + { + WriteInt(0); + } + else + { + WriteInt(list.Count); + int length = list.Count; + for (int index = 0; index < length; index++) + { + WriteByte(list[index]); + } + } + } + + public List ReadByteList() + { + int size = ReadInt(); + List list = new List(size); + if (size > 0) + { + for (int index = 0; index < size; index++) + { + list.Add(ReadByte()); + } + } + return list; + } + + public void WriteShortList(List list) + { + if ((list == null) || (list.Count == 0)) + { + WriteInt(0); + } + else + { + WriteInt(list.Count); + int length = list.Count; + for (int index = 0; index < length; index++) + { + WriteShort(list[index]); + } + } + } + + public List ReadShortList() + { + int size = ReadInt(); + List list = new List(size); + if (size > 0) + { + for (int index = 0; index < size; index++) + { + list.Add(ReadShort()); + } + } + return list; + } + + public void WriteIntList(List list) + { + if ((list == null) || (list.Count == 0)) + { + WriteInt(0); + } + else + { + WriteInt(list.Count); + int length = list.Count; + for (int index = 0; index < length; index++) + { + WriteInt(list[index]); + } + } + } + + public List ReadIntList() + { + int size = ReadInt(); + List list = new List(size); + if (size > 0) + { + for (int index = 0; index < size; index++) + { + list.Add(ReadInt()); + } + } + return list; + } + + public void WriteLongList(List list) + { + if ((list == null) || (list.Count == 0)) + { + WriteInt(0); + } + else + { + WriteInt(list.Count); + int length = list.Count; + for (int index = 0; index < length; index++) + { + WriteLong(list[index]); + } + } + } + + public List ReadLongList() + { + int size = ReadInt(); + List list = new List(size); + if (size > 0) + { + for (int index = 0; index < size; index++) + { + list.Add(ReadLong()); + } + } + return list; + } + + public void WriteFloatList(List list) + { + if ((list == null) || (list.Count == 0)) + { + WriteInt(0); + } + else + { + WriteInt(list.Count); + int length = list.Count; + for (int index = 0; index < length; index++) + { + WriteFloat(list[index]); + } + } + } + + public List ReadFloatList() + { + int size = ReadInt(); + List list = new List(size); + if (size > 0) + { + for (int index = 0; index < size; index++) + { + list.Add(ReadFloat()); + } + } + return list; + } + + public void WriteDoubleList(List list) + { + if ((list == null) || (list.Count == 0)) + { + WriteInt(0); + } + else + { + WriteInt(list.Count); + int length = list.Count; + for (int index = 0; index < length; index++) + { + WriteDouble(list[index]); + } + } + } + + public List ReadDoubleList() + { + int size = ReadInt(); + List list = new List(size); + if (size > 0) + { + for (int index = 0; index < size; index++) + { + list.Add(ReadDouble()); + } + } + return list; + } + + public void WriteCharList(List list) + { + if ((list == null) || (list.Count == 0)) + { + WriteInt(0); + } + else + { + WriteInt(list.Count); + int length = list.Count; + for (int index = 0; index < length; index++) + { + WriteDouble(list[index]); + } + } + } + + public List ReadCharList() + { + int size = ReadInt(); + List list = new List(size); + if (size > 0) + { + for (int index = 0; index < size; index++) + { + list.Add(ReadChar()); + } + } + return list; + } + + public void WriteStringList(List list) + { + if ((list == null) || (list.Count == 0)) + { + WriteInt(0); + } + else + { + WriteInt(list.Count); + int length = list.Count; + for (int index = 0; index < length; index++) + { + WriteString(list[index]); + } + } + } + + public List ReadStringList() + { + int size = ReadInt(); + List list = new List(size); + if (size > 0) + { + for (int index = 0; index < size; index++) + { + list.Add(ReadString()); + } + } + return list; + } + + public void WritePacketList(List list, short protocolId) + { + if ((list == null) || (list.Count == 0)) + { + WriteInt(0); + } + else + { + IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); + WriteInt(list.Count); + int length = list.Count; + for (int index = 0; index < length; index++) + { + protocolRegistration.Write(this, (IPacket) list[index]); + } + } + } + + public List ReadPacketList(short protocolId) + { + int size = ReadInt(); + List list = new List(size); + if (size > 0) + { + IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); + for (int index = 0; index < size; index++) + { + list.Add((T) protocolRegistration.Read(this)); + } + } + return list; + } } -} \ No newline at end of file +} diff --git a/protocol/src/test/java/com/zfoo/protocol/SpeedTest.java b/protocol/src/test/java/com/zfoo/protocol/SpeedTest.java index 20a567c8..54d898c5 100644 --- a/protocol/src/test/java/com/zfoo/protocol/SpeedTest.java +++ b/protocol/src/test/java/com/zfoo/protocol/SpeedTest.java @@ -324,8 +324,7 @@ public class SpeedTest { static { var op = GenerateOperation.NO_OPERATION; - op.getGenerateLanguages().add(CodeLanguage.JavaScript); - op.setFoldProtocol(true); +// op.getGenerateLanguages().add(CodeLanguage.JavaScript); // zfoo协议注册 ProtocolManager.initProtocol(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, ObjectA.class, ObjectB.class), op);