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); } }
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(); } }