Пример #1
0
        public static void Initialize()
        {
            Settings.Logger.Inizialize();

            // web server
            if (Settings.User.StartWebOnAppLoad)
            {
                startWeb();
            }

            if (File.Exists(".session_state"))
            {
                var data = File.ReadAllBytes(".session_state");
                using (var entry = Core.Util.lazy_bdecode(data))
                {
                    _torrentSession.load_state(entry);
                }
            }

            _torrentSession.start_dht();
            //_torrentSession.start_lsd();
            _torrentSession.start_natpmp();
            _torrentSession.start_upnp();

            Alert2Func[typeof(Core.torrent_added_alert)] = a => OnTorrentAddAlert((Core.torrent_added_alert)a);
            Alert2Func[typeof(Core.state_update_alert)]  = a => OnTorrentUpdateAlert((Core.state_update_alert)a);
            //Alert2Func[typeof(Core.torrent_paused_alert)] = a => OnTorrentPausedAlert((Core.torrent_paused_alert)a);
            //Alert2Func[typeof(Core.torrent_resumed_alert)] = a => OnTorrentResumedAlert((Core.torrent_resumed_alert)a);
            //Alert2Func[typeof(Core.torrent_removed_alert)] = a => OnTorrentRemovedAlert((Core.torrent_removed_alert)a); // torrent removed from torrentSession, always ok
            //Alert2Func[typeof(Core.torrent_deleted_alert)] = a => OnTorrentDeletedAlert((Core.torrent_deleted_alert)a); // finished deleting file for removed torrent
            Alert2Func[typeof(Core.torrent_error_alert)] = a => OnTorrentErrorAlert((Core.torrent_error_alert)a);
            //Alert2Func[typeof(Core.stats_alert)] = a => OnStatsAlert((Core.stats_alert)a);
            Alert2Func[typeof(Core.dht_stats_alert)] = a => OnDhtStatsAlert((Core.dht_stats_alert)a);

            Alert2Func[typeof(Core.save_resume_data_alert)]        = a => OnSaveResumeDataAlert((Core.save_resume_data_alert)a);
            Alert2Func[typeof(Core.save_resume_data_failed_alert)] = a => OnSaveResumeDataFailedAlert((Core.save_resume_data_failed_alert)a);
            Alert2Func[typeof(Core.piece_finished_alert)]          = a => OnPieceFinishedAlert((Core.piece_finished_alert)a);

            _dispatcherTimer.Elapsed += new ElapsedEventHandler(dispatcherTimer_Tick);
            _dispatcherTimer.Interval = Settings.Application.DISPATCHER_INTERVAL;
            _dispatcherTimer.Start();

            _sessionStatusDispatcherTimer.Elapsed += new ElapsedEventHandler(sessionStatusDispatcher_Tick);
            _sessionStatusDispatcherTimer.Interval = 1000;
            _sessionStatusDispatcherTimer.Start();

            // http://www.libtorrent.org/reference-Alerts.html

            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
            ;

            _torrentSession.set_alert_mask(alertMask);
            _torrentSession.set_alert_dispatch(HandleAlertCallback);
        }