Exemplo n.º 1
0
 #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
 public Torrent(BitSwarm bitSwarm)
 {
     this.bitSwarm = bitSwarm;
     file          = new TorrentFile();
     data          = new TorrentData();
     metadata      = new MetaData();
     file.trackers = new List <Uri>();
 }
Exemplo n.º 2
0
        public void Initialize()
        {
            Pause();

            fileIndex    = -1;
            fileDistance = -1;
            status       = Status.STOPPED;

            tsStream?.Dispose(); tsStream = null;
        }
Exemplo n.º 3
0
        public int Open(string url, StreamType streamType = StreamType.TORRENT, bool isMagnetLink = true)
        {
            this.streamType = streamType;
            status          = Status.OPENING;
            Initialize();

            if (streamType == StreamType.FILE)
            {
                fsStream = new FileStream(url, FileMode.Open, FileAccess.Read);
                fileSize = fsStream.Length;

                Torrent torrent = new Torrent("whatever");
                torrent.file         = new Torrent.TorrentFile();
                torrent.file.paths   = new List <string>();
                torrent.file.lengths = new List <long>();

                torrent.file.paths.Add("MediaFile1.mp4");
                torrent.file.paths.Add("MediaFile2.mp4");
                torrent.file.lengths.Add(123123894);
                torrent.file.lengths.Add(123123897);

                MetadataReceived(this, new BitSwarm.MetadataReceivedArgs(torrent));

                status = Status.OPENED;
            }
            else if (streamType == StreamType.TORRENT)
            {
                BitSwarm.OptionsStruct opt = BitSwarm.GetDefaultsOptions();
                opt.PieceTimeout = 4300;
                //opt.LogStats            = true;
                //opt.Verbosity           = 2;

                try
                {
                    if (isMagnetLink)
                    {
                        tsStream = new BitSwarm(new Uri(url), opt);
                    }
                    else
                    {
                        tsStream = new BitSwarm(url, opt);
                    }
                } catch (Exception e) { Log($"[MS] BitSwarm Failed Opening Url {e.Message}\r\n{e.StackTrace}"); Initialize(); status = Status.FAILED; return(-1); }

                tsStream.MetadataReceived += MetadataReceived;
                tsStream.StatsUpdated     += Stats;

                tsStream.Start();
                sortedPaths = null;
                status      = Status.OPENED;
            }

            return(0);
        }
Exemplo n.º 4
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Start")
            {
                output.Text = "";
                listBox1.Items.Clear();
                button2.Enabled = false;

                try
                {
                    opt = new Options();

                    opt.FolderComplete = downPath.Text;

                    opt.MaxThreads        = int.Parse(maxCon.Text);
                    opt.MinThreads        = int.Parse(maxThreads.Text);
                    opt.SleepModeLimit    = int.Parse(sleepLimit.Text);
                    opt.PeersFromTracker  = int.Parse(peersFromTrackers.Text);
                    opt.ConnectionTimeout = int.Parse(conTimeout.Text);
                    opt.HandshakeTimeout  = int.Parse(handTimeout.Text);
                    opt.PieceTimeout      = int.Parse(pieceTimeout.Text);
                    opt.MetadataTimeout   = int.Parse(metaTimeout.Text);

                    opt.Verbosity  = 0;
                    opt.LogDHT     = false;
                    opt.LogStats   = false;
                    opt.LogTracker = false;
                    opt.LogPeer    = false;

                    output.Text  = "Started at " + DateTime.Now.ToString("G", DateTimeFormatInfo.InvariantInfo) + "\r\n";
                    button1.Text = "Stop";

                    bitSwarm = new BitSwarm(opt);

                    bitSwarm.StatsUpdated     += BitSwarm_StatsUpdated;
                    bitSwarm.MetadataReceived += BitSwarm_MetadataReceived;
                    bitSwarm.StatusChanged    += BitSwarm_StatusChanged;

                    bitSwarm.Open(input.Text);
                    bitSwarm.Start();
                }
                catch (Exception e1)
                {
                    output.Text    += e1.Message + "\r\n" + e1.StackTrace;
                    button1.Text    = "Start";
                    button2.Enabled = false;
                }
            }
            else
            {
                bitSwarm.Dispose();
                button1.Text = "Start";
            }
        }
Exemplo n.º 5
0
        public void Initialize()
        {
            Pause();

            fileIndex     = -1;
            fileIndexNext = -1;
            fileDistance  = -1;
            status        = Status.STOPPED;

            bitSwarm?.Dispose();
            torrent?.Dispose();
            bitSwarm = null;
            torrent  = null;
        }
Exemplo n.º 6
0
        private static void BitSwarm_StatusChanged(object source, BitSwarm.StatusChangedArgs e)
        {
            if (e.Status == 0 && torrent != null && torrent.file.name != null)
            {
                Console.WriteLine($"\r\nDownload of {torrent.file.name} success!\r\n\r\n");
                Console.WriteLine(bitSwarm.DumpTorrent());
                Console.WriteLine($"\r\nDownload of {torrent.file.name} success!\r\n\r\n");
            }
            else if (e.Status == 2)
            {
                Console.WriteLine("An error has been occured :( \r\n" + e.ErrorMsg);
            }

            bitSwarm?.Dispose();
            bitSwarm        = null;
            sessionFinished = true;
        }
