Exemple #1
0
        internal byte[] Recv()
        {
            if (sock == null)
            {
                throw new PDBException(PDBErrorCode.PdbE_NET_ERROR, "网络错误:未连接");
            }

            int totalLen = 0;

            byte[] headBuf = new byte[ProtoHeader.kHeadLen];

            while (totalLen < ProtoHeader.kHeadLen)
            {
                int tmpLen  = ProtoHeader.kHeadLen - totalLen;
                int recvLen = sock.GetStream().Read(headBuf, totalLen, tmpLen);
                totalLen += recvLen;
            }

            ProtoHeader proHdr     = new ProtoHeader(headBuf);
            UInt32      headCrc    = proHdr.GetHeadCrc();
            UInt32      tmpHeadCrc = CRC.Crc32(headBuf, 0, 60);

            if (headCrc != tmpHeadCrc)
            {
                throw new PDBException(PDBErrorCode.PdbE_NET_ERROR, "报文错误:回复报文中,回复头校验错误");
            }

            int bodyLen = (int)proHdr.GetBodyLen();

            if (bodyLen == 0)
            {
                return(headBuf);
            }

            byte[] packetBuf = new byte[ProtoHeader.kHeadLen + bodyLen];
            Array.Copy(headBuf, 0, packetBuf, 0, ProtoHeader.kHeadLen);
            totalLen = 0;
            while (totalLen < bodyLen)
            {
                int tmpLen  = bodyLen - totalLen;
                int recvLen = sock.GetStream().Read(packetBuf, (ProtoHeader.kHeadLen + totalLen), tmpLen);
                totalLen += recvLen;
            }

            UInt32 bodyCrc = proHdr.GetBodyCrc();

            UInt32 tmpBodyCrc = CRC.Crc32(packetBuf, ProtoHeader.kHeadLen, bodyLen);

            if (tmpBodyCrc != bodyCrc)
            {
                throw new PDBException(PDBErrorCode.PdbE_PACKET_ERROR, "报文错误:回复报文中,回复头校验错误");
            }

            return(packetBuf);
        }