perf[protocol]: 使用模板生成协议

This commit is contained in:
jaysunxiao
2022-05-16 23:00:41 +08:00
parent 5c379dd21c
commit bb1d7cccc4
24 changed files with 227 additions and 223 deletions
@@ -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);
}
}
}
@@ -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;
}
@@ -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;
@@ -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;
}
@@ -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();
+16
View File
@@ -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