ref[protocol]: 重构protocol的CSharp的解析

This commit is contained in:
jaysunxiao
2021-10-17 22:50:01 +08:00
parent e5e849f0a7
commit c8d159def5
7 changed files with 783 additions and 12 deletions
@@ -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();
}
}
@@ -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();
}
}
@@ -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);
@@ -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);
@@ -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))
@@ -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>(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<T>(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<bool> 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<bool> ReadBooleanList()
{
int size = ReadInt();
List<bool> list = new List<bool>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
list.Add(ReadBool());
}
}
return list;
}
public void WriteByteList(List<byte> 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<byte> ReadByteList()
{
int size = ReadInt();
List<byte> list = new List<byte>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
list.Add(ReadByte());
}
}
return list;
}
public void WriteShortList(List<short> 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<short> ReadShortList()
{
int size = ReadInt();
List<short> list = new List<short>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
list.Add(ReadShort());
}
}
return list;
}
public void WriteIntList(List<int> 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<int> ReadIntList()
{
int size = ReadInt();
List<int> list = new List<int>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
list.Add(ReadInt());
}
}
return list;
}
public void WriteLongList(List<long> 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<long> ReadLongList()
{
int size = ReadInt();
List<long> list = new List<long>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
list.Add(ReadLong());
}
}
return list;
}
public void WriteFloatList(List<float> 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<float> ReadFloatList()
{
int size = ReadInt();
List<float> list = new List<float>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
list.Add(ReadFloat());
}
}
return list;
}
public void WriteDoubleList(List<double> 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<double> ReadDoubleList()
{
int size = ReadInt();
List<double> list = new List<double>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
list.Add(ReadDouble());
}
}
return list;
}
public void WriteCharList(List<char> 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<char> ReadCharList()
{
int size = ReadInt();
List<char> list = new List<char>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
list.Add(ReadChar());
}
}
return list;
}
public void WriteStringList(List<string> 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<string> ReadStringList()
{
int size = ReadInt();
List<string> list = new List<string>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
list.Add(ReadString());
}
}
return list;
}
public void WritePacketList<T>(List<T> 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<T> ReadPacketList<T>(short protocolId)
{
int size = ReadInt();
List<T> list = new List<T>(size);
if (size > 0)
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
for (int index = 0; index < size; index++)
{
list.Add((T) protocolRegistration.Read(this));
}
}
return list;
}
}
}
}
@@ -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);