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"); }
public TsunamiViewModel() { Settings.Logger.Inizialize(); _preference = new Models.Preferences(); _torrentList = new ObservableCollection <Models.TorrentItem>(); System.Windows.Data.BindingOperations.EnableCollectionSynchronization(TorrentList, _lock); _sessionStatistic = new SessionStatistics(); IsTsunamiEnabled = false; //_notifyIcon = new Models.NotifyIcon(); _torrentSession = new Core.Session(); CoreLog.Debug("created"); if (System.IO.File.Exists(".session_state")) { var data = System.IO.File.ReadAllBytes(".session_state"); using (var entry = Core.Util.lazy_bdecode(data)) { _torrentSession.load_state(entry); } } CoreLog.Debug("session_state loaded. pausing"); _torrentSession.pause(); Core.SessionSettings ss = _torrentSession.settings(); //ss.connections_limit = 500; // 200 //ss.tick_interval = 250; // 500 //ss.torrent_connect_boost = 20; // 10 //ss.connection_speed = -1; // -1 = 200 ; default 10 //ss.num_want = 400; // 200 //ss.cache_size = -1; // -1 = 1/8 RAM; default 1024 //ss.coalesce_reads = true; // false //ss.coalesce_writes = true; // false string ua = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Major.ToString() + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Minor.ToString() + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Build.ToString(); ss.user_agent = string.Format(Tsunami.Settings.Application.TSUNAMI_USER_AGENT, ua); log.Debug("using user agent {0}", ss.user_agent); // from qbit torrent ss.upnp_ignore_nonrouters = true; ss.use_dht_as_fallback = false; ss.ssl_listen = 0; // Disable support for SSL torrents for now ss.lazy_bitfields = true; // To prevent ISPs from blocking seeding ss.stop_tracker_timeout = 1; // Speed up exit ss.auto_scrape_interval = 1200; // 20 minutes ss.announce_to_all_trackers = true; // from pref->announceToAllTrackers(); ss.announce_to_all_tiers = true; // from pref->announceToAllTrackers(); ss.auto_scrape_min_interval = 900; // 15 minutes ss.cache_size = -1; ss.cache_expiry = 60; ss.disk_io_read_mode = 0; // enable_os_cache ss.anonymous_mode = false; ss.active_seeds = 3; // MaxActiveUploads ss.dont_count_slow_torrents = false; ss.active_downloads = 3; // MaxActiveDownloads + m_extraLimit ss.active_limit = 5; // MaxActiveTorrents + m_extraLimit ss.active_tracker_limit = -1; ss.active_dht_limit = -1; ss.active_lsd_limit = -1; ss.ignore_limits_on_local_network = true; // Ignore limits on LAN ss.rate_limit_ip_overhead = false; // Include overhead in transfer limits //ss.announce_ip = Utils::String::toStdString(pref->getNetworkAddress()); // IP address to announce to trackers ss.strict_super_seeding = false; // Super seeding ss.half_open_limit = 20; // * Max Half-open connections ss.connections_limit = 500; // * Max connections limit ss.unchoke_slots_limit = -1; // * Global max upload slots ss.enable_incoming_utp = true; // uTP ss.enable_outgoing_utp = true; // uTP ss.rate_limit_utp = true; // uTP rate limiting ss.mixed_mode_algorithm = 0; // prefer_tcp ss.connection_speed = 20; //default is 10 ss.no_connect_privileged_ports = false; ss.seed_choking_algorithm = 1; // fastest_upload ss.apply_ip_filter_to_trackers = false; // FilterTracker _torrentSession.set_settings(ss); // MISSING ENCRYPTION // libt::pe_settings encryptionSettings; (835 of session.cpp) Core.DhtSettings dhts = _torrentSession.get_dht_settings(); //dhts.aggressive_lookups = true; _torrentSession.set_dht_settings(dhts); var alertMask = Core.AlertMask.error_notification | Core.AlertMask.peer_notification | Core.AlertMask.port_mapping_notification | Core.AlertMask.storage_notification | Core.AlertMask.tracker_notification | Core.AlertMask.status_notification | Core.AlertMask.ip_block_notification | Core.AlertMask.progress_notification | Core.AlertMask.stats_notification | Core.AlertMask.dht_notification ; _torrentSession.set_alert_mask(alertMask); _torrentSession.set_alert_callback(HandlePendingAlertCallback); _torrentSession.set_session_callback(HandleAlertCallback); if (Settings.Application.DISPATCHER_INTERVAL < 500) { log.Warn("DISPATCHER_INTERVAL {0} should not be lower than 500", Settings.Application.DISPATCHER_INTERVAL); } _dispatcherTimer.Elapsed += new System.Timers.ElapsedEventHandler(dispatcherTimer_Tick); _dispatcherTimer.Interval = Settings.Application.DISPATCHER_INTERVAL; _dispatcherTimer.Start(); LoadFastResumeData(); _torrentSession.start_natpmp(); _torrentSession.start_upnp(); _torrentSession.start_dht(); _torrentSession.start_lsd(); _torrentSession.resume(); log.Info("created"); //Dht = new Core.AdunanzaDht(); //Dht.start(); //Dht.bootstrap("bootstrap.ring.cx", "4222"); }