Пример #1
0
        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);
            }
        }