Esempio n. 1
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());
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 其他,例如:添加用户,删除用户,删除数据 等等..
        /// </summary>
        /// <param name="sql">执行的SQL</param>
        public void ExecuteNonQuery(string sql)
        {
            SuccessCount = 0;
            lock (ConnObj)
            {
                byte[] sendPacket = MakeRequestPacket(ProtoHeader.MethodCmdNonQueryReq, sql);
                ConnObj.Request(sendPacket);

                byte[]      recvPacket = ConnObj.Recv();
                ProtoHeader proHdr     = new ProtoHeader(recvPacket);

                if (proHdr.GetMethod() != ProtoHeader.MethodCmdNonQueryRep)
                {
                    throw new PDBException(PDBErrorCode.PdbE_PACKET_ERROR, "报文错误:Error Packet");
                }

                PDBErrorCode errCode = (PDBErrorCode)proHdr.GetReturnVal();

                if (errCode != PDBErrorCode.PdbE_OK)
                {
                    throw new PDBException(errCode);
                }
            }
        }