Tracker example

Example 1 Shows how to create a basic public tracker which tracks every torrent which it sees in an announce request
Example 2 Shows how to create a private tracker which only tracks torrents explicitly registered with it

Example 1

 1         public void StartTracker ()
 2         {
 3             Tracker tracker = new Tracker();
 4 
 5             // When unregistered torrents are allowed, if a client requests peers for
 6             // a torrent which is not currently monitored by the tracker, that torrent
 7             // will be added automatically. This is useful for creating a 'public' tracker.
 8             // Normally, if this is false, announce requests for torrents not already
 9             // registered with the engine are sent an error message.
10             tracker.AllowUnregisteredTorrents = true;
11 
12             // Set the interval between announce requests to 1 hour and min interval to 10 mins
13             tracker.AnnounceInterval = TimeSpan.FromHours(1);
14             tracker.MinAnnounceInterval = TimeSpan.FromMinutes(10);
15 
16             // Create a listener which will respond to scrape/announce requests
17             // You can pass a prefix like: http://153.462.23.1/announce which will
18             // create a listener on port 80 (the default HTTP port) which will handle
19             // all announce requests.
20             HttpListener listener = new HttpListener("http://myserver.com/announce");
21             tracker.RegisterListener(listener);
22             listener.Start();
23         }

Example 2

 1          // torrentDirectory is a folder containing .torrent files
 2         // which should be loaded into the engine
 3         public void StartTracker(string torrentDirectory)
 4         {
 5             // Create the tracker and register a listener as in Example 1
 6             Tracker tracker = new Tracker();
 7             HttpListener listener = new HttpListener("http://myserver.com/announce");
 8             tracker.RegisterListener(listener);
 9             listener.Start();
10 
11             // If an announce request is received for a torrent which is not registered with the
12             //the tracker an error will be returned.
13             tracker.AllowUnregisteredTorrents = false;
14 
15             // Load all torrents into the engine. Only announce requests for these torrents
16             // (or torrents added in the future) will be processed.
17             foreach (string file in Directory.GetFiles(torrentDirectory))
18             {
19                 Torrent torrent = Torrent.Load(file);
20 
21                 // InfoHashTrackable is a basic subclass of ITrackable. It only stores
22                 // the infohash and name of the torrent. If you need to store additional
23                 // data for each torrent you're tracking, just subclass ITrackable or
24                 // InfoHashTrackable and add the extra data there.
25                 InfoHashTrackable trackable = new InfoHashTrackable(torrent);
26                 tracker.Add(trackable);
27             }
28         }