public void RefreshBucket(DhtNode dhtNode) { List <KBucket> allLeafKBuckets = GetAllLeafKBuckets(); foreach (KBucket kBucket in allLeafKBuckets) { if (kBucket._contacts != null) { if ((DateTime.UtcNow - kBucket._lastChanged).TotalSeconds > BUCKET_STALE_TIMEOUT_SECONDS) { ThreadPool.QueueUserWorkItem(delegate(object state) { //get random node ID in the bucket range BinaryNumber randomNodeID = BinaryNumber.GenerateRandomNumber160(); if (kBucket._bucketID != null) { randomNodeID = (randomNodeID >> kBucket._bucketDepth) | kBucket._bucketID; } //find closest contacts for current node id NodeContact[] initialContacts = kBucket.GetKClosestContacts(randomNodeID); if (initialContacts.Length > 0) { dhtNode.QueryFindNode(initialContacts, randomNodeID); } }); } } } }
public void CheckContactHealth(DhtNode dhtNode) { List <KBucket> allLeafKBuckets = GetAllLeafKBuckets(); foreach (KBucket kBucket in allLeafKBuckets) { NodeContact[] contacts = kBucket._contacts; if (contacts != null) { foreach (NodeContact contact in contacts) { if ((contact != null) && contact.IsStale()) { ThreadPool.QueueUserWorkItem(delegate(object state) { if (!dhtNode.Ping(contact)) { //remove stale node contact kBucket.RemoveContact(contact); } }); } } } } }