예제 #1
0
        async Task DiscoveryLoop()
        {
            while (!_node.HasQuit)
            {
                if (_discoverRounds > 10)
                {
                    await Task.Delay(Time.Minutes(60), _node.QuitToken); // do a discovery every hour
                }
                else
                {
                    await Task.Delay(Time.Seconds(4 * _discoverRounds), _node.QuitToken);
                }
                if (_node.HasQuit)
                {
                    return;
                }

                try
                {
                    var nodes = _bucket.GetNearNodes(LocalId);
                    var count = 0;
                    foreach (var node in nodes)
                    {
                        if (node.IsPublicEndPoint)
                        {
                            if (!await QueryEndPoint(node.PublicEndPoint, true))
                            {
                                _bucket.Remove(node.NodeId);
                            }

                            if (count >= 5)
                            {
                                break;
                            }
                        }
                    }

                    nodes = _bucket.GetFarNodes();
                    count = 0;
                    foreach (var node in nodes)
                    {
                        if (node.IsPublicEndPoint)
                        {
                            if (!await QueryEndPoint(node.PublicEndPoint, true))
                            {
                                _bucket.Remove(node.NodeId);
                            }

                            if (count >= 5)
                            {
                                break;
                            }
                        }
                    }

                    var packedNodes = _bucket.PackNodes();
                    if (packedNodes != null)
                    {
                        _storage.WriteFileBytes("nodes.data", packedNodes);
                    }
                }
                catch (Exception ex)
                {
                    Log.IgnoreException(ex, this);
                }

                ++_discoverRounds;
            }
        }