private void OnRecvAnnouncePeerQuery(IPEndPoint ipinfo, BDictionary data) { var t = data.Get <BString>("t"); var args = data.Get <BDictionary>("a"); var id = args.Get <BString>("id"); var infoHash = args.Get <BString>("info_hash"); var impliedPort = args.Get <BNumber>("implied_port"); int port = (impliedPort != null && impliedPort.Value == 1) ? ipinfo.Port : (int)args.Get <BNumber>("port").Value; fLogger.WriteDebug("Receive `announce_peer` query from {0} [{1}] for {2}", ipinfo.ToString(), id.Value.ToHexString(), infoHash.Value.ToHexString()); fRoutingTable.UpdateNode(new DHTNode(id.Value, ipinfo)); if (!Algorithms.ArraysEqual(infoHash.Value, fSearchInfoHash)) { // skip response for another infohash query return; } // receive `announce_peer` query for our infohash var nodesList = fRoutingTable.FindNodes(infoHash.Value); Send(ipinfo, DHTMessage.CreateAnnouncePeerResponse(t, fLocalID, nodesList)); }
public bool Equals(DHTId other) { if ((object)other == null) { return(false); } return(Algorithms.ArraysEqual(fData, other.fData)); }
private void CompareDigests() { fCurrentDigest.Files.Sort((x, y) => string.Compare(x.FileName, y.FileName, StringComparison.InvariantCulture)); fPreviousDigest.Files.Sort((x, y) => string.Compare(x.FileName, y.FileName, StringComparison.InvariantCulture)); bool hasChanges = false; foreach (var prevFile in fPreviousDigest.Files) { var currFile = fCurrentDigest.Files.Find(x => x.FileName.Equals(prevFile.FileName)); if (currFile == null) { prevFile.FileType = FileType.Missing; hasChanges = true; } else { if (Algorithms.ArraysEqual(prevFile.Hash, currFile.Hash)) { prevFile.FileType = FileType.Identical; } else { prevFile.FileType = FileType.DifferentChecksum; prevFile.Hash = currFile.Hash; hasChanges = true; } } } foreach (var newFile in fCurrentDigest.Files) { var prevFile = fPreviousDigest.Files.Find(x => x.FileName.Equals(newFile.FileName)); if (prevFile == null) { newFile.FileType = FileType.New; fPreviousDigest.Files.Add(newFile); hasChanges = true; } } lvFiles.Items.Clear(); foreach (var fileObj in fPreviousDigest.Files) { if (!fShowOnlyChanges || fileObj.FileType != FileType.Identical) { var item = lvFiles.Items.Add(fileObj.FileName); item.SubItems.Add(FCCore.Hash2Str(fileObj.Hash)); item.SubItems.Add(fileObj.FileType.ToString()); } } lvFiles.Update(); SetUpdateMode(hasChanges); }
public static bool operator ==(DHTId left, DHTId right) { if ((object)left == null) { return((object)right == null); } if ((object)right == null) { return(false); } return(Algorithms.ArraysEqual(left.Data, right.Data)); }
private string ReadStr(BinaryReader binReader) { if (fIsUnicode) { byte[] bom = binReader.ReadBytes(3); if (!Algorithms.ArraysEqual(BOM, bom)) { binReader.BaseStream.Seek(-3, SeekOrigin.Current); return(""); } byte strLen = binReader.ReadByte(); if (strLen == 0) { return(string.Empty); } else { byte[] strBytes = binReader.ReadBytes(strLen * 2); return(Encoding.Unicode.GetString(strBytes)); } } else { byte[] strBytes = new byte[255]; byte bt; bt = binReader.ReadByte(); if (bt == 0x0A) { return(string.Empty); } else { binReader.BaseStream.Seek(-1, SeekOrigin.Current); } int i = 0; while ((bt = binReader.ReadByte()) != 0x0A) { strBytes[i] = bt; i++; } if (i > 0) { return(fEncoding.GetString(strBytes, 0, i)); } else { return(string.Empty); } } }
private void OnRecvGetPeersQuery(IPEndPoint ipinfo, BDictionary data) { var t = data.Get <BString>("t"); var args = data.Get <BDictionary>("a"); var id = args.Get <BString>("id"); var infoHash = args.Get <BString>("info_hash"); fLogger.WriteDebug("Receive `get_peers` query from {0} [{1}] for {2}", ipinfo.ToString(), id.Value.ToHexString(), infoHash.Value.ToHexString()); fRoutingTable.UpdateNode(new DHTNode(id.Value, ipinfo)); var neighbor = DHTHelper.GetNeighbor(infoHash.Value, fLocalID); var peersList = (Algorithms.ArraysEqual(infoHash.Value, fSearchInfoHash)) ? fPeersHolder.GetPeersList() : null; var nodesList = fRoutingTable.FindNodes(infoHash.Value); Send(ipinfo, DHTMessage.CreateGetPeersResponse(t, neighbor, infoHash.Value, peersList, nodesList)); }
/*public void UpdateNodes(IEnumerable<DHTNode> nodes) * { * foreach (var node in nodes) { * UpdateNode(node); * } * }*/ public void UpdateNode(DHTNode node) { if (node == null || node.ID == null) { return; } if (fKTable.Count >= fMaxNodeSize && fMinLastTime + fRouteLife.Ticks < DateTime.Now.Ticks) { lock (fLock) { if (fMinLastTime + fRouteLife.Ticks < DateTime.Now.Ticks) { ClearExpireNode(); } } } if (fKTable.Count >= fMaxNodeSize) { return; } DHTNode existNode = null; if (fKTable.TryGetValue(node.RouteId, out existNode)) { if (Algorithms.ArraysEqual(node.ID, existNode.ID)) { node = existNode; } else { // replace to new fKTable[node.RouteId] = node; } } else { fKTable.Add(node.RouteId, node); } node.LastUpdateTime = DateTime.Now.Ticks; }
public bool Equals(byte[] other) { return((other == null || other.Length != 20) ? false : Algorithms.ArraysEqual(fData, other)); }