Creating Torrents
Version 10 (Alan McGovern, 07/30/2009 12:46 AM)
1 | 2 | Alan McGovern | h1. Creating Torrents |
---|---|---|---|
2 | 2 | Alan McGovern | |
3 | 8 | Alan McGovern | [[Creating Torrents#Example-1|Example 1]] shows how to create a simple torrent. |
4 | 9 | Alan McGovern | [[Creating Torrents#Example-2|Example 2]] shows how to create a torrent asynchronously so that it does not block the main thread in your application. |
5 | 9 | Alan McGovern | [[Creating Torrents#Example-3|Example 3]] shows how to abort the creation of a torrent after it has started |
6 | 5 | Alan McGovern | |
7 | 7 | Alan McGovern | h2. Example 1 |
8 | 7 | Alan McGovern | |
9 | 2 | Alan McGovern | <pre><code class="java"> |
10 | 2 | Alan McGovern | // 'path' is the location of the file/folder which is going to be converted |
11 | 2 | Alan McGovern | // to a torrent. 'savePath' is where the .torrent file will be saved. |
12 | 2 | Alan McGovern | void CreateTorrent(string path, string savePath) |
13 | 2 | Alan McGovern | { |
14 | 2 | Alan McGovern | // The class used for creating the torrent |
15 | 2 | Alan McGovern | TorrentCreator c = new TorrentCreator(); |
16 | 2 | Alan McGovern | |
17 | 2 | Alan McGovern | // Add one tier which contains two trackers |
18 | 2 | Alan McGovern | List<string> tier = new List<string>(); |
19 | 2 | Alan McGovern | tier.Add("http://www.example.com/announce"); |
20 | 2 | Alan McGovern | tier.Add("http://backup.example.com/announce"); |
21 | 2 | Alan McGovern | |
22 | 2 | Alan McGovern | c.Announces.Add(tier); |
23 | 2 | Alan McGovern | c.Comment = "This is the comment"; |
24 | 2 | Alan McGovern | c.CreatedBy = "Alan using " + VersionInfo.ClientVersion; |
25 | 2 | Alan McGovern | c.Publisher = "www.homepage.com"; |
26 | 2 | Alan McGovern | |
27 | 2 | Alan McGovern | // Set the torrent as private so it will not use DHT or peer exchange |
28 | 4 | Alan McGovern | // Generally you will not want to set this. |
29 | 2 | Alan McGovern | c.Private = true; |
30 | 2 | Alan McGovern | |
31 | 2 | Alan McGovern | // Path can be either a directory *or* a file. |
32 | 2 | Alan McGovern | c.Path = path; |
33 | 2 | Alan McGovern | |
34 | 2 | Alan McGovern | // Every time a piece has been hashed, this event will fire. It is an |
35 | 2 | Alan McGovern | // asynchronous event, so you have to handle threading yourself. |
36 | 2 | Alan McGovern | c.Hashed += delegate (object o, TorrentCreatorEventArgs e) { |
37 | 2 | Alan McGovern | Console.WriteLine("Current File is {0}% hashed", e.FileCompletion); |
38 | 2 | Alan McGovern | Console.WriteLine("Overall {0}% hashed", e.OverallCompletion); |
39 | 2 | Alan McGovern | Console.WriteLine("Total data to hash: {0}", e.OverallSize); |
40 | 2 | Alan McGovern | }; |
41 | 2 | Alan McGovern | |
42 | 10 | Alan McGovern | // Create the torrent file and save it directly to the specified path |
43 | 10 | Alan McGovern | // Different overloads of 'Create' can be used to save the data to a Stream |
44 | 10 | Alan McGovern | // or just return it as a BEncodedDictionary (its native format) so it can be |
45 | 10 | Alan McGovern | // processed in memory |
46 | 2 | Alan McGovern | c.Create(save_path); |
47 | 1 | } |
|
48 | 1 | </code></pre> |
|
49 | 1 | ||
50 | 7 | Alan McGovern | h2. Example 2 |
51 | 7 | Alan McGovern | |
52 | 5 | Alan McGovern | <pre><code class="java"> |
53 | 5 | Alan McGovern | // 'path' is the location of the file/folder which is going to be converted |
54 | 5 | Alan McGovern | // to a torrent. 'savePath' is where the .torrent file will be saved. |
55 | 5 | Alan McGovern | void CreateTorrent(string path, string savePath) |
56 | 5 | Alan McGovern | { |
57 | 5 | Alan McGovern | // The class used for creating the torrent |
58 | 5 | Alan McGovern | TorrentCreator c = new TorrentCreator(); |
59 | 5 | Alan McGovern | |
60 | 5 | Alan McGovern | // Fill in the path, trackers as in Example 1 |
61 | 5 | Alan McGovern | FillInStandardInformation(c); |
62 | 5 | Alan McGovern | |
63 | 5 | Alan McGovern | // Create the torrent asynchronously |
64 | 5 | Alan McGovern | c.BeginCreate(c, TorrentCreated); |
65 | 5 | Alan McGovern | } |
66 | 5 | Alan McGovern | |
67 | 5 | Alan McGovern | void TorrentCreated(IAsyncResult result) |
68 | 5 | Alan McGovern | { |
69 | 5 | Alan McGovern | TorrentCreator c = (TorrentCreator)result.AsyncState; |
70 | 5 | Alan McGovern | |
71 | 5 | Alan McGovern | // If any errors occured while creating the torrent, they |
72 | 5 | Alan McGovern | // will be rethrown here. |
73 | 5 | Alan McGovern | try |
74 | 5 | Alan McGovern | { |
75 | 5 | Alan McGovern | // Open the destination file and use the EndCreate overload which |
76 | 5 | Alan McGovern | // writes the data directly to the Stream. |
77 | 5 | Alan McGovern | using (FileStream stream = File.OpenWrite(savePath)) |
78 | 5 | Alan McGovern | c.EndCreate(result, stream); |
79 | 5 | Alan McGovern | } |
80 | 5 | Alan McGovern | catch (Exception ex) |
81 | 5 | Alan McGovern | { |
82 | 1 | Console.WriteLine("Error creating torrent: {0}", ex); |
|
83 | 1 | } |
|
84 | 9 | Alan McGovern | } |
85 | 9 | Alan McGovern | </code></pre> |
86 | 9 | Alan McGovern | |
87 | 9 | Alan McGovern | |
88 | 9 | Alan McGovern | h2. Example 3 |
89 | 9 | Alan McGovern | |
90 | 9 | Alan McGovern | <pre><code class="java"> |
91 | 9 | Alan McGovern | // 'path' is the location of the file/folder which is going to be converted |
92 | 9 | Alan McGovern | // to a torrent. 'savePath' is where the .torrent file will be saved. |
93 | 9 | Alan McGovern | void CreateTorrent(string path, string savePath) |
94 | 9 | Alan McGovern | { |
95 | 9 | Alan McGovern | // The class used for creating the torrent |
96 | 9 | Alan McGovern | TorrentCreator c = new TorrentCreator(); |
97 | 9 | Alan McGovern | |
98 | 9 | Alan McGovern | // Fill in the path, trackers as in Example 1 |
99 | 9 | Alan McGovern | FillInStandardInformation(c); |
100 | 9 | Alan McGovern | |
101 | 9 | Alan McGovern | // Create the torrent asynchronously and keep a reference to the |
102 | 9 | Alan McGovern | // IAsyncResult it returns |
103 | 9 | Alan McGovern | TorrentCreatorAsyncResult result = c.BeginCreate(c, TorrentCreated); |
104 | 9 | Alan McGovern | |
105 | 9 | Alan McGovern | // Abort creation of the torrent (can be called at any stage). Note, you |
106 | 9 | Alan McGovern | // can only abort the creation if the torrent is created asynchronously |
107 | 9 | Alan McGovern | result.Abort(); |
108 | 5 | Alan McGovern | } |
109 | 5 | Alan McGovern | </code></pre> |