From 08d1c2eae26e253b3637c6d71635cb53171ba0af Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Sun, 15 May 2022 21:53:13 +0800 Subject: [PATCH] =?UTF-8?q?feat[protocol]:=20=E6=94=AF=E6=8C=81C++?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generate/GenerateProtocolFile.java | 11 +- .../registration/ProtocolAnalysis.java | 16 +- .../protocol/serializer/CodeLanguage.java | 2 + .../serializer/CutDownArraySerializer.java | 308 ++-- .../serializer/CutDownListSerializer.java | 182 ++- .../serializer/CutDownMapSerializer.java | 205 ++- .../serializer/CutDownSetSerializer.java | 182 ++- .../serializer/cpp/CppArraySerializer.java | 109 ++ .../serializer/cpp/CppBooleanSerializer.java | 50 + .../serializer/cpp/CppByteSerializer.java | 51 + .../serializer/cpp/CppCharSerializer.java | 51 + .../serializer/cpp/CppDoubleSerializer.java | 51 + .../serializer/cpp/CppFloatSerializer.java | 51 + .../serializer/cpp/CppIntSerializer.java | 50 + .../serializer/cpp/CppListSerializer.java | 105 ++ .../serializer/cpp/CppLongSerializer.java | 51 + .../serializer/cpp/CppMapSerializer.java | 115 ++ .../cpp/CppObjectProtocolSerializer.java | 72 + .../serializer/cpp/CppSetSerializer.java | 104 ++ .../serializer/cpp/CppShortSerializer.java | 51 + .../serializer/cpp/CppStringSerializer.java | 52 + .../serializer/cpp/GenerateCppUtils.java | 411 +++++ .../serializer/cpp/ICppSerializer.java | 36 + protocol/src/main/resources/cpp/ByteBuffer.h | 1386 +++++++++++++++++ .../src/main/resources/cpp/PacketTemplate.h | 57 + .../resources/cpp/ProtocolManagerTemplate.h | 33 + 26 files changed, 3465 insertions(+), 327 deletions(-) create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppArraySerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppBooleanSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppByteSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppCharSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppDoubleSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppFloatSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppIntSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppListSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppLongSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppMapSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppObjectProtocolSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppSetSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppShortSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppStringSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/cpp/ICppSerializer.java create mode 100644 protocol/src/main/resources/cpp/ByteBuffer.h create mode 100644 protocol/src/main/resources/cpp/PacketTemplate.h create mode 100644 protocol/src/main/resources/cpp/ProtocolManagerTemplate.h 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 87d902d3..8155f9d8 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java @@ -17,6 +17,7 @@ import com.zfoo.protocol.registration.IProtocolRegistration; import com.zfoo.protocol.registration.ProtocolAnalysis; import com.zfoo.protocol.registration.ProtocolRegistration; import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.cpp.GenerateCppUtils; import com.zfoo.protocol.serializer.cs.GenerateCsUtils; import com.zfoo.protocol.serializer.gd.GenerateGdUtils; import com.zfoo.protocol.serializer.js.GenerateJsUtils; @@ -96,8 +97,16 @@ public abstract class GenerateProtocolFile { GenerateProtocolPath.initProtocolPath(allSortedGenerateProtocols); } - // 生成C#协议 + // 生成C++协议 var generateLanguages = generateOperation.getGenerateLanguages(); + if (generateLanguages.contains(CodeLanguage.Cpp)) { + GenerateCppUtils.init(generateOperation); + GenerateCppUtils.createProtocolManager(allSortedGenerateProtocols); + for (var protocolRegistration : allSortedGenerateProtocols) { + GenerateCppUtils.createCppProtocolFile((ProtocolRegistration) protocolRegistration); + } + } + if (generateLanguages.contains(CodeLanguage.CSharp)) { GenerateCsUtils.init(generateOperation); GenerateCsUtils.createProtocolManager(); 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 5173b0e9..63954a86 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java @@ -22,6 +22,7 @@ import com.zfoo.protocol.generate.GenerateProtocolDocument; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.generate.GenerateProtocolPath; import com.zfoo.protocol.registration.field.*; +import com.zfoo.protocol.serializer.cpp.GenerateCppUtils; import com.zfoo.protocol.serializer.cs.GenerateCsUtils; import com.zfoo.protocol.serializer.gd.GenerateGdUtils; import com.zfoo.protocol.serializer.js.GenerateJsUtils; @@ -112,7 +113,7 @@ public class ProtocolAnalysis { // 通过指定类注册的协议,全部使用字节码增强 enhanceProtocolRegistration(Arrays.stream(protocols).filter(it -> Objects.nonNull(it)).collect(Collectors.toList())); - enhanceProtocolAfter(); + enhanceProtocolAfter(generateOperation); } catch (Exception e) { throw new RuntimeException(e); } @@ -176,7 +177,7 @@ public class ProtocolAnalysis { enhanceProtocolRegistration(enhanceList); - enhanceProtocolAfter(); + enhanceProtocolAfter(generateOperation); } catch (Exception e) { throw new UnknownException(e); } @@ -211,7 +212,7 @@ public class ProtocolAnalysis { GenerateProtocolFile.generate(generateOperation); } - private static void enhanceProtocolAfter() { + private static void enhanceProtocolAfter(GenerateOperation generateOperation) { subProtocolIdMap.clear(); subProtocolIdMap = null; @@ -220,16 +221,21 @@ public class ProtocolAnalysis { baseSerializerMap.clear(); baseSerializerMap = null; + EnhanceUtils.clear(); + + if (generateOperation.getGenerateLanguages().isEmpty()) { + return; + } + GenerateProtocolDocument.clear(); GenerateProtocolPath.clear(); GenerateProtocolFile.clear(); + GenerateCppUtils.clear(); GenerateCsUtils.clear(); GenerateJsUtils.clear(); GenerateLuaUtils.clear(); GenerateGdUtils.clear(); GenerateProtobufUtils.clear(); - - EnhanceUtils.clear(); } 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 2a651718..bf8cbb44 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java @@ -23,6 +23,8 @@ public enum CodeLanguage { */ Enhance, + Cpp, + JavaScript, Lua, 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 ea752e8b..72d3b909 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java @@ -51,9 +51,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeBooleanArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); break; @@ -63,6 +60,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -72,9 +73,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeBooleanBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); break; @@ -84,6 +82,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -93,9 +95,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeByteArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); break; @@ -105,6 +104,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteByteArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -114,9 +117,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeByteBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); break; @@ -126,6 +126,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteByteArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -135,9 +139,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeShortArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); break; @@ -147,6 +148,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteShortArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -156,9 +161,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeShortBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); break; @@ -168,6 +170,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteShortArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -177,9 +183,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeIntArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); break; @@ -189,6 +192,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -198,9 +205,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeIntBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); break; @@ -210,6 +214,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -219,9 +227,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeLongArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); break; @@ -231,6 +236,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -240,9 +249,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeLongBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); break; @@ -252,6 +258,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -261,9 +271,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeFloatArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); break; @@ -273,6 +280,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteFloatArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -282,9 +293,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeFloatBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); break; @@ -294,6 +302,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteFloatArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -303,9 +315,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeDoubleArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); break; @@ -315,6 +324,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -324,9 +337,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeDoubleBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); break; @@ -336,6 +346,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -345,9 +359,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeStringArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS); break; @@ -357,6 +368,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -366,9 +381,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeCharArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeCharArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeCharArray({})", objectStr)).append(LS); break; @@ -378,6 +390,10 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteCharArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeCharArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -387,9 +403,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeCharBoxArray($1, {});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeCharArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeCharArray({})", objectStr)).append(LS); break; @@ -399,28 +412,35 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteCharArray({});", objectStr)).append(LS); break; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeCharArray({});", objectStr)).append(LS); + break; default: flag = false; } break; default: if (arrayField.getArrayElementRegistration() instanceof ObjectProtocolField) { - var objectProtocolField = (ObjectProtocolField) arrayField.getArrayElementRegistration(); + var protocolId = ((ObjectProtocolField) arrayField.getArrayElementRegistration()).getProtocolId(); switch (language) { case Enhance: - builder.append(StringUtils.format("{}.writePacketArray($1, {}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId()))); - break; - case JavaScript: - builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("{}.writePacketArray($1, {}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); break; case GdScript: - builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolId)).append(LS); break; case Lua: - builder.append(StringUtils.format("buffer:writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("buffer:writePacketArray({}, {})", objectStr, protocolId)).append(LS); break; case CSharp: - builder.append(StringUtils.format("buffer.WritePacketArray<{}>({}, {});", EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()), objectStr, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("buffer.WritePacketArray<{}>({}, {});", EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), objectStr, protocolId)).append(LS); + break; + case Cpp: + builder.append(StringUtils.format("buffer.writePacketArray<{}>({}, {});", EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), objectStr, protocolId)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, protocolId)).append(LS); break; default: flag = false; @@ -447,9 +467,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readBooleanArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", array)).append(LS); break; @@ -459,6 +476,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readBooleanArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", array)).append(LS); + break; default: flag = false; } @@ -468,9 +491,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readBooleanBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", array)).append(LS); break; @@ -480,6 +500,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readBooleanArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", array)).append(LS); + break; default: flag = false; } @@ -489,9 +515,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readByteArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readByteArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readByteArray()", array)).append(LS); break; @@ -501,6 +524,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readByteArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readByteArray();", array)).append(LS); + break; default: flag = false; } @@ -510,9 +539,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readByteBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readByteArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readByteArray()", array)).append(LS); break; @@ -522,6 +548,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readByteArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readByteArray();", array)).append(LS); + break; default: flag = false; } @@ -531,9 +563,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readShortArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readShortArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readShortArray()", array)).append(LS); break; @@ -543,6 +572,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readShortArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readShortArray();", array)).append(LS); + break; default: flag = false; } @@ -552,9 +587,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readShortBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readShortArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readShortArray()", array)).append(LS); break; @@ -564,6 +596,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readShortArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readShortArray();", array)).append(LS); + break; default: flag = false; } @@ -573,9 +611,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readIntArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readIntArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntArray()", array)).append(LS); break; @@ -585,6 +620,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readIntArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readIntArray();", array)).append(LS); + break; default: flag = false; } @@ -594,9 +635,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readIntBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readIntArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntArray()", array)).append(LS); break; @@ -606,6 +644,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readIntArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readIntArray();", array)).append(LS); + break; default: flag = false; } @@ -615,9 +659,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readLongArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readLongArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongArray()", array)).append(LS); break; @@ -627,6 +668,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readLongArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readLongArray();", array)).append(LS); + break; default: flag = false; } @@ -636,9 +683,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readLongBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readLongArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongArray()", array)).append(LS); break; @@ -648,6 +692,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readLongArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readLongArray();", array)).append(LS); + break; default: flag = false; } @@ -657,9 +707,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readFloatArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readFloatArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readFloatArray()", array)).append(LS); break; @@ -669,6 +716,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readFloatArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readFloatArray();", array)).append(LS); + break; default: flag = false; } @@ -678,9 +731,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readFloatBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readFloatArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readFloatArray()", array)).append(LS); break; @@ -690,6 +740,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readFloatArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readFloatArray();", array)).append(LS); + break; default: flag = false; } @@ -699,9 +755,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readDoubleArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", array)).append(LS); break; @@ -711,6 +764,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readDoubleArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", array)).append(LS); + break; default: flag = false; } @@ -720,9 +779,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readDoubleBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", array)).append(LS); break; @@ -732,6 +788,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readDoubleArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", array)).append(LS); + break; default: flag = false; } @@ -741,9 +803,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readStringArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readStringArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringArray()", array)).append(LS); break; @@ -753,6 +812,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readStringArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readStringArray();", array)).append(LS); + break; default: flag = false; } @@ -762,9 +827,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readCharArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readCharArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readCharArray()", array)).append(LS); break; @@ -774,6 +836,12 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadCharArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readCharArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readCharArray();", array)).append(LS); + break; default: flag = false; } @@ -783,9 +851,6 @@ public class CutDownArraySerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}[] {} = {}.readCharBoxArray($1);", arrayName, array, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readCharArray();", array)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readCharArray()", array)).append(LS); break; @@ -795,26 +860,35 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadCharArray();", array)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readCharArray();", array)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readCharArray();", array)).append(LS); + break; default: flag = false; } break; default: if (arrayField.getArrayElementRegistration() instanceof ObjectProtocolField) { - var objectProtocolField = (ObjectProtocolField) arrayField.getArrayElementRegistration(); + var protocolId = ((ObjectProtocolField) arrayField.getArrayElementRegistration()).getProtocolId(); switch (language) { // Java不支持泛型的数组初始化,这里不做任何操作 - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", array, objectProtocolField.getProtocolId())).append(LS); - break; case GdScript: - builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", array, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", array, protocolId)).append(LS); break; case Lua: - builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", array, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", array, protocolId)).append(LS); break; case CSharp: - builder.append(StringUtils.format("var {} = buffer.ReadPacketArray<{}>({});", array, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()), objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("var {} = buffer.ReadPacketArray<{}>({});", array, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); + break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readPacketArray<{}>({});", array, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", array, protocolId)).append(LS); break; 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 14da3dca..0f74b6b3 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java @@ -50,9 +50,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeBooleanList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); break; @@ -62,6 +59,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanList({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeBooleanList({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -71,9 +74,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeByteList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); break; @@ -83,6 +83,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteByteList({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeByteList({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -92,9 +98,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeShortList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); break; @@ -104,6 +107,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteShortList({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeShortList({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -113,9 +122,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeIntList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); break; @@ -125,6 +131,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntList({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeIntList({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -134,9 +146,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeLongList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); break; @@ -146,6 +155,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongList({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeLongList({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -156,9 +171,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeFloatList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); break; @@ -168,6 +180,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteFloatList({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeFloatList({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -178,9 +196,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeDoubleList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); break; @@ -190,6 +205,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleList({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeDoubleList({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -200,9 +221,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeStringList($1, (List){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS); break; @@ -212,6 +230,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringList({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeStringList({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -219,22 +243,25 @@ public class CutDownListSerializer implements ICutDownSerializer { default: // List if (listField.getListElementRegistration() instanceof ObjectProtocolField) { - var objectProtocolField = (ObjectProtocolField) listField.getListElementRegistration(); + var protocolId = ((ObjectProtocolField) listField.getListElementRegistration()).getProtocolId(); switch (language) { case Enhance: - builder.append(StringUtils.format("{}.writePacketList($1, (List){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId()))); - break; - case JavaScript: - builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("{}.writePacketList($1, (List){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); break; case GdScript: - builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolId)).append(LS); break; case Lua: - builder.append(StringUtils.format("buffer:writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("buffer:writePacketArray({}, {})", objectStr, protocolId)).append(LS); break; case CSharp: - builder.append(StringUtils.format("buffer.WritePacketList({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("buffer.WritePacketList({}, {});", objectStr, protocolId)).append(LS); + break; + case Cpp: + builder.append(StringUtils.format("buffer.writePacketList({}, {});", objectStr, protocolId)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, protocolId)).append(LS); break; default: flag = false; @@ -260,9 +287,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("List {} = {}.readBooleanList($1);", list, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", list)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", list)).append(LS); break; @@ -272,6 +296,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanList();", list)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readBooleanList();", list)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", list)).append(LS); + break; default: flag = false; } @@ -281,9 +311,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("List {} = {}.readByteList($1);", list, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readByteArray();", list)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readByteArray()", list)).append(LS); break; @@ -293,6 +320,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteList();", list)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readByteList();", list)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readByteArray();", list)).append(LS); + break; default: flag = false; } @@ -302,9 +335,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("List {} = {}.readShortList($1);", list, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readShortArray();", list)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readShortArray()", list)).append(LS); break; @@ -314,6 +344,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortList();", list)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readShortList();", list)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readShortArray();", list)).append(LS); + break; default: flag = false; } @@ -323,9 +359,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("List {} = {}.readIntList($1);", list, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readIntArray();", list)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntArray()", list)).append(LS); break; @@ -335,6 +368,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntList();", list)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readIntList();", list)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readIntArray();", list)).append(LS); + break; default: flag = false; } @@ -344,9 +383,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("List {} = {}.readLongList($1);", list, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readLongArray();", list)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongArray()", list)).append(LS); break; @@ -356,6 +392,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongList();", list)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readLongList();", list)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readLongArray();", list)).append(LS); + break; default: flag = false; } @@ -365,9 +407,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("List {} = {}.readFloatList($1);", list, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readFloatArray();", list)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readFloatArray()", list)).append(LS); break; @@ -377,6 +416,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatList();", list)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readFloatList();", list)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readFloatArray();", list)).append(LS); + break; default: flag = false; } @@ -386,9 +431,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("List {} = {}.readDoubleList($1);", list, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", list)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", list)).append(LS); break; @@ -398,6 +440,12 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleList();", list)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readDoubleList();", list)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", list)).append(LS); + break; default: flag = false; } @@ -407,9 +455,6 @@ public class CutDownListSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("List {} = {}.readStringList($1);", list, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readStringArray();", list)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringArray()", list)).append(LS); break; @@ -419,28 +464,37 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringList();", list)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readStringList();", list)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readStringArray();", list)).append(LS); + break; default: flag = false; } break; default: if (listField.getListElementRegistration() instanceof ObjectProtocolField) { - var objectProtocolField = (ObjectProtocolField) listField.getListElementRegistration(); + var protocolId = ((ObjectProtocolField) listField.getListElementRegistration()).getProtocolId(); switch (language) { case Enhance: - builder.append(StringUtils.format("List {} = {}.readPacketList($1, {});", list, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId()))); - break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", list, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("List {} = {}.readPacketList($1, {});", list, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); break; case GdScript: - builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", list, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", list, protocolId)).append(LS); break; case Lua: - builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", list, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", list, protocolId)).append(LS); break; case CSharp: - builder.append(StringUtils.format("var {} = buffer.ReadPacketList<{}>({});", list, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()), objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("var {} = buffer.ReadPacketList<{}>({});", list, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); + break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readPacketList<{}>({});", list, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", list, protocolId)).append(LS); break; 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 0a7fbdd0..95fd2c44 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java @@ -57,9 +57,6 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeIntIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; - case JavaScript: - builder.append(StringUtils.format("buffer.writeIntIntMap({});", objectStr)).append(LS); - return true; case GdScript: builder.append(StringUtils.format("buffer.writeIntIntMap({})", objectStr)).append(LS); return true; @@ -69,15 +66,16 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntIntMap({});", objectStr)).append(LS); return true; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeIntIntMap({});", objectStr)).append(LS); + return true; } } else if (valueSerializer == LongSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeIntLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; - case JavaScript: - builder.append(StringUtils.format("buffer.writeIntLongMap({});", objectStr)).append(LS); - return true; case GdScript: builder.append(StringUtils.format("buffer.writeIntLongMap({})", objectStr)).append(LS); return true; @@ -87,12 +85,19 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntLongMap({});", objectStr)).append(LS); return true; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeIntLongMap({});", objectStr)).append(LS); + return true; } } else if (valueSerializer == StringSerializer.INSTANCE) { switch (language) { 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: builder.append(StringUtils.format("buffer.writeIntStringMap({});", objectStr)).append(LS); return true; @@ -111,9 +116,6 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeIntPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId()))); return true; - case JavaScript: - builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); - return true; case GdScript: builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; @@ -123,6 +125,10 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; } } } else if (keySerializer == LongSerializer.INSTANCE) { @@ -131,9 +137,6 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeLongIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; - case JavaScript: - builder.append(StringUtils.format("buffer.writeLongIntMap({});", objectStr)).append(LS); - return true; case GdScript: builder.append(StringUtils.format("buffer.writeLongIntMap({})", objectStr)).append(LS); return true; @@ -143,15 +146,16 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongIntMap({});", objectStr)).append(LS); return true; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeLongIntMap({});", objectStr)).append(LS); + return true; } } else if (valueSerializer == LongSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeLongLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; - case JavaScript: - builder.append(StringUtils.format("buffer.writeLongLongMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); - return true; case GdScript: builder.append(StringUtils.format("buffer.writeLongLongMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; @@ -161,15 +165,16 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongLongMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeLongLongMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; } } else if (valueSerializer == StringSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeLongStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; - case JavaScript: - builder.append(StringUtils.format("buffer.writeLongStringMap({});", objectStr)).append(LS); - return true; case GdScript: builder.append(StringUtils.format("buffer.writeLongStringMap({})", objectStr)).append(LS); return true; @@ -179,15 +184,16 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongStringMap({});", objectStr)).append(LS); return true; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeLongStringMap({});", objectStr)).append(LS); + return true; } } else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeLongPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId()))); return true; - case JavaScript: - builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); - return true; case GdScript: builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; @@ -197,6 +203,10 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; } } } else if (keySerializer == StringSerializer.INSTANCE) { @@ -205,9 +215,6 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeStringIntMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; - case JavaScript: - builder.append(StringUtils.format("buffer.writeStringIntMap({});", objectStr)).append(LS); - return true; case GdScript: builder.append(StringUtils.format("buffer.writeStringIntMap({})", objectStr)).append(LS); return true; @@ -217,15 +224,16 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringIntMap({});", objectStr)).append(LS); return true; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeStringIntMap({});", objectStr)).append(LS); + return true; } } else if (valueSerializer == LongSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeStringLongMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; - case JavaScript: - builder.append(StringUtils.format("buffer.writeStringLongMap({});", objectStr)).append(LS); - return true; case GdScript: builder.append(StringUtils.format("buffer.writeStringLongMap({})", objectStr)).append(LS); return true; @@ -235,15 +243,16 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringLongMap({});", objectStr)).append(LS); return true; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeStringLongMap({});", objectStr)).append(LS); + return true; } } else if (valueSerializer == StringSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeStringStringMap($1, (Map){});", EnhanceUtils.byteBufUtils, objectStr)); return true; - case JavaScript: - builder.append(StringUtils.format("buffer.writeStringStringMap({});", objectStr)).append(LS); - return true; case GdScript: builder.append(StringUtils.format("buffer.writeStringStringMap({})", objectStr)).append(LS); return true; @@ -253,15 +262,16 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringStringMap({});", objectStr)).append(LS); return true; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeStringStringMap({});", objectStr)).append(LS); + return true; } } else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("{}.writeStringPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId()))); return true; - case JavaScript: - builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); - return true; case GdScript: builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; @@ -271,6 +281,10 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; + case Cpp: + case JavaScript: + builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + return true; } } } @@ -295,9 +309,6 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Map {} = {}.readIntIntMap($1);", map, EnhanceUtils.byteBufUtils)); return map; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readIntIntMap();", map)).append(LS); - return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntIntMap()", map)).append(LS); return map; @@ -307,15 +318,18 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: 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; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readIntIntMap();", map)).append(LS); + return map; } } else if (valueSerializer == LongSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("Map {} = {}.readIntLongMap($1);", map, EnhanceUtils.byteBufUtils)); return map; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readIntLongMap();", map)).append(LS); - return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntLongMap()", map)).append(LS); return map; @@ -325,15 +339,18 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: 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; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readIntLongMap();", map)).append(LS); + return map; } } else if (valueSerializer == StringSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("Map {} = {}.readIntStringMap($1);", map, EnhanceUtils.byteBufUtils)); return map; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readIntStringMap();", map)).append(LS); - return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntStringMap()", map)).append(LS); return map; @@ -343,6 +360,12 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: 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; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readIntStringMap();", map)).append(LS); + return map; } } else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) { var protocolId = ((ObjectProtocolField) valueRegistration).getProtocolId(); @@ -350,18 +373,21 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Map {} = {}.readIntPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId()))); return map; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readIntPacketMap({});", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); - return map; case GdScript: - builder.append(StringUtils.format("var {} = buffer.readIntPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + builder.append(StringUtils.format("var {} = buffer.readIntPacketMap({})", map, protocolId)).append(LS); return map; case Lua: - builder.append(StringUtils.format("local {} = buffer:readIntPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + builder.append(StringUtils.format("local {} = buffer:readIntPacketMap({})", map, protocolId)).append(LS); return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); return map; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readIntPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); + return map; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readIntPacketMap({});", map, protocolId)).append(LS); + return map; } } } else if (keySerializer == LongSerializer.INSTANCE) { @@ -370,9 +396,6 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Map {} = {}.readLongIntMap($1);", map, EnhanceUtils.byteBufUtils)); return map; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readLongIntMap();", map)).append(LS); - return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongIntMap()", map)).append(LS); return map; @@ -382,15 +405,18 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: 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; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readLongIntMap();", map)).append(LS); + return map; } } else if (valueSerializer == LongSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("Map {} = {}.readLongLongMap($1);", map, EnhanceUtils.byteBufUtils)); return map; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readLongLongMap();", map)).append(LS); - return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongLongMap()", map)).append(LS); return map; @@ -400,14 +426,20 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: 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; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readLongLongMap();", map)).append(LS); + return map; } } else if (valueSerializer == StringSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("Map {} = {}.readLongStringMap($1);", map, EnhanceUtils.byteBufUtils)); return map; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readLongStringMap();", map)).append(LS); + case GdScript: + builder.append(StringUtils.format("var {} = buffer.readLongStringMap()", map)).append(LS); return map; case Lua: builder.append(StringUtils.format("local {} = buffer:readLongStringMap()", map)).append(LS); @@ -415,25 +447,34 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: 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; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readLongStringMap();", map)).append(LS); + return map; } } else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) { var protocolId = ((ObjectProtocolField) valueRegistration).getProtocolId(); switch (language) { case Enhance: - builder.append(StringUtils.format("Map {} = {}.readLongPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId()))); - return map; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readLongPacketMap({});", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + builder.append(StringUtils.format("Map {} = {}.readLongPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); return map; case GdScript: - builder.append(StringUtils.format("var {} = buffer.readLongPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + builder.append(StringUtils.format("var {} = buffer.readLongPacketMap({})", map, protocolId)).append(LS); return map; case Lua: - builder.append(StringUtils.format("local {} = buffer:readLongPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + builder.append(StringUtils.format("local {} = buffer:readLongPacketMap({})", map, protocolId)).append(LS); return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); return map; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readLongPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); + return map; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readLongPacketMap({});", map, protocolId)).append(LS); + return map; } } } else if (keySerializer == StringSerializer.INSTANCE) { @@ -442,9 +483,6 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Map {} = {}.readStringIntMap($1);", map, EnhanceUtils.byteBufUtils)); return map; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readStringIntMap();", map)).append(LS); - return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringIntMap()", map)).append(LS); return map; @@ -454,15 +492,18 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: 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; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readStringIntMap();", map)).append(LS); + return map; } } else if (valueSerializer == LongSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("Map {} = {}.readStringLongMap($1);", map, EnhanceUtils.byteBufUtils)); return map; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readStringLongMap();", map)).append(LS); - return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringLongMap()", map)).append(LS); return map; @@ -472,15 +513,18 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: 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; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readStringLongMap();", map)).append(LS); + return map; } } else if (valueSerializer == StringSerializer.INSTANCE) { switch (language) { case Enhance: builder.append(StringUtils.format("Map {} = {}.readStringStringMap($1);", map, EnhanceUtils.byteBufUtils)); return map; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readStringStringMap();", map)).append(LS); - return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringStringMap()", map)).append(LS); return map; @@ -490,25 +534,34 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: 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; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readStringStringMap();", map)).append(LS); + return map; } } else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) { var protocolId = ((ObjectProtocolField) valueRegistration).getProtocolId(); switch (language) { case Enhance: - builder.append(StringUtils.format("Map {} = {}.readStringPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId()))); - return map; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readStringPacketMap({});", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + builder.append(StringUtils.format("Map {} = {}.readStringPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); return map; case GdScript: - builder.append(StringUtils.format("var {} = buffer.readStringPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + builder.append(StringUtils.format("var {} = buffer.readStringPacketMap({})", map, protocolId)).append(LS); return map; case Lua: - builder.append(StringUtils.format("local {} = buffer:readStringPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); + builder.append(StringUtils.format("local {} = buffer:readStringPacketMap({})", map, protocolId)).append(LS); return map; case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); return map; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readStringPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); + return map; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readStringPacketMap({});", map, protocolId)).append(LS); + return map; } } } 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 8c17d839..a7b28e4c 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java @@ -51,9 +51,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeBooleanSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS); break; @@ -63,6 +60,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanSet({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeBooleanSet({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -72,9 +75,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeByteSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); break; @@ -84,6 +84,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteByteSet({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeByteSet({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -93,9 +99,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeShortSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); break; @@ -105,6 +108,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteShortSet({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeShortSet({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -114,9 +123,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeIntSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); break; @@ -126,6 +132,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntSet({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeIntSet({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -135,9 +147,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeLongSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); break; @@ -147,6 +156,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongSet({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeLongSet({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -156,9 +171,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeFloatSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); break; @@ -168,6 +180,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteFloatSet({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeFloatSet({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -177,9 +195,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeDoubleSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); break; @@ -189,6 +204,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleSet({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeDoubleSet({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -198,9 +219,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeStringSet($1, (Set){});", EnhanceUtils.byteBufUtils, objectStr)); break; - case JavaScript: - builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); - break; case GdScript: builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS); break; @@ -210,6 +228,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringSet({});", objectStr)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("buffer.writeStringSet({});", objectStr)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); + break; default: flag = false; } @@ -217,22 +241,25 @@ public class CutDownSetSerializer implements ICutDownSerializer { default: // Set if (setField.getSetElementRegistration() instanceof ObjectProtocolField) { - var objectProtocolField = (ObjectProtocolField) setField.getSetElementRegistration(); + var protocolId = ((ObjectProtocolField) setField.getSetElementRegistration()).getProtocolId(); switch (language) { case Enhance: - builder.append(StringUtils.format("{}.writePacketSet($1, (Set){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId()))); - break; - case JavaScript: - builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("{}.writePacketSet($1, (Set){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); break; case GdScript: - builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, protocolId)).append(LS); break; case Lua: - builder.append(StringUtils.format("buffer:writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("buffer:writePacketArray({}, {})", objectStr, protocolId)).append(LS); break; case CSharp: - builder.append(StringUtils.format("buffer.WritePacketSet({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("buffer.WritePacketSet({}, {});", objectStr, protocolId)).append(LS); + break; + case Cpp: + builder.append(StringUtils.format("buffer.writePacketSet({}, {});", objectStr, protocolId)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, protocolId)).append(LS); break; default: flag = false; @@ -258,9 +285,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Set {} = {}.readBooleanSet($1);", set, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", set)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", set)).append(LS); break; @@ -270,6 +294,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanSet();", set)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readBooleanSet();", set)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", set)).append(LS); + break; default: flag = false; } @@ -279,9 +309,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Set {} = {}.readByteSet($1);", set, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readByteArray();", set)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readByteArray()", set)).append(LS); break; @@ -291,6 +318,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteSet();", set)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readByteSet();", set)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readByteArray();", set)).append(LS); + break; default: flag = false; } @@ -300,9 +333,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Set {} = {}.readShortSet($1);", set, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readShortArray();", set)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readShortArray()", set)).append(LS); break; @@ -312,6 +342,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortSet();", set)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readShortSet();", set)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readShortArray();", set)).append(LS); + break; default: flag = false; } @@ -321,9 +357,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Set {} = {}.readIntSet($1);", set, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readIntArray();", set)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntArray()", set)).append(LS); break; @@ -333,6 +366,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntSet();", set)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readIntSet();", set)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readIntArray();", set)).append(LS); + break; default: flag = false; } @@ -342,9 +381,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Set {} = {}.readLongSet($1);", set, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readLongArray();", set)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongArray()", set)).append(LS); break; @@ -354,6 +390,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongSet();", set)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readLongSet();", set)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readLongArray();", set)).append(LS); + break; default: flag = false; } @@ -363,9 +405,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Set {} = {}.readFloatSet($1);", set, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readFloatArray();", set)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readFloatArray()", set)).append(LS); break; @@ -375,6 +414,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatSet();", set)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readFloatSet();", set)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readFloatArray();", set)).append(LS); + break; default: flag = false; } @@ -384,9 +429,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Set {} = {}.readDoubleSet($1);", set, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", set)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", set)).append(LS); break; @@ -396,6 +438,12 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleSet();", set)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readDoubleSet();", set)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", set)).append(LS); + break; default: flag = false; } @@ -405,9 +453,6 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Set {} = {}.readStringSet($1);", set, EnhanceUtils.byteBufUtils)); break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readStringArray();", set)).append(LS); - break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringArray()", set)).append(LS); break; @@ -417,28 +462,37 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringSet();", set)).append(LS); break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readStringSet();", set)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readStringArray();", set)).append(LS); + break; default: flag = false; } break; default: if (setField.getSetElementRegistration() instanceof ObjectProtocolField) { - var objectProtocolField = (ObjectProtocolField) setField.getSetElementRegistration(); + var protocolId = ((ObjectProtocolField) setField.getSetElementRegistration()).getProtocolId(); switch (language) { case Enhance: - builder.append(StringUtils.format("Set {} = {}.readPacketSet($1, {});", set, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(objectProtocolField.getProtocolId()))); - break; - case JavaScript: - builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", set, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("Set {} = {}.readPacketSet($1, {});", set, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); break; case GdScript: - builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", set, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", set, protocolId)).append(LS); break; case Lua: - builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", set, objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("local {} = buffer:readPacketArray({})", set, protocolId)).append(LS); break; case CSharp: - builder.append(StringUtils.format("var {} = buffer.ReadPacketSet<{}>({});", set, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()), objectProtocolField.getProtocolId())).append(LS); + builder.append(StringUtils.format("var {} = buffer.ReadPacketSet<{}>({});", set, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); + break; + case Cpp: + builder.append(StringUtils.format("auto {} = buffer.readPacketSet<{}>({});", set, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); + break; + case JavaScript: + builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", set, protocolId)).append(LS); break; default: flag = false; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppArraySerializer.java new file mode 100644 index 00000000..6cb458b2 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppArraySerializer.java @@ -0,0 +1,109 @@ +/* + * 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.cpp; + +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.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownArraySerializer; +import com.zfoo.protocol.serializer.reflect.ObjectProtocolSerializer; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class CppArraySerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + var type = GenerateCppUtils.toCppClassName(field.getType().getComponentType().getSimpleName()); + return new Pair<>(StringUtils.format("vector<{}>", type), field.getName()); + } + + @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.Cpp)) { + return; + } + + ArrayField arrayField = (ArrayField) fieldRegistration; + + String length = "length" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("int32_t {} = {}.size();", length, objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeInt({});", length)).append(LS); + + String i = "i" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("for (auto {} = 0; {} < {}; {}++) {", i, i, length, i)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + String element = "element" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("{} {} = {}[{}];", GenerateCppUtils.toCppClassName(arrayField.getType().getSimpleName()), element, objectStr, i)).append(LS); + + GenerateCppUtils.cppSerializer(arrayField.getArrayElementRegistration().serializer()) + .writeObject(builder, element, deep + 1, field, arrayField.getArrayElementRegistration()); + + 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.Cpp); + if (cutDown != null) { + return cutDown; + } + + + var arrayField = (ArrayField) fieldRegistration; + var result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + var typeName = GenerateCppUtils.toCppClassName(arrayField.getType().getSimpleName()); + + var i = "index" + GenerateProtocolFile.index.getAndIncrement(); + var size = "size" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("int32_t {} = buffer.readInt();", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("vector<{}> {};", typeName, result)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("for (int {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS); + var readObject = GenerateCppUtils.cppSerializer(arrayField.getArrayElementRegistration().serializer()) + .readObject(builder, deep + 2, field, arrayField.getArrayElementRegistration()); + + var point = StringUtils.EMPTY; + if (arrayField.getArrayElementRegistration().serializer() == ObjectProtocolSerializer.INSTANCE) { + point = "*"; + } + + GenerateProtocolFile.addTab(builder, deep + 2); + builder.append(StringUtils.format("{}.emplace_back({});", result, point, readObject)); + builder.append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + + + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppBooleanSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppBooleanSerializer.java new file mode 100644 index 00000000..3cbad931 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppBooleanSerializer.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.cpp; + +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 jaysunxiao + * @version 3.0 + */ +public class CppBooleanSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + return new Pair<>("bool", field.getName()); + } + + @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("bool {} = buffer.readBool();", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppByteSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppByteSerializer.java new file mode 100644 index 00000000..a7ea6db0 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppByteSerializer.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.cpp; + +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 jaysunxiao + * @version 3.0 + */ +public class CppByteSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + return new Pair<>("int8_t", field.getName()); + } + + @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("int8_t {} = buffer.readByte();", result)).append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppCharSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppCharSerializer.java new file mode 100644 index 00000000..2d74b16b --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppCharSerializer.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.cpp; + +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 jaysunxiao + * @version 3.0 + */ +public class CppCharSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + return new Pair<>("char", field.getName()); + } + + @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("char {} = buffer.readChar();", result)).append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppDoubleSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppDoubleSerializer.java new file mode 100644 index 00000000..829bd92d --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppDoubleSerializer.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.cpp; + +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 jaysunxiao + * @version 3.0 + */ +public class CppDoubleSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + return new Pair<>("double", field.getName()); + } + + @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("double {} = buffer.readDouble();", result)).append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppFloatSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppFloatSerializer.java new file mode 100644 index 00000000..8cc7f2b3 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppFloatSerializer.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.cpp; + +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 jaysunxiao + * @version 3.0 + */ +public class CppFloatSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + return new Pair<>("float", field.getName()); + } + + @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("float {} = buffer.readFloat();", result)).append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppIntSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppIntSerializer.java new file mode 100644 index 00000000..ca04cc61 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppIntSerializer.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.cpp; + +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 jaysunxiao + * @version 3.0 + */ +public class CppIntSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + return new Pair<>("int32_t", field.getName()); + } + + @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("int32_t {} = buffer.readInt();", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppListSerializer.java new file mode 100644 index 00000000..86708331 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppListSerializer.java @@ -0,0 +1,105 @@ +/* + * 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.cpp; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Pair; +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.serializer.reflect.ObjectProtocolSerializer; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class CppListSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + var type = GenerateCppUtils.toCppClassName(field.getGenericType().toString()); + var name = StringUtils.format("{}", field.getName()); + return new Pair<>(type, name); + } + + @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.Cpp)) { + return; + } + + ListField listField = (ListField) fieldRegistration; + + builder.append(StringUtils.format("buffer.writeInt({}.size());", objectStr)).append(LS); + + + GenerateProtocolFile.addTab(builder, deep); + String i = "i" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("for (auto {} : {}) {", i, objectStr)).append(LS); + + GenerateCppUtils.cppSerializer(listField.getListElementRegistration().serializer()) + .writeObject(builder, i, deep + 1, field, listField.getListElementRegistration()); + + 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.Cpp); + if (cutDown != null) { + return cutDown; + } + + var listField = (ListField) fieldRegistration; + + var result = "result" + GenerateProtocolFile.index.getAndIncrement(); + var typeName = GenerateCppUtils.toCppClassName(listField.getType().toString()); + + var i = "index" + GenerateProtocolFile.index.getAndIncrement(); + var size = "size" + GenerateProtocolFile.index.getAndIncrement(); + + builder.append(StringUtils.format("int32_t {} = buffer.readInt();", size)).append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("{} {};", typeName, result)).append(LS); + + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("for (int {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS); + var readObject = GenerateCppUtils.cppSerializer(listField.getListElementRegistration().serializer()) + .readObject(builder, deep + 1, field, listField.getListElementRegistration()); + + var point = StringUtils.EMPTY; + if (listField.getListElementRegistration().serializer() == ObjectProtocolSerializer.INSTANCE) { + point = "*"; + } + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("{}.emplace_back({}{});", result, point, readObject)).append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + + + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppLongSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppLongSerializer.java new file mode 100644 index 00000000..e86bb284 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppLongSerializer.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.cpp; + +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 jaysunxiao + * @version 3.0 + */ +public class CppLongSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + return new Pair<>("int64_t", field.getName()); + } + + @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("auto {} = buffer.readLong();", result)).append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppMapSerializer.java new file mode 100644 index 00000000..4fc860b8 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppMapSerializer.java @@ -0,0 +1,115 @@ +/* + * 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.cpp; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Pair; +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.serializer.reflect.ObjectProtocolSerializer; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class CppMapSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + var type = GenerateCppUtils.toCppClassName(field.getGenericType().toString()); + var name = StringUtils.format("{}", field.getName()); + return new Pair<>(type, name); + } + + @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.Cpp)) { + return; + } + + MapField mapField = (MapField) fieldRegistration; + builder.append(StringUtils.format("buffer.writeInt({}.size());", objectStr)).append(LS); + + String key = "keyElement" + GenerateProtocolFile.index.getAndIncrement(); + String value = "valueElement" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("for (auto&[{}, {}] : {}) {", key, value, objectStr)).append(LS); + + var mapKeyRegistration = mapField.getMapKeyRegistration(); + var mapValueRegistration = mapField.getMapValueRegistration(); + GenerateCppUtils.cppSerializer(mapField.getMapKeyRegistration().serializer()) + .writeObject(builder, key, deep + 1, field, mapField.getMapKeyRegistration()); + GenerateCppUtils.cppSerializer(mapField.getMapValueRegistration().serializer()) + .writeObject(builder, value, deep + 1, field, mapField.getMapValueRegistration()); + 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.Cpp); + if (cutDown != null) { + return cutDown; + } + + MapField mapField = (MapField) fieldRegistration; + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + var typeName = GenerateCppUtils.toCppClassName(mapField.getType().toString()); + + String size = "size" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("int32_t {} = buffer.readInt();", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("{} {};", typeName, result)).append(LS); + + + String i = "index" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("for (auto {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS); + + String keyObject = GenerateCppUtils.cppSerializer(mapField.getMapKeyRegistration().serializer()) + .readObject(builder, deep + 1, field, mapField.getMapKeyRegistration()); + + String valueObject = GenerateCppUtils.cppSerializer(mapField.getMapValueRegistration().serializer()) + .readObject(builder, deep + 1, field, mapField.getMapValueRegistration()); + GenerateProtocolFile.addTab(builder, deep + 1); + + var keyPoint = StringUtils.EMPTY; + var valuePoint = StringUtils.EMPTY; + if (mapField.getMapKeyRegistration().serializer() == ObjectProtocolSerializer.INSTANCE) { + keyPoint = "*"; + } + if (mapField.getMapValueRegistration().serializer() == ObjectProtocolSerializer.INSTANCE) { + valuePoint = "*"; + } + + builder.append(StringUtils.format("{}.insert(make_pair({}{}, {}{}));", result, keyPoint, keyObject, valuePoint, valueObject)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppObjectProtocolSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppObjectProtocolSerializer.java new file mode 100644 index 00000000..cd012a84 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppObjectProtocolSerializer.java @@ -0,0 +1,72 @@ +/* + * 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.cpp; + +import com.zfoo.protocol.IPacket; +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Pair; +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 jaysunxiao + * @version 3.0 + */ +public class CppObjectProtocolSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; + var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()); + var type = StringUtils.format("{}", protocolSimpleName); + var name = field.getName(); + return new Pair<>(type, name); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; + GenerateProtocolFile.addTab(builder, deep); + if (IPacket.class.isAssignableFrom(field.getType())) { + builder.append(StringUtils.format("buffer.writePacket({}, {});", objectStr, objectProtocolField.getProtocolId())) + .append(LS); + } else { + builder.append(StringUtils.format("buffer.writePacket((IPacket *) &{}, {});", objectStr, objectProtocolField.getProtocolId())) + .append(LS); + } + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; + String uniquePtr = "result" + GenerateProtocolFile.index.getAndIncrement(); + String ptr = "result" + GenerateProtocolFile.index.getAndIncrement(); + + var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("auto {} = buffer.readPacket({});", uniquePtr, objectProtocolField.getProtocolId())).append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("auto *{} = ({} *) {}.get();", ptr, protocolSimpleName, uniquePtr)).append(LS); + return ptr; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppSetSerializer.java new file mode 100644 index 00000000..e62b9391 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppSetSerializer.java @@ -0,0 +1,104 @@ +/* + * 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.cpp; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Pair; +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.serializer.reflect.ObjectProtocolSerializer; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class CppSetSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + var type = GenerateCppUtils.toCppClassName(field.getGenericType().toString()); + var name = StringUtils.format("{}", field.getName()); + return new Pair<>(type, name); + } + + @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.Cpp)) { + return; + } + + SetField setField = (SetField) fieldRegistration; + + builder.append(StringUtils.format("buffer.writeInt({}.size());", objectStr)).append(LS); + + String element = "i" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("for (auto {} : {}) {", element, objectStr)).append(LS); + + GenerateCppUtils.cppSerializer(setField.getSetElementRegistration().serializer()) + .writeObject(builder, element, deep + 1, field, setField.getSetElementRegistration()); + + 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.Cpp); + if (cutDown != null) { + return cutDown; + } + + SetField setField = (SetField) fieldRegistration; + var result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + var typeName = GenerateCppUtils.toCppClassName(setField.getType().toString()); + + var i = "index" + GenerateProtocolFile.index.getAndIncrement(); + var size = "size" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("int32_t {} = buffer.readInt();", size)).append(LS); + GenerateProtocolFile.addTab(builder, deep); + // unity里不支持HashSet的初始化大小 +// builder.append("var " + result + " = new " + typeName + "(" + size + ");" + LS); + builder.append(StringUtils.format("{} {};", typeName, result)).append(LS); + + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("for (int {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS); + + var point = StringUtils.EMPTY; + if (setField.getSetElementRegistration().serializer() == ObjectProtocolSerializer.INSTANCE) { + point = "*"; + } + + var readObject = GenerateCppUtils.cppSerializer(setField.getSetElementRegistration().serializer()) + .readObject(builder, deep + 1, field, setField.getSetElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("{}.emplace({}{});", result, point, readObject)).append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppShortSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppShortSerializer.java new file mode 100644 index 00000000..551ea813 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppShortSerializer.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.cpp; + +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 jaysunxiao + * @version 3.0 + */ +public class CppShortSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + return new Pair<>("int16_t", field.getName()); + } + + @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("auto {} = buffer.readShort();", result)).append(LS); + return result; + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppStringSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppStringSerializer.java new file mode 100644 index 00000000..2a4b7e07 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CppStringSerializer.java @@ -0,0 +1,52 @@ +/* + * 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.cpp; + +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 jaysunxiao + * @version 3.0 + */ +public class CppStringSerializer implements ICppSerializer { + + @Override + public Pair field(Field field, IFieldRegistration fieldRegistration) { + return new Pair<>("string", field.getName()); + } + + @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("auto {} = buffer.readString();", result)).append(LS); + return result; + } + + +} 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 new file mode 100644 index 00000000..c0539f32 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java @@ -0,0 +1,411 @@ +/* + * 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.cpp; + +import com.zfoo.protocol.IPacket; +import com.zfoo.protocol.collection.CollectionUtils; +import com.zfoo.protocol.generate.GenerateOperation; +import com.zfoo.protocol.generate.GenerateProtocolDocument; +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.generate.GenerateProtocolPath; +import com.zfoo.protocol.model.Pair; +import com.zfoo.protocol.registration.IProtocolRegistration; +import com.zfoo.protocol.registration.ProtocolAnalysis; +import com.zfoo.protocol.registration.ProtocolRegistration; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer; +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.*; +import java.util.stream.Collectors; + +import static com.zfoo.protocol.util.FileUtils.LS; +import static com.zfoo.protocol.util.StringUtils.TAB; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public abstract class GenerateCppUtils { + + private static String protocolOutputRootPath = "zfoocpp"; + private static String protocolOutputPath = StringUtils.EMPTY; + + private static Map cppSerializerMap; + + public static ICppSerializer cppSerializer(ISerializer serializer) { + return cppSerializerMap.get(serializer); + } + + public static void init(GenerateOperation generateOperation) { + protocolOutputPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath); + + FileUtils.deleteFile(new File(protocolOutputPath)); + FileUtils.createDirectory(protocolOutputPath); + + cppSerializerMap = new HashMap<>(); + cppSerializerMap.put(BooleanSerializer.INSTANCE, new CppBooleanSerializer()); + cppSerializerMap.put(ByteSerializer.INSTANCE, new CppByteSerializer()); + cppSerializerMap.put(ShortSerializer.INSTANCE, new CppShortSerializer()); + cppSerializerMap.put(IntSerializer.INSTANCE, new CppIntSerializer()); + cppSerializerMap.put(LongSerializer.INSTANCE, new CppLongSerializer()); + cppSerializerMap.put(FloatSerializer.INSTANCE, new CppFloatSerializer()); + cppSerializerMap.put(DoubleSerializer.INSTANCE, new CppDoubleSerializer()); + cppSerializerMap.put(CharSerializer.INSTANCE, new CppCharSerializer()); + cppSerializerMap.put(StringSerializer.INSTANCE, new CppStringSerializer()); + cppSerializerMap.put(ArraySerializer.INSTANCE, new CppArraySerializer()); + cppSerializerMap.put(ListSerializer.INSTANCE, new CppListSerializer()); + cppSerializerMap.put(SetSerializer.INSTANCE, new CppSetSerializer()); + cppSerializerMap.put(MapSerializer.INSTANCE, new CppMapSerializer()); + cppSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new CppObjectProtocolSerializer()); + } + + public static void clear() { + cppSerializerMap = null; + protocolOutputRootPath = null; + protocolOutputPath = null; + } + + /** + * 生成协议依赖的工具类 + */ + public static void createProtocolManager(List protocolList) throws IOException { + var list = List.of("cpp/ByteBuffer.h"); + + for (var fileName : list) { + var fileInputStream = ClassUtils.getFileFromClassPath(fileName); + var createFile = new File(StringUtils.format("{}/{}", protocolOutputPath, StringUtils.substringAfterFirst(fileName, "cpp/"))); + FileUtils.writeInputStreamToFile(createFile, fileInputStream); + } + + var protocolManagerTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("cpp/ProtocolManagerTemplate.h"))); + + // 生成ProtocolManager.gd文件 + var headerBuilder = new StringBuilder(); + protocolList.stream() + .filter(it -> Objects.nonNull(it)) + .forEach(it -> { + var name = it.protocolConstructor().getDeclaringClass().getSimpleName(); + var path = StringUtils.format("#include \"{}/{}/{}.h\"", protocolOutputRootPath, GenerateProtocolPath.getProtocolPath(it.protocolId()), name); + path = path.replaceAll("//", StringUtils.SLASH); + headerBuilder.append(path).append(LS); + }); + + var initProtocolBuilder = new StringBuilder(); + protocolList.stream() + .filter(it -> Objects.nonNull(it)) + .forEach(it -> initProtocolBuilder.append(TAB).append(TAB).append(StringUtils.format("protocols[{}] = new {}Registration();", it.protocolId(), it.protocolConstructor().getDeclaringClass().getSimpleName())).append(LS)); + + protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, headerBuilder.toString(), initProtocolBuilder.toString().trim()); + FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.h")), protocolManagerTemplate); + } + + /** + * 生成协议类 + */ + public static void createCppProtocolFile(ProtocolRegistration registration) throws IOException { + GenerateProtocolFile.index.set(0); + + var protocolId = registration.protocolId(); + var registrationConstructor = registration.getConstructor(); + var fieldRegistrations = registration.getFieldRegistrations(); + + var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName(); + + var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("cpp/PacketTemplate.h"))); + + // protocol object + var defineProtocolName = protocolClazzName.toUpperCase(); + var includeHeaders = includeSubProtocol(registration); + var docTitle = docTitle(registration); + var fieldDefinition = fieldDefinition(registration); + var valueOfMethod = valueOfMethod(registration); + var operator = operator(registration); + var writeObject = writeObject(registration); + var readObject = readObject(registration); + + protocolTemplate = StringUtils.format(protocolTemplate, defineProtocolName, defineProtocolName, includeHeaders, docTitle + , protocolClazzName, fieldDefinition, protocolClazzName, protocolClazzName, valueOfMethod.getKey(), protocolClazzName + , valueOfMethod.getValue().trim(), protocolId, protocolClazzName, operator.trim(), + protocolClazzName, protocolId, protocolClazzName, writeObject.trim(), protocolClazzName, readObject.trim()); + + var protocolOutputPath = StringUtils.format("{}/{}/{}.h" + , GenerateCppUtils.protocolOutputPath + , GenerateProtocolPath.getCapitalizeProtocolPath(protocolId) + , protocolClazzName); + FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate); + } + + + private static String includeSubProtocol(ProtocolRegistration registration) { + short protocolId = registration.getId(); + var subProtocols = ProtocolAnalysis.getAllSubProtocolIds(protocolId); + + if (CollectionUtils.isEmpty(subProtocols)) { + return StringUtils.EMPTY; + } + var cppBuilder = new StringBuilder(); + for (var subProtocolId : subProtocols) { + var protocolClassName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(subProtocolId); + var subProtocolPath = StringUtils.format("#include \"{}/{}/{}.h\"" + , protocolOutputRootPath + , GenerateProtocolPath.getCapitalizeProtocolPath(protocolId) + , protocolClassName); + subProtocolPath = subProtocolPath.replaceAll("//", StringUtils.SLASH); + cppBuilder.append(subProtocolPath).append(LS); + } + + return cppBuilder.toString(); + } + + private static String docTitle(ProtocolRegistration registration) { + var protocolId = registration.getId(); + var protocolDocument = GenerateProtocolDocument.getProtocolDocument(protocolId); + var docTitle = protocolDocument.getKey(); + + var cppBuilder = new StringBuilder(); + if (StringUtils.isNotBlank(docTitle)) { + Arrays.stream(docTitle.split(LS)).forEach(it -> cppBuilder.append(TAB).append(it).append(LS)); + } + return cppBuilder.toString().trim(); + } + + private static String fieldDefinition(ProtocolRegistration registration) { + var protocolId = registration.getId(); + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + + var protocolDocument = GenerateProtocolDocument.getProtocolDocument(protocolId); + var docFieldMap = protocolDocument.getValue(); + + var cppBuilder = new StringBuilder(); + // 协议的属性生成 + for (int i = 0; i < fields.length; i++) { + Field field = fields[i]; + IFieldRegistration fieldRegistration = fieldRegistrations[i]; + + var propertyTypeAndName = cppSerializer(fieldRegistration.serializer()).field(field, fieldRegistration); + var propertyType = propertyTypeAndName.getKey(); + var propertyName = propertyTypeAndName.getValue(); + + var propertyFullName = StringUtils.format("{} {};", propertyType, propertyName); + // 生成注释 + var doc = docFieldMap.get(propertyName); + if (StringUtils.isNotBlank(doc)) { + Arrays.stream(doc.split(LS)).forEach(it -> cppBuilder.append(TAB + TAB).append(it).append(LS)); + } + cppBuilder.append(TAB + TAB).append(propertyFullName).append(LS); + } + + return cppBuilder.toString().trim(); + } + + private static Pair valueOfMethod(ProtocolRegistration registration) { + var protocolId = registration.getId(); + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + + var filedList = new ArrayList>(); + + for (int i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + var propertyTypeAndName = cppSerializer(fieldRegistration.serializer()).field(field, fieldRegistration); + filedList.add(propertyTypeAndName); + } + + + // ValueOf()方法 + var valueOfParams = filedList.stream() + .map(it -> StringUtils.format("{} {}", it.getKey(), it.getValue())) + .collect(Collectors.toList()); + var valueOfParamsStr = StringUtils.joinWith(StringUtils.COMMA + " ", valueOfParams.toArray()); + + var cppBuilder = new StringBuilder(); + filedList.forEach(it -> cppBuilder.append(TAB + TAB + TAB).append(StringUtils.format("packet.{} = {};", it.getValue(), it.getValue())).append(LS)); + + return new Pair<>(valueOfParamsStr, cppBuilder.toString()); + } + + private static String operator(ProtocolRegistration registration) { + var protocolId = registration.getId(); + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + + var filedList = new ArrayList>(); + for (int i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + var propertyTypeAndName = cppSerializer(fieldRegistration.serializer()).field(field, fieldRegistration); + filedList.add(propertyTypeAndName); + } + + // bool operator< + var cppBuilder = new StringBuilder(); + for (var fieldPair : filedList) { + cppBuilder.append(TAB + TAB + TAB).append(StringUtils.format("if ({} < _.{}) { return true; }", fieldPair.getValue(), fieldPair.getValue())).append(LS); + cppBuilder.append(TAB + TAB + TAB).append(StringUtils.format("if (_.{} < {}) { return false; }", fieldPair.getValue(), fieldPair.getValue())).append(LS); + } + return cppBuilder.toString(); + } + + private static String writeObject(ProtocolRegistration registration) { + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + + var csBuilder = new StringBuilder(); + for (int i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + var serializer = cppSerializer(fieldRegistration.serializer()); + if (IPacket.class.isAssignableFrom(field.getType())) { + serializer.writeObject(csBuilder, "&message->" + field.getName(), 3, field, fieldRegistration); + } else { + serializer.writeObject(csBuilder, "message->" + field.getName(), 3, field, fieldRegistration); + } + } + return csBuilder.toString(); + } + + + private static String readObject(ProtocolRegistration registration) { + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + + var csBuilder = new StringBuilder(); + for (int i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + var readObject = cppSerializer(fieldRegistration.serializer()).readObject(csBuilder, 3, field, fieldRegistration); + csBuilder.append(TAB + TAB + TAB); + if (IPacket.class.isAssignableFrom(field.getType())) { + csBuilder.append(StringUtils.format("packet->{} = *{};", field.getName(), readObject)); + } else { + csBuilder.append(StringUtils.format("packet->{} = {};", field.getName(), readObject)); + } + + csBuilder.append(LS); + } + return csBuilder.toString(); + } + + + public static String toCppClassName(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_t"; + return typeName; + case "short": + case "Short": + typeName = "int16_t"; + return typeName; + case "int": + case "Integer": + typeName = "int32_t"; + return typeName; + case "long": + case "Long": + typeName = "int64_t"; + return typeName; + case "Float": + typeName = "float"; + return typeName; + case "Double": + typeName = "double"; + return typeName; + case "Character": + typeName = "char"; + return typeName; + case "String": + typeName = "string"; + return typeName; + default: + } + + // 将boolean转为bool + typeName = typeName.replaceAll("[B|b]oolean\\[", "bool"); + typeName = typeName.replace("", "bool>"); + + // 将Byte转为byte + typeName = typeName.replace("Byte[", "int8_t"); + typeName = typeName.replace("Byte>", "int8_t>"); + typeName = typeName.replace("", "int16_t>"); + typeName = typeName.replace("", "int32_t>"); + typeName = typeName.replace("", "int64_t>"); + typeName = typeName.replace("", "float>"); + typeName = typeName.replace("", "double>"); + typeName = typeName.replace("", "char>"); + typeName = typeName.replace("", "string>"); + typeName = typeName.replace(" field(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/cpp/ByteBuffer.h b/protocol/src/main/resources/cpp/ByteBuffer.h new file mode 100644 index 00000000..fafe9735 --- /dev/null +++ b/protocol/src/main/resources/cpp/ByteBuffer.h @@ -0,0 +1,1386 @@ +#ifndef ZFOO_BYTEBUFFER_H +#define ZFOO_BYTEBUFFER_H + +#include +#include +#include +#include +#include +#include +#include + +// 网络传输默认使用大端传输 +namespace zfoo { + + using std::string; + using std::vector; + using std::list; + using std::set; + using std::map; + using std::make_pair; + using std::pair; + using std::unique_ptr; + + // Returns true if the current machine is little endian + static inline bool is_little_endian() { + static std::int32_t test = 1; + return (*reinterpret_cast(&test) == 1); + } + + // Default size of the buffer + static const int32_t DEFAULT_BUFFER_SIZE = 2048; + static const bool IS_LITTLE_ENDIAN = is_little_endian(); + static const string EMPTY_STRING = ""; + + + class ByteBuffer; + + class IPacket { + public: + virtual int16_t protocolId() = 0; + + virtual ~IPacket() { + } + }; + + class IProtocolRegistration { + public: + virtual int16_t protocolId() = 0; + + virtual void write(ByteBuffer &buffer, IPacket *packet) = 0; + + virtual IPacket *read(ByteBuffer &buffer) = 0; + }; + + IProtocolRegistration *getProtocol(int16_t protocolId); + + class ByteBuffer { + private: + int8_t *m_buffer; + int32_t m_max_capacity; + int32_t m_writerIndex; + int32_t m_readerIndex; + + public: + ByteBuffer(int32_t capacity = DEFAULT_BUFFER_SIZE) : m_max_capacity(capacity) { + m_buffer = (int8_t *) calloc(m_max_capacity, sizeof(int8_t)); + clear(); + } + + ~ByteBuffer() { + free(m_buffer); + m_buffer = nullptr; + } + + + ByteBuffer(const ByteBuffer &buffer) = delete; + + ByteBuffer &operator=(const ByteBuffer &buffer) = delete; + + + void clear() { + m_writerIndex = 0; + m_readerIndex = 0; + } + + int32_t writerIndex() const { + return m_writerIndex; + } + + int32_t readerIndex() const { + return m_readerIndex; + } + + void writerIndex(int32_t writeIndex) { + if (writeIndex > m_max_capacity) { + string errorMessage = + "writeIndex[" + std::to_string(writeIndex) + "] out of bounds exception: readerIndex: " + + std::to_string(m_readerIndex) + + ", writerIndex: " + std::to_string(m_writerIndex) + + "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + std::to_string(m_max_capacity); + throw errorMessage; + } + m_writerIndex = writeIndex; + } + + void readerIndex(int32_t readerIndex) { + if (readerIndex > m_writerIndex) { + string errorMessage = + "readIndex[" + std::to_string(readerIndex) + "] out of bounds exception: readerIndex: " + + std::to_string(m_readerIndex) + + ", writerIndex: " + std::to_string(m_writerIndex) + + "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + std::to_string(m_max_capacity); + throw errorMessage; + } + m_readerIndex = readerIndex; + } + + inline int32_t getCapacity() const { + return m_max_capacity - m_writerIndex; + } + + inline void ensureCapacity(const int32_t &capacity) { + while (capacity - getCapacity() > 0) { + int32_t newSize = m_max_capacity * 2; + int8_t *pBuf = (int8_t *) realloc(m_buffer, newSize); + if (!pBuf) { + std::cout << "relloc failed!" << std::endl; + exit(1); + } + m_buffer = pBuf; + m_max_capacity = newSize; + } + } + + inline void writeBool(const bool &value) { + ensureCapacity(1); + int8_t v = value ? 1 : 0; + m_buffer[m_writerIndex++] = v; + } + + inline bool readBool() { + int8_t value = m_buffer[m_readerIndex++]; + return value == 1; + } + + inline void writeByte(const int8_t &value) { + ensureCapacity(1); + m_buffer[m_writerIndex++] = value; + } + + inline int8_t readByte() { + return m_buffer[m_readerIndex++]; + } + + inline void setByte(const int32_t &index, const int8_t &value) { + m_buffer[index] = value; + } + + inline int8_t getByte(const int32_t &index) { + return m_buffer[index]; + } + + inline void writeBytes(const int8_t *buffer, const int32_t &length) { + ensureCapacity(length); + memcpy(&m_buffer[m_writerIndex], buffer, length); + m_writerIndex += length; + } + + inline int8_t *readBytes(const int32_t &length) { + int8_t *bytes = &m_buffer[m_readerIndex]; + m_readerIndex += length; + return bytes; + } + + + inline void writeShort(const int16_t &value) { + write(value); + } + + inline int16_t readShort() { + return read(); + } + + inline void writeInt(const int32_t &intValue) { + writeVarInt((uint32_t) ((intValue << 1) ^ (intValue >> 31))); + } + + inline void writeVarInt(const uint32_t &value) { + uint32_t a = value >> 7; + if (a == 0) { + writeByte((int8_t) value); + return; + } + + int32_t writeIndex = m_writerIndex; + ensureCapacity(5); + + setByte(writeIndex++, (int8_t) (value | 0x80)); + uint32_t b = value >> 14; + if (b == 0) { + setByte(writeIndex++, (int8_t) a); + writerIndex(writeIndex); + return; + } + + setByte(writeIndex++, (int8_t) (a | 0x80)); + a = value >> 21; + if (a == 0) { + setByte(writeIndex++, (int8_t) b); + writerIndex(writeIndex); + return; + } + + setByte(writeIndex++, (int8_t) (b | 0x80)); + b = value >> 28; + if (b == 0) { + setByte(writeIndex++, (int8_t) a); + writerIndex(writeIndex); + return; + } + + setByte(writeIndex++, (int8_t) (a | 0x80)); + setByte(writeIndex++, (int8_t) b); + writerIndex(writeIndex); + } + + inline int32_t readInt() { + int32_t readIndex = m_readerIndex; + + int32_t b = getByte(readIndex++); + uint32_t value = b; + if (b < 0) { + b = getByte(readIndex++); + value = value & 0x0000007F | b << 7; + if (b < 0) { + b = getByte(readIndex++); + value = value & 0x00003FFF | b << 14; + if (b < 0) { + b = getByte(readIndex++); + value = value & 0x001FFFFF | b << 21; + if (b < 0) { + value = value & 0x0FFFFFFF | getByte(readIndex++) << 28; + } + } + } + } + readerIndex(readIndex); + value = ((value >> 1) ^ -((int32_t) value & 1)); + return (int32_t) value; + } + + inline void writeLong(const int64_t &longValue) { + uint64_t mask = (uint64_t) ((longValue << 1) ^ (longValue >> 63)); + + if (mask >> 32 == 0) { + writeVarInt((uint32_t) mask); + return; + } + + int8_t bytes[9]; + bytes[0] = (int8_t) (mask | 0x80); + bytes[1] = (int8_t) (mask >> 7 | 0x80); + bytes[2] = (int8_t) (mask >> 14 | 0x80); + bytes[3] = (int8_t) (mask >> 21 | 0x80); + + uint32_t a = (uint32_t) (mask >> 28); + uint32_t b = (uint32_t) (mask >> 35); + if (b == 0) { + bytes[4] = (int8_t) a; + writeBytes(bytes, 5); + return; + } + + bytes[4] = (int8_t) (a | 0x80); + a = (uint32_t) (mask >> 42); + if (a == 0) { + bytes[5] = (int8_t) b; + writeBytes(bytes, 6); + return; + } + + bytes[5] = (int8_t) (b | 0x80); + b = (int) (mask >> 49); + if (b == 0) { + bytes[6] = (int8_t) a; + writeBytes(bytes, 7); + return; + } + + bytes[6] = (int8_t) (a | 0x80); + a = (int) (mask >> 56); + if (a == 0) { + bytes[7] = (int8_t) b; + writeBytes(bytes, 8); + return; + } + + bytes[7] = (int8_t) (b | 0x80); + bytes[8] = (int8_t) a; + writeBytes(bytes, 9); + } + + inline int64_t readLong() { + int32_t readIndex = m_readerIndex; + + int64_t b = getByte(readIndex++); + uint64_t value = b; + if (b < 0) { + b = getByte(readIndex++); + value = value & 0x000000000000007FLL | b << 7; + if (b < 0) { + b = getByte(readIndex++); + value = value & 0x0000000000003FFFLL | b << 14; + if (b < 0) { + b = getByte(readIndex++); + value = value & 0x00000000001FFFFFLL | b << 21; + if (b < 0) { + b = getByte(readIndex++); + value = value & 0x000000000FFFFFFFLL | b << 28; + if (b < 0) { + b = getByte(readIndex++); + value = value & 0x00000007FFFFFFFFLL | b << 35; + if (b < 0) { + b = getByte(readIndex++); + value = value & 0x000003FFFFFFFFFFLL | b << 42; + if (b < 0) { + b = getByte(readIndex++); + value = value & 0x0001FFFFFFFFFFFFLL | b << 49; + if (b < 0) { + b = getByte(readIndex++); + value = value & 0x00FFFFFFFFFFFFFFLL | b << 56; + } + } + } + } + } + } + } + } + + readerIndex(readIndex); + value = ((value >> 1) ^ -((int64_t) value & 1)); + return (int64_t) value; + } + + inline void writeFloat(const float &value) { + write(value); + } + + inline float readFloat() { + return read(); + } + + inline void writeDouble(const double &value) { + write(value); + } + + inline double readDouble() { + return read(); + } + + inline void writeString(const string &value) { + if (value.empty()) { + writeInt(0); + return; + } + int32_t length = value.size() * sizeof(value.front()); + writeInt(length); + writeBytes(reinterpret_cast(&value[0]), length); + } + + inline string readString() { + int32_t length = readInt(); + if (length <= 0) { + return EMPTY_STRING; + } + + auto bytes = readBytes(length); + string str(reinterpret_cast(bytes), length); + return str; + } + + // 很多脚本语言没有char,所以这里使用string代替 + inline void writeChar(const char &value) { + string str; + str.push_back(value); + writeString(str); + } + + inline char readChar() { + return readString()[0]; + } + + inline bool writePacketFlag(const IPacket *packet) { + bool flag = packet == nullptr; + writeBool(!flag); + return flag; + } + + inline void writePacket(IPacket *packet, const int16_t &protocolId) { + IProtocolRegistration *protocolRegistration = getProtocol(protocolId); + protocolRegistration->write(*this, packet); + } + + inline unique_ptr readPacket(const int16_t &protocolId) { + IProtocolRegistration *protocolRegistration = getProtocol(protocolId); + auto packet = protocolRegistration->read(*this); + return unique_ptr(packet); + } + + + //---------------------------------boolean-------------------------------------- + inline void writeBooleanArray(const vector &array) { + if (array.empty()) { + writeByte(0); + return; + } + int32_t length = array.size(); + writeInt(length); + for (auto value : array) { + writeBool(value); + } + } + + inline vector readBooleanArray() { + int32_t length = readInt(); + int8_t *bytes = readBytes(length); + vector array; + for (auto i = 0; i < length; i++) { + array.emplace_back((bytes[i] == 1)); + } + return array; + } + + inline void writeBooleanList(const list &list) { + if (list.empty()) { + writeByte(0); + return; + } + int32_t length = list.size(); + writeInt(length); + for (auto value : list) { + writeBool(value); + } + } + + inline list readBooleanList() { + int32_t length = readInt(); + list list; + for (auto i = 0; i < length; i++) { + list.emplace_back(readBool()); + } + return list; + } + + inline void writeBooleanSet(const set &set) { + if (set.empty()) { + writeByte(0); + return; + } + int32_t length = set.size(); + writeInt(length); + for (auto value : set) { + writeBool(value); + } + } + + inline set readBooleanSet() { + int32_t length = readInt(); + set set; + for (auto i = 0; i < length; i++) { + set.emplace(readBool()); + } + return set; + } + + //---------------------------------byte-------------------------------------- + inline void writeByteArray(const vector &array) { + if (array.empty()) { + writeByte(0); + return; + } + int32_t length = array.size(); + writeInt(length); + for (auto value : array) { + writeByte(value); + } + } + + inline vector readByteArray() { + int32_t length = readInt(); + int8_t *bytes = readBytes(length); + vector array; + for (auto i = 0; i < length; i++) { + array.emplace_back(bytes[i]); + } + return array; + } + + inline void writeByteList(const list &list) { + if (list.empty()) { + writeByte(0); + return; + } + int32_t length = list.size(); + writeInt(length); + for (auto value : list) { + writeByte(value); + } + } + + inline list readByteList() { + int32_t length = readInt(); + list list; + for (auto i = 0; i < length; i++) { + list.emplace_back(readByte()); + } + return list; + } + + inline void writeByteSet(const set &set) { + if (set.empty()) { + writeByte(0); + return; + } + int32_t length = set.size(); + writeInt(length); + for (auto value : set) { + writeByte(value); + } + } + + inline set readByteSet() { + int32_t length = readInt(); + set set; + for (auto i = 0; i < length; i++) { + set.emplace(readByte()); + } + return set; + } + + //---------------------------------short-------------------------------------- + inline void writeShortArray(const vector &array) { + if (array.empty()) { + writeByte(0); + return; + } + int32_t length = array.size(); + writeInt(length); + for (auto value : array) { + writeShort(value); + } + } + + inline vector readShortArray() { + int32_t length = readInt(); + vector array; + for (auto i = 0; i < length; i++) { + array.emplace_back(readShort()); + } + return array; + } + + inline void writeShortList(const list &list) { + if (list.empty()) { + writeByte(0); + return; + } + int32_t length = list.size(); + writeInt(length); + for (auto value : list) { + writeShort(value); + } + } + + inline list readShortList() { + int32_t length = readInt(); + list list; + for (auto i = 0; i < length; i++) { + list.emplace_back(readShort()); + } + return list; + } + + inline void writeShortSet(const set &set) { + if (set.empty()) { + writeByte(0); + return; + } + int32_t length = set.size(); + writeInt(length); + for (auto value : set) { + writeShort(value); + } + } + + inline set readShortSet() { + int32_t length = readInt(); + set set; + for (auto i = 0; i < length; i++) { + set.emplace(readShort()); + } + return set; + } + + //---------------------------------int-------------------------------------- + inline void writeIntArray(const vector &array) { + if (array.empty()) { + writeByte(0); + return; + } + int32_t length = array.size(); + writeInt(length); + for (auto value : array) { + writeInt(value); + } + } + + inline vector readIntArray() { + int32_t length = readInt(); + vector array; + for (auto i = 0; i < length; i++) { + array.emplace_back(readInt()); + } + return array; + } + + inline void writeIntList(const list &list) { + if (list.empty()) { + writeByte(0); + return; + } + int32_t length = list.size(); + writeInt(length); + for (auto value : list) { + writeInt(value); + } + } + + inline list readIntList() { + int32_t length = readInt(); + list list; + for (auto i = 0; i < length; i++) { + list.emplace_back(readInt()); + } + return list; + } + + inline void writeIntSet(const set &set) { + if (set.empty()) { + writeByte(0); + return; + } + int32_t length = set.size(); + writeInt(length); + for (auto value : set) { + writeInt(value); + } + } + + inline set readIntSet() { + int32_t length = readInt(); + set set; + for (auto i = 0; i < length; i++) { + set.emplace(readInt()); + } + return set; + } + + //---------------------------------long-------------------------------------- + inline void writeLongArray(const vector &array) { + if (array.empty()) { + writeByte(0); + return; + } + int32_t length = array.size(); + writeInt(length); + for (auto value : array) { + writeLong(value); + } + } + + inline vector readLongArray() { + int32_t length = readInt(); + vector array; + for (auto i = 0; i < length; i++) { + array.emplace_back(readLong()); + } + return array; + } + + inline void writeLongList(const list &list) { + if (list.empty()) { + writeByte(0); + return; + } + int32_t length = list.size(); + writeInt(length); + for (auto value : list) { + writeLong(value); + } + } + + inline list readLongList() { + int32_t length = readInt(); + list list; + for (auto i = 0; i < length; i++) { + list.emplace_back(readLong()); + } + return list; + } + + inline void writeLongSet(const set &set) { + if (set.empty()) { + writeByte(0); + return; + } + int32_t length = set.size(); + writeInt(length); + for (auto value : set) { + writeLong(value); + } + } + + inline set readLongSet() { + int32_t length = readInt(); + set set; + for (auto i = 0; i < length; i++) { + set.emplace(readLong()); + } + return set; + } + + //---------------------------------float-------------------------------------- + inline void writeFloatArray(const vector &array) { + if (array.empty()) { + writeByte(0); + return; + } + int32_t length = array.size(); + writeInt(length); + for (auto value : array) { + writeFloat(value); + } + } + + inline vector readFloatArray() { + int32_t length = readInt(); + vector array; + for (auto i = 0; i < length; i++) { + array.emplace_back(readFloat()); + } + return array; + } + + inline void writeFloatList(const list &list) { + if (list.empty()) { + writeByte(0); + return; + } + int32_t length = list.size(); + writeInt(length); + for (auto value : list) { + writeFloat(value); + } + } + + inline list readFloatList() { + int32_t length = readInt(); + list list; + for (auto i = 0; i < length; i++) { + list.emplace_back(readFloat()); + } + return list; + } + + inline void writeFloatSet(const set &list) { + if (list.empty()) { + writeByte(0); + return; + } + int32_t length = list.size(); + writeInt(length); + for (auto value : list) { + writeFloat(value); + } + } + + inline set readFloatSet() { + int32_t length = readInt(); + set set; + for (auto i = 0; i < length; i++) { + set.emplace(readFloat()); + } + return set; + } + + //---------------------------------double-------------------------------------- + inline void writeDoubleArray(const vector &array) { + if (array.empty()) { + writeByte(0); + return; + } + int32_t length = array.size(); + writeInt(length); + for (auto value : array) { + writeDouble(value); + } + } + + inline vector readDoubleArray() { + int32_t length = readInt(); + vector array; + for (auto i = 0; i < length; i++) { + array.emplace_back(readDouble()); + } + return array; + } + + inline void writeDoubleList(const list &list) { + if (list.empty()) { + writeByte(0); + return; + } + int32_t length = list.size(); + writeInt(length); + for (auto value : list) { + writeDouble(value); + } + } + + inline list readDoubleList() { + int32_t length = readInt(); + list list; + for (auto i = 0; i < length; i++) { + list.emplace_back(readDouble()); + } + return list; + } + + inline void writeDoubleSet(const set &set) { + if (set.empty()) { + writeByte(0); + return; + } + int32_t length = set.size(); + writeInt(length); + for (auto value : set) { + writeDouble(value); + } + } + + inline set readDoubleSet() { + int32_t length = readInt(); + set set; + for (auto i = 0; i < length; i++) { + set.emplace(readDouble()); + } + return set; + } + + //---------------------------------char-------------------------------------- + inline void writeCharArray(const vector &array) { + if (array.empty()) { + writeByte(0); + return; + } + int32_t length = array.size(); + writeInt(length); + for (auto value : array) { + writeChar(value); + } + } + + inline vector readCharArray() { + int32_t length = readInt(); + vector array; + for (auto i = 0; i < length; i++) { + array.emplace_back(readChar()); + } + return array; + } + + inline void writeCharList(const list &list) { + if (list.empty()) { + writeByte(0); + return; + } + int32_t length = list.size(); + writeInt(length); + for (auto value : list) { + writeChar(value); + } + } + + inline list readCharList() { + int32_t length = readInt(); + list list; + for (auto i = 0; i < length; i++) { + list.emplace_back(readChar()); + } + return list; + } + + inline void writeCharSet(const set &set) { + if (set.empty()) { + writeByte(0); + return; + } + int32_t length = set.size(); + writeInt(length); + for (auto value : set) { + writeChar(value); + } + } + + inline set readCharSet() { + int32_t length = readInt(); + set set; + for (auto i = 0; i < length; i++) { + set.emplace(readChar()); + } + return set; + } + + //---------------------------------string-------------------------------------- + inline void writeStringArray(const vector &array) { + if (array.empty()) { + writeByte(0); + return; + } + int32_t length = array.size(); + writeInt(length); + for (auto value : array) { + writeString(value); + } + } + + inline vector readStringArray() { + int32_t length = readInt(); + vector array; + for (auto i = 0; i < length; i++) { + array.emplace_back(readString()); + } + return array; + } + + inline void writeStringList(const list &list) { + if (list.empty()) { + writeByte(0); + return; + } + int32_t length = list.size(); + writeInt(length); + for (auto value : list) { + writeString(value); + } + } + + inline list readStringList() { + int32_t length = readInt(); + list list; + for (auto i = 0; i < length; i++) { + list.emplace_back(readString()); + } + return list; + } + + inline void writeStringSet(const set &set) { + if (set.empty()) { + writeByte(0); + return; + } + int32_t length = set.size(); + writeInt(length); + for (auto value : set) { + writeString(value); + } + } + + inline set readStringSet() { + int32_t length = readInt(); + set set; + for (auto i = 0; i < length; i++) { + set.emplace(readString()); + } + return set; + } + + inline void writeIntIntMap(const map &map) { + if (map.empty()) { + writeByte(0); + return; + } + writeInt(map.size()); + for (const auto&[key, value] : map) { + writeInt(key); + writeInt(value); + } + } + + inline map readIntIntMap() { + int32_t length = readInt(); + map map; + for (auto i = 0; i < length; i++) { + auto key = readInt(); + auto value = readInt(); + map.insert(pair(key, value)); + } + return map; + } + + inline void writeIntLongMap(const map &map) { + if (map.empty()) { + writeByte(0); + return; + } + writeInt(map.size()); + for (const auto&[key, value] : map) { + writeInt(key); + writeLong(value); + } + } + + inline map readIntLongMap() { + int32_t length = readInt(); + map map; + for (auto i = 0; i < length; i++) { + auto key = readInt(); + auto value = readLong(); + map.insert(pair(key, value)); + } + return map; + } + + inline void writeIntStringMap(const map &map) { + if (map.empty()) { + writeByte(0); + return; + } + writeInt(map.size()); + for (const auto&[key, value] : map) { + writeInt(key); + writeString(value); + } + } + + inline map readIntStringMap() { + int32_t length = readInt(); + map map; + for (auto i = 0; i < length; i++) { + auto key = readInt(); + auto value = readString(); + map.insert(pair(key, value)); + } + return map; + } + + template + inline void writeIntPacketMap(const map &map, const int16_t &protocolId) { + if (map.empty()) { + writeByte(0); + return; + } + writeInt(map.size()); + for (const auto&[key, value] : map) { + writeInt(key); + writePacket((IPacket *) &value, protocolId); + } + } + + template + inline map readIntPacketMap(const int16_t &protocolId) { + int32_t length = readInt(); + map map; + for (auto i = 0; i < length; i++) { + auto key = readInt(); + auto value = readPacket(protocolId); + auto *p = (T *) value.get(); + map.insert(pair(key, *p)); + } + return map; + } + + inline void writeLongIntMap(const map &map) { + if (map.empty()) { + writeByte(0); + return; + } + writeInt(map.size()); + for (const auto&[key, value] : map) { + writeLong(key); + writeInt(value); + } + } + + inline map readLongIntMap() { + int32_t length = readInt(); + map map; + for (auto i = 0; i < length; i++) { + auto key = readLong(); + auto value = readInt(); + map.insert(pair(key, value)); + } + return map; + } + + inline void writeLongLongMap(const map &map) { + if (map.empty()) { + writeByte(0); + return; + } + writeInt(map.size()); + for (const auto&[key, value] : map) { + writeLong(key); + writeLong(value); + } + } + + inline map readLongLongMap() { + int32_t length = readInt(); + map map; + for (auto i = 0; i < length; i++) { + auto key = readLong(); + auto value = readLong(); + map.insert(pair(key, value)); + } + return map; + } + + inline void writeLongStringMap(const map &map) { + if (map.empty()) { + writeByte(0); + return; + } + writeInt(map.size()); + for (const auto&[key, value] : map) { + writeLong(key); + writeString(value); + } + } + + inline map readLongStringMap() { + int32_t length = readInt(); + map map; + for (auto i = 0; i < length; i++) { + auto key = readLong(); + auto value = readString(); + map.insert(pair(key, value)); + } + return map; + } + + template + inline void writeLongPacketMap(const map &map, const int16_t &protocolId) { + if (map.empty()) { + writeByte(0); + return; + } + writeInt(map.size()); + for (const auto&[key, value] : map) { + writeLong(key); + writePacket((IPacket *) &value, protocolId); + } + } + + template + inline map readLongPacketMap(const int16_t &protocolId) { + int32_t length = readInt(); + map map; + for (auto i = 0; i < length; i++) { + auto key = readLong(); + auto value = readPacket(protocolId); + auto *p = (T *) value.get(); + map.insert(pair(key, *p)); + } + return map; + } + + inline void writeStringIntMap(const map &map) { + if (map.empty()) { + writeByte(0); + return; + } + writeInt(map.size()); + for (const auto&[key, value] : map) { + writeString(key); + writeInt(value); + } + } + + inline map readStringIntMap() { + int32_t length = readInt(); + map map; + for (auto i = 0; i < length; i++) { + auto key = readString(); + auto value = readInt(); + map.insert(pair(key, value)); + } + return map; + } + + inline void writeStringLongMap(const map &map) { + if (map.empty()) { + writeByte(0); + return; + } + writeInt(map.size()); + for (const auto&[key, value] : map) { + writeString(key); + writeLong(value); + } + } + + inline map readStringLongMap() { + int32_t length = readInt(); + map map; + for (auto i = 0; i < length; i++) { + auto key = readString(); + auto value = readLong(); + map.insert(pair(key, value)); + } + return map; + } + + inline void writeStringStringMap(const map &map) { + if (map.empty()) { + writeByte(0); + return; + } + writeInt(map.size()); + for (const auto&[key, value] : map) { + writeString(key); + writeString(value); + } + } + + inline map readStringStringMap() { + int32_t length = readInt(); + map map; + for (auto i = 0; i < length; i++) { + auto key = readString(); + auto value = readString(); + map.insert(pair(key, value)); + } + return map; + } + + template + inline void writeStringPacketMap(const map &map, const int16_t &protocolId) { + if (map.empty()) { + writeByte(0); + return; + } + writeInt(map.size()); + for (const auto&[key, value] : map) { + writeString(key); + writePacket((IPacket *) &value, protocolId); + } + } + + template + inline map readStringPacketMap(const int16_t &protocolId) { + int32_t length = readInt(); + map map; + for (auto i = 0; i < length; i++) { + auto key = readString(); + auto value = readPacket(protocolId); + auto *p = (T *) value.get(); + map.insert(pair(key, *p)); + } + return map; + } + + + //---------------------------------packet-------------------------------------- + template + inline void writePacketArray(const vector &array, const int16_t &protocolId) { + if (array.empty()) { + writeByte(0); + return; + } + int32_t length = array.size(); + writeInt(length); + for (auto value : array) { + writePacket((IPacket *) &value, protocolId); + } + } + + template + inline vector readPacketArray(const int16_t &protocolId) { + int32_t length = readInt(); + vector array; + for (auto i = 0; i < length; i++) { + auto value = readPacket(protocolId); + auto *p = (T *) value.get(); + array.emplace_back(*p); + } + return array; + } + + template + inline void writePacketList(const list &list, const int16_t &protocolId) { + if (list.empty()) { + writeByte(0); + return; + } + int32_t length = list.size(); + writeInt(length); + for (auto value : list) { + writePacket((IPacket *) &value, protocolId); + } + } + + template + inline list readPacketList(const int16_t &protocolId) { + int32_t length = readInt(); + list list; + for (auto i = 0; i < length; i++) { + auto value = readPacket(protocolId); + auto *p = (T *) value.get(); + list.emplace_back(*p); + } + return list; + } + + template + inline void writePacketSet(const set &set, const int16_t &protocolId) { + if (set.empty()) { + writeByte(0); + return; + } + int32_t length = set.size(); + writeInt(length); + for (auto value : set) { + writePacket((IPacket *) &value, protocolId); + } + } + + template + inline set readPacketSet(const int16_t &protocolId) { + int32_t length = readInt(); + set set; + for (auto i = 0; i < length; i++) { + auto value = readPacket(protocolId); + auto *p = (T *) value.get(); + set.emplace(*p); + } + return set; + } + + private: + template + inline void write(T value) { + ensureCapacity(sizeof(T)); + // MSDN: The htons function converts a u_short from host to TCP/IP network byte order (which is big-endian). + // ** This mean the network byte order is big-endian ** + if (IS_LITTLE_ENDIAN) { + swap_bytes(reinterpret_cast(&value)); + } + memcpy(&m_buffer[m_writerIndex], (int8_t *) &value, sizeof(T)); + m_writerIndex += sizeof(T); + } + + template + inline T read() { + T value = *((T *) &m_buffer[m_readerIndex]); + if (IS_LITTLE_ENDIAN) { + swap_bytes(reinterpret_cast(&value)); + } + m_readerIndex += sizeof(T); + return value; + } + + // Swaps the order of bytes for some chunk of memory + template + inline void swap_bytes(int8_t *data) { + for (std::size_t i = 0, end = DataSize / 2; i < end; ++i) { + std::swap(data[i], data[DataSize - i - 1]); + } + } + }; + +} + +#endif diff --git a/protocol/src/main/resources/cpp/PacketTemplate.h b/protocol/src/main/resources/cpp/PacketTemplate.h new file mode 100644 index 00000000..42ebc3ae --- /dev/null +++ b/protocol/src/main/resources/cpp/PacketTemplate.h @@ -0,0 +1,57 @@ +#ifndef ZFOO_{}_H +#define ZFOO_{}_H + +#include "zfoocpp/ByteBuffer.h" +{} +namespace zfoo { + + {} + class {} : public IPacket { + public: + {} + + ~{}() override = default; + + static {} valueOf({}) { + auto packet = {}(); + {} + return packet; + } + + int16_t protocolId() override { + return {}; + } + + bool operator<(const {} &_) const { + {} + return false; + } + }; + + + class {}Registration : public IProtocolRegistration { + public: + int16_t protocolId() override { + return {}; + } + + void write(ByteBuffer &buffer, IPacket *packet) override { + if (buffer.writePacketFlag(packet)) { + return; + } + auto *message = ({} *) packet; + {} + } + + IPacket *read(ByteBuffer &buffer) override { + auto *packet = new {}(); + if (!buffer.readBool()) { + return packet; + } + {} + return packet; + } + }; +} + +#endif diff --git a/protocol/src/main/resources/cpp/ProtocolManagerTemplate.h b/protocol/src/main/resources/cpp/ProtocolManagerTemplate.h new file mode 100644 index 00000000..a435f04e --- /dev/null +++ b/protocol/src/main/resources/cpp/ProtocolManagerTemplate.h @@ -0,0 +1,33 @@ +#ifndef ZFOO_PROTOCOLMANAGER_H +#define ZFOO_PROTOCOLMANAGER_H + +#include "ByteBuffer.h" +{} +namespace zfoo { + + const int16_t MAX_PROTOCOL_NUM = 32767; + const IProtocolRegistration *protocols[MAX_PROTOCOL_NUM]; + + void initProtocol() { + {} + } + + inline IProtocolRegistration *getProtocol(int16_t protocolId) { + return const_cast(protocols[protocolId]); + } + + void write(ByteBuffer &buffer, IPacket *packet) { + auto protocolId = packet->protocolId(); + // 写入协议号 + buffer.writeShort(protocolId); + // 写入包体 + getProtocol(protocolId)->write(buffer, packet); + } + + IPacket *read(ByteBuffer &buffer) { + auto protocolId = buffer.readShort(); + return getProtocol(protocolId)->read(buffer); + } + +} +#endif