private Node AddNode(Node node) { if (node.State < NodeState.Connected) { return(null); } return(_Nodes.AddNode(node)); }
/// <summary> /// Fill the PeerTable with fresh addresses /// </summary> public void DiscoverPeers(int peerToFind = 990) { TraceCorrelation traceCorrelation = new TraceCorrelation(NodeServerTrace.Trace, "Discovering nodes"); List <Task> tasks = new List <Task>(); using (traceCorrelation.Open()) { while (CountPeerRequired(peerToFind) != 0) { NodeServerTrace.PeerTableRemainingPeerToGet(CountPeerRequired(peerToFind)); CancellationTokenSource cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(40)); var peers = PeerTable.GetActivePeers(1000); if (peers.Length == 0) { PopulateTableWithDNSNodes(); PopulateTableWithHardNodes(); peers = PeerTable.GetActivePeers(1000); } CancellationTokenSource peerTableFull = new CancellationTokenSource(); NodeSet connected = new NodeSet(); try { Parallel.ForEach(peers, new ParallelOptions() { MaxDegreeOfParallelism = 5, CancellationToken = peerTableFull.Token, }, p => { Node n = null; try { n = GetNodeByPeer(p, cancellation.Token); if (n.State < NodeState.HandShaked) { connected.AddNode(n); n.VersionHandshake(cancellation.Token); } n.SendMessage(new GetAddrPayload()); Thread.Sleep(2000); } catch (Exception) { if (n != null) { n.Disconnect(); } } if (CountPeerRequired(peerToFind) == 0) { peerTableFull.Cancel(); } else { NodeServerTrace.Information("Need " + CountPeerRequired(peerToFind) + " more peers"); } }); } catch (OperationCanceledException) { } finally { connected.DisconnectAll(); } } NodeServerTrace.Trace.TraceInformation("Peer table is now full"); } }