From 400066262fd427b37bf395115af1346d4ff0a315 Mon Sep 17 00:00:00 2001 From: godotg Date: Thu, 15 Sep 2022 17:29:03 +0800 Subject: [PATCH] =?UTF-8?q?feat[go]:=20=E6=94=AF=E6=8C=81golang=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generate/GenerateProtocolFile.java | 11 + .../generate/GenerateProtocolNote.java | 2 + .../registration/ProtocolAnalysis.java | 6 +- .../protocol/serializer/CodeLanguage.java | 2 + .../serializer/CutDownArraySerializer.java | 107 +++ .../serializer/CutDownListSerializer.java | 52 ++ .../serializer/CutDownMapSerializer.java | 83 ++- .../serializer/CutDownSetSerializer.java | 52 ++ .../serializer/cpp/GenerateCppUtils.java | 1 - .../serializer/go/GenerateGoUtils.java | 225 ++++++ .../serializer/go/GoArraySerializer.java | 120 ++++ .../serializer/go/GoBooleanSerializer.java | 50 ++ .../serializer/go/GoByteSerializer.java | 51 ++ .../serializer/go/GoCharSerializer.java | 51 ++ .../serializer/go/GoDoubleSerializer.java | 50 ++ .../serializer/go/GoFloatSerializer.java | 50 ++ .../serializer/go/GoIntSerializer.java | 49 ++ .../serializer/go/GoListSerializer.java | 117 ++++ .../serializer/go/GoLongSerializer.java | 50 ++ .../serializer/go/GoMapSerializer.java | 127 ++++ .../go/GoObjectProtocolSerializer.java | 62 ++ .../serializer/go/GoSetSerializer.java | 117 ++++ .../serializer/go/GoShortSerializer.java | 50 ++ .../serializer/go/GoStringSerializer.java | 51 ++ .../protocol/serializer/go/IGoSerializer.java | 35 + protocol/src/main/resources/go/ByteBuffer.go | 662 +++++++++++++++--- .../resources/go/ProtocolManagerTemplate.go | 5 + .../src/main/resources/go/ProtocolTemplate.go | 26 + .../go/{buffer => protocol}/ByteBuffer.go | 477 +++++++++++++ protocol/src/test/go/protocol/NormalObject.go | 127 ++++ protocol/src/test/go/protocol/ObjectA.go | 35 + protocol/src/test/go/protocol/ObjectB.go | 27 + .../src/test/go/protocol/ProtocolManager.go | 8 + protocol/src/test/go/protocol/SimpleObject.go | 31 + .../go/{buffer => protocol}/buffer_test.go | 16 + .../src/test/resources/NormalObject.bytes | Bin 0 -> 430 bytes 36 files changed, 2888 insertions(+), 97 deletions(-) create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoArraySerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoBooleanSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoByteSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoCharSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoDoubleSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoFloatSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoIntSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoListSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoLongSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoMapSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoObjectProtocolSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoSetSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoShortSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GoStringSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/IGoSerializer.java create mode 100644 protocol/src/main/resources/go/ProtocolManagerTemplate.go create mode 100644 protocol/src/main/resources/go/ProtocolTemplate.go rename protocol/src/test/go/{buffer => protocol}/ByteBuffer.go (53%) create mode 100644 protocol/src/test/go/protocol/NormalObject.go create mode 100644 protocol/src/test/go/protocol/ObjectA.go create mode 100644 protocol/src/test/go/protocol/ObjectB.go create mode 100644 protocol/src/test/go/protocol/ProtocolManager.go create mode 100644 protocol/src/test/go/protocol/SimpleObject.go rename protocol/src/test/go/{buffer => protocol}/buffer_test.go (91%) create mode 100644 protocol/src/test/resources/NormalObject.bytes diff --git a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java index 46ac88a8..67039c71 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java @@ -20,6 +20,7 @@ import com.zfoo.protocol.serializer.CodeLanguage; import com.zfoo.protocol.serializer.cpp.GenerateCppUtils; import com.zfoo.protocol.serializer.csharp.GenerateCsUtils; import com.zfoo.protocol.serializer.gdscript.GenerateGdUtils; +import com.zfoo.protocol.serializer.go.GenerateGoUtils; import com.zfoo.protocol.serializer.javascript.GenerateJsUtils; import com.zfoo.protocol.serializer.lua.GenerateLuaUtils; import com.zfoo.protocol.serializer.protobuf.GenerateProtobufUtils; @@ -115,6 +116,16 @@ public abstract class GenerateProtocolFile { } } + // 生成Golang协议 + if (generateLanguages.contains(CodeLanguage.Go)) { + GenerateGoUtils.init(generateOperation); + GenerateGoUtils.createProtocolManager(allSortedGenerateProtocols); + for (var protocolRegistration : allSortedGenerateProtocols) { + GenerateGoUtils.createGoProtocolFile((ProtocolRegistration) protocolRegistration); + } + } + + // 生成C#协议 if (generateLanguages.contains(CodeLanguage.CSharp)) { GenerateCsUtils.init(generateOperation); GenerateCsUtils.createProtocolManager(); diff --git a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolNote.java b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolNote.java index afc0a3be..6b121877 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolNote.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolNote.java @@ -66,6 +66,7 @@ public abstract class GenerateProtocolNote { switch (language) { case Cpp: + case Go: case JavaScript: case TypeScript: case CSharp: @@ -94,6 +95,7 @@ public abstract class GenerateProtocolNote { } switch (language) { case Cpp: + case Go: case JavaScript: case TypeScript: case CSharp: diff --git a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java index 5e602ee7..1bafe503 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java @@ -28,6 +28,7 @@ import com.zfoo.protocol.registration.field.*; import com.zfoo.protocol.serializer.cpp.GenerateCppUtils; import com.zfoo.protocol.serializer.csharp.GenerateCsUtils; import com.zfoo.protocol.serializer.gdscript.GenerateGdUtils; +import com.zfoo.protocol.serializer.go.GenerateGoUtils; import com.zfoo.protocol.serializer.javascript.GenerateJsUtils; import com.zfoo.protocol.serializer.lua.GenerateLuaUtils; import com.zfoo.protocol.serializer.protobuf.GenerateProtobufUtils; @@ -218,6 +219,7 @@ public class ProtocolAnalysis { GenerateProtocolPath.clear(); GenerateProtocolFile.clear(); GenerateCppUtils.clear(); + GenerateGoUtils.clear(); GenerateCsUtils.clear(); GenerateJsUtils.clear(); GenerateTsUtils.clear(); @@ -468,7 +470,7 @@ public class ProtocolAnalysis { } catch (NoSuchMethodException e) { protocolMethod = null; } - + if (protocolMethod != null) { // 必须要有一个空的构造器 Constructor constructor = ReflectionUtils.publicEmptyConstructor(clazz); @@ -513,7 +515,7 @@ public class ProtocolAnalysis { AssertionUtils.isTrue(Modifier.isTransient(protocolIdField.getModifiers()), "[class:{}]协议序列号[{}]没有被transient修饰", clazz.getCanonicalName(), PROTOCOL_ID); // 命名只能包含字母,数字,下划线 AssertionUtils.isTrue(clazz.getSimpleName().matches("[a-zA-Z0-9_]*"), "[class:{}]的命名只能包含字母,数字,下划线", clazz.getCanonicalName(), PROTOCOL_ID); - + ReflectionUtils.makeAccessible(protocolIdField); var protocolId = (short) ReflectionUtils.getField(protocolIdField, null); if (annoProtocolId != 0) { diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java index 624cb6de..84e129f8 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java @@ -25,6 +25,8 @@ public enum CodeLanguage { Cpp, + Go, + JavaScript, TypeScript, 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 30ad4b19..2d041d78 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java @@ -60,6 +60,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteBooleanArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -83,6 +86,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteBooleanArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -106,6 +112,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteByteArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteByteArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -129,6 +138,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteByteArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteByteArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -152,6 +164,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteShortArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteShortArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -175,6 +190,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteShortArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteShortArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -198,6 +216,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteIntArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -221,6 +242,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteIntArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -244,6 +268,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteLongArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -267,6 +294,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteLongArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -290,6 +320,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteFloatArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteFloatArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -313,6 +346,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteFloatArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteFloatArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -336,6 +372,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteDoubleArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -359,6 +398,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteDoubleArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -382,6 +424,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteStringArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -405,6 +450,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteCharArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteCharArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -428,6 +476,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteCharArray({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteCharArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -460,6 +511,8 @@ public class CutDownArraySerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, protocolId)).append(LS); break; + case Go: + case Protobuf: default: flag = false; } @@ -494,6 +547,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readBooleanArray();", array)).append(LS); break; @@ -519,6 +575,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readBooleanArray();", array)).append(LS); break; @@ -544,6 +603,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadByteArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readByteArray();", array)).append(LS); break; @@ -569,6 +631,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadByteArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readByteArray();", array)).append(LS); break; @@ -594,6 +659,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadShortArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readShortArray();", array)).append(LS); break; @@ -619,6 +687,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadShortArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readShortArray();", array)).append(LS); break; @@ -644,6 +715,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadIntArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readIntArray();", array)).append(LS); break; @@ -669,6 +743,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadIntArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readIntArray();", array)).append(LS); break; @@ -694,6 +771,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadLongArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readLongArray();", array)).append(LS); break; @@ -719,6 +799,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadLongArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readLongArray();", array)).append(LS); break; @@ -744,6 +827,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadFloatArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readFloatArray();", array)).append(LS); break; @@ -769,6 +855,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadFloatArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readFloatArray();", array)).append(LS); break; @@ -794,6 +883,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readDoubleArray();", array)).append(LS); break; @@ -819,6 +911,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readDoubleArray();", array)).append(LS); break; @@ -844,6 +939,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadStringArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readStringArray();", array)).append(LS); break; @@ -869,6 +967,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadCharArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadCharArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readCharArray();", array)).append(LS); break; @@ -894,6 +995,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadCharArray();", array)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadCharArray()", array)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readCharArray();", array)).append(LS); break; @@ -926,6 +1030,9 @@ public class CutDownArraySerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", array, protocolId)).append(LS); break; + case Go: + case Protobuf: + case Enhance: default: flag = false; } 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 2063886c..19bdfed9 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java @@ -59,6 +59,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanList({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteBooleanArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -82,6 +85,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteByteList({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteByteArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -105,6 +111,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteShortList({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteShortArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -128,6 +137,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntList({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteIntArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -151,6 +163,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongList({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteLongArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -175,6 +190,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteFloatList({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteFloatArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -199,6 +217,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleList({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteDoubleArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -223,6 +244,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringList({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteStringArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -254,6 +278,8 @@ public class CutDownListSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("buffer.writePacketList({}, {});", objectStr, protocolId)).append(LS); break; + case Go: + case Protobuf: default: flag = false; } @@ -287,6 +313,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanList();", list)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray()", list)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readBooleanList();", list)).append(LS); break; @@ -312,6 +341,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteList();", list)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadByteArray()", list)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readByteList();", list)).append(LS); break; @@ -337,6 +369,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortList();", list)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadShortArray()", list)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readShortList();", list)).append(LS); break; @@ -362,6 +397,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntList();", list)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadIntArray()", list)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readIntList();", list)).append(LS); break; @@ -387,6 +425,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongList();", list)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadLongArray()", list)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readLongList();", list)).append(LS); break; @@ -412,6 +453,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatList();", list)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadFloatArray()", list)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readFloatList();", list)).append(LS); break; @@ -437,6 +481,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleList();", list)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray()", list)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readDoubleList();", list)).append(LS); break; @@ -462,6 +509,9 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringList();", list)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadStringArray()", list)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readStringList();", list)).append(LS); break; @@ -496,6 +546,8 @@ public class CutDownListSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("const {} = buffer.readPacketList({});", list, protocolId)).append(LS); break; + case Go: + case Protobuf: default: flag = false; } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java index c70f8e6b..e932b336 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java @@ -66,6 +66,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntIntMap({});", objectStr)).append(LS); return true; + case Go: + builder.append(StringUtils.format("buffer.WriteIntIntMap({})", objectStr)).append(LS); + return true; case Cpp: case JavaScript: case TypeScript: @@ -86,6 +89,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntLongMap({});", objectStr)).append(LS); return true; + case Go: + builder.append(StringUtils.format("buffer.WriteIntLongMap({})", objectStr)).append(LS); + return true; case Cpp: case JavaScript: case TypeScript: @@ -97,13 +103,6 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeIntStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; - case Cpp: - builder.append(StringUtils.format("buffer.writeIntStringMap({});", objectStr)).append(LS); - return true; - case JavaScript: - case TypeScript: - builder.append(StringUtils.format("buffer.writeIntStringMap({});", objectStr)).append(LS); - return true; case GdScript: builder.append(StringUtils.format("buffer.writeIntStringMap({})", objectStr)).append(LS); return true; @@ -113,6 +112,14 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntStringMap({});", objectStr)).append(LS); return true; + case Go: + builder.append(StringUtils.format("buffer.WriteIntStringMap({})", objectStr)).append(LS); + return true; + case Cpp: + case JavaScript: + case TypeScript: + builder.append(StringUtils.format("buffer.writeIntStringMap({});", objectStr)).append(LS); + return true; } } else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) { switch (language) { @@ -133,6 +140,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case Go: + case Protobuf: + default: } } } else if (keySerializer == LongSerializer.INSTANCE) { @@ -150,6 +160,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongIntMap({});", objectStr)).append(LS); return true; + case Go: + builder.append(StringUtils.format("buffer.WriteLongIntMap({})", objectStr)).append(LS); + return true; case Cpp: case JavaScript: case TypeScript: @@ -170,6 +183,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongLongMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case Go: + builder.append(StringUtils.format("buffer.WriteLongLongMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; case Cpp: case JavaScript: case TypeScript: @@ -190,6 +206,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongStringMap({});", objectStr)).append(LS); return true; + case Go: + builder.append(StringUtils.format("buffer.WriteLongStringMap({})", objectStr)).append(LS); + return true; case Cpp: case JavaScript: case TypeScript: @@ -215,6 +234,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case Go: + case Protobuf: + default: } } } else if (keySerializer == StringSerializer.INSTANCE) { @@ -232,6 +254,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringIntMap({});", objectStr)).append(LS); return true; + case Go: + builder.append(StringUtils.format("buffer.WriteStringIntMap({})", objectStr)).append(LS); + return true; case Cpp: case JavaScript: case TypeScript: @@ -252,6 +277,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringLongMap({});", objectStr)).append(LS); return true; + case Go: + builder.append(StringUtils.format("buffer.WriteStringLongMap({})", objectStr)).append(LS); + return true; case Cpp: case JavaScript: case TypeScript: @@ -272,6 +300,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringStringMap({});", objectStr)).append(LS); return true; + case Go: + builder.append(StringUtils.format("buffer.WriteStringStringMap({})", objectStr)).append(LS); + return true; case Cpp: case JavaScript: case TypeScript: @@ -297,6 +328,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case Go: + case Protobuf: + default: } } } @@ -330,6 +364,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntIntMap();", map)).append(LS); return map; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadIntIntMap()", map)).append(LS); + return map; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readIntIntMap();", map)).append(LS); return map; @@ -352,6 +389,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntLongMap();", map)).append(LS); return map; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadIntLongMap()", map)).append(LS); + return map; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readIntLongMap();", map)).append(LS); return map; @@ -374,6 +414,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntStringMap();", map)).append(LS); return map; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadIntStringMap()", map)).append(LS); + return map; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readIntStringMap();", map)).append(LS); return map; @@ -404,6 +447,8 @@ public class CutDownMapSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("const {} = buffer.readIntPacketMap({});", map, protocolId)).append(LS); return map; + case Go: + case Protobuf: } } } else if (keySerializer == LongSerializer.INSTANCE) { @@ -421,6 +466,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongIntMap();", map)).append(LS); return map; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadLongIntMap()", map)).append(LS); + return map; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readLongIntMap();", map)).append(LS); return map; @@ -443,6 +491,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongLongMap();", map)).append(LS); return map; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadLongLongMap()", map)).append(LS); + return map; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readLongLongMap();", map)).append(LS); return map; @@ -465,6 +516,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongStringMap();", map)).append(LS); return map; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadLongStringMap()", map)).append(LS); + return map; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readLongStringMap();", map)).append(LS); return map; @@ -495,6 +549,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("const {} = buffer.readLongPacketMap({});", map, protocolId)).append(LS); return map; + case Go: + case Protobuf: + default: } } } else if (keySerializer == StringSerializer.INSTANCE) { @@ -512,6 +569,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringIntMap();", map)).append(LS); return map; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadStringIntMap()", map)).append(LS); + return map; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readStringIntMap();", map)).append(LS); return map; @@ -534,6 +594,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringLongMap();", map)).append(LS); return map; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadStringLongMap()", map)).append(LS); + return map; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readStringLongMap();", map)).append(LS); return map; @@ -556,6 +619,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringStringMap();", map)).append(LS); return map; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadStringStringMap()", map)).append(LS); + return map; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readStringStringMap();", map)).append(LS); return map; @@ -586,6 +652,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("const {} = buffer.readStringPacketMap({});", map, protocolId)).append(LS); return map; + case Go: + case Protobuf: + default: } } } 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 4cc3939c..c0b7fb87 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java @@ -60,6 +60,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanSet({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteBooleanArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -83,6 +86,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteByteSet({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteByteArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -106,6 +112,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteShortSet({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteShortArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -129,6 +138,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntSet({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteIntArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -152,6 +164,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongSet({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteLongArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -175,6 +190,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteFloatSet({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteFloatArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -198,6 +216,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleSet({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteDoubleArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -221,6 +242,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringSet({});", objectStr)).append(LS); break; + case Go: + builder.append(StringUtils.format("buffer.WriteStringArray({})", objectStr)).append(LS); + break; case Cpp: case JavaScript: case TypeScript: @@ -252,6 +276,8 @@ public class CutDownSetSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("buffer.writePacketSet({}, {});", objectStr, protocolId)).append(LS); break; + case Go: + case Protobuf: default: flag = false; } @@ -288,6 +314,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Cpp: builder.append(StringUtils.format("auto {} = buffer.readBooleanSet();", set)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray()", set)).append(LS); + break; case JavaScript: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readBooleanSet();", set)).append(LS); @@ -310,6 +339,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteSet();", set)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadByteArray()", set)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readByteSet();", set)).append(LS); break; @@ -335,6 +367,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortSet();", set)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadShortArray()", set)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readShortSet();", set)).append(LS); break; @@ -360,6 +395,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntSet();", set)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadIntArray()", set)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readIntSet();", set)).append(LS); break; @@ -385,6 +423,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongSet();", set)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadLongArray()", set)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readLongSet();", set)).append(LS); break; @@ -410,6 +451,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatSet();", set)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadFloatArray()", set)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readFloatSet();", set)).append(LS); break; @@ -435,6 +479,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleSet();", set)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray()", set)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readDoubleSet();", set)).append(LS); break; @@ -460,6 +507,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringSet();", set)).append(LS); break; + case Go: + builder.append(StringUtils.format("var {} = buffer.ReadStringArray()", set)).append(LS); + break; case Cpp: builder.append(StringUtils.format("auto {} = buffer.readStringSet();", set)).append(LS); break; @@ -494,6 +544,8 @@ public class CutDownSetSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("const {} = buffer.readPacketSet({});", set, protocolId)).append(LS); break; + case Go: + case Protobuf: default: flag = false; } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java index ab9abeb3..a2e73449 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java @@ -100,7 +100,6 @@ public abstract class GenerateCppUtils { var protocolManagerTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("cpp/ProtocolManagerTemplate.h"))); - // 生成ProtocolManager.gd文件 var headerBuilder = new StringBuilder(); protocolList.stream() .filter(it -> Objects.nonNull(it)) diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java new file mode 100644 index 00000000..3a1b95e2 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java @@ -0,0 +1,225 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateOperation; +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.generate.GenerateProtocolNote; +import com.zfoo.protocol.registration.IProtocolRegistration; +import com.zfoo.protocol.registration.ProtocolRegistration; +import com.zfoo.protocol.registration.anno.Compatible; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.reflect.*; +import com.zfoo.protocol.util.ClassUtils; +import com.zfoo.protocol.util.FileUtils; +import com.zfoo.protocol.util.IOUtils; +import com.zfoo.protocol.util.StringUtils; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import static com.zfoo.protocol.util.FileUtils.LS; +import static com.zfoo.protocol.util.StringUtils.TAB; + +/** + * @author godotg + * @version 3.0 + */ +public abstract class GenerateGoUtils { + + private static String protocolOutputRootPath = "goProtocol/"; + + private static Map goSerializerMap; + + public static IGoSerializer goSerializer(ISerializer serializer) { + return goSerializerMap.get(serializer); + } + + public static void init(GenerateOperation generateOperation) { + protocolOutputRootPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath); + + FileUtils.deleteFile(new File(protocolOutputRootPath)); + FileUtils.createDirectory(protocolOutputRootPath); + + goSerializerMap = new HashMap<>(); + goSerializerMap.put(BooleanSerializer.INSTANCE, new GoBooleanSerializer()); + goSerializerMap.put(ByteSerializer.INSTANCE, new GoByteSerializer()); + goSerializerMap.put(ShortSerializer.INSTANCE, new GoShortSerializer()); + goSerializerMap.put(IntSerializer.INSTANCE, new GoIntSerializer()); + goSerializerMap.put(LongSerializer.INSTANCE, new GoLongSerializer()); + goSerializerMap.put(FloatSerializer.INSTANCE, new GoFloatSerializer()); + goSerializerMap.put(DoubleSerializer.INSTANCE, new GoDoubleSerializer()); + goSerializerMap.put(CharSerializer.INSTANCE, new GoCharSerializer()); + goSerializerMap.put(StringSerializer.INSTANCE, new GoStringSerializer()); + goSerializerMap.put(ArraySerializer.INSTANCE, new GoArraySerializer()); + goSerializerMap.put(ListSerializer.INSTANCE, new GoListSerializer()); + goSerializerMap.put(SetSerializer.INSTANCE, new GoSetSerializer()); + goSerializerMap.put(MapSerializer.INSTANCE, new GoMapSerializer()); + goSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new GoObjectProtocolSerializer()); + } + + public static void clear() { + goSerializerMap = null; + protocolOutputRootPath = null; + } + + /** + * 生成协议依赖的工具类 + */ + public static void createProtocolManager(List protocolList) throws IOException { + var list = List.of("go/ByteBuffer.go"); + + for (var fileName : list) { + var fileInputStream = ClassUtils.getFileFromClassPath(fileName); + var createFile = new File(StringUtils.format("{}/{}", protocolOutputRootPath, StringUtils.substringAfterFirst(fileName, "go/"))); + FileUtils.writeInputStreamToFile(createFile, fileInputStream); + } + + var initProtocolBuilder = new StringBuilder(); + protocolList.stream() + .filter(it -> Objects.nonNull(it)) + .forEach(it -> initProtocolBuilder.append(TAB).append(StringUtils.format("Protocols[{}] = new({})", it.protocolId(), it.protocolConstructor().getDeclaringClass().getSimpleName())).append(LS)); + + var protocolManagerTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("go/ProtocolManagerTemplate.go"))); + protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, initProtocolBuilder.toString().trim()); + FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.go")), protocolManagerTemplate, true); + } + + public static void createGoProtocolFile(ProtocolRegistration registration) throws IOException { + GenerateProtocolFile.index.set(0); + + var protocolId = registration.protocolId(); + var registrationConstructor = registration.getConstructor(); + var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName(); + + var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("go/ProtocolTemplate.go"))); + + var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Go); + var fieldDefinition = fieldDefinition(registration); + var writeObject = writeObject(registration); + var readObject = readObject(registration); + protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, fieldDefinition.trim() + , protocolClazzName, protocolId, protocolClazzName, protocolClazzName + , writeObject.trim() + , protocolClazzName, protocolClazzName, readObject.trim()); + + var protocolOutputPath = StringUtils.format("{}/{}.go" + , protocolOutputRootPath + , protocolClazzName); + FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate, true); + } + + private static String fieldDefinition(ProtocolRegistration registration) { + var protocolId = registration.protocolId(); + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + + var csBuilder = new StringBuilder(); + // 协议的属性生成 + for (int i = 0; i < fields.length; i++) { + Field field = fields[i]; + IFieldRegistration fieldRegistration = fieldRegistrations[i]; + var fieldName = field.getName(); + var fieldType = goSerializer(fieldRegistration.serializer()).fieldType(field, fieldRegistration); + + var propertyFullName = StringUtils.format("{} {}", fieldName, fieldType); + // 生成注释 + var filedNote = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.Go); + if (StringUtils.isNotBlank(filedNote)) { + csBuilder.append(TAB).append(filedNote).append(LS); + } + csBuilder.append(TAB).append(propertyFullName).append(LS); + } + return csBuilder.toString(); + } + + private static String writeObject(ProtocolRegistration registration) { + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + var csBuilder = new StringBuilder(); + for (var i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + goSerializer(fieldRegistration.serializer()).writeObject(csBuilder, "message." + field.getName(), 1, field, fieldRegistration); + } + return csBuilder.toString(); + } + + + private static String readObject(ProtocolRegistration registration) { + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + var csBuilder = new StringBuilder(); + for (var i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + if (field.isAnnotationPresent(Compatible.class)) { + csBuilder.append(TAB).append("if !buffer.IsReadable()").append(LS); + csBuilder.append(TAB).append("{").append(LS); + csBuilder.append(TAB + TAB).append("return packet").append(LS); + csBuilder.append(TAB).append("}").append(LS); + } + var readObject = goSerializer(fieldRegistration.serializer()).readObject(csBuilder, 1, field, fieldRegistration); + csBuilder.append(TAB).append(StringUtils.format("packet.{} = {}", field.getName(), readObject)).append(LS); + } + return csBuilder.toString(); + } + + public static String toGoClassName(String typeName) { + typeName = typeName.replaceAll("java.util.|java.lang.", StringUtils.EMPTY); + typeName = typeName.replaceAll("com\\.[a-zA-Z0-9_.]*\\.", StringUtils.EMPTY); + + // CSharp不适用基础类型的泛型,会影响性能 + switch (typeName) { + case "boolean": + case "Boolean": + typeName = "bool"; + return typeName; + case "byte": + case "Byte": + typeName = "int8"; + return typeName; + case "short": + case "Short": + typeName = "int16"; + return typeName; + case "int": + case "Integer": + typeName = "int"; + return typeName; + case "long": + case "Long": + typeName = "int64"; + return typeName; + case "Float": + typeName = "float32"; + return typeName; + case "Double": + typeName = "float64"; + return typeName; + case "Character": + typeName = "string"; + return typeName; + default: + } + + return typeName; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoArraySerializer.java new file mode 100644 index 00000000..55b7867d --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoArraySerializer.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Pair; +import com.zfoo.protocol.registration.field.ArrayField; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.registration.field.MapField; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownArraySerializer; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoArraySerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + var arrayField = (ArrayField) fieldRegistration; + var registration = arrayField.getArrayElementRegistration(); + var type = GenerateGoUtils.goSerializer(registration.serializer()).fieldType(field, registration); + return StringUtils.format("[]{}", type); + } + + @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.Go)) { + return; + } + + ArrayField arrayField = (ArrayField) fieldRegistration; + + builder.append(StringUtils.format("if ({} == nil) || (len({}) == 0) {", objectStr, objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("buffer.WriteInt(0)").append(LS); + GenerateProtocolFile.addTab(builder, deep); + + builder.append("} else {").append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("buffer.WriteInt(len({}))", objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + String length = "length" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("var {} = len({})", length, objectStr)).append(LS); + + String i = "i" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} := 0; {} < {}; {}++ {", i, i, length, i)).append(LS); + + GenerateProtocolFile.addTab(builder, deep + 2); + String element = "element" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("var {} = {}[{}]", element, objectStr, i)).append(LS); + + GenerateGoUtils.goSerializer(arrayField.getArrayElementRegistration().serializer()) + .writeObject(builder, element, deep + 2, field, arrayField.getArrayElementRegistration()); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("}").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + } + + @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.Go); + if (cutDown != null) { + return cutDown; + } + + + var arrayField = (ArrayField) fieldRegistration; + var result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + var typeName = fieldType(field, fieldRegistration); + + var i = "index" + GenerateProtocolFile.index.getAndIncrement(); + var size = "size" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("var {} = buffer.ReadInt()", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = make({}, {})", result, typeName, size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("if {} > 0 {", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} := 0; {} < {}; {}++ {", i, i, size, i)).append(LS); + var readObject = GenerateGoUtils.goSerializer(arrayField.getArrayElementRegistration().serializer()) + .readObject(builder, deep + 2, field, arrayField.getArrayElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 2); + builder.append(StringUtils.format("{}[{}] = {}", result, i, readObject)); + builder.append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("}").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + + + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoBooleanSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoBooleanSerializer.java new file mode 100644 index 00000000..cac53fbe --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoBooleanSerializer.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Pair; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoBooleanSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + return "bool"; + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.WriteBool({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.ReadBool()", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoByteSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoByteSerializer.java new file mode 100644 index 00000000..e2f417ec --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoByteSerializer.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Pair; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoByteSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + return "int8"; + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.WriteByte({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.ReadByte()", result)).append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoCharSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoCharSerializer.java new file mode 100644 index 00000000..9016598e --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoCharSerializer.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.exception.RunException; +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Pair; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoCharSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + return "string"; + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.WriteChar({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.ReadChar()", result)).append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoDoubleSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoDoubleSerializer.java new file mode 100644 index 00000000..f508f154 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoDoubleSerializer.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoDoubleSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + return "float64"; + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.WriteDouble({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.ReadDouble()", result)).append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoFloatSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoFloatSerializer.java new file mode 100644 index 00000000..53f63d36 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoFloatSerializer.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoFloatSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + return "float32"; + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.WriteFloat({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.ReadFloat()", result)).append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoIntSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoIntSerializer.java new file mode 100644 index 00000000..6c7aa071 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoIntSerializer.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoIntSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + return "int"; + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.WriteInt({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.ReadInt()", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoListSerializer.java new file mode 100644 index 00000000..bd34adc0 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoListSerializer.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +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; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoListSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + var listField = (ListField) fieldRegistration; + var registration = listField.getListElementRegistration(); + var type = GenerateGoUtils.goSerializer(registration.serializer()).fieldType(field, registration); + return StringUtils.format("[]{}", type); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + if (CutDownListSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Go)) { + return; + } + + ListField listField = (ListField) fieldRegistration; + builder.append(StringUtils.format("if {} == nil {", objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("buffer.WriteInt(0)").append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append("} else {").append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("buffer.WriteInt(len({}))", objectStr)).append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + String length = "length" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("var {} = len({})", length, objectStr)).append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + String i = "i" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("for {} := 0; {} < {}; {}++ {", i, i, length, i)).append(LS); + + GenerateProtocolFile.addTab(builder, deep + 2); + String element = "element" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("var {} = {}[{}]", element, objectStr, i)).append(LS); + + GenerateGoUtils.goSerializer(listField.getListElementRegistration().serializer()) + .writeObject(builder, element, deep + 2, field, listField.getListElementRegistration()); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("}").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + } + + @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.Go); + if (cutDown != null) { + return cutDown; + } + + var listField = (ListField) fieldRegistration; + var result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + var typeName = fieldType(field, fieldRegistration); + + var i = "index" + GenerateProtocolFile.index.getAndIncrement(); + var size = "size" + GenerateProtocolFile.index.getAndIncrement(); + + builder.append(StringUtils.format("var {} = buffer.ReadInt()", size)).append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = make({}, {})", result, typeName, size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("if {} > 0 {", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} := 0; {} < {}; {}++ {", i, i, size, i)).append(LS); + var readObject = GenerateGoUtils.goSerializer(listField.getListElementRegistration().serializer()) + .readObject(builder, deep + 2, field, listField.getListElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 2); + builder.append(StringUtils.format("{}[{}] = {}", result, i, readObject)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("}").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoLongSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoLongSerializer.java new file mode 100644 index 00000000..b89ef904 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoLongSerializer.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoLongSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + return "int64"; + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.WriteLong({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.ReadLong()", result)).append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoMapSerializer.java new file mode 100644 index 00000000..ea30eb09 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoMapSerializer.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.registration.field.MapField; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownMapSerializer; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoMapSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + var mapField = (MapField) fieldRegistration; + var keyRegistration = mapField.getMapKeyRegistration(); + var valueRegistration = mapField.getMapValueRegistration(); + + var keyType = GenerateGoUtils.goSerializer(keyRegistration.serializer()).fieldType(field, keyRegistration); + var valueType = GenerateGoUtils.goSerializer(valueRegistration.serializer()).fieldType(field, valueRegistration); + + return StringUtils.format("map[{}]{}", keyType, valueType); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + if (CutDownMapSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Go)) { + return; + } + + MapField mapField = (MapField) fieldRegistration; + + builder.append(StringUtils.format("if ({} == nil) || (len({}) == 0) {", objectStr, objectStr)).append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("buffer.WriteInt(0)").append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append("} else {").append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("buffer.WriteInt(len({}))", objectStr)).append(LS); + + + String i = "i" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep + 1); + + String key = "keyElement" + GenerateProtocolFile.index.getAndIncrement(); + String value = "valueElement" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("for {}, {} := range {} {", key, value, objectStr)).append(LS); + + GenerateGoUtils.goSerializer(mapField.getMapKeyRegistration().serializer()) + .writeObject(builder, key, deep + 2, field, mapField.getMapKeyRegistration()); + GenerateGoUtils.goSerializer(mapField.getMapValueRegistration().serializer()) + .writeObject(builder, value, deep + 2, field, mapField.getMapValueRegistration()); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("}").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + } + + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + var cutDown = CutDownMapSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Go); + if (cutDown != null) { + return cutDown; + } + + MapField mapField = (MapField) fieldRegistration; + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + var typeName = fieldType(field, fieldRegistration); + + String size = "size" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("var {} = buffer.ReadInt()", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = make({})", result, typeName)).append(LS); + + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("if {} > 0 {", size)).append(LS); + + String i = "index" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} := 0; {} < {}; {}++ {", i, i, size, i)).append(LS); + + String keyObject = GenerateGoUtils.goSerializer(mapField.getMapKeyRegistration().serializer()) + .readObject(builder, deep + 2, field, mapField.getMapKeyRegistration()); + + + String valueObject = GenerateGoUtils.goSerializer(mapField.getMapValueRegistration().serializer()) + .readObject(builder, deep + 2, field, mapField.getMapValueRegistration()); + GenerateProtocolFile.addTab(builder, deep + 2); + + builder.append(StringUtils.format("{}[{}] = {}", result, keyObject, valueObject)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("}").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoObjectProtocolSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoObjectProtocolSerializer.java new file mode 100644 index 00000000..e3b856f3 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoObjectProtocolSerializer.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.registration.field.ObjectProtocolField; +import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + + +/** + * @author godotg + * @version 3.0 + */ +public class GoObjectProtocolSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; + var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()); + var type = StringUtils.format("{}", protocolSimpleName); + return type; + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.WritePacket(&{}, {})", objectStr, objectProtocolField.getProtocolId())) + .append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = *buffer.ReadPacket({}).(*{})", result, objectProtocolField.getProtocolId(), protocolSimpleName)) + .append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoSetSerializer.java new file mode 100644 index 00000000..d56a14da --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoSetSerializer.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.registration.field.SetField; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownSetSerializer; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoSetSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + var setField = (SetField) fieldRegistration; + var registration = setField.getSetElementRegistration(); + var type = GenerateGoUtils.goSerializer(registration.serializer()).fieldType(field, registration); + return StringUtils.format("[]{}", type); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + if (CutDownSetSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Go)) { + return; + } + + SetField setField = (SetField) fieldRegistration; + + builder.append(StringUtils.format("if {} == nil {", objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("buffer.WriteInt(0)").append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append("} else {").append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("buffer.WriteInt(len({}))", objectStr)).append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + String length = "length" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("var {} = len({})", length, objectStr)).append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + String i = "i" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("for {} := 0; {} < {}; {}++ {", i, i, length, i)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 2); + String element = "element" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("var {} = {}[{}]", element, objectStr, i)).append(LS); + + GenerateGoUtils.goSerializer(setField.getSetElementRegistration().serializer()) + .writeObject(builder, element, deep + 2, field, setField.getSetElementRegistration()); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("}").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + var cutDown = CutDownSetSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Go); + if (cutDown != null) { + return cutDown; + } + + SetField setField = (SetField) fieldRegistration; + var result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + var typeName = fieldType(field, fieldRegistration); + + var i = "index" + GenerateProtocolFile.index.getAndIncrement(); + var size = "size" + GenerateProtocolFile.index.getAndIncrement(); + + builder.append(StringUtils.format("var {} = buffer.ReadInt()", size)).append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = make({}, {})", result, typeName, size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("if {} > 0 {", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for {} := 0; {} < {}; {}++ {", i, i, size, i)).append(LS); + var readObject = GenerateGoUtils.goSerializer(setField.getSetElementRegistration().serializer()) + .readObject(builder, deep + 2, field, setField.getSetElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 2); + builder.append(StringUtils.format("{}[{}] = {}", result, i, readObject)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("}").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoShortSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoShortSerializer.java new file mode 100644 index 00000000..705a1d5d --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoShortSerializer.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoShortSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + return "int16"; + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.WriteShort({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.ReadShort()", result)).append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoStringSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoStringSerializer.java new file mode 100644 index 00000000..865b487f --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoStringSerializer.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + * @version 3.0 + */ +public class GoStringSerializer implements IGoSerializer { + + @Override + public String fieldType(Field field, IFieldRegistration fieldRegistration) { + return "string"; + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.WriteString({})", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("var {} = buffer.ReadString()", result)).append(LS); + return result; + } + + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/IGoSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/IGoSerializer.java new file mode 100644 index 00000000..e5125cb7 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/IGoSerializer.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.go; + +import com.zfoo.protocol.registration.field.IFieldRegistration; + +import java.lang.reflect.Field; + +/** + * @author godotg + * @version 3.0 + */ +public interface IGoSerializer { + + /** + * 获取属性的类型和名称 + */ + String fieldType(Field field, IFieldRegistration fieldRegistration); + + void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration); + + String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration); + +} diff --git a/protocol/src/main/resources/go/ByteBuffer.go b/protocol/src/main/resources/go/ByteBuffer.go index 272d4fcc..64258db9 100644 --- a/protocol/src/main/resources/go/ByteBuffer.go +++ b/protocol/src/main/resources/go/ByteBuffer.go @@ -1,154 +1,166 @@ -// TODO: go无法支持嵌套的集合类型,所以现在无法支持这套协议 -// Golang字节保存在内存的低地址中默认的使用的是大端模式,和Java一样 -// 右移操作>>是带符号右移 -package buffer +package protocol import ( + "bytes" + "encoding/binary" + "fmt" "math" - "strconv" - "strings" ) const initSize int = 128 -const masSize int = 655537 - -const maxInt int = 2147483647 -const minInt int = -2147483648 +const maxSize int = 655537 var initArray []byte = make([]byte, initSize, initSize) type ByteBuffer struct { - buffer []byte - writeOffset int - readOffset int + buffer []byte + writeIndex int + readIndex int } // -------------------------------------------------get/set------------------------------------------------- func (byteBuffer *ByteBuffer) WriteOffset() int { - return byteBuffer.writeOffset + return byteBuffer.writeIndex } func (byteBuffer *ByteBuffer) SetWriteOffset(writeIndex int) { if writeIndex > len(byteBuffer.buffer) { - var builder strings.Builder - builder.WriteString("writeIndex[") - builder.WriteString(strconv.Itoa(writeIndex)) - builder.WriteString("] out of bounds exception: readerIndex: ") - builder.WriteString(strconv.Itoa(byteBuffer.readOffset)) - builder.WriteString(", writerIndex: ") - builder.WriteString(strconv.Itoa(byteBuffer.writeOffset)) - builder.WriteString("(expected: 0 <= readerIndex <= writerIndex <= capacity:") - builder.WriteString(strconv.Itoa(len(byteBuffer.buffer))) - builder.WriteString(")") - panic(builder.String()) + var error = fmt.Sprintf("writeIndex:[{%d}] out of bounds exception: readerIndex:[{%d}] , writerIndex:[{%d}] (expected: 0 <= readerIndex <= writerIndex <= capacity:[{%d}])", writeIndex, byteBuffer.readIndex, byteBuffer.writeIndex, len(byteBuffer.buffer)) + panic(error) } - byteBuffer.writeOffset = writeIndex + byteBuffer.writeIndex = writeIndex } func (byteBuffer *ByteBuffer) SetReadOffset(readIndex int) { - if readIndex > byteBuffer.writeOffset { - var builder strings.Builder - builder.WriteString("readIndex[") - builder.WriteString(strconv.Itoa(readIndex)) - builder.WriteString("] out of bounds exception: readerIndex: ") - builder.WriteString(strconv.Itoa(byteBuffer.readOffset)) - builder.WriteString(", writerIndex: ") - builder.WriteString(strconv.Itoa(byteBuffer.writeOffset)) - builder.WriteString("(expected: 0 <= readerIndex <= writerIndex <= capacity:") - builder.WriteString(strconv.Itoa(len(byteBuffer.buffer))) - builder.WriteString(")") - panic(builder.String()) + if readIndex > byteBuffer.writeIndex { + var error = fmt.Sprintf("readIndex:[{%d}] out of bounds exception: readerIndex:[{%d}] , writerIndex:[{%d}] (expected: 0 <= readerIndex <= writerIndex <= capacity:[{%d}])", readIndex, byteBuffer.readIndex, byteBuffer.writeIndex, len(byteBuffer.buffer)) + panic(error) } - byteBuffer.readOffset = readIndex + byteBuffer.readIndex = readIndex } func (byteBuffer *ByteBuffer) ToBytes() []byte { - return byteBuffer.buffer[0:byteBuffer.writeOffset] + return byteBuffer.buffer[0:byteBuffer.writeIndex] } func (byteBuffer *ByteBuffer) ToString() string { - var builder strings.Builder - builder.WriteString("writeOffset:") - builder.WriteString(strconv.Itoa(byteBuffer.writeOffset)) - builder.WriteString(", readOffset:") - builder.WriteString(strconv.Itoa(byteBuffer.readOffset)) - builder.WriteString(", len:") - builder.WriteString(strconv.Itoa(len(byteBuffer.buffer))) - builder.WriteString(", cap:") - builder.WriteString(strconv.Itoa(cap(byteBuffer.buffer))) - return builder.String() + return fmt.Sprintf("writeIndex:[{%d}], readIndex:[{%d}], len:[{%d}], cap:[{%d}]", byteBuffer.writeIndex, byteBuffer.readIndex, len(byteBuffer.buffer), cap(byteBuffer.buffer)) } -// -------------------------------------------------write/read------------------------------------------------- func (byteBuffer *ByteBuffer) GetCapacity() int { - return len(byteBuffer.buffer) - byteBuffer.writeOffset + return len(byteBuffer.buffer) - byteBuffer.writeIndex } func (byteBuffer *ByteBuffer) EnsureCapacity(capacity int) { - for - { - if byteBuffer.GetCapacity()-capacity > 0 { + for { + if byteBuffer.GetCapacity() > capacity { break } byteBuffer.buffer = append(byteBuffer.buffer, initArray...) - if len(byteBuffer.buffer) > masSize { + if len(byteBuffer.buffer) > maxSize { panic("Bytebuf max size is [655537], out of memory error") } } } +func (byteBuffer *ByteBuffer) Clear() { + byteBuffer.writeIndex = 0 + byteBuffer.readIndex = 0 +} + +func (byteBuffer *ByteBuffer) IsReadable() bool { + return byteBuffer.writeIndex > byteBuffer.readIndex +} + +// -------------------------------------------------write/read------------------------------------------------- + +// 整形转换成字节 +func IntToBytes(n int) []byte { + var x = int32(n) + bytesBuffer := bytes.NewBuffer([]byte{}) + binary.Write(bytesBuffer, binary.BigEndian, x) + return bytesBuffer.Bytes() +} + +// 字节转换成整形 +func BytesToInt(b []byte) int { + bytesBuffer := bytes.NewBuffer(b) + var x int32 + binary.Read(bytesBuffer, binary.BigEndian, &x) + return int(x) +} + func (byteBuffer *ByteBuffer) WriteBool(value bool) { byteBuffer.EnsureCapacity(1) if value { - byteBuffer.buffer[byteBuffer.writeOffset] = 1 + byteBuffer.buffer[byteBuffer.writeIndex] = 1 } else { - byteBuffer.buffer[byteBuffer.writeOffset] = 0 + byteBuffer.buffer[byteBuffer.writeIndex] = 0 } - byteBuffer.writeOffset++ + byteBuffer.writeIndex++ } func (byteBuffer *ByteBuffer) ReadBool() bool { - var byteValue = byteBuffer.buffer[byteBuffer.readOffset] - byteBuffer.readOffset++ + var byteValue = byteBuffer.buffer[byteBuffer.readIndex] + byteBuffer.readIndex++ return byteValue == 1 } +func (byteBuffer *ByteBuffer) WriteByte(value int8) { + byteBuffer.EnsureCapacity(1) + byteBuffer.buffer[byteBuffer.writeIndex] = byte(value) + byteBuffer.writeIndex++ +} + +func (byteBuffer *ByteBuffer) ReadByte() int8 { + var byteValue = byteBuffer.buffer[byteBuffer.readIndex] + byteBuffer.readIndex++ + return int8(byteValue) +} + func (byteBuffer *ByteBuffer) WriteUByte(value byte) { byteBuffer.EnsureCapacity(1) - byteBuffer.buffer[byteBuffer.writeOffset] = value - byteBuffer.writeOffset++ + byteBuffer.buffer[byteBuffer.writeIndex] = value + byteBuffer.writeIndex++ } func (byteBuffer *ByteBuffer) ReadUByte() byte { - var byteValue = byteBuffer.buffer[byteBuffer.readOffset] - byteBuffer.readOffset++ + var byteValue = byteBuffer.buffer[byteBuffer.readIndex] + byteBuffer.readIndex++ return byteValue } func (byteBuffer *ByteBuffer) WriteUBytes(bytes []byte) { var length = len(bytes) byteBuffer.EnsureCapacity(length) - copy(byteBuffer.buffer[byteBuffer.writeOffset:], bytes) - byteBuffer.writeOffset += length + copy(byteBuffer.buffer[byteBuffer.writeIndex:], bytes) + byteBuffer.writeIndex += length } func (byteBuffer *ByteBuffer) ReadUBytes(length int) []byte { - var readOffset = byteBuffer.readOffset - var endOffset = byteBuffer.readOffset + length + var readOffset = byteBuffer.readIndex + var endOffset = byteBuffer.readIndex + length var bytes = byteBuffer.buffer[readOffset:endOffset] + byteBuffer.readIndex += length return bytes } func (byteBuffer *ByteBuffer) WriteShort(value int16) { byteBuffer.EnsureCapacity(2) - byteBuffer.WriteUByte(byte(value >> 8)) - byteBuffer.WriteUByte(byte(value)) + var bytesBuffer = bytes.NewBuffer([]byte{}) + binary.Write(bytesBuffer, binary.BigEndian, value) + var byteArray = bytesBuffer.Bytes() + byteBuffer.WriteUBytes(byteArray) } func (byteBuffer *ByteBuffer) ReadShort() int16 { - return (int16(byteBuffer.ReadUByte()) << 8) | int16(byteBuffer.ReadUByte()) + var byteArray = byteBuffer.ReadUBytes(2) + bytesBuffer := bytes.NewBuffer(byteArray) + var value int16 + binary.Read(bytesBuffer, binary.BigEndian, &value) + return value } func (byteBuffer *ByteBuffer) WriteRawInt32(intValue int32) { @@ -163,8 +175,8 @@ func (byteBuffer *ByteBuffer) ReadRawInt32() int32 { } func (byteBuffer *ByteBuffer) WriteInt(intValue int) { - if intValue < minInt || intValue > maxInt { - panic("intValue must range between minInt:-2147483648 and maxInt:2147483647") + if intValue < math.MinInt32 || intValue > math.MaxInt32 { + panic("intValue must range between math.MinInt32:-2147483648 and math.MaxInt32:2147483647") } byteBuffer.WriteInt32(int32(intValue)) } @@ -175,7 +187,7 @@ func (byteBuffer *ByteBuffer) ReadInt() int { func (byteBuffer *ByteBuffer) WriteInt32(intValue int32) { var value uint32 = uint32(((intValue << 1) ^ (intValue >> 31))) - + // 右移操作>>是带符号右移 if value>>7 == 0 { byteBuffer.WriteUByte(byte(value)) return @@ -355,26 +367,34 @@ func (byteBuffer *ByteBuffer) ReadLong() int64 { func (byteBuffer *ByteBuffer) WriteFloat(value float32) { byteBuffer.EnsureCapacity(4) - var uintValue = math.Float32bits(value) - byteBuffer.WriteRawInt32(int32(uintValue)) + var bytesBuffer = bytes.NewBuffer([]byte{}) + binary.Write(bytesBuffer, binary.BigEndian, value) + var byteArray = bytesBuffer.Bytes() + byteBuffer.WriteUBytes(byteArray) } func (byteBuffer *ByteBuffer) ReadFloat() float32 { - var int32Value = byteBuffer.ReadRawInt32() - return math.Float32frombits(uint32(int32Value)) + var byteArray = byteBuffer.ReadUBytes(4) + bytesBuffer := bytes.NewBuffer(byteArray) + var value float32 + binary.Read(bytesBuffer, binary.BigEndian, &value) + return value } func (byteBuffer *ByteBuffer) WriteDouble(value float64) { - byteBuffer.EnsureCapacity(4) - var uintValue = math.Float64bits(value) - byteBuffer.WriteRawInt32(int32(uintValue >> 32)) - byteBuffer.WriteRawInt32(int32(uintValue)) + byteBuffer.EnsureCapacity(8) + var bytesBuffer = bytes.NewBuffer([]byte{}) + binary.Write(bytesBuffer, binary.BigEndian, value) + var byteArray = bytesBuffer.Bytes() + byteBuffer.WriteUBytes(byteArray) } func (byteBuffer *ByteBuffer) ReadDouble() float64 { - var highInt32Value = uint64(byteBuffer.ReadRawInt32()) - var lowInt32Value = uint64(byteBuffer.ReadRawInt32()) - return math.Float64frombits(highInt32Value<<32 | lowInt32Value) + var byteArray = byteBuffer.ReadUBytes(8) + bytesBuffer := bytes.NewBuffer(byteArray) + var value float64 + binary.Read(bytesBuffer, binary.BigEndian, &value) + return value } func (byteBuffer *ByteBuffer) WriteString(value string) { @@ -404,4 +424,472 @@ func (byteBuffer *ByteBuffer) WriteChar(value string) { func (byteBuffer *ByteBuffer) ReadChar() string { return byteBuffer.ReadString() -} \ No newline at end of file +} + +func (byteBuffer *ByteBuffer) WritePacketFlag(packet any) bool { + var flag = packet == nil + byteBuffer.WriteBool(!flag) + return flag +} + +func (byteBuffer *ByteBuffer) WritePacket(packet any, protocolId int16) { + var protocolRegistration = GetProtocol(protocolId) + protocolRegistration.write(byteBuffer, packet) +} + +func (byteBuffer *ByteBuffer) ReadPacket(protocolId int16) any { + var protocolRegistration = GetProtocol(protocolId) + return protocolRegistration.read(byteBuffer) +} + +// -------------------------------------------------IProtocolRegistration------------------------------------------------- +type IProtocolRegistration interface { + protocolId() int16 + + write(buffer *ByteBuffer, packet any) + + read(buffer *ByteBuffer) any +} + +// protocol map +var Protocols = make(map[int16]IProtocolRegistration) + +func GetProtocol(protocolId int16) IProtocolRegistration { + return Protocols[protocolId] +} + +func Write(buffer *ByteBuffer, packet any) { + var protocolId = packet.(IProtocolRegistration).protocolId() + buffer.WriteShort(protocolId) + var protocolRegistration = GetProtocol(protocolId) + protocolRegistration.write(buffer, packet) +} + +func Read(buffer *ByteBuffer) any { + var protocolId = buffer.ReadShort() + return GetProtocol(protocolId).read(buffer) +} + +// -------------------------------------------------CutDown------------------------------------------------- +func (byteBuffer *ByteBuffer) WriteBooleanArray(array []bool) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteBool(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadBooleanArray() []bool { + var size = byteBuffer.ReadInt() + var array = make([]bool, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadBool() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteByteArray(array []int8) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteByte(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadByteArray() []int8 { + var size = byteBuffer.ReadInt() + var array = make([]int8, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadByte() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteShortArray(array []int16) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteShort(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadShortArray() []int16 { + var size = byteBuffer.ReadInt() + var array = make([]int16, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadShort() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteIntArray(array []int) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteInt(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadIntArray() []int { + var size = byteBuffer.ReadInt() + var array = make([]int, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadInt() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteLongArray(array []int64) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteLong(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadLongArray() []int64 { + var size = byteBuffer.ReadInt() + var array = make([]int64, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadLong() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteFloatArray(array []float32) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteFloat(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadFloatArray() []float32 { + var size = byteBuffer.ReadInt() + var array = make([]float32, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadFloat() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteDoubleArray(array []float64) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteDouble(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadDoubleArray() []float64 { + var size = byteBuffer.ReadInt() + var array = make([]float64, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadDouble() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteCharArray(array []string) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteChar(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadCharArray() []string { + var size = byteBuffer.ReadInt() + var array = make([]string, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadChar() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteStringArray(array []string) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteString(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadStringArray() []string { + var size = byteBuffer.ReadInt() + var array = make([]string, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadString() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteIntIntMap(m map[int]int) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteInt(key) + byteBuffer.WriteInt(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadIntIntMap() map[int]int { + var size = byteBuffer.ReadInt() + var m = make(map[int]int, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadInt() + var value = byteBuffer.ReadInt() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteIntLongMap(m map[int]int64) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteInt(key) + byteBuffer.WriteLong(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadIntLongMap() map[int]int64 { + var size = byteBuffer.ReadInt() + var m = make(map[int]int64, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadInt() + var value = byteBuffer.ReadLong() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteIntStringMap(m map[int]string) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteInt(key) + byteBuffer.WriteString(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadIntStringMap() map[int]string { + var size = byteBuffer.ReadInt() + var m = make(map[int]string, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadInt() + var value = byteBuffer.ReadString() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteLongIntMap(m map[int64]int) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteLong(key) + byteBuffer.WriteInt(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadLongIntMap() map[int64]int { + var size = byteBuffer.ReadInt() + var m = make(map[int64]int, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadLong() + var value = byteBuffer.ReadInt() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteLongLongMap(m map[int64]int64) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteLong(key) + byteBuffer.WriteLong(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadLongLongMap() map[int64]int64 { + var size = byteBuffer.ReadInt() + var m = make(map[int64]int64, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadLong() + var value = byteBuffer.ReadLong() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteLongStringMap(m map[int64]string) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteLong(key) + byteBuffer.WriteString(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadLongStringMap() map[int64]string { + var size = byteBuffer.ReadInt() + var m = make(map[int64]string, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadLong() + var value = byteBuffer.ReadString() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteStringIntMap(m map[string]int) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteString(key) + byteBuffer.WriteInt(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadStringIntMap() map[string]int { + var size = byteBuffer.ReadInt() + var m = make(map[string]int, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadString() + var value = byteBuffer.ReadInt() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteStringLongMap(m map[string]int64) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteString(key) + byteBuffer.WriteLong(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadStringLongMap() map[string]int64 { + var size = byteBuffer.ReadInt() + var m = make(map[string]int64, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadString() + var value = byteBuffer.ReadLong() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteStringStringMap(m map[string]string) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteString(key) + byteBuffer.WriteString(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadStringStringMap() map[string]string { + var size = byteBuffer.ReadInt() + var m = make(map[string]string, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadString() + var value = byteBuffer.ReadString() + m[key] = value + } + } + return m +} diff --git a/protocol/src/main/resources/go/ProtocolManagerTemplate.go b/protocol/src/main/resources/go/ProtocolManagerTemplate.go new file mode 100644 index 00000000..f5f54697 --- /dev/null +++ b/protocol/src/main/resources/go/ProtocolManagerTemplate.go @@ -0,0 +1,5 @@ +package protocol + +func init() { + {} +} diff --git a/protocol/src/main/resources/go/ProtocolTemplate.go b/protocol/src/main/resources/go/ProtocolTemplate.go new file mode 100644 index 00000000..d59be272 --- /dev/null +++ b/protocol/src/main/resources/go/ProtocolTemplate.go @@ -0,0 +1,26 @@ +package protocol +{} +type {} struct { + {} +} + +func (protocol {}) protocolId() int16 { + return {} +} + +func (protocol {}) write(buffer *ByteBuffer, packet any) { + if buffer.WritePacketFlag(packet) { + return + } + var message = packet.(*{}) + {} +} + +func (protocol {}) read(buffer *ByteBuffer) any { + var packet = new({}) + if !buffer.ReadBool() { + return packet + } + {} + return packet +} diff --git a/protocol/src/test/go/buffer/ByteBuffer.go b/protocol/src/test/go/protocol/ByteBuffer.go similarity index 53% rename from protocol/src/test/go/buffer/ByteBuffer.go rename to protocol/src/test/go/protocol/ByteBuffer.go index d9e3517c..64258db9 100644 --- a/protocol/src/test/go/buffer/ByteBuffer.go +++ b/protocol/src/test/go/protocol/ByteBuffer.go @@ -65,6 +65,15 @@ func (byteBuffer *ByteBuffer) EnsureCapacity(capacity int) { } } +func (byteBuffer *ByteBuffer) Clear() { + byteBuffer.writeIndex = 0 + byteBuffer.readIndex = 0 +} + +func (byteBuffer *ByteBuffer) IsReadable() bool { + return byteBuffer.writeIndex > byteBuffer.readIndex +} + // -------------------------------------------------write/read------------------------------------------------- // 整形转换成字节 @@ -416,3 +425,471 @@ func (byteBuffer *ByteBuffer) WriteChar(value string) { func (byteBuffer *ByteBuffer) ReadChar() string { return byteBuffer.ReadString() } + +func (byteBuffer *ByteBuffer) WritePacketFlag(packet any) bool { + var flag = packet == nil + byteBuffer.WriteBool(!flag) + return flag +} + +func (byteBuffer *ByteBuffer) WritePacket(packet any, protocolId int16) { + var protocolRegistration = GetProtocol(protocolId) + protocolRegistration.write(byteBuffer, packet) +} + +func (byteBuffer *ByteBuffer) ReadPacket(protocolId int16) any { + var protocolRegistration = GetProtocol(protocolId) + return protocolRegistration.read(byteBuffer) +} + +// -------------------------------------------------IProtocolRegistration------------------------------------------------- +type IProtocolRegistration interface { + protocolId() int16 + + write(buffer *ByteBuffer, packet any) + + read(buffer *ByteBuffer) any +} + +// protocol map +var Protocols = make(map[int16]IProtocolRegistration) + +func GetProtocol(protocolId int16) IProtocolRegistration { + return Protocols[protocolId] +} + +func Write(buffer *ByteBuffer, packet any) { + var protocolId = packet.(IProtocolRegistration).protocolId() + buffer.WriteShort(protocolId) + var protocolRegistration = GetProtocol(protocolId) + protocolRegistration.write(buffer, packet) +} + +func Read(buffer *ByteBuffer) any { + var protocolId = buffer.ReadShort() + return GetProtocol(protocolId).read(buffer) +} + +// -------------------------------------------------CutDown------------------------------------------------- +func (byteBuffer *ByteBuffer) WriteBooleanArray(array []bool) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteBool(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadBooleanArray() []bool { + var size = byteBuffer.ReadInt() + var array = make([]bool, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadBool() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteByteArray(array []int8) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteByte(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadByteArray() []int8 { + var size = byteBuffer.ReadInt() + var array = make([]int8, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadByte() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteShortArray(array []int16) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteShort(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadShortArray() []int16 { + var size = byteBuffer.ReadInt() + var array = make([]int16, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadShort() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteIntArray(array []int) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteInt(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadIntArray() []int { + var size = byteBuffer.ReadInt() + var array = make([]int, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadInt() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteLongArray(array []int64) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteLong(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadLongArray() []int64 { + var size = byteBuffer.ReadInt() + var array = make([]int64, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadLong() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteFloatArray(array []float32) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteFloat(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadFloatArray() []float32 { + var size = byteBuffer.ReadInt() + var array = make([]float32, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadFloat() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteDoubleArray(array []float64) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteDouble(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadDoubleArray() []float64 { + var size = byteBuffer.ReadInt() + var array = make([]float64, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadDouble() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteCharArray(array []string) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteChar(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadCharArray() []string { + var size = byteBuffer.ReadInt() + var array = make([]string, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadChar() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteStringArray(array []string) { + if array == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(array)) + for _, value := range array { + byteBuffer.WriteString(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadStringArray() []string { + var size = byteBuffer.ReadInt() + var array = make([]string, size) + if size > 0 { + for i := 0; i < size; i++ { + array[i] = byteBuffer.ReadString() + } + } + return array +} + +func (byteBuffer *ByteBuffer) WriteIntIntMap(m map[int]int) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteInt(key) + byteBuffer.WriteInt(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadIntIntMap() map[int]int { + var size = byteBuffer.ReadInt() + var m = make(map[int]int, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadInt() + var value = byteBuffer.ReadInt() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteIntLongMap(m map[int]int64) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteInt(key) + byteBuffer.WriteLong(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadIntLongMap() map[int]int64 { + var size = byteBuffer.ReadInt() + var m = make(map[int]int64, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadInt() + var value = byteBuffer.ReadLong() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteIntStringMap(m map[int]string) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteInt(key) + byteBuffer.WriteString(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadIntStringMap() map[int]string { + var size = byteBuffer.ReadInt() + var m = make(map[int]string, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadInt() + var value = byteBuffer.ReadString() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteLongIntMap(m map[int64]int) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteLong(key) + byteBuffer.WriteInt(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadLongIntMap() map[int64]int { + var size = byteBuffer.ReadInt() + var m = make(map[int64]int, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadLong() + var value = byteBuffer.ReadInt() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteLongLongMap(m map[int64]int64) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteLong(key) + byteBuffer.WriteLong(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadLongLongMap() map[int64]int64 { + var size = byteBuffer.ReadInt() + var m = make(map[int64]int64, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadLong() + var value = byteBuffer.ReadLong() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteLongStringMap(m map[int64]string) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteLong(key) + byteBuffer.WriteString(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadLongStringMap() map[int64]string { + var size = byteBuffer.ReadInt() + var m = make(map[int64]string, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadLong() + var value = byteBuffer.ReadString() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteStringIntMap(m map[string]int) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteString(key) + byteBuffer.WriteInt(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadStringIntMap() map[string]int { + var size = byteBuffer.ReadInt() + var m = make(map[string]int, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadString() + var value = byteBuffer.ReadInt() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteStringLongMap(m map[string]int64) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteString(key) + byteBuffer.WriteLong(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadStringLongMap() map[string]int64 { + var size = byteBuffer.ReadInt() + var m = make(map[string]int64, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadString() + var value = byteBuffer.ReadLong() + m[key] = value + } + } + return m +} + +func (byteBuffer *ByteBuffer) WriteStringStringMap(m map[string]string) { + if m == nil { + byteBuffer.WriteInt(0) + } else { + byteBuffer.WriteInt(len(m)) + for key, value := range m { + byteBuffer.WriteString(key) + byteBuffer.WriteString(value) + } + } +} + +func (byteBuffer *ByteBuffer) ReadStringStringMap() map[string]string { + var size = byteBuffer.ReadInt() + var m = make(map[string]string, size) + if size > 0 { + for i := 0; i < size; i++ { + var key = byteBuffer.ReadString() + var value = byteBuffer.ReadString() + m[key] = value + } + } + return m +} diff --git a/protocol/src/test/go/protocol/NormalObject.go b/protocol/src/test/go/protocol/NormalObject.go new file mode 100644 index 00000000..6622d379 --- /dev/null +++ b/protocol/src/test/go/protocol/NormalObject.go @@ -0,0 +1,127 @@ +package protocol + +type NormalObject struct { + a int8 + aaa []int8 + b int16 + c int + d int64 + e float32 + f float64 + g bool + jj string + kk ObjectA + l []int + ll []int64 + lll []ObjectA + llll []string + m map[int]string + mm map[int]ObjectA + s []int + ssss []string +} + +func (protocol NormalObject) protocolId() int16 { + return 101 +} + +func (protocol NormalObject) write(buffer *ByteBuffer, packet any) { + if buffer.WritePacketFlag(packet) { + return + } + var message = packet.(*NormalObject) + buffer.WriteByte(message.a) + buffer.WriteByteArray(message.aaa) + buffer.WriteShort(message.b) + buffer.WriteInt(message.c) + buffer.WriteLong(message.d) + buffer.WriteFloat(message.e) + buffer.WriteDouble(message.f) + buffer.WriteBool(message.g) + buffer.WriteString(message.jj) + buffer.WritePacket(&message.kk, 102) + buffer.WriteIntArray(message.l) + buffer.WriteLongArray(message.ll) + if message.lll == nil { + buffer.WriteInt(0) + } else { + buffer.WriteInt(len(message.lll)) + var length0 = len(message.lll) + for i1 := 0; i1 < length0; i1++ { + var element2 = message.lll[i1] + buffer.WritePacket(&element2, 102) + } + } + buffer.WriteStringArray(message.llll) + buffer.WriteIntStringMap(message.m) + if (message.mm == nil) || (len(message.mm) == 0) { + buffer.WriteInt(0) + } else { + buffer.WriteInt(len(message.mm)) + for keyElement4, valueElement5 := range message.mm { + buffer.WriteInt(keyElement4) + buffer.WritePacket(&valueElement5, 102) + } + } + buffer.WriteIntArray(message.s) + buffer.WriteStringArray(message.ssss) +} + +func (protocol NormalObject) read(buffer *ByteBuffer) any { + var packet = new(NormalObject) + if !buffer.ReadBool() { + return packet + } + var result6 = buffer.ReadByte() + packet.a = result6 + var array7 = buffer.ReadByteArray() + packet.aaa = array7 + var result8 = buffer.ReadShort() + packet.b = result8 + var result9 = buffer.ReadInt() + packet.c = result9 + var result10 = buffer.ReadLong() + packet.d = result10 + var result11 = buffer.ReadFloat() + packet.e = result11 + var result12 = buffer.ReadDouble() + packet.f = result12 + var result13 = buffer.ReadBool() + packet.g = result13 + var result14 = buffer.ReadString() + packet.jj = result14 + var result15 = *buffer.ReadPacket(102).(*ObjectA) + packet.kk = result15 + var list16 = buffer.ReadIntArray() + packet.l = list16 + var list17 = buffer.ReadLongArray() + packet.ll = list17 + var size20 = buffer.ReadInt() + var result18 = make([]ObjectA, size20) + if size20 > 0 { + for index19 := 0; index19 < size20; index19++ { + var result21 = *buffer.ReadPacket(102).(*ObjectA) + result18[index19] = result21 + } + } + packet.lll = result18 + var list22 = buffer.ReadStringArray() + packet.llll = list22 + var map23 = buffer.ReadIntStringMap() + packet.m = map23 + var size25 = buffer.ReadInt() + var result24 = make(map[int]ObjectA) + if size25 > 0 { + for index26 := 0; index26 < size25; index26++ { + var result27 = buffer.ReadInt() + var result28 = *buffer.ReadPacket(102).(*ObjectA) + result24[result27] = result28 + } + } + packet.mm = result24 + var set29 = buffer.ReadIntArray() + packet.s = set29 + var set30 = buffer.ReadStringArray() + packet.ssss = set30 + return packet +} diff --git a/protocol/src/test/go/protocol/ObjectA.go b/protocol/src/test/go/protocol/ObjectA.go new file mode 100644 index 00000000..fc8b1f38 --- /dev/null +++ b/protocol/src/test/go/protocol/ObjectA.go @@ -0,0 +1,35 @@ +package protocol + +type ObjectA struct { + a int + m map[int]string + objectB ObjectB +} + +func (protocol ObjectA) protocolId() int16 { + return 102 +} + +func (protocol ObjectA) write(buffer *ByteBuffer, packet any) { + if buffer.WritePacketFlag(packet) { + return + } + var message = packet.(*ObjectA) + buffer.WriteInt(message.a) + buffer.WriteIntStringMap(message.m) + buffer.WritePacket(&message.objectB, 103) +} + +func (protocol ObjectA) read(buffer *ByteBuffer) any { + var packet = new(ObjectA) + if !buffer.ReadBool() { + return packet + } + var result0 = buffer.ReadInt() + packet.a = result0 + var map1 = buffer.ReadIntStringMap() + packet.m = map1 + var result2 = *buffer.ReadPacket(103).(*ObjectB) + packet.objectB = result2 + return packet +} diff --git a/protocol/src/test/go/protocol/ObjectB.go b/protocol/src/test/go/protocol/ObjectB.go new file mode 100644 index 00000000..a6be28a5 --- /dev/null +++ b/protocol/src/test/go/protocol/ObjectB.go @@ -0,0 +1,27 @@ +package protocol + +type ObjectB struct { + flag bool +} + +func (protocol ObjectB) protocolId() int16 { + return 103 +} + +func (protocol ObjectB) write(buffer *ByteBuffer, packet any) { + if buffer.WritePacketFlag(packet) { + return + } + var message = packet.(*ObjectB) + buffer.WriteBool(message.flag) +} + +func (protocol ObjectB) read(buffer *ByteBuffer) any { + var packet = new(ObjectB) + if !buffer.ReadBool() { + return packet + } + var result0 = buffer.ReadBool() + packet.flag = result0 + return packet +} diff --git a/protocol/src/test/go/protocol/ProtocolManager.go b/protocol/src/test/go/protocol/ProtocolManager.go new file mode 100644 index 00000000..11483317 --- /dev/null +++ b/protocol/src/test/go/protocol/ProtocolManager.go @@ -0,0 +1,8 @@ +package protocol + +func init() { + Protocols[101] = new(NormalObject) + Protocols[102] = new(ObjectA) + Protocols[103] = new(ObjectB) + Protocols[104] = new(SimpleObject) +} diff --git a/protocol/src/test/go/protocol/SimpleObject.go b/protocol/src/test/go/protocol/SimpleObject.go new file mode 100644 index 00000000..f6ef71bf --- /dev/null +++ b/protocol/src/test/go/protocol/SimpleObject.go @@ -0,0 +1,31 @@ +package protocol + +type SimpleObject struct { + c int + g bool +} + +func (protocol SimpleObject) protocolId() int16 { + return 104 +} + +func (protocol SimpleObject) write(buffer *ByteBuffer, packet any) { + if buffer.WritePacketFlag(packet) { + return + } + var message = packet.(*SimpleObject) + buffer.WriteInt(message.c) + buffer.WriteBool(message.g) +} + +func (protocol SimpleObject) read(buffer *ByteBuffer) any { + var packet = new(SimpleObject) + if !buffer.ReadBool() { + return packet + } + var result0 = buffer.ReadInt() + packet.c = result0 + var result1 = buffer.ReadBool() + packet.g = result1 + return packet +} diff --git a/protocol/src/test/go/buffer/buffer_test.go b/protocol/src/test/go/protocol/buffer_test.go similarity index 91% rename from protocol/src/test/go/buffer/buffer_test.go rename to protocol/src/test/go/protocol/buffer_test.go index ca07fcf9..c406c441 100644 --- a/protocol/src/test/go/buffer/buffer_test.go +++ b/protocol/src/test/go/protocol/buffer_test.go @@ -13,7 +13,9 @@ package protocol import ( + "fmt" "math" + "os" "testing" ) @@ -34,6 +36,7 @@ func TestByteBuffer(t *testing.T) { floatTest(buffer) doubleTest(buffer) stringTest(buffer) + packetTest() } func boolTest(buffer *ByteBuffer) { @@ -118,3 +121,16 @@ func stringTest(buffer *ByteBuffer) { buffer.WriteString(value) assert(buffer.ReadString() == value) } + +func packetTest() { + bytes, _ := os.ReadFile("../../resources/NormalObject.bytes") + var buffer = new(ByteBuffer) + buffer.WriteUBytes(bytes) + var packet = Read(buffer) + fmt.Println(packet) + + buffer.Clear() + Write(buffer, packet) + var newPacket = Read(buffer) + fmt.Println(newPacket) +} diff --git a/protocol/src/test/resources/NormalObject.bytes b/protocol/src/test/resources/NormalObject.bytes new file mode 100644 index 0000000000000000000000000000000000000000..95e125352c363cbd5ff4317ef086f66ddb78a73e GIT binary patch literal 430 zcmZQbWlZL3n9Gn{ug?Fkd42r9|LuRy?m4XNvu}@r