private static byte[] Encode(TorrentInfo torrentInfo, int interval, int minInterval) { var response = new Dictionary<string, object> { ["complete"] = torrentInfo.GetCompletedPeersCount(), ["incomplete"] = torrentInfo.GetIncompletedPeersCount(), ["interval"] = interval, ["min interval"] = minInterval, ["peers"] = GetPeersAsBytesArray(torrentInfo.Peers) }; return BencodeUtility.Encode(response).ToArray(); }
public async Task<TorrentInfo> ProcessAnnounce(string infoHash, TorrentEvent @event, PeerInfo peer) { var torrent = await _torrents.Find(t => t.InfoHash == infoHash).FirstOrDefaultAsync(); if (torrent != null) { var currentPeer = torrent.Peers.SingleOrDefault(p => p.IpAddress == peer.IpAddress); switch (@event) { case TorrentEvent.Started: if (currentPeer != null) { currentPeer.State = State.Downloading; break; } torrent.Peers.Add(peer); break; case TorrentEvent.Stopped: if (currentPeer != null) { torrent.Peers.Remove(currentPeer); } break; case TorrentEvent.Completed: if (currentPeer != null) { currentPeer.State = State.Downloaded; } break; case TorrentEvent.None: if (currentPeer != null) { torrent.Peers.Add(peer); } break; } torrent.LastUpdateDate = DateTime.Now; await _torrents.ReplaceOneAsync(t => t.Id == torrent.Id, torrent); } else { torrent = new TorrentInfo(infoHash); torrent.Peers.Add(peer); torrent.LastUpdateDate = DateTime.Now; await _torrents.InsertOneAsync(torrent); } return torrent; }
public AnnounceResult(TorrentInfo torrentInfo, int interval, int minInterval) { _torrentInfo = torrentInfo; _interval = interval; _minInterval = minInterval; }