From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- Client/Assets/Scripts/XMainClient/ProtocolHead.cs | 163 ++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 Client/Assets/Scripts/XMainClient/ProtocolHead.cs (limited to 'Client/Assets/Scripts/XMainClient/ProtocolHead.cs') diff --git a/Client/Assets/Scripts/XMainClient/ProtocolHead.cs b/Client/Assets/Scripts/XMainClient/ProtocolHead.cs new file mode 100644 index 00000000..6e2a20d2 --- /dev/null +++ b/Client/Assets/Scripts/XMainClient/ProtocolHead.cs @@ -0,0 +1,163 @@ +using System; +using System.IO; +using XUtliPoolLib; + +namespace XMainClient +{ + public class ProtocolHead + { + public bool IsPtc + { + get + { + return !this.TestBit(this.flag, 0); + } + } + + public bool IsRpc + { + get + { + return this.TestBit(this.flag, 0); + } + } + + public bool IsRpcReply + { + get + { + return !this.TestBit(this.flag, 1); + } + } + + public bool IsRpcRequest + { + get + { + return this.TestBit(this.flag, 1); + } + } + + public bool IsCompressed + { + get + { + return this.TestBit(this.flag, 2); + } + } + + public bool IsRpcNull + { + get + { + return this.TestBit(this.flag, 3); + } + } + + public int Size + { + get + { + bool isRpc = this.IsRpc; + int result; + if (isRpc) + { + result = 16; + } + else + { + result = 12; + } + return result; + } + } + + public static ProtocolHead SharedHead = new ProtocolHead(); + + public static byte[] sharedUIntBuffer = new byte[4]; + + public uint len; + + public uint type; + + public uint flag; + + public uint tagID; + + public const uint MinSize = 12u; + + public bool TestBit(uint value, int bit) + { + return ((ulong)value & (ulong)(1L << (bit & 31))) > 0UL; + } + + public ProtocolHead() + { + this.len = 0u; + this.type = 0u; + this.flag = 0u; + } + + public void Reset() + { + this.len = 0u; + this.type = 0u; + this.flag = 0u; + this.tagID = 0u; + } + + public void Deserialize(byte[] bytes) + { + this.len = BitConverter.ToUInt32(bytes, 0); + this.type = BitConverter.ToUInt32(bytes, 4); + this.flag = BitConverter.ToUInt32(bytes, 8); + bool isRpc = this.IsRpc; + if (isRpc) + { + this.tagID = BitConverter.ToUInt32(bytes, 12); + } + } + + private uint ToUInt32(ref SmallBuffer sb, int startIndex) + { + uint num = (uint)sb[startIndex]; + uint num2 = (uint)sb[startIndex + 1]; + uint num3 = (uint)sb[startIndex + 2]; + uint num4 = (uint)sb[startIndex + 3]; + return num | num2 << 8 | num3 << 16 | num4 << 24; + } + + public void Deserialize(ref SmallBuffer sb) + { + this.len = this.ToUInt32(ref sb, 0); + this.type = this.ToUInt32(ref sb, 4); + this.flag = this.ToUInt32(ref sb, 8); + bool isRpc = this.IsRpc; + if (isRpc) + { + this.tagID = this.ToUInt32(ref sb, 12); + } + } + + private byte[] GetBytes(uint value) + { + ProtocolHead.sharedUIntBuffer[0] = (byte)(value & 255u); + ProtocolHead.sharedUIntBuffer[1] = (byte)((value & 65280u) >> 8); + ProtocolHead.sharedUIntBuffer[2] = (byte)((value & 16711680u) >> 16); + ProtocolHead.sharedUIntBuffer[3] = (byte)((value & 4278190080u) >> 24); + return ProtocolHead.sharedUIntBuffer; + } + + public void Serialize(MemoryStream stream) + { + stream.Write(this.GetBytes(this.len), 0, 4); + stream.Write(this.GetBytes(this.type), 0, 4); + stream.Write(this.GetBytes(this.flag), 0, 4); + bool isRpc = this.IsRpc; + if (isRpc) + { + stream.Write(this.GetBytes(this.tagID), 0, 4); + } + } + } +} -- cgit v1.1-26-g67d0