示例#1
0
        private void PieceFinishedAlert(piece_finished_alert a)
        {
            Interlocked.MemoryBarrier();
            using (Core.TorrentHandle th = a.handle)
                using (Core.Sha1Hash hash = th.info_hash())
                {
                    if (TorrentList.ToList().Any(x => x.Hash == hash.ToString()))
                    {
                        Models.TorrentItem ti = TorrentList.First(z => z.Hash == hash.ToString());
                        if (!ReferenceEquals(null, ti.Pieces.Parts) && ti.Pieces.Parts.Any(q => q.Id == a.piece_index))
                        {
                            ti.Pieces.Parts[a.piece_index].Downloaded = true;
                            log.Debug("{0} for {1}", a.piece_index, ti.Name);
                            foreach (Models.FileEntry item in ti.FileList)
                            {
                                foreach (Models.Part part in item.Pieces.Where(k => k.Id == a.piece_index))
                                {
                                    part.Downloaded = true;
                                }
                            }

                            if (ti.SequentialDownload)
                            {
                                if (ti.Pieces.Parts.Any(w => w.Downloaded == false))
                                {
                                    Models.Part mp = ti.Pieces.Parts.First(w => w.Downloaded == false);
                                    mp.Priority = 7;
                                    th.piece_priority(mp.Id, 7);
                                }
                            }
                        }
                    }
                }
        }
示例#2
0
 private void TorrentRemovedAlert(Core.torrent_removed_alert a)
 {
     Interlocked.MemoryBarrier();
     using (Core.Sha1Hash sha1hash = a.info_hash)
     {
         string hash = sha1hash.ToString();
         if (TorrentList.ToList().Any(e => e.Hash == hash))
         {
             Models.TorrentItem ti = TorrentList.ToList().First(f => f.Hash == hash);
             log.Debug("removing {0}", ti.Name);
             Application.Current.Dispatcher.BeginInvoke(
                 System.Windows.Threading.DispatcherPriority.Normal,
                 (Action) delegate()
             {
                 TorrentList.Remove(ti);
             });
             log.Info("{0} removed", ti.Name);
         }
         if (File.Exists("./Fastresume/" + hash + ".fastresume"))
         {
             File.Delete("./Fastresume/" + hash + ".fastresume");
         }
         if (File.Exists("./Fastresume/" + hash + ".torrent"))
         {
             File.Delete("./Fastresume/" + hash + ".torrent");
         }
     }
 }
示例#3
0
 public FileEntry(Core.FileEntry fe)
 {
     Pieces = new ObservableCollection <Part>();
     using (Core.Sha1Hash hash = fe.filehash)
     {
         Filehash = hash.ToString();
     }
     Size = fe.size;
     Update(fe);
 }
示例#4
0
 private async void StateUpdateAlert(Core.state_update_alert a)
 {
     Interlocked.MemoryBarrier();
     log.Trace("state update alert for {0} torrents", a.status.Count());
     if (a.status.Count() > 0 && log.IsDebugEnabled)
     {
         log.Debug("state update alert for {0} torrents", a.status.Count());
     }
     foreach (Core.TorrentStatus ts in a.status)
     {
         using (ts)
             using (Core.Sha1Hash hash = ts.info_hash)
             {
                 if (TorrentList.ToList().Any(z => z.Hash == hash.ToString()))
                 {
                     Models.TorrentItem ti = TorrentList.First(e => e.Hash == hash.ToString());
                     await System.Threading.Tasks.Task.Run(() => ti.Update(ts));
                 }
             }
     }
 }
示例#5
0
        public TorrentItem(Core.TorrentStatus ts)
        {
            Formatter = x => ((int)x).ToString() + "%";
            FileList  = new ObservableCollection <FileEntry>();

            using (Core.Sha1Hash hash = ts.info_hash)
            {
                Hash = hash.ToString();
            }

            FileEntry fe;
            int       piecesOffset = 0;

            using (Core.TorrentInfo tf = ts.torrent_file())
                using (Core.FileStorage fs = tf.files())
                {
                    // per ogni file nel torrent
                    for (int i = 0; i <= tf.num_files() - 1; i++)
                    {
                        using (Core.FileEntry cfe = fs.at(i))
                            using (Core.Sha1Hash hash = cfe.filehash)
                            {
                                // lo inserisco
                                fe           = new FileEntry(cfe);
                                fe.FileName  = fs.file_name(i);
                                fe.IsValid   = fs.is_valid();
                                fe.PieceSize = fs.piece_size(i);

                                if (fe.PieceSize > fe.Size)
                                {
                                    // one piece
                                    fe.Pieces.Add(new Part()
                                    {
                                        Id = piecesOffset, Downloaded = false, Priority = 4
                                    });
                                }
                                else
                                {
                                    decimal piecesUsed = fe.Size / fe.PieceSize;
                                    for (int u = 0; u < piecesUsed; u++)
                                    {
                                        fe.Pieces.Add(new Part()
                                        {
                                            Id = piecesOffset, Downloaded = false, Priority = 4
                                        });
                                        piecesOffset++;
                                    }
                                }
                                FileList.Add(fe);
                            }
                    }
                }
            if (FileList.Count == 0)
            {
                // non ci sono file nel torrent
                fe          = new FileEntry();
                fe.FileName = ts.name;
                FileList.Add(fe);
            }

            SequentialDownload = ts.sequential_download;
            Update(ts);
        }