Ejemplo n.º 1
0
        public void Terminate()
        {
            Interlocked.MemoryBarrier();
            log.Debug("Terminating");
            IsTsunamiEnabled = false;
            _torrentSession.stop_natpmp();
            _torrentSession.stop_upnp();
            _torrentSession.stop_dht();
            _torrentSession.stop_lsd();
            _torrentSession.pause();
            log.Debug("torrentSession paused");

            log.Debug("stopping ticker");
            _dispatcherTimer.Stop();
            _dispatcherTimer.Enabled = false;
            log.Debug("ticker stopped");

            while (!_torrentSession.alerts_empty())
            {
                log.Debug("torrentSession alerts not empty. waiting 250ms");
                Thread.Sleep(250);
            }
            log.Debug("torrentSession alerts empty!");

            //stopWeb();
            log.Debug("start save_state");
            TerminateSaveResume();
            log.Debug("save_state finished");

            //outstanding_resume_data = 0;
            List <Models.TorrentItem> myList = new List <Models.TorrentItem>(TorrentList);

            log.Debug("need to process {0} torrents", myList.Count);
            int index = 0;

            foreach (Models.TorrentItem item in myList)
            {
                index++;
                log.Debug("processing torrent {0}", index);
                using (Core.Sha1Hash sha1hash = new Core.Sha1Hash(item.Hash))
                {
                    using (Core.TorrentHandle th = _torrentSession.find_torrent(sha1hash))
                    {
                        if (th.has_metadata() && th.need_save_resume_data())
                        {
                            log.Debug("{0} has metadata and need save resume. processing.", index);
                            th.save_resume_data(1 | 2 | 4);
                            ++outstanding_resume_data;
                            log.Debug("{0} save resume executed: waiting 100ms", index);
                            System.Threading.Thread.Sleep(100);
                        }
                        else
                        {
                            log.Debug("{0} no metadata or need save resume", index);
                        }
                    }
                }
            }
            //foreach (Models.TorrentItem item in myList)
            //{
            //    index++;
            //    log.Debug("processing torrent " + index);
            //    using (Core.Sha1Hash sha1hash = new Core.Sha1Hash(item.Hash))
            //    using (Core.TorrentHandle th = _torrentSession.find_torrent(sha1hash))
            //    {
            //        if (th.has_metadata() && th.need_save_resume_data())
            //        {
            //            log.Debug(index + " has metadata and need save resume. processing.");
            //            th.save_resume_data(1 | 2 | 4);
            //            ++outstanding_resume_data;
            //            log.Debug(index + " save resume executed: waiting 100ms");
            //            System.Threading.Thread.Sleep(100);
            //        }
            //        else
            //        {
            //            log.Debug(index + " no metadata or need save resume");
            //        }
            //    }
            //}
            no_more_resume = true;
            if (outstanding_resume_data != 0)
            {
                log.Debug("outstanding_resume_data = {0}: wait one", outstanding_resume_data);
                no_more_data.WaitOne();
            }

            log.Debug("starting clear_alert_callback");
            _torrentSession.clear_alert_callback();
            log.Debug("clear_alert_callback finished");
            log.Info("closed");
        }