test[csharp]: csharp support compatible field

This commit is contained in:
godotg
2023-10-17 17:20:54 +08:00
parent 170d5ef904
commit d6cf7fe025
17 changed files with 2221 additions and 4047 deletions
@@ -1,7 +0,0 @@
namespace CsProtocol.Buffer
{
public interface IProtocol
{
short ProtocolId();
}
}
@@ -1,12 +0,0 @@
namespace CsProtocol.Buffer
{
public interface IProtocolRegistration
{
short ProtocolId();
void Write(ByteBuffer buffer, IProtocol packet);
IProtocol Read(ByteBuffer buffer);
}
}
@@ -1,56 +0,0 @@
using System;
using System.Collections.Generic;
using CsProtocol.Buffer;
namespace CsProtocol
{
public class ObjectB : IProtocol
{
public bool flag;
public static ObjectB ValueOf(bool flag)
{
var packet = new ObjectB();
packet.flag = flag;
return packet;
}
public short ProtocolId()
{
return 103;
}
}
public class ObjectBRegistration : IProtocolRegistration
{
public short ProtocolId()
{
return 103;
}
public void Write(ByteBuffer buffer, IProtocol packet)
{
if (buffer.WritePacketFlag(packet))
{
return;
}
ObjectB message = (ObjectB) packet;
buffer.WriteBool(message.flag);
}
public IProtocol Read(ByteBuffer buffer)
{
if (!buffer.ReadBool())
{
return null;
}
ObjectB packet = new ObjectB();
bool result0 = buffer.ReadBool();
packet.flag = result0;
return packet;
}
}
}
@@ -1,69 +0,0 @@
using System;
using System.Collections.Generic;
using CsProtocol.Buffer;
namespace CsProtocol
{
public class ProtocolManager
{
public static readonly short MAX_PROTOCOL_NUM = short.MaxValue;
private static readonly IProtocolRegistration[] protocolList = new IProtocolRegistration[MAX_PROTOCOL_NUM];
public static void InitProtocol()
{
var protocolRegistrationTypeList = new List<Type>();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.Equals(typeof(ProtocolManager).Assembly))
{
var results = new List<Type>();
results.AddRange(assembly.GetTypes());
foreach (var type in results)
{
if (type.IsClass && !type.IsAbstract && typeof(IProtocolRegistration).IsAssignableFrom(type))
{
protocolRegistrationTypeList.Add(type);
}
}
}
}
foreach (var protocolRegistrationType in protocolRegistrationTypeList)
{
var protocolRegistration = (IProtocolRegistration) Activator.CreateInstance(protocolRegistrationType);
protocolList[protocolRegistration.ProtocolId()] = protocolRegistration;
}
}
public static IProtocolRegistration GetProtocol(short protocolId)
{
var protocol = protocolList[protocolId];
if (protocol == null)
{
throw new Exception("[protocolId:" + protocolId + "]协议不存在");
}
return protocol;
}
public static void Write(ByteBuffer buffer, IProtocol packet)
{
var protocolId = packet.ProtocolId();
// 写入协议号
buffer.WriteShort(protocolId);
// 写入包体
GetProtocol(protocolId).Write(buffer, packet);
}
public static IProtocol Read(ByteBuffer buffer)
{
var protocolId = buffer.ReadShort();
return GetProtocol(protocolId).Read(buffer);
}
}
}
+28 -20
View File
@@ -1,19 +1,41 @@
using System;
using System.IO;
using CsProtocol;
using CsProtocol.Buffer;
namespace csharp
namespace zfoocs
{
class Program
{
static void Main(string[] args)
{
ByteBufferTest();
ProtocolManager.InitProtocol();
ByteBufferTest();
complexObjectTest();
compatibleTest();
}
public static void compatibleTest()
{
// var bytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-no-compatible.bytes");
// var bytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes");
// var bytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes");
// var bytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes");
var bytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes");
var buffer = ByteBuffer.ValueOf();
buffer.WriteBytes(bytes);
var packet = ProtocolManager.Read(buffer);
var newBuffer = ByteBuffer.ValueOf();
ProtocolManager.Write(newBuffer, packet);
var newBytes = newBuffer.ToBytes();
Console.Out.WriteLine("source size " + bytes.Length);
Console.Out.WriteLine("target size " + newBytes.Length);
}
public static void complexObjectTest()
{
// 获取复杂对象的字节流
var complexObjectBytes = File.ReadAllBytes("../resources/ComplexObject.bytes");
var complexObjectBytes = File.ReadAllBytes("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\complexObject.bytes");
var buffer = ByteBuffer.ValueOf();
buffer.WriteBytes(complexObjectBytes);
var packet = ProtocolManager.Read(buffer);
@@ -34,7 +56,6 @@ namespace csharp
longTest();
floatTest();
doubleTest();
charTest();
stringTest();
}
@@ -130,19 +151,6 @@ namespace csharp
AssertEquals(value, readValue);
}
public static void charTest()
{
char value = 'a';
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
writerByteBuffer.WriteChar(value);
byte[] bytes = writerByteBuffer.ToBytes();
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
readerByteBuffer.WriteBytes(bytes);
char readValue = readerByteBuffer.ReadChar();
AssertEquals(value, readValue);
}
public static void stringTest()
{
string value = "aaa";
@@ -1,4 +1,4 @@
namespace CsProtocol.Buffer
namespace zfoocs
{
public class BigEndianByteBuffer : ByteBuffer
{
@@ -4,7 +4,7 @@ using System.Text;
// CSharp字节保存在内存的低地址中是根据操作系统来的,所以有可能是大端模式,也有可能是小端模式
// 右移操作>>是带符号右移
namespace CsProtocol.Buffer
namespace zfoocs
{
public abstract class ByteBuffer
{
@@ -49,6 +49,30 @@ namespace CsProtocol.Buffer
readOffset = 0;
}
}
public void AdjustPadding(int predictionLength, int beforeWriteIndex) {
// 因为写入的是可变长的int,如果预留的位置过多,则清除多余的位置
var currentWriteIndex = WriteOffset();
var predictionCount = WriteIntCount(predictionLength);
var length = currentWriteIndex - beforeWriteIndex - predictionCount;
var lengthCount = WriteIntCount(length);
var padding = lengthCount - predictionCount;
if (padding == 0) {
SetWriteOffset(beforeWriteIndex);
WriteInt(length);
SetWriteOffset(currentWriteIndex);
} else {
var bytes = new byte[length];
Array.Copy(buffer, currentWriteIndex - length, bytes, 0, length);
SetWriteOffset(beforeWriteIndex);
WriteInt(length);
WriteBytes(bytes);
}
}
public bool CompatibleRead(int beforeReadIndex, int length) {
return length != -1 && ReadOffset() < length + beforeReadIndex;
}
// -------------------------------------------------get/set-------------------------------------------------
public int WriteOffset()
@@ -69,6 +93,11 @@ namespace CsProtocol.Buffer
writeOffset = writeIndex;
}
public int ReadOffset()
{
return readOffset;
}
public void SetReadOffset(int readIndex)
{
if (readIndex > writeOffset)
@@ -235,6 +264,31 @@ namespace CsProtocol.Buffer
return (int) (value >> 1) ^ -((int) (value) & 1);
}
public int WriteIntCount(int intValue)
{
// 用Zigzag算法压缩int和long的值
// 再用Varint紧凑算法表示数字的有效位
uint value = (uint) ((intValue << 1) ^ (intValue >> 31));
if (value >> 7 == 0)
{
return 1;
}
if (value >> 14 == 0)
{
return 2;
}
if (value >> 21 == 0)
{
return 3;
}
if (value >> 28 == 0)
{
return 4;
}
return 5;
}
// 写入没有压缩的int
public abstract void WriteRawInt(int value);
@@ -391,20 +445,6 @@ namespace CsProtocol.Buffer
public abstract void WriteDouble(double value);
public abstract double ReadDouble();
// *******************************************char***************************************************
public char ReadChar()
{
// need check
var str = ReadString();
return string.IsNullOrEmpty(str) ? char.MinValue : str[0];
}
public void WriteChar(char value)
{
// need check
WriteString(new string(value, 1));
}
// *******************************************String***************************************************
public void WriteString(string value)
@@ -563,13 +603,6 @@ namespace CsProtocol.Buffer
return Encoding.UTF8.GetString(value, 0, value.Length);
}
public bool WritePacketFlag(IProtocol packet)
{
bool flag = packet == null;
WriteBool(!flag);
return flag;
}
public void WriteBooleanArray(bool[] array)
{
if ((array == null) || (array.Length == 0))
@@ -794,38 +827,6 @@ namespace CsProtocol.Buffer
return array;
}
public void WriteCharArray(char[] array)
{
if ((array == null) || (array.Length == 0))
{
WriteInt(0);
}
else
{
WriteInt(array.Length);
int length = array.Length;
for (int index = 0; index < length; index++)
{
WriteChar(array[index]);
}
}
}
public char[] ReadCharArray()
{
int size = ReadInt();
char[] array = new char[size];
if (size > 0)
{
for (int index = 0; index < size; index++)
{
array[index] = ReadChar();
}
}
return array;
}
public void WriteStringArray(string[] array)
{
if ((array == null) || (array.Length == 0))
@@ -871,7 +872,7 @@ namespace CsProtocol.Buffer
int length = array.Length;
for (int index = 0; index < length; index++)
{
protocolRegistration.Write(this, (IProtocol) array[index]);
protocolRegistration.Write(this, array[index]);
}
}
}
@@ -1116,38 +1117,6 @@ namespace CsProtocol.Buffer
return list;
}
public void WriteCharList(List<char> list)
{
if ((list == null) || (list.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(list.Count);
int length = list.Count;
for (int index = 0; index < length; index++)
{
WriteDouble(list[index]);
}
}
}
public List<char> ReadCharList()
{
int size = ReadInt();
List<char> list = new List<char>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
list.Add(ReadChar());
}
}
return list;
}
public void WriteStringList(List<string> list)
{
if ((list == null) || (list.Count == 0))
@@ -1193,7 +1162,7 @@ namespace CsProtocol.Buffer
int length = list.Count;
for (int index = 0; index < length; index++)
{
protocolRegistration.Write(this, (IProtocol) list[index]);
protocolRegistration.Write(this, list[index]);
}
}
}
@@ -1400,37 +1369,6 @@ namespace CsProtocol.Buffer
return set;
}
public void WriteCharSet(HashSet<char> set)
{
if ((set == null) || (set.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(set.Count);
foreach (var element in set)
{
WriteChar(element);
}
}
}
public HashSet<char> ReadCharSet()
{
int size = ReadInt();
HashSet<char> set = new HashSet<char>();
if (size > 0)
{
for (int index = 0; index < size; index++)
{
set.Add(ReadChar());
}
}
return set;
}
public void WriteStringSet(HashSet<string> set)
{
if ((set == null) || (set.Count == 0))
@@ -1474,7 +1412,7 @@ namespace CsProtocol.Buffer
WriteInt(set.Count);
foreach (var element in set)
{
protocolRegistration.Write(this, (IProtocol) element);
protocolRegistration.Write(this, element);
}
}
}
@@ -1611,7 +1549,7 @@ namespace CsProtocol.Buffer
foreach (var element in map)
{
WriteInt(element.Key);
protocolRegistration.Write(this, (IProtocol) element.Value);
protocolRegistration.Write(this, element.Value);
}
}
}
@@ -1750,7 +1688,7 @@ namespace CsProtocol.Buffer
foreach (var element in map)
{
WriteLong(element.Key);
protocolRegistration.Write(this, (IProtocol) element.Value);
protocolRegistration.Write(this, element.Value);
}
}
}
@@ -1889,7 +1827,7 @@ namespace CsProtocol.Buffer
foreach (var element in map)
{
WriteString(element.Key);
protocolRegistration.Write(this, (IProtocol) element.Value);
protocolRegistration.Write(this, element.Value);
}
}
}
@@ -1915,7 +1853,7 @@ namespace CsProtocol.Buffer
public void WritePacket<T>(T packet, short protocolId)
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
protocolRegistration.Write(this, (IProtocol) packet);
protocolRegistration.Write(this, packet);
}
public T ReadPacket<T>(short protocolId)
@@ -1,6 +1,6 @@
using System;
namespace CsProtocol.Buffer
namespace zfoocs
{
public class LittleEndianByteBuffer : ByteBuffer
{
@@ -0,0 +1,12 @@
namespace zfoocs
{
public interface IProtocolRegistration
{
short ProtocolId();
void Write(ByteBuffer buffer, object packet);
object Read(ByteBuffer buffer);
}
}
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using CsProtocol.Buffer;
namespace CsProtocol
namespace zfoocs
{
// 复杂的对象,包括了各种复杂的结构,数组,List,Set,Map
public class ComplexObject : IProtocol
public class ComplexObject
{
// byte类型,最简单的整形
public byte a;
@@ -38,10 +37,6 @@ namespace CsProtocol
public bool gg;
public bool[] ggg;
public bool[] gggg;
public char h;
public char hh;
public char[] hhh;
public char[] hhhh;
public string jj;
public string[] jjj;
public ObjectA kk;
@@ -65,7 +60,7 @@ namespace CsProtocol
public int myCompatible;
public ObjectA myObject;
public static ComplexObject ValueOf(byte a, byte aa, byte[] aaa, byte[] aaaa, short b, short bb, short[] bbb, short[] bbbb, int c, int cc, int[] ccc, int[] cccc, long d, long dd, long[] ddd, long[] dddd, float e, float ee, float[] eee, float[] eeee, double f, double ff, double[] fff, double[] ffff, bool g, bool gg, bool[] ggg, bool[] gggg, char h, char hh, char[] hhh, char[] hhhh, string jj, string[] jjj, ObjectA kk, ObjectA[] kkk, List<int> l, List<List<List<int>>> ll, List<List<ObjectA>> lll, List<string> llll, List<Dictionary<int, string>> lllll, Dictionary<int, string> m, Dictionary<int, ObjectA> mm, Dictionary<ObjectA, List<int>> mmm, Dictionary<List<List<ObjectA>>, List<List<List<int>>>> mmmm, Dictionary<List<Dictionary<int, string>>, HashSet<Dictionary<int, string>>> mmmmm, HashSet<int> s, HashSet<HashSet<List<int>>> ss, HashSet<HashSet<ObjectA>> sss, HashSet<string> ssss, HashSet<Dictionary<int, string>> sssss, int myCompatible, ObjectA myObject)
public static ComplexObject ValueOf(byte a, byte aa, byte[] aaa, byte[] aaaa, short b, short bb, short[] bbb, short[] bbbb, int c, int cc, int[] ccc, int[] cccc, long d, long dd, long[] ddd, long[] dddd, float e, float ee, float[] eee, float[] eeee, double f, double ff, double[] fff, double[] ffff, bool g, bool gg, bool[] ggg, bool[] gggg, string jj, string[] jjj, ObjectA kk, ObjectA[] kkk, List<int> l, List<List<List<int>>> ll, List<List<ObjectA>> lll, List<string> llll, List<Dictionary<int, string>> lllll, Dictionary<int, string> m, Dictionary<int, ObjectA> mm, Dictionary<ObjectA, List<int>> mmm, Dictionary<List<List<ObjectA>>, List<List<List<int>>>> mmmm, Dictionary<List<Dictionary<int, string>>, HashSet<Dictionary<int, string>>> mmmmm, HashSet<int> s, HashSet<HashSet<List<int>>> ss, HashSet<HashSet<ObjectA>> sss, HashSet<string> ssss, HashSet<Dictionary<int, string>> sssss, int myCompatible, ObjectA myObject)
{
var packet = new ComplexObject();
packet.a = a;
@@ -96,10 +91,6 @@ namespace CsProtocol
packet.gg = gg;
packet.ggg = ggg;
packet.gggg = gggg;
packet.h = h;
packet.hh = hh;
packet.hhh = hhh;
packet.hhhh = hhhh;
packet.jj = jj;
packet.jjj = jjj;
packet.kk = kk;
@@ -123,12 +114,6 @@ namespace CsProtocol
packet.myObject = myObject;
return packet;
}
public short ProtocolId()
{
return 100;
}
}
@@ -139,13 +124,16 @@ namespace CsProtocol
return 100;
}
public void Write(ByteBuffer buffer, IProtocol packet)
public void Write(ByteBuffer buffer, object packet)
{
if (buffer.WritePacketFlag(packet))
if (packet == null)
{
buffer.WriteInt(0);
return;
}
ComplexObject message = (ComplexObject) packet;
int beforeWriteIndex = buffer.WriteOffset();
buffer.WriteInt(36962);
buffer.WriteByte(message.a);
buffer.WriteByte(message.aa);
buffer.WriteByteArray(message.aaa);
@@ -174,10 +162,6 @@ namespace CsProtocol
buffer.WriteBool(message.gg);
buffer.WriteBooleanArray(message.ggg);
buffer.WriteBooleanArray(message.gggg);
buffer.WriteChar(message.h);
buffer.WriteChar(message.hh);
buffer.WriteCharArray(message.hhh);
buffer.WriteCharArray(message.hhhh);
buffer.WriteString(message.jj);
buffer.WriteStringArray(message.jjj);
buffer.WritePacket(message.kk, 102);
@@ -400,14 +384,17 @@ namespace CsProtocol
}
buffer.WriteInt(message.myCompatible);
buffer.WritePacket(message.myObject, 102);
buffer.AdjustPadding(36962, beforeWriteIndex);
}
public IProtocol Read(ByteBuffer buffer)
public object Read(ByteBuffer buffer)
{
if (!buffer.ReadBool())
int length = buffer.ReadInt();
if (length == 0)
{
return null;
}
int beforeReadIndex = buffer.ReadOffset();
ComplexObject packet = new ComplexObject();
byte result38 = buffer.ReadByte();
packet.a = result38;
@@ -465,131 +452,133 @@ namespace CsProtocol
packet.ggg = array64;
var array65 = buffer.ReadBooleanArray();
packet.gggg = array65;
char result66 = buffer.ReadChar();
packet.h = result66;
char result67 = buffer.ReadChar();
packet.hh = result67;
var array68 = buffer.ReadCharArray();
packet.hhh = array68;
var array69 = buffer.ReadCharArray();
packet.hhhh = array69;
string result70 = buffer.ReadString();
packet.jj = result70;
var array71 = buffer.ReadStringArray();
packet.jjj = array71;
ObjectA result72 = buffer.ReadPacket<ObjectA>(102);
packet.kk = result72;
var array73 = buffer.ReadPacketArray<ObjectA>(102);
packet.kkk = array73;
var list74 = buffer.ReadIntList();
packet.l = list74;
int size77 = buffer.ReadInt();
var result75 = new List<List<List<int>>>(size77);
if (size77 > 0)
string result66 = buffer.ReadString();
packet.jj = result66;
var array67 = buffer.ReadStringArray();
packet.jjj = array67;
ObjectA result68 = buffer.ReadPacket<ObjectA>(102);
packet.kk = result68;
var array69 = buffer.ReadPacketArray<ObjectA>(102);
packet.kkk = array69;
var list70 = buffer.ReadIntList();
packet.l = list70;
int size73 = buffer.ReadInt();
var result71 = new List<List<List<int>>>(size73);
if (size73 > 0)
{
for (int index76 = 0; index76 < size77; index76++)
for (int index72 = 0; index72 < size73; index72++)
{
int size80 = buffer.ReadInt();
var result78 = new List<List<int>>(size80);
if (size80 > 0)
int size76 = buffer.ReadInt();
var result74 = new List<List<int>>(size76);
if (size76 > 0)
{
for (int index79 = 0; index79 < size80; index79++)
for (int index75 = 0; index75 < size76; index75++)
{
var list81 = buffer.ReadIntList();
result78.Add(list81);
var list77 = buffer.ReadIntList();
result74.Add(list77);
}
}
result75.Add(result78);
result71.Add(result74);
}
}
packet.ll = result75;
int size84 = buffer.ReadInt();
var result82 = new List<List<ObjectA>>(size84);
if (size84 > 0)
packet.ll = result71;
int size80 = buffer.ReadInt();
var result78 = new List<List<ObjectA>>(size80);
if (size80 > 0)
{
for (int index83 = 0; index83 < size84; index83++)
for (int index79 = 0; index79 < size80; index79++)
{
var list85 = buffer.ReadPacketList<ObjectA>(102);
result82.Add(list85);
var list81 = buffer.ReadPacketList<ObjectA>(102);
result78.Add(list81);
}
}
packet.lll = result82;
var list86 = buffer.ReadStringList();
packet.llll = list86;
int size89 = buffer.ReadInt();
var result87 = new List<Dictionary<int, string>>(size89);
if (size89 > 0)
packet.lll = result78;
var list82 = buffer.ReadStringList();
packet.llll = list82;
int size85 = buffer.ReadInt();
var result83 = new List<Dictionary<int, string>>(size85);
if (size85 > 0)
{
for (int index88 = 0; index88 < size89; index88++)
for (int index84 = 0; index84 < size85; index84++)
{
var map90 = buffer.ReadIntStringMap();
result87.Add(map90);
var map86 = buffer.ReadIntStringMap();
result83.Add(map86);
}
}
packet.lllll = result87;
var map91 = buffer.ReadIntStringMap();
packet.m = map91;
var map92 = buffer.ReadIntPacketMap<ObjectA>(102);
packet.mm = map92;
int size94 = buffer.ReadInt();
var result93 = new Dictionary<ObjectA, List<int>>(size94);
if (size94 > 0)
packet.lllll = result83;
var map87 = buffer.ReadIntStringMap();
packet.m = map87;
var map88 = buffer.ReadIntPacketMap<ObjectA>(102);
packet.mm = map88;
int size90 = buffer.ReadInt();
var result89 = new Dictionary<ObjectA, List<int>>(size90);
if (size90 > 0)
{
for (var index95 = 0; index95 < size94; index95++)
for (var index91 = 0; index91 < size90; index91++)
{
ObjectA result96 = buffer.ReadPacket<ObjectA>(102);
var list97 = buffer.ReadIntList();
result93[result96] = list97;
ObjectA result92 = buffer.ReadPacket<ObjectA>(102);
var list93 = buffer.ReadIntList();
result89[result92] = list93;
}
}
packet.mmm = result93;
int size99 = buffer.ReadInt();
var result98 = new Dictionary<List<List<ObjectA>>, List<List<List<int>>>>(size99);
if (size99 > 0)
packet.mmm = result89;
int size95 = buffer.ReadInt();
var result94 = new Dictionary<List<List<ObjectA>>, List<List<List<int>>>>(size95);
if (size95 > 0)
{
for (var index100 = 0; index100 < size99; index100++)
for (var index96 = 0; index96 < size95; index96++)
{
int size99 = buffer.ReadInt();
var result97 = new List<List<ObjectA>>(size99);
if (size99 > 0)
{
for (int index98 = 0; index98 < size99; index98++)
{
var list100 = buffer.ReadPacketList<ObjectA>(102);
result97.Add(list100);
}
}
int size103 = buffer.ReadInt();
var result101 = new List<List<ObjectA>>(size103);
var result101 = new List<List<List<int>>>(size103);
if (size103 > 0)
{
for (int index102 = 0; index102 < size103; index102++)
{
var list104 = buffer.ReadPacketList<ObjectA>(102);
result101.Add(list104);
}
}
int size107 = buffer.ReadInt();
var result105 = new List<List<List<int>>>(size107);
if (size107 > 0)
{
for (int index106 = 0; index106 < size107; index106++)
{
int size110 = buffer.ReadInt();
var result108 = new List<List<int>>(size110);
if (size110 > 0)
int size106 = buffer.ReadInt();
var result104 = new List<List<int>>(size106);
if (size106 > 0)
{
for (int index109 = 0; index109 < size110; index109++)
for (int index105 = 0; index105 < size106; index105++)
{
var list111 = buffer.ReadIntList();
result108.Add(list111);
var list107 = buffer.ReadIntList();
result104.Add(list107);
}
}
result105.Add(result108);
result101.Add(result104);
}
}
result98[result101] = result105;
result94[result97] = result101;
}
}
packet.mmmm = result98;
int size113 = buffer.ReadInt();
var result112 = new Dictionary<List<Dictionary<int, string>>, HashSet<Dictionary<int, string>>>(size113);
if (size113 > 0)
packet.mmmm = result94;
int size109 = buffer.ReadInt();
var result108 = new Dictionary<List<Dictionary<int, string>>, HashSet<Dictionary<int, string>>>(size109);
if (size109 > 0)
{
for (var index114 = 0; index114 < size113; index114++)
for (var index110 = 0; index110 < size109; index110++)
{
int size113 = buffer.ReadInt();
var result111 = new List<Dictionary<int, string>>(size113);
if (size113 > 0)
{
for (int index112 = 0; index112 < size113; index112++)
{
var map114 = buffer.ReadIntStringMap();
result111.Add(map114);
}
}
int size117 = buffer.ReadInt();
var result115 = new List<Dictionary<int, string>>(size117);
var result115 = new HashSet<Dictionary<int, string>>();
if (size117 > 0)
{
for (int index116 = 0; index116 < size117; index116++)
@@ -598,78 +587,67 @@ namespace CsProtocol
result115.Add(map118);
}
}
int size121 = buffer.ReadInt();
var result119 = new HashSet<Dictionary<int, string>>();
if (size121 > 0)
result108[result111] = result115;
}
}
packet.mmmmm = result108;
var set119 = buffer.ReadIntSet();
packet.s = set119;
int size122 = buffer.ReadInt();
var result120 = new HashSet<HashSet<List<int>>>();
if (size122 > 0)
{
for (int index121 = 0; index121 < size122; index121++)
{
int size125 = buffer.ReadInt();
var result123 = new HashSet<List<int>>();
if (size125 > 0)
{
for (int index120 = 0; index120 < size121; index120++)
for (int index124 = 0; index124 < size125; index124++)
{
var map122 = buffer.ReadIntStringMap();
result119.Add(map122);
var list126 = buffer.ReadIntList();
result123.Add(list126);
}
}
result112[result115] = result119;
result120.Add(result123);
}
}
packet.mmmmm = result112;
var set123 = buffer.ReadIntSet();
packet.s = set123;
int size126 = buffer.ReadInt();
var result124 = new HashSet<HashSet<List<int>>>();
if (size126 > 0)
packet.ss = result120;
int size129 = buffer.ReadInt();
var result127 = new HashSet<HashSet<ObjectA>>();
if (size129 > 0)
{
for (int index125 = 0; index125 < size126; index125++)
for (int index128 = 0; index128 < size129; index128++)
{
int size129 = buffer.ReadInt();
var result127 = new HashSet<List<int>>();
if (size129 > 0)
{
for (int index128 = 0; index128 < size129; index128++)
{
var list130 = buffer.ReadIntList();
result127.Add(list130);
}
}
result124.Add(result127);
var set130 = buffer.ReadPacketSet<ObjectA>(102);
result127.Add(set130);
}
}
packet.ss = result124;
int size133 = buffer.ReadInt();
var result131 = new HashSet<HashSet<ObjectA>>();
if (size133 > 0)
packet.sss = result127;
var set131 = buffer.ReadStringSet();
packet.ssss = set131;
int size134 = buffer.ReadInt();
var result132 = new HashSet<Dictionary<int, string>>();
if (size134 > 0)
{
for (int index132 = 0; index132 < size133; index132++)
for (int index133 = 0; index133 < size134; index133++)
{
var set134 = buffer.ReadPacketSet<ObjectA>(102);
result131.Add(set134);
var map135 = buffer.ReadIntStringMap();
result132.Add(map135);
}
}
packet.sss = result131;
var set135 = buffer.ReadStringSet();
packet.ssss = set135;
int size138 = buffer.ReadInt();
var result136 = new HashSet<Dictionary<int, string>>();
if (size138 > 0)
{
for (int index137 = 0; index137 < size138; index137++)
{
var map139 = buffer.ReadIntStringMap();
result136.Add(map139);
}
packet.sssss = result132;
if (buffer.CompatibleRead(beforeReadIndex, length)) {
int result136 = buffer.ReadInt();
packet.myCompatible = result136;
}
packet.sssss = result136;
if (!buffer.IsReadable())
{
return packet;
if (buffer.CompatibleRead(beforeReadIndex, length)) {
ObjectA result137 = buffer.ReadPacket<ObjectA>(102);
packet.myObject = result137;
}
int result140 = buffer.ReadInt();
packet.myCompatible = result140;
if (!buffer.IsReadable())
{
return packet;
if (length > 0) {
buffer.SetReadOffset(beforeReadIndex + length);
}
ObjectA result141 = buffer.ReadPacket<ObjectA>(102);
packet.myObject = result141;
return packet;
}
}
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using CsProtocol.Buffer;
namespace CsProtocol
namespace zfoocs
{
public class EmptyObject : IProtocol
public class EmptyObject
{
@@ -15,12 +14,6 @@ namespace CsProtocol
return packet;
}
public short ProtocolId()
{
return 0;
}
}
@@ -31,24 +24,30 @@ namespace CsProtocol
return 0;
}
public void Write(ByteBuffer buffer, IProtocol packet)
public void Write(ByteBuffer buffer, object packet)
{
if (buffer.WritePacketFlag(packet))
if (packet == null)
{
buffer.WriteInt(0);
return;
}
EmptyObject message = (EmptyObject) packet;
buffer.WriteInt(-1);
}
public IProtocol Read(ByteBuffer buffer)
public object Read(ByteBuffer buffer)
{
if (!buffer.ReadBool())
int length = buffer.ReadInt();
if (length == 0)
{
return null;
}
int beforeReadIndex = buffer.ReadOffset();
EmptyObject packet = new EmptyObject();
if (length > 0) {
buffer.SetReadOffset(beforeReadIndex + length);
}
return packet;
}
}
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using CsProtocol.Buffer;
namespace CsProtocol
namespace zfoocs
{
public class NormalObject : IProtocol
public class NormalObject
{
public byte a;
public byte[] aaa;
@@ -25,8 +24,9 @@ namespace CsProtocol
public Dictionary<int, ObjectA> mm;
public HashSet<int> s;
public HashSet<string> ssss;
public int outCompatibleValue;
public static NormalObject ValueOf(byte a, byte[] aaa, short b, int c, long d, float e, double f, bool g, string jj, ObjectA kk, List<int> l, List<long> ll, List<ObjectA> lll, List<string> llll, Dictionary<int, string> m, Dictionary<int, ObjectA> mm, HashSet<int> s, HashSet<string> ssss)
public static NormalObject ValueOf(byte a, byte[] aaa, short b, int c, long d, float e, double f, bool g, string jj, ObjectA kk, List<int> l, List<long> ll, List<ObjectA> lll, List<string> llll, Dictionary<int, string> m, Dictionary<int, ObjectA> mm, HashSet<int> s, HashSet<string> ssss, int outCompatibleValue)
{
var packet = new NormalObject();
packet.a = a;
@@ -47,14 +47,9 @@ namespace CsProtocol
packet.mm = mm;
packet.s = s;
packet.ssss = ssss;
packet.outCompatibleValue = outCompatibleValue;
return packet;
}
public short ProtocolId()
{
return 101;
}
}
@@ -65,13 +60,16 @@ namespace CsProtocol
return 101;
}
public void Write(ByteBuffer buffer, IProtocol packet)
public void Write(ByteBuffer buffer, object packet)
{
if (buffer.WritePacketFlag(packet))
if (packet == null)
{
buffer.WriteInt(0);
return;
}
NormalObject message = (NormalObject) packet;
int beforeWriteIndex = buffer.WriteOffset();
buffer.WriteInt(854);
buffer.WriteByte(message.a);
buffer.WriteByteArray(message.aaa);
buffer.WriteShort(message.b);
@@ -90,14 +88,18 @@ namespace CsProtocol
buffer.WriteIntPacketMap(message.mm, 102);
buffer.WriteIntSet(message.s);
buffer.WriteStringSet(message.ssss);
buffer.WriteInt(message.outCompatibleValue);
buffer.AdjustPadding(854, beforeWriteIndex);
}
public IProtocol Read(ByteBuffer buffer)
public object Read(ByteBuffer buffer)
{
if (!buffer.ReadBool())
int length = buffer.ReadInt();
if (length == 0)
{
return null;
}
int beforeReadIndex = buffer.ReadOffset();
NormalObject packet = new NormalObject();
byte result0 = buffer.ReadByte();
packet.a = result0;
@@ -135,6 +137,13 @@ namespace CsProtocol
packet.s = set16;
var set17 = buffer.ReadStringSet();
packet.ssss = set17;
if (buffer.CompatibleRead(beforeReadIndex, length)) {
int result18 = buffer.ReadInt();
packet.outCompatibleValue = result18;
}
if (length > 0) {
buffer.SetReadOffset(beforeReadIndex + length);
}
return packet;
}
}
@@ -1,30 +1,25 @@
using System;
using System.Collections.Generic;
using CsProtocol.Buffer;
namespace CsProtocol
namespace zfoocs
{
public class ObjectA : IProtocol
public class ObjectA
{
public int a;
public Dictionary<int, string> m;
public ObjectB objectB;
public int innerCompatibleValue;
public static ObjectA ValueOf(int a, Dictionary<int, string> m, ObjectB objectB)
public static ObjectA ValueOf(int a, Dictionary<int, string> m, ObjectB objectB, int innerCompatibleValue)
{
var packet = new ObjectA();
packet.a = a;
packet.m = m;
packet.objectB = objectB;
packet.innerCompatibleValue = innerCompatibleValue;
return packet;
}
public short ProtocolId()
{
return 102;
}
}
@@ -35,24 +30,31 @@ namespace CsProtocol
return 102;
}
public void Write(ByteBuffer buffer, IProtocol packet)
public void Write(ByteBuffer buffer, object packet)
{
if (buffer.WritePacketFlag(packet))
if (packet == null)
{
buffer.WriteInt(0);
return;
}
ObjectA message = (ObjectA) packet;
int beforeWriteIndex = buffer.WriteOffset();
buffer.WriteInt(201);
buffer.WriteInt(message.a);
buffer.WriteIntStringMap(message.m);
buffer.WritePacket(message.objectB, 103);
buffer.WriteInt(message.innerCompatibleValue);
buffer.AdjustPadding(201, beforeWriteIndex);
}
public IProtocol Read(ByteBuffer buffer)
public object Read(ByteBuffer buffer)
{
if (!buffer.ReadBool())
int length = buffer.ReadInt();
if (length == 0)
{
return null;
}
int beforeReadIndex = buffer.ReadOffset();
ObjectA packet = new ObjectA();
int result0 = buffer.ReadInt();
packet.a = result0;
@@ -60,6 +62,13 @@ namespace CsProtocol
packet.m = map1;
ObjectB result2 = buffer.ReadPacket<ObjectB>(103);
packet.objectB = result2;
if (buffer.CompatibleRead(beforeReadIndex, length)) {
int result3 = buffer.ReadInt();
packet.innerCompatibleValue = result3;
}
if (length > 0) {
buffer.SetReadOffset(beforeReadIndex + length);
}
return packet;
}
}
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
namespace zfoocs
{
public class ObjectB
{
public bool flag;
public int innerCompatibleValue;
public static ObjectB ValueOf(bool flag, int innerCompatibleValue)
{
var packet = new ObjectB();
packet.flag = flag;
packet.innerCompatibleValue = innerCompatibleValue;
return packet;
}
}
public class ObjectBRegistration : IProtocolRegistration
{
public short ProtocolId()
{
return 103;
}
public void Write(ByteBuffer buffer, object packet)
{
if (packet == null)
{
buffer.WriteInt(0);
return;
}
ObjectB message = (ObjectB) packet;
int beforeWriteIndex = buffer.WriteOffset();
buffer.WriteInt(4);
buffer.WriteBool(message.flag);
buffer.WriteInt(message.innerCompatibleValue);
buffer.AdjustPadding(4, beforeWriteIndex);
}
public object Read(ByteBuffer buffer)
{
int length = buffer.ReadInt();
if (length == 0)
{
return null;
}
int beforeReadIndex = buffer.ReadOffset();
ObjectB packet = new ObjectB();
bool result0 = buffer.ReadBool();
packet.flag = result0;
if (buffer.CompatibleRead(beforeReadIndex, length)) {
int result1 = buffer.ReadInt();
packet.innerCompatibleValue = result1;
}
if (length > 0) {
buffer.SetReadOffset(beforeReadIndex + length);
}
return packet;
}
}
}
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using CsProtocol.Buffer;
namespace CsProtocol
namespace zfoocs
{
public class SimpleObject : IProtocol
public class SimpleObject
{
public int c;
public bool g;
@@ -17,12 +16,6 @@ namespace CsProtocol
packet.g = g;
return packet;
}
public short ProtocolId()
{
return 104;
}
}
@@ -33,28 +26,35 @@ namespace CsProtocol
return 104;
}
public void Write(ByteBuffer buffer, IProtocol packet)
public void Write(ByteBuffer buffer, object packet)
{
if (buffer.WritePacketFlag(packet))
if (packet == null)
{
buffer.WriteInt(0);
return;
}
SimpleObject message = (SimpleObject) packet;
buffer.WriteInt(-1);
buffer.WriteInt(message.c);
buffer.WriteBool(message.g);
}
public IProtocol Read(ByteBuffer buffer)
public object Read(ByteBuffer buffer)
{
if (!buffer.ReadBool())
int length = buffer.ReadInt();
if (length == 0)
{
return null;
}
int beforeReadIndex = buffer.ReadOffset();
SimpleObject packet = new SimpleObject();
int result0 = buffer.ReadInt();
packet.c = result0;
bool result1 = buffer.ReadBool();
packet.g = result1;
if (length > 0) {
buffer.SetReadOffset(beforeReadIndex + length);
}
return packet;
}
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
namespace zfoocs
{
public class ProtocolManager
{
public static readonly short MAX_PROTOCOL_NUM = short.MaxValue;
private static readonly IProtocolRegistration[] protocols = new IProtocolRegistration[MAX_PROTOCOL_NUM];
private static readonly Dictionary<Type, short> protocolIdMap = new Dictionary<Type, short>();
public static void InitProtocol()
{
protocols[0] = new EmptyObjectRegistration();
protocolIdMap[typeof(EmptyObject)] = 0;
protocols[1] = new VeryBigObjectRegistration();
protocolIdMap[typeof(VeryBigObject)] = 1;
protocols[100] = new ComplexObjectRegistration();
protocolIdMap[typeof(ComplexObject)] = 100;
protocols[101] = new NormalObjectRegistration();
protocolIdMap[typeof(NormalObject)] = 101;
protocols[102] = new ObjectARegistration();
protocolIdMap[typeof(ObjectA)] = 102;
protocols[103] = new ObjectBRegistration();
protocolIdMap[typeof(ObjectB)] = 103;
protocols[104] = new SimpleObjectRegistration();
protocolIdMap[typeof(SimpleObject)] = 104;
}
public static IProtocolRegistration GetProtocol(short protocolId)
{
var protocol = protocols[protocolId];
if (protocol == null)
{
throw new Exception("[protocolId:" + protocolId + "] not exist");
}
return protocol;
}
public static void Write(ByteBuffer buffer, object packet)
{
var protocolId = protocolIdMap[packet.GetType()];
// 写入协议号
buffer.WriteShort(protocolId);
// 写入包体
GetProtocol(protocolId).Write(buffer, packet);
}
public static object Read(ByteBuffer buffer)
{
var protocolId = buffer.ReadShort();
return GetProtocol(protocolId).Read(buffer);
}
}
}