Пример #1
0
        private DhtFriend AddNode(IPEndPoint endpoint, byte[] publicKey)
        {
            lock (_friendsLock)
            {
                var friend = GetNode(endpoint, publicKey);
                if (friend != null)
                    return friend;

                //if the list is full, remove a node that's further away
                //TODO: actually remove the node with the biggest distance difference
                int index = _friends.Count;
                if (_friends.Count == _friends.Capacity)
                {
                    if (ShouldInsert(publicKey, ref index))
                        return null;

                    if (_friends.Count == _friends.Capacity)
                        _friends.RemoveAt(index);
                }

                byte[] sharedKey = CryptoBox.BeforeNm(publicKey, KeyPair.SecretKey);
                friend = new DhtFriend(endpoint, publicKey, sharedKey);

                _friends.Insert(index, friend);
                Console.WriteLine("Added a new node to our close list: {0}", endpoint);

                return friend;
            }
        }
Пример #2
0
 public void SendPacket(DhtFriend friend, IToxPacket packet)
 {
     byte[] data = packet.Pack(friend.SharedKey);
     Socket.SendTo(data, friend.EndPoint);
 }
Пример #3
0
        private void SendPingRequest(DhtFriend friend)
        {
            var pingRequest = new PingRequest(KeyPair.PublicKey, CryptoRandom.NextUInt64());
            friend.AddPing(pingRequest.PingID);

            _net.SendPacket(friend, pingRequest);
        }
Пример #4
0
 private void SendPingResponse(DhtFriend friend, ulong pingID)
 {
     var response = new PingResponse(KeyPair.PublicKey, pingID);
     _net.SendPacket(friend, response);
 }
Пример #5
0
        private void SendGetNodes(DhtFriend friend, byte[] publicKey)
        {
            var packet = new GetNodesRequest(KeyPair.PublicKey, publicKey);
            friend.AddPing(packet.PingID);

            _net.SendPacket(friend, packet);
            _lastGetNodes = DateTime.Now;
        }