« Previous - Version 14/18 (diff) - Next » - Current version
Alan McGovern, 07/30/2009 01:06 AM


Managing Torrents

Example 1 shows how to add a torrent to the engine and start the download
Example 2 shows how to hash check a torrent and show a progress indicator

Example 1

 1     class MainClass
 2     {
 3         ClientEngine engine;
 4         string savePath;
 5 
 6         // savePath is the directory where downloads will be stored
 7         public MainClass(string savePath)
 8         {
 9             // Create a basic ClientEngine without changing any settings
10             this.engine = new ClientEngine(new EngineSettings());
11             this.savePath = savePath;
12         }
13 
14         public void DownloadTorrent(string path)
15         {
16             // Open the .torrent file
17             Torrent torrent = Torrent.Load(path);
18 
19             // Create the manager which will download the torrent to savePath
20             // using the default settings.
21             TorrentManager manager = new TorrentManager(torrent, savePath, new TorrentSettings());
22 
23             // Register the manager with the engine
24             engine.Register(manager);
25 
26             // Begin the download
27             manager.Start();
28         }
29     }

Example 2

 1         public void HashTorrent(TorrentManager manager)
 2         {
 3             // Note: The manager must be in the 'Stopped' state in order to perform
 4             // a hash check. Also, to make the sample easier the event handler
 5             // is not unregistered. In your application be careful you don't
 6             // accidentally attach a new event handler every time the torrent is hashed
 7             manager.PieceHashed += delegate (object o, PieceHashedEventArgs e) {
 8                 int pieceIndex = e.PieceIndex;
 9                 int totalPieces = e.TorrentManager.Torrent.Pieces.Count;
10                 double progress = (double) pieceIndex / totalPieces * 100.0;
11                 if (e.HashPassed)
12                     Console.WriteLine("Piece {0} of {1} is complete", pieceIndex, totalPieces);
13                 else
14                     Console.WriteLine("Piece {0} of {1} is corrupt or incomplete ", pieceIndex, totalPieces);
15 
16                 // This shows how complete the hashing is.
17                 Console.WriteLine("Total progress is: {0}%", progress);
18 
19                 // This shows the percentage completion of the download. This value
20                 // is updated as the torrent is hashed or new pieces are downloaded
21                 Console.WriteLine("{0}% of the torrent is complete");
22             };
23 
24             // If 'true' is passed, the torrent will automatically go to the 'Downloading' or 'Seeding'
25             // state once the hash check is finished. Otherwise it will return to the 'Stopped' state.
26             manager.HashCheck(false);
27         }
 1 
 2 ClientEngine engine = new ClientEngine(new EngineSettings(downloadsPath, port));
 3 
 4 //DHT
 5 DhtListener dhtListner = new UdpListener (new IPEndPoint (IPAddress.Any, port));
 6 DhtEngine dht = new DhtEngine (dhtListner);
 7 engine.RegisterDht(dht);
 8 dhtListner.Start();
 9 //byte array of dht nodes can be null if you have never connect to DHT before today!
10 engine.DhtEngine.Start(nodes);
11 
12 torrent = Torrent.Load("test.torrent");
13 TorrentManager manager = new TorrentManager(torrent, downloadsPath, torrentDefaults);
14 
15 //FastResume code need a BencodedDictionnary
16 //If you jsut start the torrent, you have no fast resume
17 // but if you have download a part of the torrent you can save fast resume data 
18 //(manager.SaveFastResume()) and restore it later to do a quicker load
19 manager.LoadFastResume(new FastResume (BEncDictFastResume));
20 
21 engine.Register(manager);
22 manager.Start ();
23 

Also available in: HTML TXT