コード例 #1
0
        void SetupTorrent(TorrentManager manager)
        {
            // Every time a piece is hashed, this is fired.
            manager.PieceHashed += delegate(object o, PieceHashedEventArgs e)
            {
                var tm = (TorrentManager)o;

                /*lock (this)
                 * {
                 *  if (tm.State != TorrentState.Hashing)
                 *      Console.WriteLine(string.Format("Piece Hashed: {0} - {1}", e.PieceIndex, e.HashPassed ? "Pass" : "Fail"));
                 * }*/
            };

            // Every time the state changes (Stopped -> Seeding -> Downloading -> Hashing) this is fired
            manager.TorrentStateChanged += delegate(object o, TorrentStateChangedEventArgs e)
            {
                var tm = (TorrentManager)o;

                var name = !tm.HasMetadata ? "Magnet" : tm.Torrent.Name;

                lock (this)
                    Console.WriteLine("simpletorrent: [{1}] {0}",
                                      e.NewState.ToString(), name);

                lock (seedingLimitTorrents)
                {
                    if (e.NewState == TorrentState.Seeding && seedingLimit.HasValue &&
                        seedingLimitTorrents.Where(a => a.Item2 == tm).Count() == 0)
                    {
                        Console.WriteLine("simpletorrent: Queuing \"{0}\" for automatic removal...", name);
                        seedingLimitTorrents.Add(new Tuple <DateTime, TorrentManager>(DateTime.Now, tm));
                    }
                }

                if (e.NewState == TorrentState.Stopped)
                {
                    try
                    {
                        var ti = torrentInformation[tm.InfoHash.ToHex()];
                        if (ti.ToRemove != null)
                        {
                            if (tm.HasMetadata && tm.Torrent != null)
                            {
                                File.Delete(tm.Torrent.TorrentPath);
                            }
                            engine.Unregister(tm);
                            torrentInformation.Remove(manager.InfoHash.ToHex());
                        }

                        if (ti.ToRemove == "delete-torrent-and-data")
                        {
                            System.Threading.Thread.Sleep(200);
                            if (Directory.Exists(Path.Combine(tm.SavePath, tm.Torrent.Name)))
                            {
                                Directory.Delete(Path.Combine(tm.SavePath, tm.Torrent.Name), true);
                            }
                            else
                            {
                                File.Delete(Path.Combine(tm.SavePath, tm.Torrent.Name));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        debugWriter.WriteLine("Exception when attempting to stop torrent: " + ex.ToString());
                    }
                }
                else
                {
                    try
                    {
                        var ti = torrentInformation[tm.InfoHash.ToHex()];
                        ti.CreationDateTime = File.GetCreationTime(tm.Torrent.TorrentPath);
                    }
                    catch
                    {
                    }
                }
            };

            // Every time the tracker's state changes, this is fired
            foreach (TrackerTier tier in manager.TrackerManager)
            {
                foreach (MonoTorrent.Client.Tracker.Tracker t in tier.Trackers)
                {
                    t.AnnounceComplete += delegate(object sender, AnnounceResponseEventArgs e)
                    {
                        //Console.WriteLine(string.Format("{0}: {1}", e.Successful, e.Tracker.ToString()));
                    };
                }
            }

            try
            {
                torrentInformation.Remove(manager.InfoHash.ToHex());
            }
            catch
            {
            }

            TorrentInformation nTi = new TorrentInformation();

            try
            {
                nTi.CreationDateTime = File.GetCreationTime(manager.Torrent.TorrentPath);
            }
            catch
            {
            }

            torrentInformation.Add(manager.InfoHash.ToHex(), nTi);

            // Start the torrentmanager. The file will then hash (if required) and begin downloading/seeding
            manager.Start();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: senditu/simpletorrent
        void SetupTorrent(TorrentManager manager)
        {
            // Every time a piece is hashed, this is fired.
            manager.PieceHashed += delegate(object o, PieceHashedEventArgs e)
            {
                var tm = (TorrentManager)o;
                /*lock (this)
                {
                    if (tm.State != TorrentState.Hashing)
                        Console.WriteLine(string.Format("Piece Hashed: {0} - {1}", e.PieceIndex, e.HashPassed ? "Pass" : "Fail"));
                }*/
            };

            // Every time the state changes (Stopped -> Seeding -> Downloading -> Hashing) this is fired
            manager.TorrentStateChanged += delegate(object o, TorrentStateChangedEventArgs e)
            {
                var tm = (TorrentManager)o;

                var name = !tm.HasMetadata ? "Magnet" : tm.Torrent.Name;

                lock (this)
                        Console.WriteLine("simpletorrent: [{1}] {0}",
                            e.NewState.ToString(), name);

                lock (seedingLimitTorrents)
                {
                    if (e.NewState == TorrentState.Seeding && seedingLimit.HasValue
                        && seedingLimitTorrents.Where(a => a.Item2 == tm).Count() == 0)
                    {
                        Console.WriteLine("simpletorrent: Queuing \"{0}\" for automatic removal...", name);
                        seedingLimitTorrents.Add(new Tuple<DateTime, TorrentManager>(DateTime.Now, tm));
                    }
                }

                if (e.NewState == TorrentState.Stopped)
                {
                    try
                    {
                        var ti = torrentInformation[tm.InfoHash.ToHex()];
                        if (ti.ToRemove != null)
                        {
                            if (tm.HasMetadata && tm.Torrent != null)
                            {
                                File.Delete(tm.Torrent.TorrentPath);
                            }
                            engine.Unregister(tm);
                            torrentInformation.Remove(manager.InfoHash.ToHex());
                        }

                        if (ti.ToRemove == "delete-torrent-and-data")
                        {
                            System.Threading.Thread.Sleep(200);
                            if (Directory.Exists(Path.Combine(tm.SavePath, tm.Torrent.Name)))
                            {
                                Directory.Delete(Path.Combine(tm.SavePath, tm.Torrent.Name), true);
                            }
                            else
                            {
                                File.Delete(Path.Combine (tm.SavePath, tm.Torrent.Name));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        debugWriter.WriteLine("Exception when attempting to stop torrent: " + ex.ToString());
                    }
                }
                else
                {
                    try
                    {
                        var ti = torrentInformation[tm.InfoHash.ToHex()];
                        ti.CreationDateTime = File.GetCreationTime(tm.Torrent.TorrentPath);
                    }
                    catch
                    {

                    }
                }
            };

            // Every time the tracker's state changes, this is fired
            foreach (TrackerTier tier in manager.TrackerManager)
            {
                foreach (MonoTorrent.Client.Tracker.Tracker t in tier.Trackers)
                {
                    t.AnnounceComplete += delegate(object sender, AnnounceResponseEventArgs e)
                    {
                        //Console.WriteLine(string.Format("{0}: {1}", e.Successful, e.Tracker.ToString()));
                    };
                }
            }

            try
            {
                torrentInformation.Remove(manager.InfoHash.ToHex());
            }
            catch
            {

            }

            TorrentInformation nTi = new TorrentInformation();

            try
            {
                nTi.CreationDateTime = File.GetCreationTime(manager.Torrent.TorrentPath);
            }
            catch
            {

            }

            torrentInformation.Add(manager.InfoHash.ToHex(), nTi);

            // Start the torrentmanager. The file will then hash (if required) and begin downloading/seeding
            manager.Start();
        }