コード例 #1
0
        public FileStat(FileMetaData metaData, bool present)
        {
            var size   = metaData.Size;
            var name   = metaData.Name;
            var server = metaData.Server;

            HashOfBlock = metaData.BlockHashs;
            locateInfos = new List <LocateInfo>();
            Size        = size;
            BlockCount  = (Size % 4096 == 0) ? Size / 4096 : (Size / 4096) + 1;
            IfPrsent    = new List <bool>(BlockCount);
            for (int i = 0; i < BlockCount; ++i)
            {
                IfPrsent.Add(present);
            }
            Name      = name;
            Server    = server;
            FailBlock = new List <int>();
        }
コード例 #2
0
        public void DownloadLoop(FileMetaData metaData)
        {
            var hash       = metaData.Hash;
            var blockCount = metaData.BlockCount;

            fmanager.AddNewDownloadFile(metaData);
            Queue <int> indexs      = GenBlockQueue(metaData.BlockCount);
            IPAddress   lastDownLoc = metaData.Server;
            int         loopCount   = 0;

            while (indexs.Count != 0)
            {
                int count = 0;

                // 调节下载速率的最重要参数,1为最小值(为0是不能下载的)。
                const int  MaxCount      = 1;
                List <int> download_list = new List <int>();
                var        index         = indexs.Dequeue();
                var        ip            = fmanager.GetConsultForDownload(hash, index);
                download_list.Add(index);
                count++;
                while (count < MaxCount && indexs.Count != 0)
                {
                    index = indexs.Dequeue();
                    if (fmanager.GetConsultForDownload(hash, index) == ip)
                    {
                        download_list.Add(index);
                        count++;
                    }
                    else
                    {
                        indexs.Enqueue(index);
                        break;
                    }
                }
                var d_list = download_list.Where((x) => (!fmanager.IfDownloadIndex(hash, x))).ToArray();
                if (loopCount % 4 == 0)
                {
                    try
                    {
                        RefreshFileInfo(lastDownLoc, hash);
                    }
                    catch (FailToConnectException e)
                    {
                        _logger.Warn(e);
                    }
                }
                else
                {
                }

                /*
                 * 默认Server必须拥有所有的原始数据,如果FileManager推荐的地址下载失败,
                 * 到Server处下载,如果再次失败,记录这个文件块为失败的文件块。
                 */
                var failedThisTime = DownloadData(hash, d_list, ip);
                if (failedThisTime.Count != 0)
                {
                    failedThisTime = DownloadData(hash, failedThisTime.ToArray(), ip);
                }
                foreach (var i in failedThisTime)
                {
                    fmanager.OnBlockDownloadFail(hash, i);
                }
                loopCount++;
            }
            var failed = fmanager.ProvideFailBlock(hash);

            // 再次尝试下载失败的数据块
            if (failed.Count != 0)
            {
                DownloadData(hash, failed.ToArray(), metaData.Server);
            }
            fmanager.OnDownloadComplete(metaData.Hash);
        }
コード例 #3
0
 public void AddNewDownloadFile(FileMetaData metaData)
 {
     AddNewFile(metaData, false);
     InitNewFile();
 }
コード例 #4
0
        static FileMetaData ParseFromFile(string path)
        {
            var str = File.ReadAllText(path);

            return(FileMetaData.Deserialize(str));
        }