Exemple #1
0
        public static void addTorrent(string filename)
        {
            var    data = File.ReadAllBytes(filename);
            string newfilePath;

            using (var atp = new Core.AddTorrentParams())
                using (var ti = new Core.TorrentInfo(filename))
                {
                    atp.save_path = Settings.User.PathDownload;
                    atp.ti        = ti;
                    atp.flags    &= ~Core.ATPFlags.flag_auto_managed;         // remove auto managed flag
                    atp.flags    &= ~Core.ATPFlags.flag_paused;               // remove pause on added torrent
                    atp.flags    &= ~Core.ATPFlags.flag_use_resume_save_path; //
                    newfilePath   = "./Fastresume/" + ti.info_hash().ToString() + ".torrent";
                    if (!File.Exists(newfilePath))
                    {
                        using (var bw = new BinaryWriter(new FileStream(newfilePath, FileMode.Create)))
                        {
                            bw.Write(data);
                            bw.Close();
                        }
                    }
                    _torrentSession.async_add_torrent(atp);
                }
        }
        public List <Models.FileEntry> getTorrentFiles(string hash)
        {
            List <Models.FileEntry> feList = new List <Models.FileEntry>();

            Models.FileEntry   fe;
            Core.TorrentHandle th = getTorrentHandle(hash);
            using (Core.TorrentInfo ti = th.torrent_file())
            {
                if (ti == null)
                {
                    // non ci sono file nel torrent
                    fe          = new Models.FileEntry();
                    fe.FileName = th.ToString();
                    feList.Add(fe);
                }
                else
                {
                    for (int i = 0; i <= ti.num_files() - 1; i++)
                    {
                        fe           = new Models.FileEntry(ti.files().at(i));
                        fe.FileName  = ti.files().file_name(i);
                        fe.IsValid   = ti.files().is_valid();
                        fe.PieceSize = ti.piece_size(i);
                        //ti.files().name(); ???
                        //ti.trackers();
                        feList.Add(fe);
                    }
                }
            }
            //Core.TorrentHandle th = getTorrentHandle(hash);
            //Core.TorrentInfo ti = th.torrent_file();
            return(feList);
        }
Exemple #3
0
 public static void addTorrent(byte[] buffer)
 {
     using (var atp = new Core.AddTorrentParams())
         using (var ti = new Core.TorrentInfo(buffer))
         {
             atp.save_path = Settings.User.PathDownload;
             atp.ti        = ti;
             atp.flags    &= ~Core.ATPFlags.flag_auto_managed; // remove auto managed flag
             atp.flags    &= ~Core.ATPFlags.flag_paused;       // remove pause on added torrent
             _torrentSession.async_add_torrent(atp);
         }
 }
Exemple #4
0
 public void LoadTorrent(string filename)
 {
     using (AddTorrentParams atp = new AddTorrentParams())
         using (TorrentInfo ti = new TorrentInfo(filename))
         {
             atp.ti       = ti;
             atp.SavePath = @"C:\Download";
             //atp.Flags &= ~Core.ATPFlags.flag_auto_managed; // remove auto managed flag
             //atp.Flags &= ~Core.ATPFlags.flag_paused; // remove pause on added torrent
             // atp.Flags &= ~Core.ATPFlags.flag_use_resume_save_path; //
             Session_AsyncAddTorrent(atp.Handle);
         }
 }
Exemple #5
0
        public static List <Models.FileEntry> getTorrentFiles(string hash)
        {
            List <Models.FileEntry> feList = new List <Models.FileEntry>();

            Models.FileEntry fe;

            Core.TorrentHandle th = getTorrentHandle(hash);
            Core.TorrentInfo   ti = th.torrent_file();
            for (int i = 0; i <= ti.num_files() - 1; i++)
            {
                fe           = new Models.FileEntry(ti.files().at(i));
                fe.FileName  = ti.files().file_name(i);
                fe.IsValid   = ti.files().is_valid();
                fe.PieceSize = ti.piece_size(i);
                //ti.files().name(); ???
                //ti.trackers();
                feList.Add(fe);
            }
            return(feList);
        }
Exemple #6
0
        public StreamTorrent(string hash, int fileIndex, EventHandler <string> _callback)
        {
            if (_callback != null)
            {
                BufferReady += _callback;
            }
            _hash          = hash;
            _torrentHandle = SessionManager.Instance.getTorrentHandle(hash);
            if (!_torrentHandle.has_metadata())
            {
                return;
            }
            Core.TorrentInfo ti = _torrentHandle.torrent_file();
            var files           = ti.files();

            if (fileIndex < 0 || fileIndex > files.num_files())
            {
                throw new ArgumentOutOfRangeException();
            }
            var fileEntry = files.at(fileIndex);

            file_path = Settings.User.PathDownload + "\\" + fileEntry.path;
            var peer_req = ti.map_file(fileIndex, 0, 1048576);

            starting_point = last_have_piece = peer_req.piece;
            piece_length   = ti.piece_length();
            num_pieces     = (int)Math.Ceiling((double)(fileEntry.size / piece_length));
            end_piece      = Math.Min(last_have_piece + num_pieces, ti.num_pieces() - 1);

            //set first piece with higher priority
            _torrentHandle.piece_priority(last_have_piece, 7);
            _onStreaming = true;
            if (_torrentHandle.have_piece(last_have_piece))
            {
                CountHavePieces();
                InvokeStreaming();
            }
        }
Exemple #7
0
 public static void LoadFastResumeData()
 {
     if (Directory.Exists("Fastresume"))
     {
         string[] files = Directory.GetFiles("Fastresume", "*.fastresume");
         foreach (string s in files)
         {
             var data      = File.ReadAllBytes(s);
             var info_hash = Path.GetFileNameWithoutExtension(s);
             var filename  = "Fastresume/" + info_hash + ".torrent";
             Core.TorrentInfo ti;
             if (File.Exists(filename))
             {
                 ti = new Core.TorrentInfo(filename);
             }
             else
             {
                 ti = new Core.TorrentInfo(new Core.Sha1Hash(info_hash));
             }
             using (var atp = new Core.AddTorrentParams())
                 using (ti)
                 {
                     atp.ti          = ti;
                     atp.save_path   = Settings.User.PathDownload;
                     atp.resume_data = (sbyte[])(Array)data;
                     atp.flags      &= ~Core.ATPFlags.flag_auto_managed; // remove auto managed flag
                     atp.flags      &= ~Core.ATPFlags.flag_paused;       // remove pause on added torrent
                     _torrentSession.async_add_torrent(atp);
                 }
         }
     }
     else
     {
         Directory.CreateDirectory("Fastresume");
     }
 }