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 object GetStoreData(byte[] buf, ref int offset, int valType) { object obj; switch ((PDBType)valType) { case PDBType.Null: return(DBNull.Value); case PDBType.Bool: return(IntTool.ReadSInt8(buf, ref offset) != 0); case PDBType.TinyInt: return(Convert.ToSByte(IntTool.DecodeZigZag64(IntTool.ReadRawVarint64(buf, ref offset)))); case PDBType.ShortInt: return(Convert.ToInt16(IntTool.DecodeZigZag64(IntTool.ReadRawVarint64(buf, ref offset)))); case PDBType.Int: return(Convert.ToInt32(IntTool.DecodeZigZag64(IntTool.ReadRawVarint64(buf, ref offset)))); case PDBType.BigInt: return(IntTool.DecodeZigZag64(IntTool.ReadRawVarint64(buf, ref offset))); case PDBType.Float: return(IntTool.ReadFloat(buf, ref offset)); case PDBType.Double: return(IntTool.ReadDouble(buf, ref offset)); case PDBType.DateTime: long tmpTs = IntTool.DecodeZigZag64(IntTool.ReadRawVarint64(buf, ref offset)); return(new DateTime(1970, 1, 1).Add(TimeZoneInfo.Local.BaseUtcOffset).AddTicks((long)tmpTs * 10)); case PDBType.String: int valLen = 0; valLen = (int)IntTool.ReadRawVarint32(buf, ref offset); obj = (object)Encoding.UTF8.GetString(buf, offset, valLen); offset += valLen; return(obj); case PDBType.Blob: valLen = (int)IntTool.ReadRawVarint32(buf, ref offset); byte[] blobVal = new byte[valLen]; Array.Copy(buf, offset, blobVal, 0, valLen); obj = (object)blobVal; offset += valLen; return(obj); } return(DBNull.Value); }
private List <object> GetRecord(uint fieldCnt, byte[] buf, ref int offset) { int valType = 0; List <object> objList = new List <object>(); for (uint i = 0; i < fieldCnt; i++) { valType = (int)IntTool.ReadRawVarint32(buf, ref offset); object obj = GetStoreData(buf, ref offset, valType); objList.Add(obj); } return(objList); }
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()); } }
public static void WriteVarint64(ByteStream byteStream, long value) { const int B = 128; int pos = 0; ulong uVal = IntTool.EncodeZigZag64(value); byte[] buf = new byte[10]; buf[pos++] = (byte)PDBType.BigInt; while (uVal >= B) { buf[pos++] = (byte)((uVal & (B - 1)) | B); uVal >>= 7; } buf[pos++] = (byte)uVal; byteStream.Write(buf, 0, pos); }
private object GetStoreData(byte[] buf, ref int offset, int valType) { object obj; switch ((PDBType)valType) { case PDBType.Null: return(DBNull.Value); case PDBType.Bool: return(IntTool.ReadSInt8(buf, ref offset) != 0); case PDBType.BigInt: return(IntTool.DecodeZigZag64(IntTool.ReadRawVarint64(buf, ref offset))); case PDBType.Double: return(IntTool.ReadDouble(buf, ref offset)); case PDBType.DateTime: ulong lval = IntTool.ReadRawVarint64(buf, ref offset); DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); return((object)dtStart.AddMilliseconds(lval)); case PDBType.String: int valLen = 0; valLen = (int)IntTool.ReadRawVarint32(buf, ref offset); obj = (object)Encoding.UTF8.GetString(buf, offset, valLen); offset += valLen; return(obj); case PDBType.Blob: valLen = (int)IntTool.ReadRawVarint32(buf, ref offset); byte[] blobVal = new byte[valLen]; Array.Copy(buf, offset, blobVal, 0, valLen); obj = (object)blobVal; offset += valLen; return(obj); } return(DBNull.Value); }
public void SetRecordCnt(UInt32 recordCnt) { IntTool.PutUint32(headProto, kHeadRecordCntOffset, recordCnt); }
public void SetFieldCnt(UInt32 fieldCnt) { IntTool.PutUint32(headProto, kHeadFieldCntOffset, fieldCnt); }
public void SetReturnVal(UInt32 retVal) { IntTool.PutUint32(headProto, kHeadRetValOffset, retVal); }
public UInt32 GetReturnVal() { return(IntTool.GetUint32(headProto, kHeadRetValOffset)); }
public UInt32 GetMethod() { return(IntTool.GetUint32(headProto, kHeadMethodOffset)); }
public void SetHeadCrc() { IntTool.PutUint32(headProto, kHeadHeadCrcOffset, CRC.Crc32(headProto, 0, 60)); }
public UInt32 GetErrPos() { return(IntTool.GetUint32(headProto, kHeadErrPosOffset)); }
public UInt32 GetRecordCnt() { return(IntTool.GetUint32(headProto, kHeadRecordCntOffset)); }
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); }
public void SetErrPos(UInt32 errPos) { IntTool.PutUint32(headProto, kHeadErrPosOffset, errPos); }
public void SetBodyCrc(UInt32 bodyCrc) { IntTool.PutUint32(headProto, kHeadBodyCrcOffset, bodyCrc); }
public UInt32 GetHeadCrc() { return(IntTool.GetUint32(headProto, kHeadHeadCrcOffset)); }
public UInt32 GetVersion() { return(IntTool.GetUint32(headProto, kHeadVerOffset)); }
public void SetVersion(UInt32 version) { IntTool.PutUint32(headProto, kHeadVerOffset, version); }
public UInt32 GetBodyLen() { return(IntTool.GetUint32(headProto, kHeadBodyLenOffset)); }
public void SetMethod(UInt32 methodId) { IntTool.PutUint32(headProto, kHeadMethodOffset, methodId); }
public UInt32 GetFieldCnt() { return(IntTool.GetUint32(headProto, kHeadFieldCntOffset)); }
public void SetBodyLen(UInt32 bodyLen) { IntTool.PutUint32(headProto, kHeadBodyLenOffset, bodyLen); }