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
@@ -0,0 +1,74 @@
namespace CsProtocol.Buffer
{
public class BigEndianByteBuffer : ByteBuffer
{
// fast int to byte[] conversion and vice versa
// -> test with 100k conversions:
// BitConverter.GetBytes(ushort): 144ms
// bit shifting: 11ms
// -> 10x speed improvement makes this optimization actually worth it
// -> this way we don't need to allocate BinaryWriter/Reader either
// -> 4 bytes because some people may want to send messages larger than
// 64K bytes
// => big endian is standard for network transmissions, and necessary
// for compatibility with erlang
public static byte[] IntToBytesBigEndian(int value)
{
return new byte[]
{
(byte) (value >> 24),
(byte) (value >> 16),
(byte) (value >> 8),
(byte) value
};
}
public static int BytesToIntBigEndian(byte[] bytes)
{
return (bytes[0] << 24) |
(bytes[1] << 16) |
(bytes[2] << 8) |
bytes[3];
}
public override void WriteShort(short value)
{
WriteBytes(GetBytes(value));
}
public override short ReadShort()
{
return GetInt16(ReadBytes(2));
}
public override void WriteRawInt(int value)
{
WriteBytes(IntToBytesBigEndian(value));
}
public override int ReadRawInt()
{
return BytesToIntBigEndian(ReadBytes(4));
}
public override void WriteFloat(float value)
{
WriteBytes(GetBytes(value));
}
public override float ReadFloat()
{
return GetSingle(ReadBytes(4));
}
public override void WriteDouble(double value)
{
WriteBytes(GetBytes(value));
}
public override double ReadDouble()
{
return GetDouble(ReadBytes(8));
}
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,56 @@
using System;
namespace CsProtocol.Buffer
{
public class LittleEndianByteBuffer : ByteBuffer
{
/**
* 翻转字节数组,如果本地字节序列为低字节序列,则进行翻转以转换为高字节序列
*/
private static byte[] reverse(byte[] bytes)
{
Array.Reverse(bytes);
return bytes;
}
public override void WriteShort(short value)
{
WriteBytes(reverse(GetBytes(value)));
}
public override short ReadShort()
{
return GetInt16(reverse(ReadBytes(2)));
}
public override void WriteRawInt(int value)
{
WriteBytes(reverse(GetBytes(value)));
}
public override int ReadRawInt()
{
return GetInt32(reverse(ReadBytes(4)));
}
public override void WriteFloat(float value)
{
WriteBytes(reverse(GetBytes(value)));
}
public override float ReadFloat()
{
return GetSingle(reverse(ReadBytes(4)));
}
public override void WriteDouble(double value)
{
WriteBytes(reverse(GetBytes(value)));
}
public override double ReadDouble()
{
return GetDouble(reverse(ReadBytes(8)));
}
}
}
@@ -0,0 +1,7 @@
namespace CsProtocol.Buffer
{
public interface IPacket
{
short ProtocolId();
}
}
@@ -0,0 +1,12 @@
namespace CsProtocol.Buffer
{
public interface IProtocolRegistration
{
short ProtocolId();
void Write(ByteBuffer buffer, IPacket packet);
IPacket Read(ByteBuffer buffer);
}
}
@@ -0,0 +1,662 @@
using System;
using System.Collections.Generic;
using CsProtocol.Buffer;
namespace CsProtocol
{
// 复杂的对象
// 包括了各种复杂的结构,数组,List,Set,Map
//
// @author jaysunxiao
// @version 3.0
public class ComplexObject : IPacket
{
// byte类型,最简单的整形
public byte a;
// byte的包装类型
// 优先使用基础类型,包装类型会有装箱拆箱
public byte aa;
// 数组类型
public byte[] aaa;
public byte[] aaaa;
public short b;
public short bb;
public short[] bbb;
public short[] bbbb;
public int c;
public int cc;
public int[] ccc;
public int[] cccc;
public long d;
public long dd;
public long[] ddd;
public long[] dddd;
public float e;
public float ee;
public float[] eee;
public float[] eeee;
public double f;
public double ff;
public double[] fff;
public double[] ffff;
public bool g;
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;
public ObjectA[] kkk;
public List<int> l;
public List<List<List<int>>> ll;
public List<List<ObjectA>> lll;
public List<string> llll;
public List<Dictionary<int, string>> lllll;
public Dictionary<int, string> m;
public Dictionary<int, ObjectA> mm;
public Dictionary<ObjectA, List<int>> mmm;
public Dictionary<List<List<ObjectA>>, List<List<List<int>>>> mmmm;
public Dictionary<List<Dictionary<int, string>>, HashSet<Dictionary<int, string>>> mmmmm;
public HashSet<int> s;
public HashSet<HashSet<List<int>>> ss;
public HashSet<HashSet<ObjectA>> sss;
public HashSet<string> ssss;
public HashSet<Dictionary<int, string>> sssss;
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)
{
var packet = new ComplexObject();
packet.a = a;
packet.aa = aa;
packet.aaa = aaa;
packet.aaaa = aaaa;
packet.b = b;
packet.bb = bb;
packet.bbb = bbb;
packet.bbbb = bbbb;
packet.c = c;
packet.cc = cc;
packet.ccc = ccc;
packet.cccc = cccc;
packet.d = d;
packet.dd = dd;
packet.ddd = ddd;
packet.dddd = dddd;
packet.e = e;
packet.ee = ee;
packet.eee = eee;
packet.eeee = eeee;
packet.f = f;
packet.ff = ff;
packet.fff = fff;
packet.ffff = ffff;
packet.g = g;
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;
packet.kkk = kkk;
packet.l = l;
packet.ll = ll;
packet.lll = lll;
packet.llll = llll;
packet.lllll = lllll;
packet.m = m;
packet.mm = mm;
packet.mmm = mmm;
packet.mmmm = mmmm;
packet.mmmmm = mmmmm;
packet.s = s;
packet.ss = ss;
packet.sss = sss;
packet.ssss = ssss;
packet.sssss = sssss;
return packet;
}
public short ProtocolId()
{
return 100;
}
}
public class ComplexObjectRegistration : IProtocolRegistration
{
public short ProtocolId()
{
return 100;
}
public void Write(ByteBuffer buffer, IPacket packet)
{
if (buffer.WritePacketFlag(packet))
{
return;
}
ComplexObject message = (ComplexObject) packet;
buffer.WriteByte(message.a);
buffer.WriteByte(message.aa);
buffer.WriteByteArray(message.aaa);
buffer.WriteByteArray(message.aaaa);
buffer.WriteShort(message.b);
buffer.WriteShort(message.bb);
buffer.WriteShortArray(message.bbb);
buffer.WriteShortArray(message.bbbb);
buffer.WriteInt(message.c);
buffer.WriteInt(message.cc);
buffer.WriteIntArray(message.ccc);
buffer.WriteIntArray(message.cccc);
buffer.WriteLong(message.d);
buffer.WriteLong(message.dd);
buffer.WriteLongArray(message.ddd);
buffer.WriteLongArray(message.dddd);
buffer.WriteFloat(message.e);
buffer.WriteFloat(message.ee);
buffer.WriteFloatArray(message.eee);
buffer.WriteFloatArray(message.eeee);
buffer.WriteDouble(message.f);
buffer.WriteDouble(message.ff);
buffer.WriteDoubleArray(message.fff);
buffer.WriteDoubleArray(message.ffff);
buffer.WriteBool(message.g);
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);
buffer.WritePacketArray<ObjectA>(message.kkk, 102);
buffer.WriteIntList(message.l);
if (message.ll == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(message.ll.Count);
int length0 = message.ll.Count;
for (int i1 = 0; i1 < length0; i1++)
{
var element2 = message.ll[i1];
if (element2 == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(element2.Count);
int length3 = element2.Count;
for (int i4 = 0; i4 < length3; i4++)
{
var element5 = element2[i4];
buffer.WriteIntList(element5);
}
}
}
}
if (message.lll == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(message.lll.Count);
int length6 = message.lll.Count;
for (int i7 = 0; i7 < length6; i7++)
{
var element8 = message.lll[i7];
buffer.WritePacketList(element8, 102);
}
}
buffer.WriteStringList(message.llll);
if (message.lllll == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(message.lllll.Count);
int length9 = message.lllll.Count;
for (int i10 = 0; i10 < length9; i10++)
{
var element11 = message.lllll[i10];
buffer.WriteIntStringMap(element11);
}
}
buffer.WriteIntStringMap(message.m);
buffer.WriteIntPacketMap(message.mm, 102);
if ((message.mmm == null) || (message.mmm.Count == 0))
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(message.mmm.Count);
foreach (var i12 in message.mmm)
{
var keyElement13 = i12.Key;
var valueElement14 = i12.Value;
buffer.WritePacket(keyElement13, 102);
buffer.WriteIntList(valueElement14);
}
}
if ((message.mmmm == null) || (message.mmmm.Count == 0))
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(message.mmmm.Count);
foreach (var i15 in message.mmmm)
{
var keyElement16 = i15.Key;
var valueElement17 = i15.Value;
if (keyElement16 == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(keyElement16.Count);
int length18 = keyElement16.Count;
for (int i19 = 0; i19 < length18; i19++)
{
var element20 = keyElement16[i19];
buffer.WritePacketList(element20, 102);
}
}
if (valueElement17 == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(valueElement17.Count);
int length21 = valueElement17.Count;
for (int i22 = 0; i22 < length21; i22++)
{
var element23 = valueElement17[i22];
if (element23 == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(element23.Count);
int length24 = element23.Count;
for (int i25 = 0; i25 < length24; i25++)
{
var element26 = element23[i25];
buffer.WriteIntList(element26);
}
}
}
}
}
}
if ((message.mmmmm == null) || (message.mmmmm.Count == 0))
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(message.mmmmm.Count);
foreach (var i27 in message.mmmmm)
{
var keyElement28 = i27.Key;
var valueElement29 = i27.Value;
if (keyElement28 == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(keyElement28.Count);
int length30 = keyElement28.Count;
for (int i31 = 0; i31 < length30; i31++)
{
var element32 = keyElement28[i31];
buffer.WriteIntStringMap(element32);
}
}
if (valueElement29 == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(valueElement29.Count);
foreach (var i33 in valueElement29)
{
buffer.WriteIntStringMap(i33);
}
}
}
}
buffer.WriteIntSet(message.s);
if (message.ss == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(message.ss.Count);
foreach (var i34 in message.ss)
{
if (i34 == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(i34.Count);
foreach (var i35 in i34)
{
buffer.WriteIntList(i35);
}
}
}
}
if (message.sss == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(message.sss.Count);
foreach (var i36 in message.sss)
{
buffer.WritePacketSet(i36, 102);
}
}
buffer.WriteStringSet(message.ssss);
if (message.sssss == null)
{
buffer.WriteInt(0);
}
else
{
buffer.WriteInt(message.sssss.Count);
foreach (var i37 in message.sssss)
{
buffer.WriteIntStringMap(i37);
}
}
}
public IPacket Read(ByteBuffer buffer)
{
if (!buffer.ReadBool())
{
return null;
}
ComplexObject packet = new ComplexObject();
byte result38 = buffer.ReadByte();
packet.a = result38;
byte result39 = buffer.ReadByte();
packet.aa = result39;
var array40 = buffer.ReadByteArray();
packet.aaa = array40;
var array41 = buffer.ReadByteArray();
packet.aaaa = array41;
short result42 = buffer.ReadShort();
packet.b = result42;
short result43 = buffer.ReadShort();
packet.bb = result43;
var array44 = buffer.ReadShortArray();
packet.bbb = array44;
var array45 = buffer.ReadShortArray();
packet.bbbb = array45;
int result46 = buffer.ReadInt();
packet.c = result46;
int result47 = buffer.ReadInt();
packet.cc = result47;
var array48 = buffer.ReadIntArray();
packet.ccc = array48;
var array49 = buffer.ReadIntArray();
packet.cccc = array49;
long result50 = buffer.ReadLong();
packet.d = result50;
long result51 = buffer.ReadLong();
packet.dd = result51;
var array52 = buffer.ReadLongArray();
packet.ddd = array52;
var array53 = buffer.ReadLongArray();
packet.dddd = array53;
float result54 = buffer.ReadFloat();
packet.e = result54;
float result55 = buffer.ReadFloat();
packet.ee = result55;
var array56 = buffer.ReadFloatArray();
packet.eee = array56;
var array57 = buffer.ReadFloatArray();
packet.eeee = array57;
double result58 = buffer.ReadDouble();
packet.f = result58;
double result59 = buffer.ReadDouble();
packet.ff = result59;
var array60 = buffer.ReadDoubleArray();
packet.fff = array60;
var array61 = buffer.ReadDoubleArray();
packet.ffff = array61;
bool result62 = buffer.ReadBool();
packet.g = result62;
bool result63 = buffer.ReadBool();
packet.gg = result63;
var array64 = buffer.ReadBooleanArray();
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)
{
for (int index76 = 0; index76 < size77; index76++)
{
int size80 = buffer.ReadInt();
var result78 = new List<List<int>>(size80);
if (size80 > 0)
{
for (int index79 = 0; index79 < size80; index79++)
{
var list81 = buffer.ReadIntList();
result78.Add(list81);
}
}
result75.Add(result78);
}
}
packet.ll = result75;
int size84 = buffer.ReadInt();
var result82 = new List<List<ObjectA>>(size84);
if (size84 > 0)
{
for (int index83 = 0; index83 < size84; index83++)
{
var list85 = buffer.ReadPacketList<ObjectA>(102);
result82.Add(list85);
}
}
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)
{
for (int index88 = 0; index88 < size89; index88++)
{
var map90 = buffer.ReadIntStringMap();
result87.Add(map90);
}
}
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)
{
for (var index95 = 0; index95 < size94; index95++)
{
ObjectA result96 = buffer.ReadPacket<ObjectA>(102);
var list97 = buffer.ReadIntList();
result93[result96] = list97;
}
}
packet.mmm = result93;
int size99 = buffer.ReadInt();
var result98 = new Dictionary<List<List<ObjectA>>, List<List<List<int>>>>(size99);
if (size99 > 0)
{
for (var index100 = 0; index100 < size99; index100++)
{
int size103 = buffer.ReadInt();
var result101 = new List<List<ObjectA>>(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)
{
for (int index109 = 0; index109 < size110; index109++)
{
var list111 = buffer.ReadIntList();
result108.Add(list111);
}
}
result105.Add(result108);
}
}
result98[result101] = result105;
}
}
packet.mmmm = result98;
int size113 = buffer.ReadInt();
var result112 = new Dictionary<List<Dictionary<int, string>>, HashSet<Dictionary<int, string>>>(size113);
if (size113 > 0)
{
for (var index114 = 0; index114 < size113; index114++)
{
int size117 = buffer.ReadInt();
var result115 = new List<Dictionary<int, string>>(size117);
if (size117 > 0)
{
for (int index116 = 0; index116 < size117; index116++)
{
var map118 = buffer.ReadIntStringMap();
result115.Add(map118);
}
}
int size121 = buffer.ReadInt();
var result119 = new HashSet<Dictionary<int, string>>();
if (size121 > 0)
{
for (int index120 = 0; index120 < size121; index120++)
{
var map122 = buffer.ReadIntStringMap();
result119.Add(map122);
}
}
result112[result115] = result119;
}
}
packet.mmmmm = result112;
var set123 = buffer.ReadIntSet();
packet.s = set123;
int size126 = buffer.ReadInt();
var result124 = new HashSet<HashSet<List<int>>>();
if (size126 > 0)
{
for (int index125 = 0; index125 < size126; index125++)
{
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);
}
}
packet.ss = result124;
int size133 = buffer.ReadInt();
var result131 = new HashSet<HashSet<ObjectA>>();
if (size133 > 0)
{
for (int index132 = 0; index132 < size133; index132++)
{
var set134 = buffer.ReadPacketSet<ObjectA>(102);
result131.Add(set134);
}
}
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 = result136;
return packet;
}
}
}
@@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using CsProtocol.Buffer;
namespace CsProtocol
{
// @author jaysunxiao
// @version 3.0
public class NormalObject : IPacket
{
public byte a;
public byte[] aaa;
public short b;
public int c;
public long d;
public float e;
public double f;
public bool g;
public string jj;
public ObjectA kk;
public List<int> l;
public List<long> ll;
public List<ObjectA> lll;
public List<string> llll;
public Dictionary<int, string> m;
public Dictionary<int, ObjectA> mm;
public HashSet<int> s;
public 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)
{
var packet = new NormalObject();
packet.a = a;
packet.aaa = aaa;
packet.b = b;
packet.c = c;
packet.d = d;
packet.e = e;
packet.f = f;
packet.g = g;
packet.jj = jj;
packet.kk = kk;
packet.l = l;
packet.ll = ll;
packet.lll = lll;
packet.llll = llll;
packet.m = m;
packet.mm = mm;
packet.s = s;
packet.ssss = ssss;
return packet;
}
public short ProtocolId()
{
return 101;
}
}
public class NormalObjectRegistration : IProtocolRegistration
{
public short ProtocolId()
{
return 101;
}
public void Write(ByteBuffer buffer, IPacket packet)
{
if (buffer.WritePacketFlag(packet))
{
return;
}
NormalObject message = (NormalObject) packet;
buffer.WriteByte(message.a);
buffer.WriteByteArray(message.aaa);
buffer.WriteShort(message.b);
buffer.WriteInt(message.c);
buffer.WriteLong(message.d);
buffer.WriteFloat(message.e);
buffer.WriteDouble(message.f);
buffer.WriteBool(message.g);
buffer.WriteString(message.jj);
buffer.WritePacket(message.kk, 102);
buffer.WriteIntList(message.l);
buffer.WriteLongList(message.ll);
buffer.WritePacketList(message.lll, 102);
buffer.WriteStringList(message.llll);
buffer.WriteIntStringMap(message.m);
buffer.WriteIntPacketMap(message.mm, 102);
buffer.WriteIntSet(message.s);
buffer.WriteStringSet(message.ssss);
}
public IPacket Read(ByteBuffer buffer)
{
if (!buffer.ReadBool())
{
return null;
}
NormalObject packet = new NormalObject();
byte result0 = buffer.ReadByte();
packet.a = result0;
var array1 = buffer.ReadByteArray();
packet.aaa = array1;
short result2 = buffer.ReadShort();
packet.b = result2;
int result3 = buffer.ReadInt();
packet.c = result3;
long result4 = buffer.ReadLong();
packet.d = result4;
float result5 = buffer.ReadFloat();
packet.e = result5;
double result6 = buffer.ReadDouble();
packet.f = result6;
bool result7 = buffer.ReadBool();
packet.g = result7;
string result8 = buffer.ReadString();
packet.jj = result8;
ObjectA result9 = buffer.ReadPacket<ObjectA>(102);
packet.kk = result9;
var list10 = buffer.ReadIntList();
packet.l = list10;
var list11 = buffer.ReadLongList();
packet.ll = list11;
var list12 = buffer.ReadPacketList<ObjectA>(102);
packet.lll = list12;
var list13 = buffer.ReadStringList();
packet.llll = list13;
var map14 = buffer.ReadIntStringMap();
packet.m = map14;
var map15 = buffer.ReadIntPacketMap<ObjectA>(102);
packet.mm = map15;
var set16 = buffer.ReadIntSet();
packet.s = set16;
var set17 = buffer.ReadStringSet();
packet.ssss = set17;
return packet;
}
}
}
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using CsProtocol.Buffer;
namespace CsProtocol
{
// @author jaysunxiao
// @version 3.0
public class ObjectA : IPacket
{
public int a;
public Dictionary<int, string> m;
public ObjectB objectB;
public static ObjectA ValueOf(int a, Dictionary<int, string> m, ObjectB objectB)
{
var packet = new ObjectA();
packet.a = a;
packet.m = m;
packet.objectB = objectB;
return packet;
}
public short ProtocolId()
{
return 102;
}
}
public class ObjectARegistration : IProtocolRegistration
{
public short ProtocolId()
{
return 102;
}
public void Write(ByteBuffer buffer, IPacket packet)
{
if (buffer.WritePacketFlag(packet))
{
return;
}
ObjectA message = (ObjectA) packet;
buffer.WriteInt(message.a);
buffer.WriteIntStringMap(message.m);
buffer.WritePacket(message.objectB, 103);
}
public IPacket Read(ByteBuffer buffer)
{
if (!buffer.ReadBool())
{
return null;
}
ObjectA packet = new ObjectA();
int result0 = buffer.ReadInt();
packet.a = result0;
var map1 = buffer.ReadIntStringMap();
packet.m = map1;
ObjectB result2 = buffer.ReadPacket<ObjectB>(103);
packet.objectB = result2;
return packet;
}
}
}
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using CsProtocol.Buffer;
namespace CsProtocol
{
// @author jaysunxiao
// @version 3.0
public class ObjectB : IPacket
{
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, IPacket packet)
{
if (buffer.WritePacketFlag(packet))
{
return;
}
ObjectB message = (ObjectB) packet;
buffer.WriteBool(message.flag);
}
public IPacket Read(ByteBuffer buffer)
{
if (!buffer.ReadBool())
{
return null;
}
ObjectB packet = new ObjectB();
bool result0 = buffer.ReadBool();
packet.flag = result0;
return packet;
}
}
}
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using CsProtocol.Buffer;
namespace CsProtocol
{
// @author jaysunxiao
// @version 3.0
public class SimpleObject : IPacket
{
public int c;
public bool g;
public static SimpleObject ValueOf(int c, bool g)
{
var packet = new SimpleObject();
packet.c = c;
packet.g = g;
return packet;
}
public short ProtocolId()
{
return 104;
}
}
public class SimpleObjectRegistration : IProtocolRegistration
{
public short ProtocolId()
{
return 104;
}
public void Write(ByteBuffer buffer, IPacket packet)
{
if (buffer.WritePacketFlag(packet))
{
return;
}
SimpleObject message = (SimpleObject) packet;
buffer.WriteInt(message.c);
buffer.WriteBool(message.g);
}
public IPacket Read(ByteBuffer buffer)
{
if (!buffer.ReadBool())
{
return null;
}
SimpleObject packet = new SimpleObject();
int result0 = buffer.ReadInt();
packet.c = result0;
bool result1 = buffer.ReadBool();
packet.g = result1;
return packet;
}
}
}
@@ -0,0 +1,69 @@
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, IPacket packet)
{
var protocolId = packet.ProtocolId();
// 写入协议号
buffer.WriteShort(protocolId);
// 写入包体
GetProtocol(protocolId).Write(buffer, packet);
}
public static IPacket Read(ByteBuffer buffer)
{
var protocolId = buffer.ReadShort();
return GetProtocol(protocolId).Read(buffer);
}
}
}
+190
View File
@@ -0,0 +1,190 @@
using System;
using System.IO;
using CsProtocol;
using CsProtocol.Buffer;
namespace csharp
{
class Program
{
static void Main(string[] args)
{
ByteBufferTest();
ProtocolManager.InitProtocol();
// 获取复杂对象的字节流
var complexObjectBytes = File.ReadAllBytes("C:\\zfoo\\protocol\\src\\test\\resources\\ComplexObject.bytes");
var buffer = ByteBuffer.ValueOf();
buffer.WriteBytes(complexObjectBytes);
var packet = ProtocolManager.Read(buffer);
var newBuffer = ByteBuffer.ValueOf();
ProtocolManager.Write(newBuffer, packet);
var bytes = newBuffer.ToBytes();
// set和map是无序的,所以有的时候输入和输出的字节流有可能不一致,但是长度一定是一致的
}
public static void ByteBufferTest()
{
byteTest();
bytesTest();
shortTest();
intTest();
longTest();
floatTest();
doubleTest();
charTest();
stringTest();
}
public static void byteTest()
{
byte value = 9;
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
writerByteBuffer.WriteByte(value);
byte[] bytes = writerByteBuffer.ToBytes();
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
readerByteBuffer.WriteBytes(bytes);
byte readValue = readerByteBuffer.ReadByte();
AssertEquals(value, readValue);
}
public static void bytesTest()
{
var value = new byte[] {1, 2, 3};
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
writerByteBuffer.WriteBytes(value);
byte[] bytes = writerByteBuffer.ToBytes();
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
readerByteBuffer.WriteBytes(bytes);
var readValue = readerByteBuffer.ReadBytes(3);
AssertEquals<byte>(value, readValue);
}
public static void shortTest()
{
short value = 9999;
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
writerByteBuffer.WriteShort(value);
byte[] bytes = writerByteBuffer.ToBytes();
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
readerByteBuffer.WriteBytes(bytes);
short readValue = readerByteBuffer.ReadShort();
AssertEquals(value, readValue);
}
public static void intTest()
{
int value = 99999999;
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
writerByteBuffer.WriteInt(value);
byte[] bytes = writerByteBuffer.ToBytes();
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
readerByteBuffer.WriteBytes(bytes);
int readValue = readerByteBuffer.ReadInt();
AssertEquals(value, readValue);
}
public static void longTest()
{
long value = 9999999999999999L;
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
writerByteBuffer.WriteLong(value);
byte[] bytes = writerByteBuffer.ToBytes();
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
readerByteBuffer.WriteBytes(bytes);
long readValue = readerByteBuffer.ReadLong();
AssertEquals(value, readValue);
}
public static void floatTest()
{
float value = 999999.56F;
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
writerByteBuffer.WriteFloat(value);
byte[] bytes = writerByteBuffer.ToBytes();
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
readerByteBuffer.WriteBytes(bytes);
float readValue = readerByteBuffer.ReadFloat();
AssertEquals(value, readValue);
}
public static void doubleTest()
{
double value = 999999.56;
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
writerByteBuffer.WriteDouble(value);
byte[] bytes = writerByteBuffer.ToBytes();
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
readerByteBuffer.WriteBytes(bytes);
double readValue = readerByteBuffer.ReadDouble();
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";
ByteBuffer writerByteBuffer = ByteBuffer.ValueOf();
writerByteBuffer.WriteString(value);
byte[] bytes = writerByteBuffer.ToBytes();
ByteBuffer readerByteBuffer = ByteBuffer.ValueOf();
readerByteBuffer.WriteBytes(bytes);
string readValue = readerByteBuffer.ReadString();
AssertEquals(value, readValue);
}
public static void AssertEquals(object a, object b)
{
if (a.Equals(b))
{
return;
}
throw new Exception("a is not equals b");
}
public static void AssertEquals<T>(T[] a, T[] b)
{
if (a == b)
{
return;
}
if (a != null && b != null && a.Length == b.Length)
{
for (var i = 0; i < a.Length; i++)
{
AssertEquals(a[i], b[i]);
}
return;
}
throw new Exception("a is not equals b");
}
}
}
+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