public void CheckContactHealth(DhtClient dhtClient)
 {
     _lock.TryEnterReadLock(LOCK_TIMEOUT);
     try
     {
         if (_contacts == null)
         {
             _leftBucket.CheckContactHealth(dhtClient);
             _rightBucket.CheckContactHealth(dhtClient);
         }
         else
         {
             foreach (NodeContact contact in _contacts.Values)
             {
                 if (contact.IsStale())
                 {
                     ThreadPool.QueueUserWorkItem(CheckContactHealthAsync, new object[] { dhtClient, contact });
                 }
             }
         }
     }
     finally
     {
         _lock.ExitReadLock();
     }
 }
        private void CheckContactHealthAsync(object state)
        {
            object[] param = state as object[];

            DhtClient   dhtClient = param[0] as DhtClient;
            NodeContact contact   = param[1] as NodeContact;
            int         retries   = 0;

            do
            {
                try
                {
                    if (dhtClient.Ping(contact))
                    {
                        return; //contact replied; do nothing.
                    }
                }
                catch
                { }

                retries++;
            }while (retries < HEALTH_PING_MAX_RETRIES);

            try
            {
                //remove stale node
                RemoveContactFromCurrentBucket(contact);
            }
            catch
            { }
        }
        private void RefreshBucketAsync(object state)
        {
            try
            {
                DhtClient dhtClient = state as DhtClient;

                //get random node ID in the bucket range
                BinaryID randomNodeID = (BinaryID.GenerateRandomID160() << _bucketDepth) | _bucketID;

                //find closest contacts for current node id
                NodeContact[] initialContacts = GetKClosestContacts(randomNodeID);

                if (initialContacts.Length > 0)
                {
                    dhtClient.QueryFindNode(initialContacts, randomNodeID); //query manager auto add contacts that respond
                }
            }
            catch
            { }
        }
 public void RefreshBucket(DhtClient dhtClient)
 {
     _lock.TryEnterReadLock(LOCK_TIMEOUT);
     try
     {
         if (_contacts == null)
         {
             _leftBucket.RefreshBucket(dhtClient);
             _rightBucket.RefreshBucket(dhtClient);
         }
         else
         {
             if ((DateTime.UtcNow - _lastChanged).TotalSeconds > BUCKET_STALE_TIMEOUT_SECONDS)
             {
                 ThreadPool.QueueUserWorkItem(RefreshBucketAsync, dhtClient);
             }
         }
     }
     finally
     {
         _lock.ExitReadLock();
     }
 }