Beispiel #1
0
        private byte[] MakeRequestPacket(uint reqMethod, string sql)
        {
            int bodyLen  = 0;
            int totalLen = 0;

            if (sql.Length == 0)
            {
                throw new PDBException(PDBErrorCode.PdbE_SQL_ERROR, "SQL语句错误:执行的SQL语句不能为空");
            }

            byte[] tmpSql = Encoding.UTF8.GetBytes(sql);

            bodyLen  = tmpSql.Count();
            totalLen = ProtoHeader.kHeadLen + bodyLen;

            byte[] packetBuf = new byte[totalLen];
            Array.Clear(packetBuf, 0, totalLen);

            Array.Copy(tmpSql, 0, packetBuf, ProtoHeader.kHeadLen, tmpSql.Count());
            uint bodyCrc = CRC.Crc32(packetBuf, ProtoHeader.kHeadLen, bodyLen);

            ProtoHeader proHdr = new ProtoHeader(packetBuf);

            proHdr.SetVersion(ProtoHeader.kProtoVersion);
            proHdr.SetMethod(reqMethod);
            proHdr.SetBodyLen((uint)bodyLen);
            proHdr.SetReturnVal(0);
            proHdr.SetRecordCnt(1);
            proHdr.SetBodyCrc(bodyCrc);
            proHdr.SetHeadCrc();

            return(packetBuf);
        }
Beispiel #2
0
        private void Login()
        {
            byte[]      loginPacket = new byte[ProtoHeader.kHeadLen + kUserNameLen + kPwdLen];
            ProtoHeader proHdr      = new ProtoHeader(loginPacket);

            Array.Clear(loginPacket, 0, (loginPacket.Length));

            byte[] nameArray = System.Text.Encoding.Default.GetBytes(UserName);
            byte[] pwdArray  = System.Text.Encoding.Default.GetBytes(Password);

            if (nameArray.Length >= kUserNameLen)
            {
                throw new PDBException(PDBErrorCode.PdbE_INVALID_USER_NAME, "非法的用户名:用户名不合法");
            }
            for (int i = 0; i < nameArray.Length; i++)
            {
                loginPacket[ProtoHeader.kHeadLen + i] = nameArray[i];
            }
            UInt64 pwd64 = CRC.Crc64(pwdArray);
            UInt32 pwd32 = (UInt32)(pwd64 & 0xFFFFFFFF);

            IntTool.PutUint32(loginPacket, (ProtoHeader.kHeadLen + kUserNameLen), pwd32);

            UInt32 bodyCrc = CRC.Crc32(loginPacket, ProtoHeader.kHeadLen, (kUserNameLen + kPwdLen));

            proHdr.SetVersion(ProtoHeader.kProtoVersion);
            proHdr.SetMethod(ProtoHeader.MethodCmdLoginReq);
            proHdr.SetBodyLen((uint)(kUserNameLen + kPwdLen));
            proHdr.SetReturnVal(0);
            proHdr.SetRecordCnt((uint)0);
            proHdr.SetBodyCrc(bodyCrc);
            proHdr.SetHeadCrc();

            Request(loginPacket);

            byte[]      resBuf = Recv();
            ProtoHeader resHdr = new ProtoHeader(resBuf);

            if (resHdr.GetMethod() != ProtoHeader.MethodCmdLoginRep)
            {
                throw new PDBException(PDBErrorCode.PdbE_PACKET_ERROR);
            }
            if (resHdr.GetReturnVal() != 0)
            {
                throw new PDBException(PDBErrorCode.PdbE_NET_ERROR, "网络错误:登录失败," + resHdr.GetReturnVal().ToString());
            }
        }