Beispiel #1
0
        //解析数据包
        public void UnPack(byte[] data)
        {
            PackHead head       = new PackHead();
            int      headLength = SerializeUtil.ToPtr(head).Length;
            int      packLength = 0;

            byte[] totalData = new byte[LeftBytes.Length + data.Length];
            if (LeftBytes.Length > 0)
            {
                Array.Copy(LeftBytes, 0, totalData, 0, LeftBytes.Length);
            }
            Array.Copy(data, 0, totalData, LeftBytes.Length, data.Length);

            if (totalData.Length < headLength)
            {
                //消息头不足
                LeftBytes = totalData;
                return;
            }
            byte[] headData = new byte[headLength];
            Array.Copy(totalData, headData, headLength);
            head       = SerializeUtil.FromPtr <PackHead>(headData);
            packLength = head.BodyLength + headLength;

            if (totalData.Length < packLength)
            {
                //接受消息大小不足
                LeftBytes = totalData;
                return;
            }
            byte[] bodyData = new byte[head.BodyLength];
            Array.Copy(totalData, headLength, bodyData, 0, bodyData.Length);
            //整包发送
            SendPack(head, bodyData);
            LeftBytes = new byte[totalData.Length - packLength];
            if (LeftBytes.Length > 0)
            {
                //发生粘包
                Array.Copy(totalData, packLength, LeftBytes, 0, LeftBytes.Length);
                if (LeftBytes.Length >= headLength)
                {
                    //采用递归进行再次拆包
                    UnPack(new byte[0]);
                }
            }
        }