/// <summary> /// Starts the specified torrent. /// </summary> /// <param name="torrentInfo">The torrent information.</param> public void Start(TorrentInfo torrentInfo) { torrentInfo.CannotBeNull(); TransferManager transfer; Debug.WriteLine($"starting torrent {torrentInfo.InfoHash}"); if (this.IsRunning) { lock (((IDictionary)this.transfers).SyncRoot) { if (!this.transfers.ContainsKey(torrentInfo.InfoHash)) { transfer = new TransferManager(this.ListeningPort, torrentInfo, this.throttlingManager, new PersistenceManager(this.BaseDirectory, torrentInfo.Length, torrentInfo.PieceLength, torrentInfo.PieceHashes, torrentInfo.Files)); transfer.TorrentHashing += this.Transfer_TorrentHashing; transfer.TorrentLeeching += this.Transfer_TorrentLeeching; transfer.TorrentSeeding += this.Transfer_TorrentSeeding; transfer.TorrentStarted += this.Transfer_TorrentStarted; transfer.TorrentStopped += this.Transfer_TorrentStopped; transfer.Start(); this.transfers.Add(torrentInfo.InfoHash, transfer); } else { throw new TorrentClientException($"Torrent {torrentInfo.InfoHash} is already active."); } } } else { throw new TorrentClientException("Torrent client is not running."); } }
/// <summary> /// Initializes a new instance of the <see cref="TransferManager" /> class. /// </summary> /// <param name="listeningPort">The port.</param> /// <param name="torrentInfo">The torrent information.</param> /// <param name="throttlingManager">The throttling manager.</param> /// <param name="persistenceManager">The persistence manager.</param> public TransferManager(int listeningPort, TorrentInfo torrentInfo, ThrottlingManager throttlingManager, PersistenceManager persistenceManager) { listeningPort.MustBeGreaterThanOrEqualTo(IPEndPoint.MinPort); listeningPort.MustBeLessThanOrEqualTo(IPEndPoint.MaxPort); torrentInfo.CannotBeNull(); throttlingManager.CannotBeNull(); persistenceManager.CannotBeNull(); Tracker tracker = null; this.PeerId = "-AB1100-" + "0123456789ABCDEF".Random(24); Debug.WriteLine($"creating torrent manager for torrent {torrentInfo.InfoHash}"); Debug.WriteLine($"local peer id {this.PeerId}"); this.TorrentInfo = torrentInfo; this.throttlingManager = throttlingManager; this.persistenceManager = persistenceManager; // initialize trackers foreach (var trackerUri in torrentInfo.AnnounceList) { if (trackerUri.Scheme == "http" || trackerUri.Scheme == "https") { tracker = new HttpTracker(trackerUri, this.PeerId, torrentInfo.InfoHash, listeningPort); } else if (trackerUri.Scheme == "udp") { tracker = new UdpTracker(trackerUri, this.PeerId, torrentInfo.InfoHash, listeningPort); } if (tracker != null) { tracker.TrackingEvent = TrackingEvent.Started; tracker.Announcing += this.Tracker_Announcing; tracker.Announced += this.Tracker_Announced; tracker.TrackingFailed += this.Tracker_TrackingFailed; tracker.BytesLeftToDownload = this.TorrentInfo.Length - this.Downloaded; tracker.WantedPeerCount = 30; // we can handle 30 peers at a time this.trackers.Add(trackerUri, tracker); } else { // unsupported tracker protocol Debug.WriteLine($"unsupported tracker protocol {trackerUri.Scheme}"); } } }
/// <summary> /// Initializes a new instance of the <see cref="TorrentLeechingEventArgs" /> class. /// </summary> /// <param name="torrentInfo">The torrent information.</param> public TorrentLeechingEventArgs(TorrentInfo torrentInfo) { torrentInfo.CannotBeNull(); this.TorrentInfo = torrentInfo; }
/// <summary> /// Initializes a new instance of the <see cref="TorrentStartedEventArgs" /> class. /// </summary> /// <param name="torrentInfo">The torrent information.</param> public TorrentStartedEventArgs(TorrentInfo torrentInfo) { torrentInfo.CannotBeNull(); this.TorrentInfo = torrentInfo; }