コード例 #1
0
        public void Start()
        {
            this.logger.LogTrace("()");

            this.Parameters.UserAgent = $"{this.NodeSettings.Agent}:{this.GetVersion()}";
            this.Parameters.Version   = this.NodeSettings.ProtocolVersion;

            NetworkPeerConnectionParameters clonedParameters = this.Parameters.Clone();

            clonedParameters.TemplateBehaviors.Add(new ConnectionManagerBehavior(false, this, this.loggerFactory));

            // Don't start peer discovery if we have specified any nodes using the -connect arg.
            if (!this.connectionManagerSettings.Connect.Any())
            {
                if (this.Parameters.PeerAddressManagerBehaviour().Mode.HasFlag(PeerAddressManagerBehaviourMode.Discover))
                {
                    this.logger.LogInformation("Starting peer discovery...");

                    this.peerDiscoveryLoop = new PeerDiscoveryLoop(this.asyncLoopFactory, this.Network, clonedParameters, this.nodeLifetime, this.peerAddressManager, this.networkPeerFactory);
                    this.peerDiscoveryLoop.DiscoverPeers();
                }

                this.DiscoverNodesPeerConnector = this.CreatePeerConnector(clonedParameters, this.discoveredNodeRequiredService, WellKnownPeerConnectorSelectors.ByNetwork, PeerIntroductionType.Discover);
            }
            else
            {
                // Use if we have specified any nodes using the -connect arg
                var peers = this.connectionManagerSettings.Connect.Select(node => new NetworkAddress(node)).ToArray();
                this.peerAddressManager.AddPeers(peers, IPAddress.Loopback, PeerIntroductionType.Connect);
                clonedParameters.PeerAddressManagerBehaviour().Mode = PeerAddressManagerBehaviourMode.None;

                this.ConnectNodePeerConnector = this.CreatePeerConnector(clonedParameters, NetworkPeerServices.Nothing, WellKnownPeerConnectorSelectors.ByEndpoint, PeerIntroductionType.Connect, this.connectionManagerSettings.Connect.Count);
            }

            {
                // Use if we have specified any nodes using the -addnode arg
                var peers = this.connectionManagerSettings.AddNode.Select(node => new NetworkAddress(node)).ToArray();
                this.peerAddressManager.AddPeers(peers, IPAddress.Loopback, PeerIntroductionType.Add);
                clonedParameters.PeerAddressManagerBehaviour().Mode = PeerAddressManagerBehaviourMode.AdvertiseDiscover;

                this.AddNodePeerConnector = this.CreatePeerConnector(clonedParameters, NetworkPeerServices.Nothing, WellKnownPeerConnectorSelectors.ByEndpoint, PeerIntroductionType.Add, this.connectionManagerSettings.AddNode.Count);
            }

            // Relate the peer connectors to each other to prevent duplicate connections.
            var relatedPeerConnectors = new RelatedPeerConnectors();

            relatedPeerConnectors.Register("Discovery", this.DiscoverNodesPeerConnector);
            relatedPeerConnectors.Register("Connect", this.ConnectNodePeerConnector);
            relatedPeerConnectors.Register("AddNode", this.AddNodePeerConnector);

            this.DiscoverNodesPeerConnector?.StartConnectAsync();
            this.ConnectNodePeerConnector?.StartConnectAsync();
            this.AddNodePeerConnector?.StartConnectAsync();

            this.StartNodeServer();

            this.logger.LogTrace("(-)");
        }
コード例 #2
0
        /// <inheritdoc/>
        public void DiscoverPeers(NetworkPeerConnectionParameters parameters)
        {
            // If peers are specified in the -connect arg then discovery does not happen.
            if (this.nodeSettings.ConnectionManager.Connect.Any())
            {
                return;
            }

            if (!parameters.PeerAddressManagerBehaviour().Mode.HasFlag(PeerAddressManagerBehaviourMode.Discover))
            {
                return;
            }

            this.currentParameters = parameters;
            this.peersToFind       = this.currentParameters.PeerAddressManagerBehaviour().PeersToDiscover;

            this.logger.LogInformation("Starting peer discovery...");
            this.asyncLoop = this.asyncLoopFactory.Run(nameof(this.DiscoverPeersAsync), async token =>
            {
                if (this.peerAddressManager.Peers.Count < this.peersToFind)
                {
                    await this.DiscoverPeersAsync();
                }
            },
                                                       this.nodeLifetime.ApplicationStopping,
                                                       TimeSpans.Minute);
        }