Exemplo n.º 1
0
 private void Download(int threadId)
 {
     while (true)
     {
         try
         {
             var info = new KeyValuePair <InfoHash, IPEndPoint>();
             lock (this)
             {
                 info = _queue.Dequeue();
             }
             if (info.Key == null || info.Value == null)
             {
                 Thread.Sleep(1000);
                 continue;
             }
             var hash = BitConverter.ToString(info.Key.Hash).Replace("-", "");
             using (WireClient client = new WireClient(info.Value))
             {
                 var metadata = client.GetMetaData(info.Key);
                 if (metadata != null)
                 {
                     var name = ((BEncodedString)metadata["name"]).Text;
                     if (_option.IsSaveTorrent)
                     {
                         var filepath = $"{_option.TorrentSavePath}\\{hash}.torrent";
                         File.WriteAllBytes(filepath, metadata.Encode());
                     }
                     var list = new List <string>();
                     //foreach (var item in metadata)
                     //{
                     //    list.Add($"[key]={item.Key}[val]={item.Value}");
                     //}
                     //var str = string.Join("&", list);
                     //Logger.Warn($"{hash} {name} {str}");
                     Logger.ConsoleWrite($"线程[{threadId}]下载完成   Name:{name} ");
                 }
             }
         }
         catch (Exception ex)
         {
             //Logger.Error($"Download {ex.Message}");
         }
         finally
         {
             Thread.Sleep(1000);
         }
     }
 }
Exemplo n.º 2
0
        private static void StartDownTorrent()
        {
            if (DownTaskList.Count >= MaxDownTaskNum)
            {
                return;
            }
            Task downTask = Task.Factory.StartNew(() =>
            {
                while (running)
                {
                    try
                    {
                        if (!DownLoadQueue.TryTake(out var info))
                        {
                            lock (InfoStore)
                            {
                                if (!DownLoadQueue.TryTake(out info))
                                {
                                    var infos = InfoStore.ReadLast(Math.Min(MaxDownTaskNum, DownLoadQueue.BoundedCapacity - DownLoadQueue.Count));
                                    if (infos.Count > 0)
                                    {
                                        for (int i = 0; i < infos.Count - 1; i++)
                                        {
                                            var item = infos[i];
                                            if (!DownLoadQueue.TryAdd(item))
                                            {
                                                InfoStore.Add(item);
                                            }
                                        }
                                        info = infos.Last();
                                    }
                                }
                            }
                        }
                        if (info == null)
                        {
                            if (!DownLoadQueue.TryTake(out info, DownTimeOutSeconds))
                            {
                                if (DownTaskList.Count > MinDownTaskNum)
                                {
                                    return;
                                }
                                continue;
                            }
                        }
                        if (DownlaodedSet.Contains(info.Value))
                        {
                            continue;
                        }
                        watchLog.Info($"thread {Task.CurrentId:x2} downloading {info.Value}");
                        foreach (var peer in info.Peers.Where(p => p.Address.IsPublic()))
                        {
                            if (DownlaodedSet.Contains(info.Value))
                            {
                                break;
                            }
                            var longPeer = peer.ToInt64();
                            try
                            {
                                if (BadAddress.TryGetValue(longPeer, out var expireTime))
                                {
                                    if (expireTime > DateTime.Now)
                                    {
                                        continue;
                                    }
                                    BadAddress.TryRemove(longPeer, out expireTime);
                                }
                                using (var client = new WireClient(peer))
                                {
                                    var meta = client.GetMetaData(new global::BitTorrent.InfoHash(info.Bytes), out var netError);
                                    if (meta == null)
                                    {
                                        if (netError)
                                        {
                                            BadAddress.AddOrUpdate(longPeer, DateTime.Now.AddDays(1),
                                                                   (ip, before) => DateTime.Now.AddDays(1));
                                        }
                                        continue;
                                    }
                                    DownlaodedSet.Add(info.Value);
                                    var torrent      = ParseBitTorrent(meta);
                                    torrent.InfoHash = info.Value;
                                    var subdirectory = TorrentDirectory.CreateSubdirectory(DateTime.Now.ToString("yyyy-MM-dd"));
                                    var path         = Path.Combine(subdirectory.FullName, torrent.InfoHash + ".json");
                                    var hasLock      = false;
                                    writeLock.Enter(ref hasLock);
                                    try
                                    {
                                        File.WriteAllText(Path.Combine(TorrentPath, path), torrent.ToJson());
                                        File.AppendAllText(DownloadInfoPath, torrent.InfoHash + Environment.NewLine);
                                    }
                                    finally
                                    {
                                        if (hasLock)
                                        {
                                            writeLock.Exit(false);
                                        }
                                    }
                                    watchLog.Info($"download {torrent.InfoHash} success");
                                }
                                break;
                            }
                            catch (SocketException)
                            {
                                BadAddress.AddOrUpdate(longPeer, DateTime.Now.AddDays(1), (ip, before) => DateTime.Now.AddDays(1));
                            }
                            catch (IOException)
                            {
                                BadAddress.AddOrUpdate(longPeer, DateTime.Now.AddDays(1), (ip, before) => DateTime.Now.AddDays(1));
                            }
                            catch (Exception ex)
                            {
                                log.Error("下载失败", ex);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("并行下载时错误", ex);
                    }
                }
            }, TaskCreationOptions.LongRunning);

            downTask.ContinueWith(t => DownTaskList.Remove(t));
            DownTaskList.Add(downTask);
        }
Exemplo n.º 3
0
        private void MetadataResolver(object id)
        {
            while (true)
            {
                try
                {
                    var info = spider.Pop();
                    if (info.Key == null || info.Value == null)
                    {
                        if ((bool)(id ?? true))
                        {
                            return;
                        }
                        Thread.Sleep(1000);
                        continue;
                    }
                    OnSavingMetadata?.Invoke(Task.CurrentId, new EventArgs());
                    //using (WireClient client = new WireClient(info.Value, 60000 + (int)(id ?? 0)))
                    using (WireClient client = new WireClient(info.Value))
                    {
                        var metadata = client.GetMetaData(info.Key);
                        if (metadata != null)
                        {
                            var name = ((BEncodedString)metadata["name"]).Text;
                            var hash = BitConverter.ToString(info.Key.Hash).Replace("-", "");

                            if (Program.options.SaveSeed)
                            {
                                File.WriteAllBytes(@"seeds\" + hash + ".torrent", metadata.Encode());
                            }

                            Trace.Indent();
                            Trace.WriteLine(name, "Seed");
                            Trace.WriteLine(hash, "InfoHash");
                            Trace.Unindent();


                            if (!Program.options.NoMongo)
                            {
                                cargo.Add(metadata, info.Key.Hash);
                            }
                            OnSavedMetadata?.Invoke(Task.CurrentId, name);
                        }
                    }
                }
                catch (SocketException ex)
                {
                    MessageLoop_OnError(this, "Socket " + ex.ErrorCode.ToString());
                    Trace.WriteLine(ex.ErrorCode, "Socket");
                }
                catch (IOException)
                {
                    MessageLoop_OnError(this, "Socket Closed");
                }
                catch (AggregateException ex)
                {
                    MessageLoop_OnError(this, ex.ToString());
                }
                catch (Exception ex)
                {
                    Debug.Fail(ex.ToString());
                    MessageLoop_OnError(this, ex.ToString());
                }
                finally
                {
                    Trace.Flush();
                }
            }
        }