Creating Torrents

Version 5 (Alan McGovern, 07/30/2009 12:28 AM)

1 2 Alan McGovern
h1. Creating Torrents
2 2 Alan McGovern
3 5 Alan McGovern
The first example shows you how to create a torrent synchronously. The second example builds on this and shows you how to create a torrent asynchronously, so that it does not block the main thread in your application.
4 5 Alan McGovern
5 5 Alan McGovern
*Example 1:*
6 2 Alan McGovern
<pre><code class="java">
7 2 Alan McGovern
        // 'path' is the location of the file/folder which is going to be converted
8 2 Alan McGovern
        // to a torrent. 'savePath' is where the .torrent file will be saved.
9 2 Alan McGovern
        void CreateTorrent(string path, string savePath)
10 2 Alan McGovern
        {
11 2 Alan McGovern
            // The class used for creating the torrent
12 2 Alan McGovern
            TorrentCreator c = new TorrentCreator();
13 2 Alan McGovern
            
14 2 Alan McGovern
            // Add one tier which contains two trackers
15 2 Alan McGovern
            List<string> tier = new List<string>();
16 2 Alan McGovern
            tier.Add("http://www.example.com/announce");
17 2 Alan McGovern
            tier.Add("http://backup.example.com/announce");
18 2 Alan McGovern
            
19 2 Alan McGovern
            c.Announces.Add(tier);
20 2 Alan McGovern
            c.Comment = "This is the comment";
21 2 Alan McGovern
            c.CreatedBy = "Alan using " + VersionInfo.ClientVersion;
22 2 Alan McGovern
            c.Publisher = "www.homepage.com";
23 2 Alan McGovern
24 2 Alan McGovern
            // Set the torrent as private so it will not use DHT or peer exchange
25 4 Alan McGovern
            // Generally you will not want to set this.
26 2 Alan McGovern
            c.Private = true;
27 2 Alan McGovern
28 2 Alan McGovern
            // Path can be either a directory *or* a file.
29 2 Alan McGovern
            c.Path = path;
30 2 Alan McGovern
31 2 Alan McGovern
            // Every time a piece has been hashed, this event will fire. It is an
32 2 Alan McGovern
            // asynchronous event, so you have to handle threading yourself.
33 2 Alan McGovern
            c.Hashed += delegate (object o, TorrentCreatorEventArgs e) {
34 2 Alan McGovern
                Console.WriteLine("Current File is {0}% hashed", e.FileCompletion);
35 2 Alan McGovern
                Console.WriteLine("Overall {0}% hashed", e.OverallCompletion);
36 2 Alan McGovern
                Console.WriteLine("Total data to hash: {0}", e.OverallSize);
37 2 Alan McGovern
            };
38 2 Alan McGovern
39 2 Alan McGovern
            // Create the torrent file and save it to the specified path
40 2 Alan McGovern
            c.Create(save_path);
41 1
        }
42 5 Alan McGovern
</code></pre>
43 5 Alan McGovern
44 5 Alan McGovern
45 5 Alan McGovern
*Example 2:*
46 5 Alan McGovern
<pre><code class="java">
47 5 Alan McGovern
        // 'path' is the location of the file/folder which is going to be converted
48 5 Alan McGovern
        // to a torrent. 'savePath' is where the .torrent file will be saved.
49 5 Alan McGovern
        void CreateTorrent(string path, string savePath)
50 5 Alan McGovern
        {
51 5 Alan McGovern
            // The class used for creating the torrent
52 5 Alan McGovern
            TorrentCreator c = new TorrentCreator();
53 5 Alan McGovern
54 5 Alan McGovern
            // Fill in the path, trackers as in Example 1
55 5 Alan McGovern
            FillInStandardInformation(c);
56 5 Alan McGovern
57 5 Alan McGovern
            // Create the torrent asynchronously
58 5 Alan McGovern
            c.BeginCreate(c, TorrentCreated);
59 5 Alan McGovern
        }
60 5 Alan McGovern
61 5 Alan McGovern
        void TorrentCreated(IAsyncResult result)
62 5 Alan McGovern
        {
63 5 Alan McGovern
            TorrentCreator c = (TorrentCreator)result.AsyncState;
64 5 Alan McGovern
65 5 Alan McGovern
            // If any errors occured while creating the torrent, they
66 5 Alan McGovern
            // will be rethrown here.
67 5 Alan McGovern
            try
68 5 Alan McGovern
            {   
69 5 Alan McGovern
                // Open the destination file and use the EndCreate overload which
70 5 Alan McGovern
                // writes the data directly to the Stream. 
71 5 Alan McGovern
                using (FileStream stream = File.OpenWrite(savePath))
72 5 Alan McGovern
                    c.EndCreate(result, stream);
73 5 Alan McGovern
            }
74 5 Alan McGovern
            catch (Exception ex)
75 5 Alan McGovern
            {
76 5 Alan McGovern
                Console.WriteLine("Error creating torrent: {0}", ex);
77 5 Alan McGovern
            }
78 5 Alan McGovern
        }
79 5 Alan McGovern
</code></pre>