Exemplo n.º 7
0
        public void Dispose()
        {
            if (Disposed)
            {
                return;
            }

            Log("Stopped");

            player.decoder.Stop();
            player.decoder.demuxer.ioStream = null;
            bitSwarm?.Dispose();
            Torrent?.Dispose();
            bitSwarm            = null;
            Torrent             = null;
            sortedPaths         = null;
            downloadNextStarted = false;
            Disposed            = true;
        }
Exemplo n.º 8
0
        private void Initialize()
        {
            Dispose();
            ParseSettingsToBitSwarm();

            bitSwarmOpt.EnableBuffering = false;

            if (player.verbosity > 0)
            {
                bitSwarmOpt.Verbosity = 4;
                bitSwarmOpt.LogStats  = true;
            }

            bitSwarm = new BitSwarm(bitSwarmOpt);
            bitSwarm.MetadataReceived += MetadataReceived;
            bitSwarm.StatsUpdated     += StatsUpdated;
            bitSwarm.OnFinishing      += OnFinishing;

            Disposed = false;
        }
Exemplo n.º 9
0
        public void StartDownloading(string magnetLinkOrTorrentFile, string savingLocation, Action <string> writeLog,
                                     Action <IList <string> > onMetadataReceived, Action <TorrentDownloadStats> onDownloadingProgress, Action onDownloadFinished)
        {
            CreateProcessingFoldersIfNotExist();
            var opt = new Options
            {
                FolderComplete   = savingLocation,
                FolderIncomplete = Path.Combine(Directory.GetCurrentDirectory(), IncompleteFolderName),
                FolderTorrents   = Path.Combine(Directory.GetCurrentDirectory(), TorrentsFolderName),
                FolderSessions   = Path.Combine(Directory.GetCurrentDirectory(), SessionsFolderName),
            };

            _bitSwarm = new BitSwarm(opt);

            _bitSwarm.MetadataReceived += (source, e) => OnMetadataReceived(e, writeLog, onMetadataReceived);
            _bitSwarm.StatsUpdated     += (source, e) => OnStatsUpdated(e, writeLog, onDownloadingProgress);
            _bitSwarm.StatusChanged    += (source, e) => OnStatusChanged(e, writeLog, onDownloadFinished);
            _bitSwarm.OnFinishing      += (source, e) => writeLog($"[OnFinishing] Finishing...");

            _bitSwarm.Open(magnetLinkOrTorrentFile);
            _downloadedFiles.Clear();
            _bitSwarm.Start();
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            // Initialize LibVLC
            Core.Initialize();
            libVLC      = new LibVLC();
            libVLC.Log += (s, e) => WriteLine($"LibVLC -> {e.FormattedLog}");

            // Initialize BitSwarm
            Options opt = new Options();

            opt.BlockRequests = 2;      // To avoid timeouts for large byte ranges
            opt.PieceTimeout  = 1000;   // It will reset the requested blocks after 1sec of timeout (this means that we might get them twice or more) | More drop bytes vs Faster streaming (should be set only during open/seek)
            opt.PieceRetries  = 5;      // To avoid disconnecting the peer (reset pieces on first retry but keep the peer alive)
            //opt.Verbosity = 2;
            //opt.LogStats = true;

            bitSwarm = new BitSwarm(opt);
            bitSwarm.MetadataReceived += BitSwarm_MetadataReceived;
            bitSwarm.StatsUpdated     += BitSwarm_StatsUpdated;
            bitSwarm.Open(TORRENT_MAGNET_OR_HASH);
            bitSwarm.Start();

            ReadKey();
        }
