public MyTorrent() { setupEngine();//.Wait(); listener = new Top10Listener(10); monitorTimer.Interval = 2000; monitorTimer.Elapsed += CheckDownloadProgress; TorrentInfoList = new ObservableCollection <TorrentInfoModel>(); MessagingCenter.Subscribe <TorrentActionPopup, Int32>(this, "StartThisTorrent", (sender, args) => { Console.WriteLine("Message: StartThisTorrent= {0}", args); if (args >= 0) { DoTorrentAction(TorrentInfoList[args], TorrentAction.START); } } ); MessagingCenter.Subscribe <TorrentActionPopup, Int32>(this, "StopThisTorrent", (sender, args) => { Console.WriteLine("Message: StopThisTorrent= {0}", args); if (args >= 0) { DoTorrentAction(TorrentInfoList[args], TorrentAction.STOP); } } ); MessagingCenter.Subscribe <TorrentActionPopup, Int32>(this, "PauseThisTorrent", (sender, args) => { Console.WriteLine("Message: PauseThisTorrent= {0}", args); if (args >= 0) { DoTorrentAction(TorrentInfoList[args], TorrentAction.PAUSE); } } ); MessagingCenter.Subscribe <TorrentActionPopup, Int32>(this, "RemoveThisTorrent", (sender, args) => { Console.WriteLine("Message: RemoveThisTorrent= {0}", args); if (args >= 0) { DoTorrentAction(TorrentInfoList[args], TorrentAction.REMOVE); } } ); MessagingCenter.Subscribe <TorrentActionPopup, Int32>(this, "DeleteThisTorrent", (sender, args) => { Console.WriteLine("Message: DeleteThisTorrent= {0}", args); if (args >= 0) { DoTorrentAction(TorrentInfoList[args], TorrentAction.DELETE); } } ); }
static void Main(string[] args) { /* Generate the paths to the folder we will save .torrent files to and where we download files to */ _basePath = Environment.CurrentDirectory; // This is the directory we are currently in _torrentsPath = Path.Combine(_basePath, "Torrents"); // This is the directory we will save .torrents to _downloadsPath = Path.Combine(_basePath, "Downloads"); // This is the directory we will save downloads to _fastResumeFile = Path.Combine(_torrentsPath, "fastresume.data"); _dhtNodeFile = Path.Combine(_basePath, "DhtNodes"); _torrents = new List<TorrentManager>(); // This is where we will store the torrentmanagers _listener = new Top10Listener(10); // We need to cleanup correctly when the user closes the window by using ctrl-c // or an unhandled exception happens Console.CancelKeyPress += delegate { Shutdown(); }; AppDomain.CurrentDomain.ProcessExit += delegate { Shutdown(); }; AppDomain.CurrentDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(e.ExceptionObject); Shutdown(); }; Thread.GetDomain().UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(e.ExceptionObject); Shutdown(); }; StartEngine(); }
static Top10Listener _listener; // This is a subclass of TraceListener which remembers the last 20 statements sent to it static void Main(string[] args) { /* Generate the paths to the folder we will save .torrent files to and where we download files to */ _basePath = Environment.CurrentDirectory; // This is the directory we are currently in _torrentsPath = Path.Combine(_basePath, "Torrents"); // This is the directory we will save .torrents to _downloadsPath = Path.Combine(_basePath, "Downloads"); // This is the directory we will save downloads to _fastResumeFile = Path.Combine(_torrentsPath, "fastresume.data"); _dhtNodeFile = Path.Combine(_basePath, "DhtNodes"); _torrents = new List <TorrentManager>(); // This is where we will store the torrentmanagers _listener = new Top10Listener(10); // We need to cleanup correctly when the user closes the window by using ctrl-c // or an unhandled exception happens Console.CancelKeyPress += delegate { Shutdown(); }; AppDomain.CurrentDomain.ProcessExit += delegate { Shutdown(); }; AppDomain.CurrentDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(e.ExceptionObject); Shutdown(); }; Thread.GetDomain().UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(e.ExceptionObject); Shutdown(); }; StartEngine(); }
private void DownloaderWorker_DoWork(object sender, DoWorkEventArgs e) { try { if (e.Argument == null) { return; } basePath = Environment.CurrentDirectory; dhtNodeFile = System.IO.Path.Combine(basePath, "DhtNodes"); downloadsPath = System.IO.Path.Combine(basePath, "Downloads"); torrentsPath = System.IO.Path.Combine(basePath, "Torrents"); fastResumeFile = System.IO.Path.Combine(torrentsPath, "fastresume.data"); torrents = new List <TorrentManager>(); // The list where all the torrentManagers will be stored that the engine gives us listener = new Top10Listener(10); string torrentpath = e.Argument.ToString(); int port = 6969; Torrent torrent = null; // Create the settings which the engine will use // downloadsPath - this is the path where we will save all the files to // port - this is the port we listen for connections on EngineSettings engineSettings = new EngineSettings(downloadsPath, port); engineSettings.PreferEncryption = false; engineSettings.AllowedEncryption = EncryptionTypes.All; //engineSettings.GlobalMaxUploadSpeed = 30 * 1024; //engineSettings.GlobalMaxDownloadSpeed = 100 * 1024; //engineSettings.MaxReadRate = 1 * 1024 * 1024; // Create the default settings which a torrent will have. // 4 Upload slots - a good ratio is one slot per 5kB of upload speed // 50 open connections - should never really need to be changed // Unlimited download speed - valid range from 0 -> int.Max // Unlimited upload speed - valid range from 0 -> int.Max TorrentSettings torrentDefaults = new TorrentSettings(4, 150, 0, 0); // Create an instance of the engine. engine = new ClientEngine(engineSettings); engine.ChangeListenEndpoint(new IPEndPoint(IPAddress.Any, port)); byte[] nodes = null; try { nodes = File.ReadAllBytes(dhtNodeFile); } catch { Console.WriteLine("No existing dht nodes could be loaded"); } DhtListener dhtListner = new DhtListener(new IPEndPoint(IPAddress.Any, port)); DhtEngine dht = new DhtEngine(dhtListner); engine.RegisterDht(dht); dhtListner.Start(); engine.DhtEngine.Start(nodes); // If the SavePath does not exist, we want to create it. if (!Directory.Exists(engine.Settings.SavePath)) { Directory.CreateDirectory(engine.Settings.SavePath); } // If the torrentsPath does not exist, we want to create it if (!Directory.Exists(torrentsPath)) { Directory.CreateDirectory(torrentsPath); } BEncodedDictionary fastResume; try { fastResume = BEncodedValue.Decode <BEncodedDictionary>(File.ReadAllBytes(fastResumeFile)); } catch { fastResume = new BEncodedDictionary(); } // Load the .torrent from the file into a Torrent instance // You can use this to do preprocessing should you need to torrent = Torrent.Load(torrentpath); // When any preprocessing has been completed, you create a TorrentManager // which you then register with the engine. TorrentManager manager = new TorrentManager(torrent, downloadsPath, torrentDefaults); //if (fastResume.ContainsKey(torrent.InfoHash.ToHex())) // manager.LoadFastResume(new FastResume((BEncodedDictionary)fastResume[torrent.infoHash.ToHex()])); engine.Register(manager); // Store the torrent manager in our list so we can access it later torrents.Add(manager); manager.PeersFound += new EventHandler <PeersAddedEventArgs>(manager_PeersFound); // Every time a piece is hashed, this is fired. manager.PieceHashed += delegate(object o, PieceHashedEventArgs ec) { lock (listener) listener.WriteLine(string.Format("Piece Hashed: {0} - {1}", ec.PieceIndex, ec.HashPassed ? "Pass" : "Fail")); }; // Every time the state changes (Stopped -> Seeding -> Downloading -> Hashing) this is fired manager.TorrentStateChanged += delegate(object o, TorrentStateChangedEventArgs ev) { lock (listener) listener.WriteLine("OldState: " + ev.OldState.ToString() + " NewState: " + ev.NewState.ToString()); }; // Every time the tracker's state changes, this is fired //foreach (TrackerTier tier in manager.TrackerManager) //{ // //foreach (MonoTorrent.Client.Tracker.Tracker t in tier.Trackers) // //{ // // t.AnnounceComplete += delegate (object sender, AnnounceResponseEventArgs e) // // { // // listener.WriteLine(string.Format("{0}: {1}", e.Successful, e.Tracker.ToString())); // // }; // //} //} // Start the torrentmanager. The file will then hash (if required) and begin downloading/seeding manager.Start(); // While the torrents are still running, print out some stats to the screen. // Details for all the loaded torrent managers are shown. bool running = true; StringBuilder sb = new StringBuilder(1024); while (running) { //if ((i++) % 10 == 0) //{ sb.Remove(0, sb.Length); running = torrents.Exists(delegate(TorrentManager m) { return(m.State != TorrentState.Stopped); }); //AppendFormat(sb, "Total Download Rate: {0:0.00}kB/sec", engine.TotalDownloadSpeed / 1024.0); downloadspeed = (engine.TotalDownloadSpeed / 1024.0).ToString(); //AppendFormat(sb, "Total Upload Rate: {0:0.00}kB/sec", engine.TotalUploadSpeed / 1024.0); //AppendFormat(sb, "Disk Read Rate: {0:0.00} kB/s", engine.DiskManager.ReadRate / 1024.0); //AppendFormat(sb, "Disk Write Rate: {0:0.00} kB/s", engine.DiskManager.WriteRate / 1024.0); //AppendFormat(sb, "Total Read: {0:0.00} kB", engine.DiskManager.TotalRead / 1024.0); //AppendFormat(sb, "Total Written: {0:0.00} kB", engine.DiskManager.TotalWritten / 1024.0); //AppendFormat(sb, "Open Connections: {0}", engine.ConnectionManager.OpenConnections); //AppendSeperator(sb); //AppendFormat(sb, "State: {0}", manager.State); //AppendFormat(sb, "Name: {0}", manager.Torrent == null ? "MetaDataMode" : manager.Torrent.Name); //AppendFormat(sb, "Progress: {0:0.00}", manager.Progress); //progress = manager.Progress.ToString(); //AppendFormat(sb, "Download Speed: {0:0.00} kB/s", manager.Monitor.DownloadSpeed / 1024.0); //AppendFormat(sb, "Upload Speed: {0:0.00} kB/s", manager.Monitor.UploadSpeed / 1024.0); //AppendFormat(sb, "Total Downloaded: {0:0.00} MB", manager.Monitor.DataBytesDownloaded / (1024.0 * 1024.0)); //AppendFormat(sb, "Total Uploaded: {0:0.00} MB", manager.Monitor.DataBytesUploaded / (1024.0 * 1024.0)); MonoTorrent.Client.Tracker.Tracker tracker = manager.TrackerManager.CurrentTracker; //AppendFormat(sb, "Tracker Status: {0}", tracker == null ? "<no tracker>" : tracker.State.ToString()); //AppendFormat(sb, "Warning Message: {0}", tracker == null ? "<no tracker>" : tracker.WarningMessage); //AppendFormat(sb, "Failure Message: {0}", tracker == null ? "<no tracker>" : tracker.FailureMessage); //if (manager.PieceManager != null) // AppendFormat(sb, "Current Requests: {0}", manager.PieceManager.CurrentRequestCount()); //foreach (PeerId p in manager.GetPeers()) // AppendFormat(sb, "\t{2} - {1:0.00}/{3:0.00}kB/sec - {0}", p.Peer.ConnectionUri, // p.Monitor.DownloadSpeed / 1024.0, // p.AmRequestingPiecesCount, // p.Monitor.UploadSpeed / 1024.0); //AppendFormat(sb, "", null); //if (manager.Torrent != null) // foreach (TorrentFile file in manager.Torrent.Files) // AppendFormat(sb, "{1:0.00}% - {0}", file.Path, file.BitField.PercentComplete); //Console.Clear(); //Console.WriteLine(sb.ToString()); //listener.ExportTo(Console.Out); //} DownloaderWorker.ReportProgress(Convert.ToInt32(manager.Progress), manager.State.ToString()); System.Threading.Thread.Sleep(500); } } catch (Exception ex) { } }
public static void button59_Click(object sender, EventArgs e) { basePath = ftg; basePaths = ftg; // This is the directory we are currently in torrentsPath = basePath; // This is the directory we will save .torrents to downloadsPath = basePaths; // This is the directory we will save downloads to if (!File.Exists(basePath + "/fastresume.data")) { Console.Write("copy fastresume"); File.Copy("fastresume.data", basePath + "/fastresume.data"); } fastResumeFile = torrentsPath + "/fastresume.data"; if (!File.Exists(basePath + "/DhtNodes")) { Console.Write("copy node"); File.Copy("DhtNodes", basePath + "/DhtNodes"); } dhtNodeFile = basePath + "/DhtNodes"; torrents = new List <TorrentManager>(); // This is where we will store the torrentmanagers listener = new Top10Listener(10); string[] fgt = Directory.GetFiles(basePath, "*.torrent"); foreach (string fy in fgt) { if (fy.Contains(".torrent")) { Console.Write("true contain"); kjl = true; } else { Console.Write("false contain"); kjl = false; } } if (kjl) { StartEngine(); } else { FolderBrowserDialog ljk = new FolderBrowserDialog(); ljk.ShowDialog(); string hfj = ljk.SelectedPath; string[] fgts = Directory.GetFiles(ljk.SelectedPath, "*.torrent"); Console.Write("get to " + ljk.SelectedPath); foreach (string fy in fgts) { if (fy.Contains(".torrent")) { kjl = true; basePath = ljk.SelectedPath; StartEngine(); } else { kjl = false; Application.DoEvents(); MessageBox.Show("error no .torrent"); } } } try { String jg = "tada.wav"; Application.DoEvents(); SoundPlayer hv = new SoundPlayer(); hv.SoundLocation = jg; hv.Play(); } catch { } }