Пример #1
0
        void RefreshFileInfo(IPAddress lastDownLoc, byte[] hash)
        {
            List <P2PMessage> result;

            using (var transMan = new DownloadTransManager(lastDownLoc))
            {
                transMan.GetFileInfo(hash);
                result = transMan.GetReply(1);
                transMan.SendCloseMessage();
            }
            foreach (var i in result)
            {
                if (i.type == P2PMessageType.FILE_INFO)
                {
                    if (i.length != 0)
                    {
                        var info = (FileInfoMessage)i;
                        foreach (var j in info.locateInfos)
                        {
                            fmanager.AddOrRefreshFileInfo(
                                j.hash, new IPAddress(j.locate), j.indexs);
                        }
                    }
                    break;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 返回未能成功下载的块标记
        /// </summary>
        List <int> DownloadData(byte[] hash, int[] indexs, IPAddress iP)
        {
            var failStat = $"target: {iP} don't have this block";
            List <P2PMessage> result;
            StringBuilder     @string = new StringBuilder();

            foreach (var i in indexs)
            {
                @string.Append($"{i} ");
            }
            _logger.Info($"Now Download {@string} from {iP}");
            using (var transMan = new DownloadTransManager(iP))
            {
                transMan.GetData(hash, indexs);
                result = transMan.GetReply(indexs.Length);
                if (result.Count == 0)
                {
                    transMan.SendCloseMessage();
                    // 如果进入了这里,说明请求的IP里,请求的块一个也没有
                    return(new List <int>(indexs));
                }
                HashSet <int> set = new System.Collections.Generic.HashSet <int>(indexs);
                foreach (var i in result)
                {
                    _logger.Info($"Get {i.type} from {iP}");
                    if (i.type == P2PMessageType.DATA)
                    {
                        var data  = (DataMessage)i;
                        var index = data.p2PDatas.block.index;
                        if (set.Contains(index))
                        {
                            transMan.SendOkMessage(hash, data.p2PDatas.block.index);
                            try
                            {
                                fmanager.OnBlockDownloadSuccess(hash, index, data.p2PDatas.data);
                                set.Remove(index);
                            }
                            catch (FailToDownloadException)
                            {
                            }
                        }
                    }
                    else if (i.type == P2PMessageType.ERROR)
                    {
                        var index = ((ErrorMessage)i).Block.index;
                        fmanager.SetRemoteBlockPresentStat(hash, iP, index, false);
                    }
                }
                transMan.SendCloseMessage();
                return(set.ToList());
            }
        }