public NodeID GetClosest(NodeID node1, NodeID node2) { byte[] key = this; byte[] key1 = node1; byte[] key2 = node2; for (int i = 0; i < CryptoBox.PublicKeySize; i++) { byte distance1 = (byte)(key[i] ^ key1[i]); byte distance2 = (byte)(key[i] ^ key2[i]); if (distance1 < distance2) return node1; if (distance1 > distance2) return node2; } return this; }
private bool ShouldInsert(NodeID nodeID, ref int index) { //we don't have to lock here because this method is only called from GetNode which already locks _friendsLock for (int i = 0; i < _friends.Count; i++) { var selfNode = (NodeID)KeyPair.PublicKey; if (selfNode.GetClosest(_friends[i].PublicKey, nodeID) == nodeID) { index = i; return true; } } return false; }