mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-18 19:26:42 +00:00
perf[protocol]: 使用模板生成协议
This commit is contained in:
@@ -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协议
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<String, String> valueOfMethod(ProtocolRegistration registration) {
|
||||
var fields = registration.getFields();
|
||||
var filedList = new ArrayList<Pair<String, String>>();
|
||||
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<Pair<String, String>>();
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+13
-13
@@ -566,18 +566,6 @@ namespace CsProtocol.Buffer
|
||||
return flag;
|
||||
}
|
||||
|
||||
public void WritePacket<T>(T packet, short protocolId)
|
||||
{
|
||||
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
|
||||
protocolRegistration.Write(this, (IPacket) packet);
|
||||
}
|
||||
|
||||
public T ReadPacket<T>(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>(T packet, short protocolId)
|
||||
{
|
||||
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
|
||||
protocolRegistration.Write(this, (IPacket) packet);
|
||||
}
|
||||
|
||||
public T ReadPacket<T>(short protocolId)
|
||||
{
|
||||
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
|
||||
return (T) protocolRegistration.Read(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
-2
@@ -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>(T packet, short protocolId)
|
||||
{
|
||||
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
|
||||
protocolRegistration.Write(this, (IPacket) packet);
|
||||
}
|
||||
|
||||
public T ReadPacket<T>(short protocolId)
|
||||
{
|
||||
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
|
||||
return (T) protocolRegistration.Read(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -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<ObjectA>(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<ObjectA>(102);
|
||||
packet.kk = result72;
|
||||
var array73 = buffer.ReadPacketArray<ObjectA>(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<ObjectA>(102);
|
||||
var list97 = buffer.ReadIntList();
|
||||
result93[result96] = list97;
|
||||
}
|
||||
+2
-2
@@ -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<ObjectA>(102);
|
||||
packet.kk = result9;
|
||||
var list10 = buffer.ReadIntList();
|
||||
packet.l = list10;
|
||||
+2
-2
@@ -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<ObjectB>(103);
|
||||
packet.objectB = result2;
|
||||
return packet;
|
||||
}
|
||||
+18
-21
@@ -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<byte>(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();
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user