/// <summary> /// Updates the peer in the tracker database based on the announce parameters /// </summary> /// <param name="par"></param> internal void Update(AnnounceRequest par) { object peerKey = Comparer.GetKey(par); if (!Peers.TryGetValue(peerKey, out Peer? peer)) { peer = new Peer(par, peerKey); Add(peer); } else { Debug.WriteLine($"Updating: {peer.ClientAddress} with key {peerKey}"); peer.Update(par); } if (par.Event == TorrentEvent.Completed) { Downloaded++; } else if (par.Event == TorrentEvent.Stopped) { Remove(peer); } Tracker.RaisePeerAnnounced(new AnnounceEventArgs(peer, this)); UpdateCounts(); }
void ListenerReceivedAnnounce(object?sender, AnnounceRequest e) { if (Disposed) { e.Response.Add(TrackerRequest.FailureKey, (BEncodedString)"The tracker has been shut down"); return; } Requests.AnnounceReceived(); SimpleTorrentManager?manager; // Check to see if we're monitoring the requested torrent lock (Torrents) { if (!Torrents.TryGetValue(e.InfoHash !, out manager)) { if (AllowUnregisteredTorrents) { Add(new InfoHashTrackable(e.InfoHash !.ToHex(), e.InfoHash)); manager = Torrents[e.InfoHash]; } else { e.Response.Add(TrackerRequest.FailureKey, (BEncodedString)"The requested torrent is not registered with this tracker"); return; } } } // If a non-compact response is expected and we do not allow non-compact responses // bail out if (!AllowNonCompact && !e.HasRequestedCompact) { e.Response.Add(TrackerRequest.FailureKey, (BEncodedString)"This tracker does not support non-compact responses"); return; } lock (manager) { // Update the tracker with the peers information. This adds the peer to the tracker, // updates it's information or removes it depending on the context manager.Update(e); // Clear any peers who haven't announced within the allowed timespan and may be inactive manager.ClearZombiePeers(DateTime.Now.Add(-TimeoutInterval)); // Fulfill the announce request manager.GetPeers(e.Response, e.NumberWanted, e.HasRequestedCompact); } e.Response.Add(IntervalKey, new BEncodedNumber((int)AnnounceInterval.TotalSeconds)); e.Response.Add(MinIntervalKey, new BEncodedNumber((int)MinAnnounceInterval.TotalSeconds)); e.Response.Add(TrackerIdKey, TrackerId); // FIXME: Is this right? e.Response.Add(CompleteKey, new BEncodedNumber(manager.Complete)); e.Response.Add(IncompleteKey, new BEncodedNumber(manager.Incomplete)); e.Response.Add(DownloadedKey, new BEncodedNumber(manager.Downloaded)); //FIXME is this the right behaivour //if (par.TrackerId == null) // par.TrackerId = "monotorrent-tracker"; }
internal Peer(AnnounceRequest par, object dictionaryKey) { DictionaryKey = dictionaryKey; Update(par); if (ClientAddress is null || PeerId is null) { throw new NotSupportedException(); } }
internal void Update(AnnounceRequest parameters) { DateTime now = DateTime.Now; double elapsedTime = (now - LastAnnounceTime).TotalSeconds; if (elapsedTime < 1) { elapsedTime = 1; } ClientAddress = parameters.ClientAddress; DownloadSpeed = (int)((parameters.Downloaded - Downloaded) / elapsedTime); UploadSpeed = (int)((parameters.Uploaded - Uploaded) / elapsedTime); Downloaded = parameters.Downloaded; Uploaded = parameters.Uploaded; Remaining = parameters.Left; PeerId = parameters.PeerId; ClientApp = new Software(parameters.PeerId); LastAnnounceTime = now; }
internal Peer(AnnounceRequest par, object dictionaryKey) { DictionaryKey = dictionaryKey; Update(par); }
public object GetKey(AnnounceRequest parameters) { return(parameters.Uploaded); }