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", "data");
            }

            bitfield.From(data.Bitfield);
            for (int i = 0; i < torrent.Pieces.Count; i++)
            {
                RaisePieceHashed(new PieceHashedEventArgs(this, i, bitfield[i]));
            }

            this.hashChecked = true;

            if (data.Priorities != null)
            {
                if (data.Priorities.Length != torrent.Files.Length)
                {
                    throw new ArgumentException("The fast resume data does not match this torrent", "data");
                }

                for (var i = 0; i < data.Priorities.Length; i++)
                {
                    torrent.Files[i].Priority = data.Priorities[i];
                }
            }
        }
Example #2
0
        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);
        }
Example #3
0
 public PeriodicTorrent LoadFastResume(FastResume resume, TorrentWrapper torrent)
 {
     var periodicTorrent = new PeriodicTorrent(torrent);
     Torrents.Add(periodicTorrent);
     torrent.Index = Torrents.Count;
     Task.Factory.StartNew(() =>
         {
             torrent.LoadFastResume(resume);
             Client.Register(torrent);
             torrent.Start();
         });
     return periodicTorrent;
 }
Example #4
0
 public static bool TryLoad(string fastResumeFilePath, out FastResume fastResume)
 {
     fastResume = null;
     try {
         if (File.Exists(fastResumeFilePath))
         {
             using (FileStream s = File.Open(fastResumeFilePath, FileMode.Open)) {
                 fastResume = Load(s);
             }
         }
     } catch {
     }
     return(fastResume != null);
 }
        public void LoadV1FastResumeData()
        {
            var v1Data = new BEncodedDictionary {
                { FastResume.VersionKey, (BEncodedNumber)1 },
                { FastResume.InfoHashKey, new BEncodedString(InfoHash.Hash) },
                { FastResume.BitfieldKey, new BEncodedString(new BitField(10).SetAll(true).ToByteArray()) },
                { FastResume.BitfieldLengthKey, (BEncodedNumber)10 },
            };

            // If this is a v1 FastResume data then it comes from a version of MonoTorrent which always
            // hashes the entire file.
            var fastResume = new FastResume(v1Data);

            Assert.IsTrue(fastResume.UnhashedPieces.AllFalse, "#1");
            Assert.IsTrue(fastResume.Bitfield.AllTrue, "#2");
        }
        public void LoadEncoded()
        {
            var unhashedPieces = new BitField(10).SetAll(false);
            var downloaded     = new BitField(10).SetAll(true);
            var fastResume     = new FastResume(InfoHashes, downloaded, unhashedPieces);
            var stream         = new MemoryStream();

            fastResume.Encode(stream);
            Assert.IsTrue(stream.Length > 0, "#1");

            stream.Seek(0, SeekOrigin.Begin);
            Assert.IsTrue(FastResume.TryLoad(stream, out var newFastResume), "#2");
            Assert.IsNotNull(newFastResume, "#3");
            Assert.IsTrue(newFastResume.UnhashedPieces.AllFalse, "#4");
            Assert.IsTrue(newFastResume.Bitfield.AllTrue, "#5");
        }
Example #7
0
 public static bool TryLoad(string fastResumeFilePath, out FastResume fastResume)
 {
     try {
         if (File.Exists(fastResumeFilePath))
         {
             var data = (BEncodedDictionary)BEncodedDictionary.Decode(File.ReadAllBytes(fastResumeFilePath));
             fastResume = new FastResume(data);
         }
         else
         {
             fastResume = null;
         }
     } catch {
         fastResume = null;
     }
     return(fastResume != null);
 }
        public void LoadV2FastResumeData()
        {
            var v1Data = new BEncodedDictionary {
                { FastResume.VersionKey, (BEncodedNumber)1 },
                { FastResume.InfoHashKey, new BEncodedString(InfoHash.Span.ToArray()) },
                { FastResume.BitfieldKey, new BEncodedString(new BitField(10).SetAll(false).Set(0, true).ToBytes()) },
                { FastResume.BitfieldLengthKey, (BEncodedNumber)10 },
                { FastResume.UnhashedPiecesKey, new BEncodedString(new BitField(10).SetAll(true).Set(0, false).ToBytes()) },
            };

            // If this is a v1 FastResume data then it comes from a version of MonoTorrent which always
            // hashes the entire file.
            var fastResume = new FastResume(v1Data);

            Assert.AreEqual(1, fastResume.Bitfield.TrueCount, "#1");
            Assert.AreEqual(9, fastResume.UnhashedPieces.TrueCount, "#2");
        }
Example #9
0
        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", "fastResumeData");
            }

            for (int i = 0; i < Torrent.Pieces.Count; i++)
            {
                OnPieceHashed(i, data.Bitfield[i]);
            }

            this.HashChecked = true;
        }
Example #10
0
 public PeriodicTorrent LoadFastResume(FastResume resume, TorrentWrapper torrent)
 {
     var periodicTorrent = new PeriodicTorrent(torrent);
     Task.Factory.StartNew(() =>
         {
             torrent.LoadFastResume(resume);
             Client.Register(torrent);
             if (SettingsManager.StartTorrentsImmediately)
                 torrent.Start();
         });
     Application.Current.Dispatcher.BeginInvoke(new Action(() => Torrents.Add(periodicTorrent)));
     return periodicTorrent;
 }
Example #11
0
        public void LoadFastResume(byte[] data)
        {
            var d = BEncodedValue.Decode<BEncodedDictionary>(data);
            var f = new FastResume(d);

            _manager.LoadFastResume(f);
        }
Example #12
0
 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;
 }
Example #13
0
 public void loadFastResume(List<TorrentManager> managers)
 {
     if (File.Exists(fastResumePath))
     {
         BEncodedList list = (BEncodedList)BEncodedValue.Decode(File.ReadAllBytes(fastResumePath));
         foreach (BEncodedDictionary fastResume in list)
         {
             FastResume data = new FastResume(fastResume);
             foreach (TorrentManager manager in managers)
             {
                 if (manager.InfoHash == data.Infohash)
                 {
                     manager.LoadFastResume(data);
                 }
             }
         }
     }
 }
Example #14
0
 public static bool TryLoad(Stream s, out FastResume fastResume)
 {
     fastResume = Load(s);
     return(fastResume != null);
 }
Example #15
0
    public void LoadFastResume(List <TorrentManager> managers)
    {
        // Read the main dictionary from disk and iterate through
        // all the fast resume items
        List<TorrentManager> temp = new List<TorrentManager>(managers.Count);
        for (int i = 0; i< managers.Count; i++)
            temp.Add(managers[i]);

        if (File.Exists(fastResumePath))
        {
            BEncodedList list = (BEncodedList) BEncodedValue.Decode (File.ReadAllBytes (fastResumePath));
            foreach (BEncodedDictionary fastResume in list)
            {
                // Decode the FastResume data from the BEncodedDictionary
                FastResume data = new FastResume (fastResume);

                // Find the torrentmanager that the fastresume belongs to
                // and then load it
                foreach (TorrentManager manager in managers)
                    if (manager.InfoHash == data.Infohash)
                    {
                        manager.LoadFastResume (data);
                        temp.Remove(manager);
                    }
            }

            foreach (TorrentManager manager in temp)
                {
                    //manager.HashCheck(true);
                    manager.Stop();
                    XenoTorrent.TorrentSettings ts = new XenoTorrent.TorrentSettings (manager);
                    ts.Show ();
                }

        }
    }