public void Stop() { if (torrentManager.State != TorrentState.Hashing) { torrentManager.Stop(); while (torrentManager.State != TorrentState.Stopped) { Thread.Sleep(250); } BEncodedList bl = new BEncodedList(); FastResume fr = torrentManager.SaveFastResume(); BEncodedDictionary item = fr.Encode(); bl.Add(item); File.WriteAllBytes(fastResumeFile, bl.Encode()); } else { torrentManager.Stop(); } File.WriteAllBytes(dhtNodesFile, clientEngine.DhtEngine.SaveNodes()); clientEngine.Dispose(); }
public void Deserialize(BEncodedDictionary dict) { fastResume = new FastResume((BEncodedDictionary)dict["FastResume"]); savePath = dict["SavePath"].ToString(); torrentPath = dict["TorrentPath"].ToString(); string sb = dict["Settings"].ToString(); XmlSerializer s = new XmlSerializer(typeof(TorrentSettings)); using (System.IO.TextReader reader = new System.IO.StringReader(sb)) settings = (TorrentSettings)s.Deserialize(reader); }
private void SaveFastResume(TorrentManager[] managers) { BEncodedList list = new BEncodedList(); foreach (TorrentManager manager in managers) { FastResume data = manager.SaveFastResume(); BEncodedDictionary fastResume = data.Encode(); list.Add(fastResume); } File.WriteAllBytes(pathToFolderApplicationData + "\\FastResume.dat", list.Encode()); }
public void Add(string s) { if (!File.Exists(s)) { return; } foreach (TorrentManager mgr in items) { if (mgr.Torrent.TorrentPath == s) { Application.Info("Info", "The specified torrent is already loaded"); continue; } } Torrent torrent = Torrent.Load(s); FastResume resume = null; foreach (FastResume r in fastResume) { if (((BEncodedString)r.InfoHash).Equals((BEncodedString)torrent.InfoHash)) { resume = r; } } TorrentManager manager; if (resume == null) { manager = new TorrentManager(torrent, engine_settings.SavePath, (TorrentSettings)torrent_settings.Clone()); } else { manager = new MonoTorrent.Client.TorrentManager(torrent, engine_settings.SavePath, (TorrentSettings)torrent_settings.Clone(), resume); } manager.PeerConnected += new EventHandler <PeerConnectionEventArgs>(cm_PeerConnected); manager.PeerDisconnected += new EventHandler <PeerConnectionEventArgs>(cm_PeerDisconnected); engine.Register(manager); items.Add(manager); if (view != null) { view.ProviderChanged(); } manager.Start(); }
public Download addTorrent(Torrent torrent, bool startTorrent, bool removeOriginal, TorrentSettings savedSettings, string savePath, bool isUrl) { string originalPath = torrent.TorrentPath; Download manager; if (!Directory.Exists(savePath)) { throw new TorrentException("Torrent save path does not exist, " + savePath); } // Check to see if torrent already exists if (engine.Contains(torrent)) { logger.Error("Failed to add torrent, " + torrent.Name + " already exists."); throw new TorrentException("Failed to add torrent, " + torrent.Name + " already exists."); } // Move the .torrent to the local storage folder if it's not there already MoveToStorage(ref torrent); TorrentSettings settings = savedSettings ?? defaultTorrentSettings.Clone(); FastResume resume = this.fastResume.Find(delegate(FastResume f) { return(f.Infohash == torrent.InfoHash); }); manager = new Download(savePath, new TorrentManager(torrent, savePath, settings)); if (resume != null) { manager.Manager.LoadFastResume(resume); } engine.Register(manager.Manager); if (removeOriginal) { logger.Info("Removing {0}", originalPath); File.Delete(originalPath); } Event.Raise <DownloadAddedEventArgs> (Added, this, new DownloadAddedEventArgs(manager)); allTorrents.Add(manager); if (startTorrent) { logger.Info("Auto starting torrent " + manager.Torrent.Name); manager.Start(); } logger.Info("Added torrent " + manager.Torrent.Name); return(manager); }
private void LoadFastResume(List <TorrentManager> managers) { BEncodedList list = (BEncodedList)BEncodedValue.Decode(File.ReadAllBytes(pathToFolderApplicationData + "\\FastResume.dat")); foreach (BEncodedDictionary fastResume in list) { FastResume data = new FastResume(fastResume); foreach (TorrentManager manager in managers) { if (manager.InfoHash == data.Infohash) { manager.LoadFastResume(data); } } } }
private async Task LoadFastResumeData() { try { var fastResumeData = await File.ReadAllBytesAsync(FastResumeFile); var fastResume = BEncodedValue.Decode <BEncodedDictionary>(fastResumeData); Console.WriteLine("Fast resume data found initializing torrent with existing data."); if (fastResume.ContainsKey(_manager.InfoHash.ToHex())) { var fastResumeForTorrent = new FastResume((BEncodedDictionary)fastResume[_manager.InfoHash.ToHex()]); _manager.LoadFastResume(fastResumeForTorrent); } } catch (FileNotFoundException) { Console.WriteLine("No fast resume data found starting fresh torrent."); } }
public BEncodedDictionary Serialize() { BEncodedDictionary d = new BEncodedDictionary(); d.Add("FastResume", FastResume.Encode()); d.Add("SavePath", (BEncodedString)SavePath); d.Add("TorrentPath", (BEncodedString)TorrentPath); StringBuilder sb = new System.Text.StringBuilder(); XmlSerializer s = new XmlSerializer(typeof(TorrentSettings)); using (System.IO.TextWriter writer = new System.IO.StringWriter(sb)) s.Serialize(writer, Settings); d.Add("Settings", (BEncodedString)sb.ToString()); return(d); }
public void LoadFastResume(FastResume data) { Check.Data(data); CheckMetadata(); if (State != TorrentState.Stopped) { throw new InvalidOperationException("Can only load FastResume when the torrent is stopped"); } if (InfoHash != data.Infohash || Torrent.Pieces.Count != data.Bitfield.Length) { throw new ArgumentException("The fast resume data does not match this torrent", nameof(data)); } Bitfield.From(data.Bitfield); for (var i = 0; i < Torrent.Pieces.Count; i++) { RaisePieceHashed(new PieceHashedEventArgs(this, i, Bitfield[i])); } HashChecked = true; }
private void initFastResume() { BEncodedList bl; try { bl = (BEncodedList)BEncodedValue.Decode(File.ReadAllBytes(fastResumeFile)); } catch { bl = new BEncodedList(); } foreach (BEncodedDictionary item in bl) { fastResume = new FastResume(item); } // already cleanup the previous fast resume file File.Delete(fastResumeFile); }
public PeriodicTorrent LoadFastResume(FastResume resume, TorrentWrapper torrent, bool startImmediately) { // Apply settings torrent.Settings.UseDht = SettingsManager.EnableDHT; torrent.Settings.MaxConnections = SettingsManager.MaxConnectionsPerTorrent; torrent.Settings.MaxDownloadSpeed = SettingsManager.MaxDownloadSpeed; torrent.Settings.MaxUploadSpeed = SettingsManager.MaxUploadSpeed; torrent.Settings.UploadSlots = SettingsManager.UploadSlotsPerTorrent; var periodicTorrent = new PeriodicTorrent(torrent); Task.Factory.StartNew(() => { torrent.LoadFastResume(resume); Client.Register(torrent); if (startImmediately) { torrent.Start(); } }); Application.Current.Dispatcher.BeginInvoke(new Action(() => Torrents.Add(periodicTorrent))); return(periodicTorrent); }