Exemple #1
0
        public string GetAddressWithIp(string ipValue)
        {
            if (!_bFilePathInitialized)
            {
                return("");
            }

            Initialize();

            var ip = IptoUInt32(ipValue);

            while (true)
            {
                //首先初始化本轮查找的区间

                //区间头
                _searchSet = IndexInfoAtPos(SearchIndexSet);
                //区间尾
                _searchEnd = IndexInfoAtPos(SearchIndexEnd);

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


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

                //计算出区间中点
                _searchMid = IndexInfoAtPos((SearchIndexEnd + SearchIndexSet) / 2);

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

                //本轮没有找到,准备下一轮
                if (ip < _searchMid.IpSet)
                {
                    //IP比区间中点要小,将区间尾设为现在的中点,将区间缩小1倍。
                    SearchIndexEnd = (SearchIndexEnd + SearchIndexSet) / 2;
                }
                else
                {
                    //IP比区间中点要大,将区间头设为现在的中点,将区间缩小1倍。
                    SearchIndexSet = (SearchIndexEnd + SearchIndexSet) / 2;
                }
            }

            //return "";
        }
Exemple #2
0
        private CzIndexInfo IndexInfoAtPos(UInt32 indexPos)
        {
            var indexInfo = new CzIndexInfo();

            //根据索引编号计算出在文件中在偏移位置

            FileStrm.Seek(IndexSet + 7 * indexPos, SeekOrigin.Begin);
            indexInfo.IpSet  = GetUInt32();
            indexInfo.Offset = GetOffset();
            FileStrm.Seek(indexInfo.Offset, SeekOrigin.Begin);
            indexInfo.IpEnd = GetUInt32();

            return(indexInfo);
        }