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 a1525f4c..3faf8a17 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java @@ -110,7 +110,9 @@ public abstract class GenerateProtocolFile { if (generateLanguages.contains(CodeLanguage.CSharp)) { GenerateCsUtils.init(generateOperation); GenerateCsUtils.createProtocolManager(); - allSortedGenerateProtocols.forEach(it -> GenerateCsUtils.createCsProtocolFile((ProtocolRegistration) it)); + for (var protocolRegistration : allSortedGenerateProtocols) { + GenerateCsUtils.createCsProtocolFile((ProtocolRegistration) protocolRegistration); + } } // 生成Javascript协议 diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java index c36b2133..4cc3aea3 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java @@ -130,7 +130,7 @@ public abstract class GenerateCppUtils { var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName(); - var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("cpp/PacketTemplate.h"))); + var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("cpp/ProtocolTemplate.h"))); // protocol object var defineProtocolName = protocolClazzName.toUpperCase(); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cs/GenerateCsUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cs/GenerateCsUtils.java index 2b1e8b82..658fa334 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cs/GenerateCsUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cs/GenerateCsUtils.java @@ -19,15 +19,14 @@ import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.generate.GenerateProtocolPath; import com.zfoo.protocol.model.Pair; import com.zfoo.protocol.registration.ProtocolRegistration; -import com.zfoo.protocol.registration.field.IFieldRegistration; 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; @@ -80,16 +79,16 @@ public abstract class GenerateCsUtils { * 生成协议依赖的工具类 */ public static void createProtocolManager() throws IOException { - var list = List.of("cs/ProtocolManager.cs" - , "cs/IProtocolRegistration.cs" - , "cs/IPacket.cs" - , "cs/Buffer/ByteBuffer.cs" - , "cs/Buffer/LittleEndianByteBuffer.cs" - , "cs/Buffer/BigEndianByteBuffer.cs"); + var list = List.of("csharp/ProtocolManager.cs" + , "csharp/IProtocolRegistration.cs" + , "csharp/IPacket.cs" + , "csharp/Buffer/ByteBuffer.cs" + , "csharp/Buffer/LittleEndianByteBuffer.cs" + , "csharp/Buffer/BigEndianByteBuffer.cs"); for (var fileName : list) { var fileInputStream = ClassUtils.getFileFromClassPath(fileName); - var createFile = new File(StringUtils.format("{}/{}", protocolOutputRootPath, StringUtils.substringAfterFirst(fileName, "cs/"))); + var createFile = new File(StringUtils.format("{}/{}", protocolOutputRootPath, StringUtils.substringAfterFirst(fileName, "csharp/"))); FileUtils.writeInputStreamToFile(createFile, fileInputStream); } } @@ -97,48 +96,111 @@ public abstract class GenerateCsUtils { /** * 生成协议类 */ - public static void createCsProtocolFile(ProtocolRegistration registration) { + public static void createCsProtocolFile(ProtocolRegistration registration) throws IOException { GenerateProtocolFile.index.set(0); var protocolId = registration.protocolId(); var registrationConstructor = registration.getConstructor(); - IFieldRegistration[] fieldRegistrations = registration.getFieldRegistrations(); - var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName(); - var csBuilder = new StringBuilder(); - csBuilder.append("using System;").append(LS); - csBuilder.append("using System.Collections.Generic;").append(LS); - csBuilder.append("using CsProtocol.Buffer;").append(LS).append(LS); - csBuilder.append("namespace CsProtocol").append(LS); - csBuilder.append("{").append(LS); + var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("csharp/ProtocolTemplate.cs"))); - - // protocol object - csBuilder.append(protocolClass(registration)); - - csBuilder.append(TAB).append(StringUtils.format("public class {}Registration : IProtocolRegistration", protocolClazzName)).append(LS); - csBuilder.append(TAB).append("{").append(LS); - - // ProtocolId method - csBuilder.append(packetProtocolId(registration)); - - // writeObject method - csBuilder.append(writeObject(registration)); - - // readObject method - csBuilder.append(readObject(registration)); - - csBuilder.append(TAB).append("}").append(LS); - csBuilder.append("}").append(LS); + var docTitle = docTitle(registration); + var fieldDefinition = fieldDefinition(registration); + var valueOfMethod = valueOfMethod(registration); + var writeObject = writeObject(registration); + var readObject = readObject(registration); + protocolTemplate = StringUtils.format(protocolTemplate, docTitle, protocolClazzName, fieldDefinition.trim() + , protocolClazzName, valueOfMethod.getKey().trim(), protocolClazzName, valueOfMethod.getValue().trim() + , protocolId, protocolClazzName, protocolId, protocolClazzName, protocolClazzName,writeObject.trim() + , protocolClazzName, protocolClazzName, readObject.trim()); var protocolOutputPath = StringUtils.format("{}/{}/{}.cs" , protocolOutputRootPath , GenerateProtocolPath.getCapitalizeProtocolPath(protocolId) , protocolClazzName); - FileUtils.writeStringToFile(new File(protocolOutputPath), csBuilder.toString()); + FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate); } + private static String docTitle(ProtocolRegistration registration) { + var protocolId = registration.getId(); + var protocolDocument = GenerateProtocolDocument.getProtocolDocument(protocolId); + var docTitle = protocolDocument.getKey(); + + var csBuilder = new StringBuilder(); + if (StringUtils.isNotBlank(docTitle)) { + Arrays.stream(docTitle.split(LS)).forEach(it -> csBuilder.append(TAB).append(it).append(LS)); + } + return csBuilder.toString().trim(); + } + + private static String fieldDefinition(ProtocolRegistration registration) { + var protocolId = registration.getId(); + var fields = registration.getFields(); + var protocolDocument = GenerateProtocolDocument.getProtocolDocument(protocolId); + var docFieldMap = protocolDocument.getValue(); + + var csBuilder = new StringBuilder(); + // 协议的属性生成 + for (var field : fields) { + var propertyType = toCsClassName(field.getGenericType().getTypeName()); + var propertyName = field.getName(); + var propertyFullName = StringUtils.format("public {} {};", propertyType, propertyName); + // 生成注释 + var doc = docFieldMap.get(propertyName); + if (StringUtils.isNotBlank(doc)) { + Arrays.stream(doc.split(LS)).forEach(it -> csBuilder.append(TAB + TAB).append(it).append(LS)); + } + csBuilder.append(TAB + TAB).append(propertyFullName).append(LS); + } + return csBuilder.toString(); + } + + private static Pair valueOfMethod(ProtocolRegistration registration) { + var fields = registration.getFields(); + var filedList = new ArrayList>(); + for (var field : fields) { + var propertyType = toCsClassName(field.getGenericType().getTypeName()); + var propertyName = field.getName(); + filedList.add(new Pair<>(propertyType, propertyName)); + } + + // 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 csBuilder = new StringBuilder(); + filedList.forEach(it -> csBuilder.append(TAB + TAB + TAB).append(StringUtils.format("packet.{} = {};", it.getValue(), it.getValue())).append(LS)); + return new Pair<>(valueOfParamsStr, csBuilder.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]; + csSerializer(fieldRegistration.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]; + String readObject = csSerializer(fieldRegistration.serializer()).readObject(csBuilder, 3, field, fieldRegistration); + csBuilder.append(TAB + TAB + TAB) + .append(StringUtils.format("packet.{} = {};", field.getName(), readObject)) + .append(LS); + } + return csBuilder.toString(); + } public static String toCsClassName(String typeName) { typeName = typeName.replaceAll("java.util.|java.lang.", StringUtils.EMPTY); @@ -244,144 +306,4 @@ public abstract class GenerateCsUtils { return typeName; } - - private static String protocolClass(ProtocolRegistration registration) { - short protocolId = registration.getId(); - Field[] fields = registration.getFields(); - var protocolClazzName = registration.getConstructor().getDeclaringClass().getSimpleName(); - - var protocolDocument = GenerateProtocolDocument.getProtocolDocument(protocolId); - var docTitle = protocolDocument.getKey(); - var docFieldMap = protocolDocument.getValue(); - - var csBuilder = new StringBuilder(); - if (StringUtils.isNotBlank(docTitle)) { - Arrays.stream(docTitle.split(LS)).forEach(it -> csBuilder.append(TAB).append(it).append(LS)); - } - csBuilder.append(TAB) - .append(StringUtils.format("public class {} : IPacket", protocolClazzName)) - .append(LS); - csBuilder.append(TAB).append("{").append(LS); - - // 协议的属性生成 - var filedList = new ArrayList>(); - for (var field : fields) { - var propertyType = toCsClassName(field.getGenericType().getTypeName()); - var propertyName = field.getName(); - - var propertyFullName = StringUtils.format("public {} {};", propertyType, propertyName); - // 生成注释 - var doc = docFieldMap.get(propertyName); - if (StringUtils.isNotBlank(doc)) { - Arrays.stream(doc.split(LS)).forEach(it -> csBuilder.append(TAB + TAB).append(it).append(LS)); - } - - csBuilder.append(TAB + TAB).append(propertyFullName).append(LS); - filedList.add(new Pair<>(propertyType, propertyName)); - } - - csBuilder.append(LS); - - // ValueOf()方法 - var valueOfParams = filedList.stream() - .map(it -> StringUtils.format("{} {}", it.getKey(), it.getValue())) - .collect(Collectors.toList()); - - csBuilder.append(TAB + TAB) - .append(StringUtils.format("public static {} ValueOf({})", protocolClazzName, StringUtils.joinWith(StringUtils.COMMA + " ", valueOfParams.toArray()))) - .append(LS); - - csBuilder.append(TAB + TAB).append("{").append(LS); - csBuilder.append(TAB + TAB + TAB) - .append(StringUtils.format("var packet = new {}();", protocolClazzName)) - .append(LS); - filedList.forEach(it -> csBuilder.append(TAB + TAB + TAB).append(StringUtils.format("packet.{} = {};", it.getValue(), it.getValue())).append(LS)); - csBuilder.append(TAB + TAB + TAB).append("return packet;").append(LS); - csBuilder.append(TAB + TAB).append("}").append(LS); - csBuilder.append(LS).append(LS); - - // ProtocolId()方法 - csBuilder.append(TAB + TAB).append("public short ProtocolId()").append(LS); - csBuilder.append(TAB + TAB).append("{").append(LS); - csBuilder.append(TAB + TAB + TAB).append(StringUtils.format("return {};", registration.protocolId())).append(LS); - csBuilder.append(TAB + TAB).append("}").append(LS); - csBuilder.append(TAB).append("}").append(LS); - csBuilder.append(LS).append(LS); - - return csBuilder.toString(); - } - - private static String packetProtocolId(ProtocolRegistration registration) { - var csBuilder = new StringBuilder(); - csBuilder.append(TAB + TAB).append("public short ProtocolId()").append(LS); - csBuilder.append(TAB + TAB).append("{").append(LS); - csBuilder.append(TAB + TAB + TAB).append(StringUtils.format("return {};", registration.protocolId())).append(LS); - csBuilder.append(TAB + TAB).append("}"); - csBuilder.append(LS).append(LS); - return csBuilder.toString(); - } - - private static String writeObject(ProtocolRegistration registration) { - Field[] fields = registration.getFields(); - IFieldRegistration[] fieldRegistrations = registration.getFieldRegistrations(); - var protocolClazzName = registration.getConstructor().getDeclaringClass().getSimpleName(); - - var csBuilder = new StringBuilder(); - csBuilder.append(TAB + TAB).append("public void Write(ByteBuffer buffer, IPacket packet)").append(LS); - csBuilder.append(TAB + TAB).append("{").append(LS); - csBuilder.append(TAB + TAB + TAB).append("if (buffer.WritePacketFlag(packet))").append(LS); - csBuilder.append(TAB + TAB + TAB).append("{").append(LS); - csBuilder.append(TAB + TAB + TAB + TAB).append("return;").append(LS); - csBuilder.append(TAB + TAB + TAB + "}").append(LS); - - csBuilder.append(TAB + TAB + TAB) - .append(StringUtils.format("{} message = ({}) packet;", protocolClazzName, protocolClazzName)) - .append(LS); - for (int i = 0; i < fields.length; i++) { - Field field = fields[i]; - IFieldRegistration fieldRegistration = fieldRegistrations[i]; - - csSerializer(fieldRegistration.serializer()).writeObject(csBuilder, "message." + field.getName(), 3, field, fieldRegistration); - } - - csBuilder.append(TAB + TAB + "}").append(LS).append(LS); - return csBuilder.toString(); - } - - - private static String readObject(ProtocolRegistration registration) { - Field[] fields = registration.getFields(); - IFieldRegistration[] fieldRegistrations = registration.getFieldRegistrations(); - var protocolClazzName = registration.getConstructor().getDeclaringClass().getSimpleName(); - - var csBuilder = new StringBuilder(); - csBuilder.append(TAB + TAB).append("public IPacket Read(ByteBuffer buffer)").append(LS); - csBuilder.append(TAB + TAB).append("{").append(LS); - csBuilder.append(TAB + TAB + TAB).append("if (!buffer.ReadBool())").append(LS); - csBuilder.append(TAB + TAB + TAB).append("{").append(LS); - csBuilder.append(TAB + TAB + TAB + TAB).append("return null;").append(LS); - csBuilder.append(TAB + TAB + TAB).append("}").append(LS); - - csBuilder.append(TAB + TAB + TAB) - .append(StringUtils.format("{} packet = new {}();", protocolClazzName, protocolClazzName)) - .append(LS); - - for (int i = 0; i < fields.length; i++) { - Field field = fields[i]; - IFieldRegistration fieldRegistration = fieldRegistrations[i]; - - String readObject = csSerializer(fieldRegistration.serializer()).readObject(csBuilder, 3, field, fieldRegistration); - csBuilder.append(TAB + TAB + TAB) - .append(StringUtils.format("packet.{} = {};", field.getName(), readObject)) - .append(LS); - } - - csBuilder.append(TAB + TAB + TAB).append("return packet;").append(LS); - - csBuilder.append(TAB + TAB).append("}").append(LS); - - return csBuilder.toString(); - } - - } diff --git a/protocol/src/main/resources/cpp/PacketTemplate.h b/protocol/src/main/resources/cpp/ProtocolTemplate.h similarity index 100% rename from protocol/src/main/resources/cpp/PacketTemplate.h rename to protocol/src/main/resources/cpp/ProtocolTemplate.h diff --git a/protocol/src/main/resources/cs/Buffer/BigEndianByteBuffer.cs b/protocol/src/main/resources/csharp/Buffer/BigEndianByteBuffer.cs similarity index 100% rename from protocol/src/main/resources/cs/Buffer/BigEndianByteBuffer.cs rename to protocol/src/main/resources/csharp/Buffer/BigEndianByteBuffer.cs diff --git a/protocol/src/main/resources/cs/Buffer/ByteBuffer.cs b/protocol/src/main/resources/csharp/Buffer/ByteBuffer.cs similarity index 99% rename from protocol/src/main/resources/cs/Buffer/ByteBuffer.cs rename to protocol/src/main/resources/csharp/Buffer/ByteBuffer.cs index 4687c01b..473b6040 100644 --- a/protocol/src/main/resources/cs/Buffer/ByteBuffer.cs +++ b/protocol/src/main/resources/csharp/Buffer/ByteBuffer.cs @@ -566,18 +566,6 @@ namespace CsProtocol.Buffer return flag; } - public void WritePacket(T packet, short protocolId) - { - IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); - protocolRegistration.Write(this, (IPacket) packet); - } - - public T ReadPacket(short protocolId) - { - IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); - return (T) protocolRegistration.Read(this); - } - public void WriteBooleanArray(bool[] array) { if ((array == null) || (array.Length == 0)) @@ -1919,5 +1907,17 @@ namespace CsProtocol.Buffer return map; } + + public void WritePacket(T packet, short protocolId) + { + IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); + protocolRegistration.Write(this, (IPacket) packet); + } + + public T ReadPacket(short protocolId) + { + IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); + return (T) protocolRegistration.Read(this); + } } -} +} \ No newline at end of file diff --git a/protocol/src/main/resources/cs/Buffer/LittleEndianByteBuffer.cs b/protocol/src/main/resources/csharp/Buffer/LittleEndianByteBuffer.cs similarity index 100% rename from protocol/src/main/resources/cs/Buffer/LittleEndianByteBuffer.cs rename to protocol/src/main/resources/csharp/Buffer/LittleEndianByteBuffer.cs diff --git a/protocol/src/main/resources/cs/IPacket.cs b/protocol/src/main/resources/csharp/IPacket.cs similarity index 100% rename from protocol/src/main/resources/cs/IPacket.cs rename to protocol/src/main/resources/csharp/IPacket.cs diff --git a/protocol/src/main/resources/cs/IProtocolRegistration.cs b/protocol/src/main/resources/csharp/IProtocolRegistration.cs similarity index 100% rename from protocol/src/main/resources/cs/IProtocolRegistration.cs rename to protocol/src/main/resources/csharp/IProtocolRegistration.cs diff --git a/protocol/src/main/resources/cs/ProtocolManager.cs b/protocol/src/main/resources/csharp/ProtocolManager.cs similarity index 100% rename from protocol/src/main/resources/cs/ProtocolManager.cs rename to protocol/src/main/resources/csharp/ProtocolManager.cs diff --git a/protocol/src/main/resources/csharp/ProtocolTemplate.cs b/protocol/src/main/resources/csharp/ProtocolTemplate.cs new file mode 100644 index 00000000..16103c9d --- /dev/null +++ b/protocol/src/main/resources/csharp/ProtocolTemplate.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using CsProtocol.Buffer; + +namespace CsProtocol +{ + {} + public class {} : IPacket + { + {} + + public static {} ValueOf({}) + { + var packet = new {}(); + {} + return packet; + } + + + public short ProtocolId() + { + return {}; + } + } + + + public class {}Registration : IProtocolRegistration + { + public short ProtocolId() + { + return {}; + } + + public void Write(ByteBuffer buffer, IPacket packet) + { + if (buffer.WritePacketFlag(packet)) + { + return; + } + {} message = ({}) packet; + {} + } + + public IPacket Read(ByteBuffer buffer) + { + if (!buffer.ReadBool()) + { + return null; + } + {} packet = new {}(); + {} + return packet; + } + } +} diff --git a/protocol/src/test/resources/csTest/CsProtocol/Buffer/BigEndianByteBuffer.cs b/protocol/src/test/csharp/CsProtocol/Buffer/BigEndianByteBuffer.cs similarity index 100% rename from protocol/src/test/resources/csTest/CsProtocol/Buffer/BigEndianByteBuffer.cs rename to protocol/src/test/csharp/CsProtocol/Buffer/BigEndianByteBuffer.cs diff --git a/protocol/src/test/resources/csTest/CsProtocol/Buffer/ByteBuffer.cs b/protocol/src/test/csharp/CsProtocol/Buffer/ByteBuffer.cs similarity index 98% rename from protocol/src/test/resources/csTest/CsProtocol/Buffer/ByteBuffer.cs rename to protocol/src/test/csharp/CsProtocol/Buffer/ByteBuffer.cs index 71609a34..473b6040 100644 --- a/protocol/src/test/resources/csTest/CsProtocol/Buffer/ByteBuffer.cs +++ b/protocol/src/test/csharp/CsProtocol/Buffer/ByteBuffer.cs @@ -751,7 +751,7 @@ namespace CsProtocol.Buffer { for (int index = 0; index < size; index++) { - array[index] = ReadLong(); + array[index] = ReadFloat(); } } @@ -783,7 +783,7 @@ namespace CsProtocol.Buffer { for (int index = 0; index < size; index++) { - array[index] = ReadLong(); + array[index] = ReadDouble(); } } @@ -1907,5 +1907,17 @@ namespace CsProtocol.Buffer return map; } + + public void WritePacket(T packet, short protocolId) + { + IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); + protocolRegistration.Write(this, (IPacket) packet); + } + + public T ReadPacket(short protocolId) + { + IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); + return (T) protocolRegistration.Read(this); + } } } \ No newline at end of file diff --git a/protocol/src/test/resources/csTest/CsProtocol/Buffer/LittleEndianByteBuffer.cs b/protocol/src/test/csharp/CsProtocol/Buffer/LittleEndianByteBuffer.cs similarity index 100% rename from protocol/src/test/resources/csTest/CsProtocol/Buffer/LittleEndianByteBuffer.cs rename to protocol/src/test/csharp/CsProtocol/Buffer/LittleEndianByteBuffer.cs diff --git a/protocol/src/test/resources/csTest/CsProtocol/IPacket.cs b/protocol/src/test/csharp/CsProtocol/IPacket.cs similarity index 100% rename from protocol/src/test/resources/csTest/CsProtocol/IPacket.cs rename to protocol/src/test/csharp/CsProtocol/IPacket.cs diff --git a/protocol/src/test/resources/csTest/CsProtocol/IProtocolRegistration.cs b/protocol/src/test/csharp/CsProtocol/IProtocolRegistration.cs similarity index 100% rename from protocol/src/test/resources/csTest/CsProtocol/IProtocolRegistration.cs rename to protocol/src/test/csharp/CsProtocol/IProtocolRegistration.cs diff --git a/protocol/src/test/resources/csTest/CsProtocol/Packet/ComplexObject.cs b/protocol/src/test/csharp/CsProtocol/Packet/ComplexObject.cs similarity index 98% rename from protocol/src/test/resources/csTest/CsProtocol/Packet/ComplexObject.cs rename to protocol/src/test/csharp/CsProtocol/Packet/ComplexObject.cs index 9caab25e..0903be46 100644 --- a/protocol/src/test/resources/csTest/CsProtocol/Packet/ComplexObject.cs +++ b/protocol/src/test/csharp/CsProtocol/Packet/ComplexObject.cs @@ -180,7 +180,7 @@ namespace CsProtocol buffer.WriteCharArray(message.hhhh); buffer.WriteString(message.jj); buffer.WriteStringArray(message.jjj); - ProtocolManager.GetProtocol(102).Write(buffer, message.kk); + buffer.WritePacket(message.kk, 102); buffer.WritePacketArray(message.kkk, 102); buffer.WriteIntList(message.l); if (message.ll == null) @@ -252,7 +252,7 @@ namespace CsProtocol { var keyElement13 = i12.Key; var valueElement14 = i12.Value; - ProtocolManager.GetProtocol(102).Write(buffer, keyElement13); + buffer.WritePacket(keyElement13, 102); buffer.WriteIntList(valueElement14); } } @@ -475,7 +475,7 @@ namespace CsProtocol packet.jj = result70; var array71 = buffer.ReadStringArray(); packet.jjj = array71; - ObjectA result72 = (ObjectA) ProtocolManager.GetProtocol(102).Read(buffer); + ObjectA result72 = buffer.ReadPacket(102); packet.kk = result72; var array73 = buffer.ReadPacketArray(102); packet.kkk = array73; @@ -535,7 +535,7 @@ namespace CsProtocol { for (var index95 = 0; index95 < size94; index95++) { - ObjectA result96 = (ObjectA) ProtocolManager.GetProtocol(102).Read(buffer); + ObjectA result96 = buffer.ReadPacket(102); var list97 = buffer.ReadIntList(); result93[result96] = list97; } diff --git a/protocol/src/test/resources/csTest/CsProtocol/Packet/NormalObject.cs b/protocol/src/test/csharp/CsProtocol/Packet/NormalObject.cs similarity index 96% rename from protocol/src/test/resources/csTest/CsProtocol/Packet/NormalObject.cs rename to protocol/src/test/csharp/CsProtocol/Packet/NormalObject.cs index f4129f54..885cba52 100644 --- a/protocol/src/test/resources/csTest/CsProtocol/Packet/NormalObject.cs +++ b/protocol/src/test/csharp/CsProtocol/Packet/NormalObject.cs @@ -82,7 +82,7 @@ namespace CsProtocol buffer.WriteDouble(message.f); buffer.WriteBool(message.g); buffer.WriteString(message.jj); - ProtocolManager.GetProtocol(102).Write(buffer, message.kk); + buffer.WritePacket(message.kk, 102); buffer.WriteIntList(message.l); buffer.WriteLongList(message.ll); buffer.WritePacketList(message.lll, 102); @@ -118,7 +118,7 @@ namespace CsProtocol packet.g = result7; string result8 = buffer.ReadString(); packet.jj = result8; - ObjectA result9 = (ObjectA) ProtocolManager.GetProtocol(102).Read(buffer); + ObjectA result9 = buffer.ReadPacket(102); packet.kk = result9; var list10 = buffer.ReadIntList(); packet.l = list10; diff --git a/protocol/src/test/resources/csTest/CsProtocol/Packet/ObjectA.cs b/protocol/src/test/csharp/CsProtocol/Packet/ObjectA.cs similarity index 90% rename from protocol/src/test/resources/csTest/CsProtocol/Packet/ObjectA.cs rename to protocol/src/test/csharp/CsProtocol/Packet/ObjectA.cs index fe134d6f..3f0b9243 100644 --- a/protocol/src/test/resources/csTest/CsProtocol/Packet/ObjectA.cs +++ b/protocol/src/test/csharp/CsProtocol/Packet/ObjectA.cs @@ -45,7 +45,7 @@ namespace CsProtocol ObjectA message = (ObjectA) packet; buffer.WriteInt(message.a); buffer.WriteIntStringMap(message.m); - ProtocolManager.GetProtocol(103).Write(buffer, message.objectB); + buffer.WritePacket(message.objectB, 103); } public IPacket Read(ByteBuffer buffer) @@ -59,7 +59,7 @@ namespace CsProtocol packet.a = result0; var map1 = buffer.ReadIntStringMap(); packet.m = map1; - ObjectB result2 = (ObjectB) ProtocolManager.GetProtocol(103).Read(buffer); + ObjectB result2 = buffer.ReadPacket(103); packet.objectB = result2; return packet; } diff --git a/protocol/src/test/resources/csTest/CsProtocol/Packet/ObjectB.cs b/protocol/src/test/csharp/CsProtocol/Packet/ObjectB.cs similarity index 100% rename from protocol/src/test/resources/csTest/CsProtocol/Packet/ObjectB.cs rename to protocol/src/test/csharp/CsProtocol/Packet/ObjectB.cs diff --git a/protocol/src/test/resources/csTest/CsProtocol/Packet/SimpleObject.cs b/protocol/src/test/csharp/CsProtocol/Packet/SimpleObject.cs similarity index 100% rename from protocol/src/test/resources/csTest/CsProtocol/Packet/SimpleObject.cs rename to protocol/src/test/csharp/CsProtocol/Packet/SimpleObject.cs diff --git a/protocol/src/test/resources/csTest/CsProtocol/ProtocolManager.cs b/protocol/src/test/csharp/CsProtocol/ProtocolManager.cs similarity index 100% rename from protocol/src/test/resources/csTest/CsProtocol/ProtocolManager.cs rename to protocol/src/test/csharp/CsProtocol/ProtocolManager.cs diff --git a/protocol/src/test/resources/csTest/CsProtocolTest.cs b/protocol/src/test/csharp/Main.cs similarity index 90% rename from protocol/src/test/resources/csTest/CsProtocolTest.cs rename to protocol/src/test/csharp/Main.cs index 1027e43c..d566218f 100644 --- a/protocol/src/test/resources/csTest/CsProtocolTest.cs +++ b/protocol/src/test/csharp/Main.cs @@ -1,19 +1,19 @@ -using System; +using System; using System.IO; using CsProtocol; using CsProtocol.Buffer; -using NUnit.Framework; -namespace Test.Editor.Net +namespace csharp { - public class CsProtocolTest + class Program { - [Test] - public void ComplexObjectTest() + static void Main(string[] args) { + ByteBufferTest(); + ProtocolManager.InitProtocol(); // 获取复杂对象的字节流 - var complexObjectBytes = File.ReadAllBytes("D:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes"); + var complexObjectBytes = File.ReadAllBytes("C:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes"); var buffer = ByteBuffer.ValueOf(); buffer.WriteBytes(complexObjectBytes); var packet = ProtocolManager.Read(buffer); @@ -23,12 +23,9 @@ namespace Test.Editor.Net var bytes = newBuffer.ToBytes(); // set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的 - AssertEquals(complexObjectBytes, bytes); } - - - [Test] - public void ByteBufferTest() + + public static void ByteBufferTest() { byteTest(); bytesTest(); @@ -42,7 +39,7 @@ namespace Test.Editor.Net } - public void byteTest() + public static void byteTest() { byte value = 9; ByteBuffer writerByteBuffer = ByteBuffer.ValueOf(); @@ -55,7 +52,7 @@ namespace Test.Editor.Net AssertEquals(value, readValue); } - public void bytesTest() + public static void bytesTest() { var value = new byte[] {1, 2, 3}; ByteBuffer writerByteBuffer = ByteBuffer.ValueOf(); @@ -68,7 +65,7 @@ namespace Test.Editor.Net AssertEquals(value, readValue); } - public void shortTest() + public static void shortTest() { short value = 9999; ByteBuffer writerByteBuffer = ByteBuffer.ValueOf(); @@ -81,7 +78,7 @@ namespace Test.Editor.Net AssertEquals(value, readValue); } - public void intTest() + public static void intTest() { int value = 99999999; ByteBuffer writerByteBuffer = ByteBuffer.ValueOf(); @@ -94,7 +91,7 @@ namespace Test.Editor.Net AssertEquals(value, readValue); } - public void longTest() + public static void longTest() { long value = 9999999999999999L; ByteBuffer writerByteBuffer = ByteBuffer.ValueOf(); @@ -107,7 +104,7 @@ namespace Test.Editor.Net AssertEquals(value, readValue); } - public void floatTest() + public static void floatTest() { float value = 999999.56F; ByteBuffer writerByteBuffer = ByteBuffer.ValueOf(); @@ -120,7 +117,7 @@ namespace Test.Editor.Net AssertEquals(value, readValue); } - public void doubleTest() + public static void doubleTest() { double value = 999999.56; ByteBuffer writerByteBuffer = ByteBuffer.ValueOf(); @@ -133,7 +130,7 @@ namespace Test.Editor.Net AssertEquals(value, readValue); } - public void charTest() + public static void charTest() { char value = 'a'; ByteBuffer writerByteBuffer = ByteBuffer.ValueOf(); @@ -146,7 +143,7 @@ namespace Test.Editor.Net AssertEquals(value, readValue); } - public void stringTest() + public static void stringTest() { string value = "aaa"; ByteBuffer writerByteBuffer = ByteBuffer.ValueOf(); diff --git a/protocol/src/test/csharp/csharp.sln b/protocol/src/test/csharp/csharp.sln new file mode 100644 index 00000000..4f7dd79c --- /dev/null +++ b/protocol/src/test/csharp/csharp.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "csharp", "csharp.csproj", "{A80D8031-35D0-4F9C-83C5-054D48240447}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A80D8031-35D0-4F9C-83C5-054D48240447}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A80D8031-35D0-4F9C-83C5-054D48240447}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A80D8031-35D0-4F9C-83C5-054D48240447}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A80D8031-35D0-4F9C-83C5-054D48240447}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal