Exemple #1
0
        protected CZ_INDEX_INFO IndexInfoAtPos(UInt32 Index_Pos)
        {
            CZ_INDEX_INFO Index_Info = new CZ_INDEX_INFO();

            //根据索引编号计算出在文件中在偏移位置
            MemStream.Seek(Index_Set + 7 * Index_Pos, SeekOrigin.Begin);
            Index_Info.IpSet  = GetUInt32();
            Index_Info.Offset = GetOffset();
            MemStream.Seek(Index_Info.Offset, SeekOrigin.Begin);
            Index_Info.IpEnd = GetUInt32();

            return(Index_Info);
        }
Exemple #2
0
        public string GetLocation(string IPValue)
        {
            if (!IsIpDatabaseInitialized)
            {
                throw new Exception("IP库尚未初始化!");
            }

            try
            {
                Initialize();

                UInt32 ip = IPToUInt32(IPValue);
                while (true)
                {
                    //首先初始化本轮查找的区间

                    //区间头
                    Search_Set = IndexInfoAtPos(Search_Index_Set);
                    //区间尾
                    Search_End = IndexInfoAtPos(Search_Index_End);

                    //判断IP是否在区间头内
                    if (ip >= Search_Set.IpSet && ip <= Search_Set.IpEnd)
                    {
                        return(ReadAddressInfoAtOffset(Search_Set.Offset));
                    }


                    //判断IP是否在区间尾内
                    if (ip >= Search_End.IpSet && ip <= Search_End.IpEnd)
                    {
                        return(ReadAddressInfoAtOffset(Search_End.Offset));
                    }

                    //计算出区间中点
                    Search_Mid = IndexInfoAtPos((Search_Index_End + Search_Index_Set) / 2);

                    //判断IP是否在中点
                    if (ip >= Search_Mid.IpSet && ip <= Search_Mid.IpEnd)
                    {
                        return(ReadAddressInfoAtOffset(Search_Mid.Offset));
                    }

                    //本轮没有找到,准备下一轮
                    if (ip < Search_Mid.IpSet)
                    {
                        //IP比区间中点要小,将区间尾设为现在的中点,将区间缩小1倍。
                        Search_Index_End = (Search_Index_End + Search_Index_Set) / 2;
                    }
                    else
                    {
                        //IP比区间中点要大,将区间头设为现在的中点,将区间缩小1倍。
                        Search_Index_Set = (Search_Index_End + Search_Index_Set) / 2;
                    }
                }
            }
            catch (Exception e)
            {
                Common.LogIt(e.ToString());
                throw;
            }
        }