コード例 #1
0
ファイル: PDBConnection.cs プロジェクト: xiaomuwing/pinusdb
        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;
                if (recvLen == 0)
                {
                    throw new PDBException(PDBErrorCode.PdbE_NET_ERROR);
                }
            }

            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;
                if (recvLen == 0)
                {
                    throw new PDBException(PDBErrorCode.PdbE_NET_ERROR);
                }
            }

            UInt32 bodyCrc = proHdr.GetBodyCrc();

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

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

            return(packetBuf);
        }
コード例 #2
0
 public void SetHeadCrc()
 {
     IntTool.PutUint32(headProto, kHeadHeadCrcOffset, CRC.Crc32(headProto, 0, 60));
 }
コード例 #3
0
ファイル: PDBCommand.cs プロジェクト: loobins/pinusdb
        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);
        }