Creating Torrents

Version 11 (Alan McGovern, 10/03/2010 08:28 PM) → Version 12/13 (Alan McGovern, 10/25/2010 11:11 PM)


h1. Creating Torrents

[[Creating Torrents#Example-1|Example 1]] shows how to create a simple torrent.
[[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.
[[Creating Torrents#Example-3|Example 3]] shows how to abort the creation of a torrent after it has started
[[Creating Torrents#Example-4|Example 4]] shows how to create a torrent and load it without having to hash the data a second time

h2. Example 1

<pre><code class="java">

// 'path' is the location of the file/folder which is going to be converted

// to a torrent. 'savePath' is where the .torrent file will be saved.

void CreateTorrent(string path, string savePath)
{

// The class used for creating the torrent

TorrentCreator c = new TorrentCreator();



// Add one tier which contains two trackers
RawTrackerTier

List<string>
tier = new RawTrackerTier ();
tier.Add("http://localhost/announce");

List<string>();
tier.Add("http://www.example.com/announce");
tier.Add("http://backup.example.com/announce");

c.Announces.Add(tier);

c.Comment = "This is the comment";

c.CreatedBy = "Doug "Alan using " + VersionInfo.ClientVersion;

c.Publisher = "www.aaronsen.com";

"www.homepage.com";

// Set the torrent as private so it will not use DHT or peer exchange

// Generally you will not want to set this.

c.Private = true;



// Path can be either a directory *or* a file.
c.Path = path;

//
Every time a piece has been hashed, this event will fire. It is an

// asynchronous event, so you have to handle threading yourself.

c.Hashed += delegate (object o, TorrentCreatorEventArgs e) {

Console.WriteLine("Current File is {0}% hashed", e.FileCompletion);

Console.WriteLine("Overall {0}% hashed", e.OverallCompletion);

Console.WriteLine("Total data to hash: {0}", e.OverallSize);

};



// ITorrentFileSource can be implemented to provide the TorrentCreator
// with a list of files which will be added to the torrent metadata.
// The default implementation takes a path to a single file or a path
// to a directory. If the path is a directory, all files will be
// recursively added
ITorrentFileSource fileSource = new TorrentFileSource (path);

//
Create the torrent file and save it directly to the specified path

// Different overloads of 'Create' can be used to save the data to a Stream
// or just return it as a BEncodedDictionary (its native format) so it can be
// processed in memory
c.Create(save_path);
}

</code></pre>

h2. Example 2

<pre><code class="java">
// 'path' is the location of the file/folder which is going to be converted
// to a torrent. 'savePath' is where the .torrent file will be saved.
void CreateTorrent(string path, string savePath)
{
// The class used for creating the torrent
TorrentCreator c = new TorrentCreator();

// Fill in the path, trackers as in Example 1
FillInStandardInformation(c);

// Create the torrent asynchronously
c.BeginCreate(c, TorrentCreated);
}

void TorrentCreated(IAsyncResult result)
{
TorrentCreator c = (TorrentCreator)result.AsyncState;

// If any errors occured while creating the torrent, they
// will be rethrown here.
try
{
// Open the destination file and use the EndCreate overload which
// writes the data directly to the Stream.
using (FileStream stream = File.OpenWrite(savePath))
c.EndCreate(result, stream);
}
catch (Exception ex)
{
Console.WriteLine("Error creating torrent: {0}", ex);
}
}
</code></pre>

h2. Example 3

<pre><code class="java">
// 'path' is the location of the file/folder which is going to be converted
// to a torrent. 'savePath' is where the .torrent file will be saved.
void CreateTorrent(string path, string savePath)
{
// The class used for creating the torrent
TorrentCreator c = new TorrentCreator();

// Fill in the path, trackers as in Example 1
FillInStandardInformation(c);

// Create the torrent asynchronously and keep a reference to the
// IAsyncResult it returns
TorrentCreatorAsyncResult result = c.BeginCreate(c, TorrentCreated);

// Abort creation of the torrent (can be called at any stage). Note, you
// can only abort the creation if the torrent is created asynchronously
result.Abort();
}
</code></pre>

h2. Example 4

<pre><code class="java">
public static void CreateAndLoadTorrent (ClientEngine engine)
{
// Instantiate a torrent creator
TorrentCreator creator = new TorrentCreator ();

// Create a TorrentFileSource which is used to populate the .torrent
ITorrentFileSource files = new TorrentFileSource ("somefile.dat");

// Create the Torrent metadata blob
BEncodedDictionary torrentDict = creator.Create (files);

// Instantiate a torrent
Torrent torrent = Torrent.Load (torrentDict);

// Create a fake fast resume data with all the pieces set to true so we
// don't have to hash the torrent when adding it directly to the engine.
BitField bitfield = new BitField (torrent.Pieces.Count).Not ();
FastResume fastResumeData = new FastResume(torrent.InfoHash, bitfield);

// Create a TorrentManager so the torrent can be downloaded and load
// the FastResume data so we don't have to hash it again.
TorrentManager manager = new TorrentManager (torrent, "SavePath", new TorrentSettings ());
manager.LoadFastResume (fastResumeData);

// Register it with the engine and start the download
engine.Register (manager);
engine.StartAll ();
}
</code></pre>