async Task <bool> Announce(TorrentEvent clientEvent, ITracker tracker) { var trackerTier = Tiers.First(t => t.Trackers.Contains(tracker)); try { // If we have not announced to this Tracker tier yet then we should replace the ClientEvent. // But if we end up announcing to a different Tracker tier we may want to send the // original/unmodified args. AnnounceParameters actualArgs = RequestFactory.CreateAnnounce(clientEvent); if (!trackerTier.SentStartedEvent) { actualArgs = actualArgs.WithClientEvent(TorrentEvent.Started); } List <Peer> peers = await tracker.AnnounceAsync(actualArgs); trackerTier.LastAnnounceSucceeded = true; trackerTier.ActiveTrackerIndex = trackerTier.Trackers.IndexOf(tracker); trackerTier.SentStartedEvent |= actualArgs.ClientEvent == TorrentEvent.Started; trackerTier.LastAnnounce = ValueStopwatch.StartNew(); AnnounceComplete?.InvokeAsync(this, new AnnounceResponseEventArgs(tracker, true, peers.AsReadOnly())); return(true); } catch { } trackerTier.LastAnnounceSucceeded = false; AnnounceComplete?.InvokeAsync(this, new AnnounceResponseEventArgs(tracker, false)); return(false); }
private async Task Announce(ITracker tracker, TorrentEvent clientEvent, bool trySubsequent) { ClientEngine engine = manager.Engine; // If the engine is null, we have been unregistered if (engine == null) { return; } this.updateSucceeded = true; this.lastUpdated = DateTime.Now; EncryptionTypes e = engine.Settings.AllowedEncryption; bool requireEncryption = !Toolbox.HasEncryption(e, EncryptionTypes.PlainText); bool supportsEncryption = Toolbox.HasEncryption(e, EncryptionTypes.RC4Full) || Toolbox.HasEncryption(e, EncryptionTypes.RC4Header); requireEncryption = requireEncryption && ClientEngine.SupportsEncryption; supportsEncryption = supportsEncryption && ClientEngine.SupportsEncryption; IPEndPoint reportedAddress = engine.Settings.ReportedAddress; string ip = reportedAddress == null ? null : reportedAddress.Address.ToString(); int port = reportedAddress == null ? engine.Listener.Endpoint.Port : reportedAddress.Port; // FIXME: In metadata mode we need to pretend we need to download data otherwise // tracker optimisations might result in no peers being sent back. long bytesLeft = 1000; if (manager.HasMetadata) { bytesLeft = (long)((1 - this.manager.Bitfield.PercentComplete / 100.0) * this.manager.Torrent.Size); } AnnounceParameters p = new AnnounceParameters(this.manager.Monitor.DataBytesDownloaded, this.manager.Monitor.DataBytesUploaded, bytesLeft, clientEvent, this.infoHash, requireEncryption, manager.Engine.PeerId, ip, port); p.SupportsEncryption = supportsEncryption; try { var peers = await tracker.AnnounceAsync(p); await OnAnnounceComplete(tracker, peers, trySubsequent, clientEvent, true); } catch { await OnAnnounceComplete(tracker, new List <Peer>(), trySubsequent, clientEvent, false); } }