private void SetupTorrentWatcher() { watcher = new TorrentFolderWatcher(Path.GetFullPath(TORRENT_DIR), "*.torrent"); watcher.TorrentFound += delegate(object sender, TorrentWatcherEventArgs e) { try { // This is a hack to work around the issue where a file triggers the event // before it has finished copying. As the filesystem still has an exclusive lock // on the file, monotorrent can't access the file and throws an exception. // The best way to handle this depends on the actual application. // Generally the solution is: Wait a few hundred milliseconds // then try load the file. Thread.Sleep(500); var t = Torrent.Load(e.TorrentPath); // There is also a predefined 'InfoHashTrackable' MonoTorrent.Tracker which // just stores the infohash and name of the torrent. This is all that the tracker // needs to run. So if you want an ITrackable that "just works", then use InfoHashTrackable. // ITrackable trackable = new InfoHashTrackable(t); ITrackable trackable = new CustomITrackable(t); // The lock is here because the TorrentFound event is asyncronous and I have // to ensure that only 1 thread access the tracker at the same time. lock (tracker) tracker.Add(trackable); } catch (Exception ex) { Debug.WriteLine("Error loading torrent from disk: {0}", ex.Message); Debug.WriteLine("Stacktrace: {0}", ex.ToString()); } }; watcher.Start(); watcher.ForceScan(); }
public SimpleTracker(string announcementEndpoint, string torrentsDirectoryPath) { // Make the listner. var listener = new HttpListener(announcementEndpoint); // Make the tracker. Tracker = new Tracker (); Tracker.AnnounceInterval = new TimeSpan (0, 1, 0); Tracker.AllowUnregisteredTorrents = true; Tracker.RegisterListener (listener); // Make mappings. Mappings = new ConcurrentDictionary<string, InfoHashTrackable> (); // Make watcher. Watcher = new TorrentFolderWatcher (torrentsDirectoryPath, "*.torrent"); Watcher.TorrentFound += (sender, e) => { try { // Wait for file to finish copying. System.Threading.Thread.Sleep (500); // Make InfoHashTrackable from torrent. var torrent = Torrent.Load (e.TorrentPath); var trackable = new InfoHashTrackable (torrent); // Add to tracker. lock (Tracker) Tracker.Add (trackable); // Save to mappings. Mappings[e.TorrentPath] = trackable; // Log. Console.WriteLine("Added {0}", e.TorrentPath); } catch (Exception exception) { Debug.WriteLine ("Error loading torrent from disk: {0}", exception.Message); Debug.WriteLine ("Stacktrace: {0}", exception.ToString ()); } }; Watcher.TorrentLost += (sender, e) => { try { // Get from mappings. var trackable = Mappings[e.TorrentPath]; // Remove from tracker. lock(Tracker) Tracker.Remove(trackable); // Remove from mappings. Mappings.TryRemove(e.TorrentPath, out trackable); // Log. Console.WriteLine("Removed {0}", e.TorrentPath); } catch(Exception exception) { Debug.WriteLine ("Error uploading torrent from disk: {0}", exception.Message); Debug.WriteLine ("Stacktrace: {0}", exception.ToString ()); } }; // Register close events. AppDomain.CurrentDomain.ProcessExit += (sender, e) => Tracker.Dispose (); // Run. listener.Start(); Watcher.Start(); Watcher.ForceScan(); }
internal void AddWatcher(string line) { TorrentFolderWatcher watcher = new TorrentFolderWatcher(line, "*.torrent"); watcher.TorrentFound += TorrentFound; watcher.ForceScan(); watcher.Start(); watchers.Add(line, watcher); }