/// <summary> /// Announces Peer to DHT with AnnounceParameters infomation. /// </summary> /// <returns>True if successful</returns> public void AnnouncePeer(byte[] infoHash, AnnounceParameters pars) { PeerEntry entry = new PeerEntry(pars); Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format("Peer to be announced to DHT:\n{0}", entry.ToString())); AnnouncePeer(infoHash, entry); }
/// <summary> /// Gets peers from DHT. /// </summary> /// <param name="infoHash">The infoHash of the torrent, used as the name in Dht</param> /// <returns> /// A List of PeerEntries which could have duplicated peers with different /// states. Empty List if no peers for this infoHash or the network communication /// is temporarily down. /// </returns> public IEnumerable <PeerEntry> GetPeers(byte[] infoHash) { Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format("Getting peers for infoHash: {0}", ServiceUtil.GetUrlCompatibleString(infoHash))); ICollection <PeerEntry> peers = new List <PeerEntry>(); // Fire DHT Get DictionaryServiceData results; try { byte[] key = ServiceUtil.GetUrlCompatibleBytes(infoHash); results = _dictSvc.Get(key); } catch (DictionaryServiceException ex) { Logger.WriteLineIf(LogLevel.Error, _log_props, string.Format( "Exception caught when retrieving peers. Returning empty peers collection.\n{0}", ex)); // No big deal. We simply wait until the next time to query again. // Return empty collection. return(peers); } Logger.WriteLineIf(LogLevel.Info, _log_props, string.Format("{0} peer(s) retrieved from DHT", results.DataEntries.Length)); int index = 0; foreach (var r in results.DataEntries) { try { PeerEntry entry = (PeerEntry)DictionaryData.CreateDictionaryData(r.Value); Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format("Peer entry #{0} built:\n{1}", index++, entry.ToString())); peers.Add(entry); } catch (Exception ex) { Logger.WriteLineIf(LogLevel.Error, _log_props, string.Format( "Error occurred when deserializing result from DHT. Continue to parse next.\n{0}", ex)); // Ignore this entry and continue to parse others. continue; } } return(peers); }