mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-05 18:25:26 +00:00
perf[protocol]: csharp protocol test
This commit is contained in:
@@ -563,7 +563,7 @@ namespace CsProtocol.Buffer
|
||||
return Encoding.UTF8.GetString(value, 0, value.Length);
|
||||
}
|
||||
|
||||
public bool WritePacketFlag(IPacket packet)
|
||||
public bool WritePacketFlag(IProtocol packet)
|
||||
{
|
||||
bool flag = packet == null;
|
||||
WriteBool(!flag);
|
||||
@@ -871,7 +871,7 @@ namespace CsProtocol.Buffer
|
||||
int length = array.Length;
|
||||
for (int index = 0; index < length; index++)
|
||||
{
|
||||
protocolRegistration.Write(this, (IPacket) array[index]);
|
||||
protocolRegistration.Write(this, (IProtocol) array[index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1193,7 +1193,7 @@ namespace CsProtocol.Buffer
|
||||
int length = list.Count;
|
||||
for (int index = 0; index < length; index++)
|
||||
{
|
||||
protocolRegistration.Write(this, (IPacket) list[index]);
|
||||
protocolRegistration.Write(this, (IProtocol) list[index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1474,7 +1474,7 @@ namespace CsProtocol.Buffer
|
||||
WriteInt(set.Count);
|
||||
foreach (var element in set)
|
||||
{
|
||||
protocolRegistration.Write(this, (IPacket) element);
|
||||
protocolRegistration.Write(this, (IProtocol) element);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1611,7 +1611,7 @@ namespace CsProtocol.Buffer
|
||||
foreach (var element in map)
|
||||
{
|
||||
WriteInt(element.Key);
|
||||
protocolRegistration.Write(this, (IPacket) element.Value);
|
||||
protocolRegistration.Write(this, (IProtocol) element.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1750,7 +1750,7 @@ namespace CsProtocol.Buffer
|
||||
foreach (var element in map)
|
||||
{
|
||||
WriteLong(element.Key);
|
||||
protocolRegistration.Write(this, (IPacket) element.Value);
|
||||
protocolRegistration.Write(this, (IProtocol) element.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1889,7 +1889,7 @@ namespace CsProtocol.Buffer
|
||||
foreach (var element in map)
|
||||
{
|
||||
WriteString(element.Key);
|
||||
protocolRegistration.Write(this, (IPacket) element.Value);
|
||||
protocolRegistration.Write(this, (IProtocol) element.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1915,7 +1915,7 @@ namespace CsProtocol.Buffer
|
||||
public void WritePacket<T>(T packet, short protocolId)
|
||||
{
|
||||
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
|
||||
protocolRegistration.Write(this, (IPacket) packet);
|
||||
protocolRegistration.Write(this, (IProtocol) packet);
|
||||
}
|
||||
|
||||
public T ReadPacket<T>(short protocolId)
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
namespace CsProtocol.Buffer
|
||||
{
|
||||
public interface IPacket
|
||||
public interface IProtocol
|
||||
{
|
||||
short ProtocolId();
|
||||
}
|
||||
@@ -4,9 +4,9 @@
|
||||
{
|
||||
short ProtocolId();
|
||||
|
||||
void Write(ByteBuffer buffer, IPacket packet);
|
||||
void Write(ByteBuffer buffer, IProtocol packet);
|
||||
|
||||
IPacket Read(ByteBuffer buffer);
|
||||
IProtocol Read(ByteBuffer buffer);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,17 +4,12 @@ using CsProtocol.Buffer;
|
||||
|
||||
namespace CsProtocol
|
||||
{
|
||||
// 复杂的对象
|
||||
// 包括了各种复杂的结构,数组,List,Set,Map
|
||||
//
|
||||
// @author godotg
|
||||
// @version 3.0
|
||||
public class ComplexObject : IPacket
|
||||
// 复杂的对象,包括了各种复杂的结构,数组,List,Set,Map
|
||||
public class ComplexObject : IProtocol
|
||||
{
|
||||
// byte类型,最简单的整形
|
||||
public byte a;
|
||||
// byte的包装类型
|
||||
// 优先使用基础类型,包装类型会有装箱拆箱
|
||||
// byte的包装类型,优先使用基础类型,包装类型会有装箱拆箱
|
||||
public byte aa;
|
||||
// 数组类型
|
||||
public byte[] aaa;
|
||||
@@ -144,7 +139,7 @@ namespace CsProtocol
|
||||
return 100;
|
||||
}
|
||||
|
||||
public void Write(ByteBuffer buffer, IPacket packet)
|
||||
public void Write(ByteBuffer buffer, IProtocol packet)
|
||||
{
|
||||
if (buffer.WritePacketFlag(packet))
|
||||
{
|
||||
@@ -407,7 +402,7 @@ namespace CsProtocol
|
||||
buffer.WritePacket(message.myObject, 102);
|
||||
}
|
||||
|
||||
public IPacket Read(ByteBuffer buffer)
|
||||
public IProtocol Read(ByteBuffer buffer)
|
||||
{
|
||||
if (!buffer.ReadBool())
|
||||
{
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CsProtocol.Buffer;
|
||||
|
||||
namespace CsProtocol
|
||||
{
|
||||
|
||||
public class EmptyObject : IProtocol
|
||||
{
|
||||
|
||||
|
||||
public static EmptyObject ValueOf()
|
||||
{
|
||||
var packet = new EmptyObject();
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
|
||||
public short ProtocolId()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class EmptyObjectRegistration : IProtocolRegistration
|
||||
{
|
||||
public short ProtocolId()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void Write(ByteBuffer buffer, IProtocol packet)
|
||||
{
|
||||
if (buffer.WritePacketFlag(packet))
|
||||
{
|
||||
return;
|
||||
}
|
||||
EmptyObject message = (EmptyObject) packet;
|
||||
|
||||
}
|
||||
|
||||
public IProtocol Read(ByteBuffer buffer)
|
||||
{
|
||||
if (!buffer.ReadBool())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
EmptyObject packet = new EmptyObject();
|
||||
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,8 @@ using CsProtocol.Buffer;
|
||||
|
||||
namespace CsProtocol
|
||||
{
|
||||
// @author godotg
|
||||
// @version 3.0
|
||||
public class NormalObject : IPacket
|
||||
|
||||
public class NormalObject : IProtocol
|
||||
{
|
||||
public byte a;
|
||||
public byte[] aaa;
|
||||
@@ -66,7 +65,7 @@ namespace CsProtocol
|
||||
return 101;
|
||||
}
|
||||
|
||||
public void Write(ByteBuffer buffer, IPacket packet)
|
||||
public void Write(ByteBuffer buffer, IProtocol packet)
|
||||
{
|
||||
if (buffer.WritePacketFlag(packet))
|
||||
{
|
||||
@@ -93,7 +92,7 @@ namespace CsProtocol
|
||||
buffer.WriteStringSet(message.ssss);
|
||||
}
|
||||
|
||||
public IPacket Read(ByteBuffer buffer)
|
||||
public IProtocol Read(ByteBuffer buffer)
|
||||
{
|
||||
if (!buffer.ReadBool())
|
||||
{
|
||||
|
||||
@@ -4,9 +4,8 @@ using CsProtocol.Buffer;
|
||||
|
||||
namespace CsProtocol
|
||||
{
|
||||
// @author godotg
|
||||
// @version 3.0
|
||||
public class ObjectA : IPacket
|
||||
|
||||
public class ObjectA : IProtocol
|
||||
{
|
||||
public int a;
|
||||
public Dictionary<int, string> m;
|
||||
@@ -36,7 +35,7 @@ namespace CsProtocol
|
||||
return 102;
|
||||
}
|
||||
|
||||
public void Write(ByteBuffer buffer, IPacket packet)
|
||||
public void Write(ByteBuffer buffer, IProtocol packet)
|
||||
{
|
||||
if (buffer.WritePacketFlag(packet))
|
||||
{
|
||||
@@ -48,7 +47,7 @@ namespace CsProtocol
|
||||
buffer.WritePacket(message.objectB, 103);
|
||||
}
|
||||
|
||||
public IPacket Read(ByteBuffer buffer)
|
||||
public IProtocol Read(ByteBuffer buffer)
|
||||
{
|
||||
if (!buffer.ReadBool())
|
||||
{
|
||||
|
||||
@@ -4,9 +4,8 @@ using CsProtocol.Buffer;
|
||||
|
||||
namespace CsProtocol
|
||||
{
|
||||
// @author godotg
|
||||
// @version 3.0
|
||||
public class ObjectB : IPacket
|
||||
|
||||
public class ObjectB : IProtocol
|
||||
{
|
||||
public bool flag;
|
||||
|
||||
@@ -32,7 +31,7 @@ namespace CsProtocol
|
||||
return 103;
|
||||
}
|
||||
|
||||
public void Write(ByteBuffer buffer, IPacket packet)
|
||||
public void Write(ByteBuffer buffer, IProtocol packet)
|
||||
{
|
||||
if (buffer.WritePacketFlag(packet))
|
||||
{
|
||||
@@ -42,7 +41,7 @@ namespace CsProtocol
|
||||
buffer.WriteBool(message.flag);
|
||||
}
|
||||
|
||||
public IPacket Read(ByteBuffer buffer)
|
||||
public IProtocol Read(ByteBuffer buffer)
|
||||
{
|
||||
if (!buffer.ReadBool())
|
||||
{
|
||||
|
||||
@@ -4,9 +4,8 @@ using CsProtocol.Buffer;
|
||||
|
||||
namespace CsProtocol
|
||||
{
|
||||
// @author godotg
|
||||
// @version 3.0
|
||||
public class SimpleObject : IPacket
|
||||
|
||||
public class SimpleObject : IProtocol
|
||||
{
|
||||
public int c;
|
||||
public bool g;
|
||||
@@ -34,7 +33,7 @@ namespace CsProtocol
|
||||
return 104;
|
||||
}
|
||||
|
||||
public void Write(ByteBuffer buffer, IPacket packet)
|
||||
public void Write(ByteBuffer buffer, IProtocol packet)
|
||||
{
|
||||
if (buffer.WritePacketFlag(packet))
|
||||
{
|
||||
@@ -45,7 +44,7 @@ namespace CsProtocol
|
||||
buffer.WriteBool(message.g);
|
||||
}
|
||||
|
||||
public IPacket Read(ByteBuffer buffer)
|
||||
public IProtocol Read(ByteBuffer buffer)
|
||||
{
|
||||
if (!buffer.ReadBool())
|
||||
{
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -50,7 +50,7 @@ namespace CsProtocol
|
||||
return protocol;
|
||||
}
|
||||
|
||||
public static void Write(ByteBuffer buffer, IPacket packet)
|
||||
public static void Write(ByteBuffer buffer, IProtocol packet)
|
||||
{
|
||||
var protocolId = packet.ProtocolId();
|
||||
// 写入协议号
|
||||
@@ -60,7 +60,7 @@ namespace CsProtocol
|
||||
GetProtocol(protocolId).Write(buffer, packet);
|
||||
}
|
||||
|
||||
public static IPacket Read(ByteBuffer buffer)
|
||||
public static IProtocol Read(ByteBuffer buffer)
|
||||
{
|
||||
var protocolId = buffer.ReadShort();
|
||||
return GetProtocol(protocolId).Read(buffer);
|
||||
|
||||
@@ -34,18 +34,9 @@ public class GenerateTest {
|
||||
var generateLanguages = op.getGenerateLanguages();
|
||||
|
||||
// generate the jsProtocol folder and its corresponding js protocol file in the current protocol directory
|
||||
generateLanguages.add(CodeLanguage.Cpp);
|
||||
generateLanguages.add(CodeLanguage.Go);
|
||||
generateLanguages.add(CodeLanguage.JavaScript);
|
||||
generateLanguages.add(CodeLanguage.TypeScript);
|
||||
generateLanguages.add(CodeLanguage.Lua);
|
||||
generateLanguages.add(CodeLanguage.CSharp);
|
||||
generateLanguages.add(CodeLanguage.GdScript);
|
||||
generateLanguages.add(CodeLanguage.Python);
|
||||
|
||||
// Protobuf needs to specify protobuf.xml to generate a protocol
|
||||
op.setProtocolParam("protobuf=protobuf.xml");
|
||||
generateLanguages.add(CodeLanguage.Protobuf);
|
||||
|
||||
// Initialize and then generate the protocol
|
||||
ProtocolManager.initProtocolAuto(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, EmptyObject.class, VeryBigObject.class), op);
|
||||
|
||||
Reference in New Issue
Block a user