示例#1
0
 public static bool resumeTorrent(string hash)
 {
     log.Trace("requested resume({0})", hash);
     Core.TorrentHandle th = getTorrentHandle(hash);
     th.resume();
     return(th.status().paused == true);
 }
示例#2
0
        private static void OnTorrentAddAlert(Core.torrent_added_alert a)
        {
            Core.TorrentHandle th = a.handle;
            if (TorrentHandles.TryAdd(th.info_hash().ToString(), th))
            {
                using (Core.TorrentStatus ts = th.status())
                {
                    var stat = "Paused";
                    if (!ts.paused)
                    {
                        stat = Utils.GiveMeStateFromEnum(ts.state);
                    }
                    var evnt = new EventsArgs.OnTorrentAddedEventArgs
                    {
                        Hash          = th.info_hash().ToString(),
                        Name          = ts.name,
                        Progress      = ts.progress,
                        QueuePosition = ts.queue_position,
                        Status        = stat
                    };
                    //log.Debug("torrent added: name {0}; status {1}; hash {2}", ts.name, ts.state.ToString(), ts.info_hash.ToString());

                    // notify web that a new id must be requested via webapi
                    if (webServer != null)
                    {
                        var context = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext <www.SignalRHub>();
                        context.Clients.All.notifyTorrentAdded(evnt.Hash);
                    }

                    TorrentAdded?.Invoke(null, evnt);
                }
            }
        }
示例#3
0
        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);
        }
示例#4
0
 public static void deleteTorrent(string hash, bool deleteFileToo = false)
 {
     log.Trace("requested delete({0}, deleteFile:{1})", hash, deleteFileToo);
     Core.TorrentHandle th = getTorrentHandle(hash);
     _torrentSession.remove_torrent(th, Convert.ToInt32(deleteFileToo));
     TorrentHandles.TryRemove(hash, out th);
     File.Delete(Environment.CurrentDirectory + "./Fastresume/" + hash + ".fastresume");
     File.Delete(Environment.CurrentDirectory + "./Fastresume/" + hash + ".torrent");
 }
示例#5
0
        public string GetFilePathFromHash(string hash, int fileIndex)
        {
            Core.TorrentHandle th = getTorrentHandle(hash);
            var    ti             = th.torrent_file();
            var    files          = ti.files();
            var    fileEntry      = files.at(fileIndex);
            string path           = Settings.User.PathDownload + "\\" + fileEntry.path;

            return(path);
        }
示例#6
0
        public static List <Models.TorrentStatus> getTorrentStatusList()
        {
            _dispatcherTimer.Stop();
            _sessionStatusDispatcherTimer.Stop();
            List <Models.TorrentStatus> thl = new List <Models.TorrentStatus>();

            foreach (KeyValuePair <string, Core.TorrentHandle> item in TorrentHandles)
            {
                Core.TorrentHandle th = item.Value;
                thl.Add(new Models.TorrentStatus(th.status()));// Models.TorrentHandle(item.Value));
            }
            _dispatcherTimer.Start();
            _sessionStatusDispatcherTimer.Start();
            return(thl);
        }
示例#7
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);
        }
示例#8
0
        public static void StreamTorrent(Core.TorrentHandle handle, int fileIndex)
        {
            var ti    = handle.torrent_file();
            var files = ti.files();

            if (fileIndex < 0 || fileIndex > files.num_files())
            {
                throw new ArgumentOutOfRangeException();
            }
            var fileEntry    = files.at(fileIndex);
            var peer_req     = ti.map_file(fileIndex, 0, 1048576);
            var startPiece   = peer_req.piece;
            var piece_length = ti.piece_length();
            var num_pieces   = (int)Math.Ceiling((double)(fileEntry.size / piece_length));
            var end_piece    = Math.Min(startPiece + num_pieces, ti.num_pieces() - 1);

            for (int i = startPiece; i < end_piece; i++)
            {
                handle.piece_priority(i, 0);
            }

            //set first piece with higher priority
            handle.piece_priority(startPiece, 7);
            var lastPiece = startPiece;

            start_window = startPiece;
            end_window   = Math.Min(end_window, lastPiece);
            for (int i = start_window; i <= end_window; i++)
            {
                handle.piece_priority(i, 1);
            }
            while (start_window <= end_window)
            {
                if (handle.have_piece(start_window))
                {
                    handle.piece_priority(++start_window, 7);
                    continue;
                }
                Thread.Sleep(200);
            }
        }
示例#9
0
        public void deleteTorrent(string hash, bool deleteFileToo = false)
        {
            if (_streamingHash == hash && _isStreaming)
            {
                _streamingList[hash].StopStreaming(BufferingReadyCallback);
            }


            log.Trace("requested delete({0}, deleteFile:{1})", hash, deleteFileToo);
            Core.TorrentHandle th = getTorrentHandle(hash);
            _torrentSession.remove_torrent(th, Convert.ToInt32(deleteFileToo));
            TorrentHandles.TryRemove(hash, out th);
            th?.Dispose();
            th = null;
            try
            {
                File.Delete(Environment.CurrentDirectory + "./Fastresume/" + hash + ".fastresume");
                File.Delete(Environment.CurrentDirectory + "./Fastresume/" + hash + ".torrent");
            }
            catch (Exception) { }
        }
示例#10
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();
            }
        }
示例#11
0
 public torrent_alert(IntPtr alert)
     : base(alert)
 {
     h = new TorrentHandle(Alert_TorrentAlert_TorrentHandle_Get(handle));
 }