예제 #1
0
파일: NodeID.cs 프로젝트: Renha/Tox.NET
        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;
        }
예제 #2
0
파일: Dht.cs 프로젝트: Renha/Tox.NET
        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;
        }