private void DecodeInsertTablePacket(byte[] resPacket, out PDBErrorCode retVal) { ProtoHeader proHdr = new ProtoHeader(resPacket); InsertResult.Clear(); SuccessCount = 0; retVal = (PDBErrorCode)proHdr.GetReturnVal(); if (retVal == PDBErrorCode.PdbE_OK) { SuccessCount = (int)proHdr.GetRecordCnt(); for (int i = 0; i < SuccessCount; i++) { InsertResult.Add(PDBErrorCode.PdbE_OK); } } else if (retVal == PDBErrorCode.PdbE_INSERT_PART_ERROR) { int offset = ProtoHeader.kHeadLen; int recCnt = (int)proHdr.GetRecordCnt(); for (int i = 0; i < recCnt; i++) { long recErrVal = IntTool.DecodeZigZag64(IntTool.ReadRawVarint64(resPacket, ref offset)); PDBErrorCode recErrCode = (PDBErrorCode)(recErrVal); if (recErrCode == PDBErrorCode.PdbE_OK) { SuccessCount++; } InsertResult.Add(recErrCode); } } }
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); }
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); }
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()); } }
private void DecodeInsertPacket(byte[] resPacket, out PDBErrorCode retVal) { ProtoHeader proHdr = new ProtoHeader(resPacket); if (resPacket.Count() != ProtoHeader.kHeadLen) { retVal = PDBErrorCode.PdbE_PACKET_ERROR; return; } SuccessCount = (int)proHdr.GetRecordCnt(); InsertResult.Clear(); for (int i = 0; i < SuccessCount; i++) { InsertResult.Add(PDBErrorCode.PdbE_OK); } retVal = (PDBErrorCode)proHdr.GetReturnVal(); if (retVal != PDBErrorCode.PdbE_OK) { InsertResult.Add(retVal); } }
/// <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); } } }
private void DecodePacket(byte[] resPacket, out PDBErrorCode retVal, DataTable resultTab) { ProtoHeader proHdr = new ProtoHeader(resPacket); retVal = (PDBErrorCode)proHdr.GetReturnVal(); resultTab.Clear(); resultTab.Columns.Clear(); int offset = ProtoHeader.kHeadLen; uint fieldCnt = proHdr.GetFieldCnt(); if (retVal != PDBErrorCode.PdbE_OK) { return; } List <object> headList = GetRecord(fieldCnt, resPacket, ref offset); foreach (object obj in headList) { if (obj.GetType() == typeof(System.String)) { string str = (string)obj; string[] strArr = str.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (strArr.Count() != 2) { throw new PDBException(PDBErrorCode.PdbE_PACKET_ERROR, "报文错误:非法的表头"); } DataColumn colInfo = null; switch (strArr[0]) { case "bool": colInfo = new DataColumn(strArr[1], typeof(bool)); break; case "tinyint": colInfo = new DataColumn(strArr[1], typeof(sbyte)); break; case "smallint": colInfo = new DataColumn(strArr[1], typeof(short)); break; case "int": colInfo = new DataColumn(strArr[1], typeof(int)); break; case "bigint": colInfo = new DataColumn(strArr[1], typeof(long)); break; case "datetime": colInfo = new DataColumn(strArr[1], typeof(DateTime)); break; case "float": colInfo = new DataColumn(strArr[1], typeof(float)); break; case "double": colInfo = new DataColumn(strArr[1], typeof(double)); break; case "string": colInfo = new DataColumn(strArr[1], typeof(string)); break; case "blob": colInfo = new DataColumn(strArr[1], typeof(byte[])); break; default: throw new PDBException(PDBErrorCode.PdbE_PACKET_ERROR, "报文错误:非法的表头"); } resultTab.Columns.Add(colInfo); } else { throw new PDBException(PDBErrorCode.PdbE_PACKET_ERROR, "报文错误:非法的表头"); } } while (offset < resPacket.Count()) { DataRow dataRow = resultTab.NewRow(); List <object> dataList = GetRecord(fieldCnt, resPacket, ref offset); if (dataList.Count() != headList.Count()) { throw new PDBException(PDBErrorCode.PdbE_PACKET_ERROR); } for (int i = 0; i < dataList.Count(); i++) { if (dataList[i] != null) { dataRow[i] = dataList[i]; } else { dataRow[i] = DBNull.Value; } } resultTab.Rows.Add(dataRow); } }
private byte[] MakeInsertTableRequestPacket(string tabName, DataTable dataTable) { int bodyLen = 0; int totalLen = 0; int fieldCnt = dataTable.Columns.Count; if (string.IsNullOrEmpty(tabName)) { throw new PDBException(PDBErrorCode.PdbE_INVALID_TABLE_NAME, "非法的表名"); } if (Encoding.UTF8.GetByteCount(tabName) >= kTableNameLen) { throw new PDBException(PDBErrorCode.PdbE_INVALID_TABLE_NAME, "表名过长"); } ByteStream byteStream = new ByteStream(); IntTool.WriteString(byteStream, tabName); foreach (DataColumn colInfo in dataTable.Columns) { IntTool.WriteString(byteStream, colInfo.ColumnName); } foreach (DataRow dr in dataTable.Rows) { for (int fieldIdx = 0; fieldIdx < fieldCnt; fieldIdx++) { if (dr[fieldIdx] is DBNull) { IntTool.WriteNull(byteStream); } else if (dr[fieldIdx] is bool) { IntTool.WriteBool(byteStream, Convert.ToBoolean(dr[fieldIdx])); } else if (dr[fieldIdx] is sbyte) { IntTool.WriteVarint64(byteStream, Convert.ToSByte(dr[fieldIdx])); } else if (dr[fieldIdx] is short) { IntTool.WriteVarint16(byteStream, Convert.ToInt16(dr[fieldIdx])); } else if (dr[fieldIdx] is int) { IntTool.WriteVarint32(byteStream, Convert.ToInt32(dr[fieldIdx])); } else if (dr[fieldIdx] is long) { IntTool.WriteVarint64(byteStream, Convert.ToInt64(dr[fieldIdx])); } else if (dr[fieldIdx] is float) { IntTool.WriteFloat(byteStream, Convert.ToSingle(dr[fieldIdx])); } else if (dr[fieldIdx] is double) { IntTool.WriteDouble(byteStream, Convert.ToDouble(dr[fieldIdx])); } else if (dr[fieldIdx] is DateTime) { IntTool.WriteDatetime(byteStream, Convert.ToDateTime(dr[fieldIdx])); } else if (dr[fieldIdx] is string) { IntTool.WriteString(byteStream, Convert.ToString(dr[fieldIdx])); } else if (dr[fieldIdx] is byte[]) { IntTool.WriteBlob(byteStream, (byte[])dr[fieldIdx]); } else { throw new Exception("不支持的值"); } } } bodyLen = byteStream.GetTotalLen(); totalLen = ProtoHeader.kHeadLen + bodyLen; byte[] packetBuf = new byte[totalLen]; Array.Clear(packetBuf, 0, totalLen); byteStream.Read(packetBuf, ProtoHeader.kHeadLen); uint bodyCrc = CRC.Crc32(packetBuf, ProtoHeader.kHeadLen, bodyLen); ProtoHeader proHdr = new ProtoHeader(packetBuf); proHdr.SetVersion(ProtoHeader.kProtoVersion); proHdr.SetMethod(ProtoHeader.MethodCmdInsertTableReq); proHdr.SetBodyLen((uint)bodyLen); proHdr.SetFieldCnt((uint)fieldCnt); proHdr.SetRecordCnt((uint)dataTable.Rows.Count); proHdr.SetBodyCrc(bodyCrc); proHdr.SetHeadCrc(); return(packetBuf); }