Exemplo n.º 11
0
        public int Open(string url, StreamType streamType = StreamType.TORRENT)
        {
            this.streamType = streamType;
            status          = Status.OPENING;
            Initialize();

            if (streamType == StreamType.FILE)
            {
                fsStream = new FileStream(url, FileMode.Open, FileAccess.Read);
                FileSize = fsStream.Length;

                Torrent torrent = new Torrent(null);
                torrent.file         = new Torrent.TorrentFile();
                torrent.file.paths   = new List <string>();
                torrent.file.lengths = new List <long>();

                torrent.file.paths.Add("MediaFile1.mp4");
                torrent.file.paths.Add("MediaFile2.mp4");
                torrent.file.lengths.Add(123123894);
                torrent.file.lengths.Add(123123897);

                MetadataReceived(this, new BitSwarm.MetadataReceivedArgs(torrent));

                status = Status.OPENED;
            }
            else if (streamType == StreamType.TORRENT)
            {
                ParseSettingsToBitSwarm(); // Resets Options (Note: can be changed during a sessions)
                bitSwarmOpt.PieceTimeout = config.TimeoutOpen;
                bitSwarmOpt.PieceRetries = config.RetriesOpen;

                // Testing
                //bitSwarmOpt.SleepModeLimit  = config.SleepMode;
                //bitSwarmOpt.MinThreads      = config.MinThreads;
                //bitSwarmOpt.MaxThreads      = config.MaxThreads;
                //bitSwarmOpt.BlockRequests   = config.BlockRequests;

                //bitSwarmOpt.TrackersPath    = @"c:\root\trackers2.txt";

                //bitSwarmOpt.FolderComplete      = @"c:\root\_f01";
                //bitSwarmOpt.FolderIncomplete    = @"c:\root\_f01";
                //bitSwarmOpt.FolderSessions      = @"c:\root\_f01";
                //bitSwarmOpt.FolderTorrents      = @"c:\root\_f01";

                //bitSwarmOpt.LogPeer = true;
                //bitSwarmOpt.LogTracker = true;
                //bitSwarmOpt.LogStats = true;
                //bitSwarmOpt.Verbosity = 4;

                bitSwarm = new BitSwarm(bitSwarmOpt);

                bitSwarm.MetadataReceived += MetadataReceived;
                bitSwarm.OnFinishing      += DownloadCompleted;
                bitSwarm.StatsUpdated     += Stats;

                try
                {
                    //seemsCompleted = false;
                    bitSwarm.Open(url);
                }
                catch (Exception e)
                {
                    Log($"BitSwarm Failed Opening Url {e.Message} [Will consider completed]");

                    if (System.Text.RegularExpressions.Regex.IsMatch(e.Message, "completed or is invalid"))
                    {
                        //seemsCompleted = true;
                        sortedPaths = null;
                        status      = Status.OPENED;

                        MetadataReceived(this, new BitSwarm.MetadataReceivedArgs(bitSwarm.torrent));

                        return(0);
                    }

                    Initialize();
                    status = Status.FAILED;
                    return(-1);
                }

                bitSwarm.Start();
                sortedPaths = null;
                status      = Status.OPENED;
            }

            return(0);
        }
Exemplo n.º 12
0
        private static void Run(Options userOptions)
        {
            try
            {
                Console.WriteLine($"[BitSwarm v{BitSwarm.Version}] Initializing ...");

                if (userOptions.Input == "config")
                {
                    BitSwarmOptions.CreateConfig(new BitSwarmOptions());
                    Console.WriteLine($"[BitSwarm v{BitSwarm.Version}] Config {BitSwarmOptions.ConfigFile} created.");
                    return;
                }
                else if ((bitSwarmOptions = BitSwarmOptions.LoadConfig()) != null)
                {
                    Console.WriteLine($"[BitSwarm v{BitSwarm.Version}] Config {BitSwarmOptions.ConfigFile} loaded.");
                }

                if (bitSwarmOptions == null)
                {
                    bitSwarmOptions = new BitSwarmOptions();
                }
                if (!Options.ParseOptionsToBitSwarm(userOptions, ref bitSwarmOptions))
                {
                    return;
                }

                // BitSwarm [Create | Subscribe | Open | Start]
                bitSwarm = new BitSwarm(bitSwarmOptions);

                bitSwarm.MetadataReceived += BitSwarm_MetadataReceived;     // Receives torrent data [on torrent file will fire directly, on magnetlink will fire on metadata received]
                bitSwarm.StatsUpdated     += BitSwarm_StatsUpdated;         // Stats refresh every 2 seconds
                bitSwarm.StatusChanged    += BitSwarm_StatusChanged;        // Paused/Stopped or Finished

                bitSwarm.Open(userOptions.Input);
                bitSwarm.Start();

                // Stats | Torrent | Peers Views [Until Stop or Finish]
                ConsoleKeyInfo cki;
                Console.TreatControlCAsInput = true;
                prevHeight = Console.WindowHeight;

                while (!sessionFinished)
                {
                    try
                    {
                        cki = Console.ReadKey();

                        if (sessionFinished)
                        {
                            break;
                        }
                        if ((cki.Modifiers & ConsoleModifiers.Control) != 0 && cki.Key == ConsoleKey.C)
                        {
                            break;
                        }

                        lock (lockRefresh)
                            switch (cki.Key)
                            {
                            case ConsoleKey.D1:
                                view = View.Stats;
                                Console.Clear();
                                Console.WriteLine(bitSwarm.DumpStats());
                                PrintMenu();
                                break;

                            case ConsoleKey.D2:
                                view = View.Torrent;
                                Console.Clear();
                                Console.WriteLine(bitSwarm.DumpTorrent());
                                PrintMenu();

                                break;

                            case ConsoleKey.D3:
                                view = View.Torrent;
                                Console.Clear();
                                Console.WriteLine(bitSwarm.DumpPeers());
                                PrintMenu();

                                break;

                            case ConsoleKey.D4:
                                view = View.Peers;
                                Console.Clear();
                                Console.WriteLine(bitSwarm.DumpPeers());
                                PrintMenu();

                                break;

                            default:
                                break;
                            }
                    } catch (Exception) { }
                }

                // Dispose (force) BitSwarm
                if (bitSwarm != null)
                {
                    bitSwarm.Dispose(true);
                }
            } catch (Exception e) { Console.WriteLine($"[ERROR] {e.Message}"); }
        }