public CNetData Encode(CMessage msg) { ushort id = msg.GetID(); CNetData data = new CNetData(4096); SGTMsgHeader head = new SGTMsgHeader(); head.wMsgID = id; head.Encode(data); EncoderFunc pfnEncode = FindEncodeFunc(id); if (null == pfnEncode) { return(null); } if (-1 == pfnEncode(msg, ref data)) { return(null); } head.wDataLen = (ushort)(data.GetDataLen() - 8); head.wCheckSum = (ushort)((head.wDataLen ^ 0xBBCC) & 0x88AA); data.Replace(0, BitConverter.GetBytes(CNetData.Inverse(head.wCheckSum))); data.Replace(4, BitConverter.GetBytes(CNetData.Inverse(head.wDataLen))); return(data); }
public static uint ParsePackage(ref byte[] data, uint offset, uint size) { //包头不完整 if (size < 14) { return(0); } else { int temp = (int)offset + DATA_LEN_OFFSET; ushort iDataLen = CNetData.Inverse(Helper.ReadU16(data, ref temp)); temp = (int)offset + MSG_OFFSET; ushort iMsgID = CNetData.Inverse(Helper.ReadU16(data, ref temp)); { if (iDataLen + 8 <= size) //大于一个包 { return((uint)iDataLen + 8); } else //包内容不完整 { return(0); } } } }