/// <inheritdoc/>
 public void AddPeers(NetworkAddress[] networkAddresses, IPAddress source, PeerIntroductionType peerIntroductionType)
 {
     foreach (var networkAddress in networkAddresses)
     {
         this.AddPeer(networkAddress, source, peerIntroductionType);
     }
 }
        /// <summary>
        /// Creates a new peer address instance and sets the loopback address (source).
        /// </summary>
        /// <param name="address">The network address of the peer.</param>
        /// <param name="loopback">The loopback (source) of the peer.</param>
        /// <param name="peerIntroductionType">How the peer will be introduced to the address manager.</param>
        public static PeerAddress Create(NetworkAddress address, IPAddress loopback, PeerIntroductionType peerIntroductionType)
        {
            var peer = Create(address, peerIntroductionType);

            peer.loopback = loopback.ToString();
            return(peer);
        }
Exemple #3
0
        /// <summary>Constructor used by dependency injection.</summary>
        internal PeerConnector(Network network,
                               INodeLifetime nodeLifeTime,
                               NetworkPeerConnectionParameters parameters,
                               NetworkPeerRequirement requirements,
                               Func <IPEndPoint, byte[]> groupSelector,
                               IAsyncLoopFactory asyncLoopFactory,
                               IPeerAddressManager peerAddressManager,
                               PeerIntroductionType peerIntroductionType,
                               INetworkPeerFactory networkPeerFactory)
        {
            this.asyncLoopFactory       = asyncLoopFactory;
            this.ConnectedPeers         = new NetworkPeerCollection();
            this.groupSelector          = groupSelector;
            this.MaximumNodeConnections = 8;
            this.network              = network;
            this.nodeLifetime         = nodeLifeTime;
            this.parentParameters     = parameters;
            this.peerAddressManager   = peerAddressManager;
            this.peerIntroductionType = peerIntroductionType;
            this.Requirements         = requirements;
            this.networkPeerFactory   = networkPeerFactory;

            this.currentParameters = this.parentParameters.Clone();
            this.currentParameters.TemplateBehaviors.Add(new PeerConnectorBehaviour(this));
            this.currentParameters.ConnectCancellation = this.nodeLifetime.ApplicationStopping;
        }
 /// <summary>
 /// Creates a new peer address instance.
 /// </summary>
 /// <param name="address">The network address of the peer.</param>
 /// <param name="peerIntroductionType">How the peer will be introduced to the address manager.</param>
 public static PeerAddress Create(NetworkAddress address, PeerIntroductionType peerIntroductionType)
 {
     return(new PeerAddress
     {
         ConnectionAttempts = 0,
         endPoint = address.Endpoint,
         loopback = IPAddress.Loopback.ToString(),
         PeerIntroductionType = peerIntroductionType
     });
 }
        /// <inheritdoc/>
        public void AddPeer(NetworkAddress networkAddress, IPAddress source, PeerIntroductionType peerIntroductionType)
        {
            if (networkAddress.Endpoint.Address.IsRoutable(true) == false)
            {
                return;
            }

            var peerToAdd = PeerAddress.Create(networkAddress, source, peerIntroductionType);

            this.Peers.TryAdd(peerToAdd.NetworkAddress.Endpoint, peerToAdd);
        }
Exemple #6
0
        /// <summary>Constructor used for unit testing.</summary>
        internal PeerConnector(
            IPeerAddressManager peerAddressManager,
            PeerIntroductionType peerIntroductionType)
        {
            Guard.NotNull(peerAddressManager, nameof(peerAddressManager));

            this.nodeLifetime         = new NodeLifetime();
            this.peerAddressManager   = peerAddressManager;
            this.peerIntroductionType = peerIntroductionType;
            this.RelatedPeerConnector = new RelatedPeerConnectors();
        }
        /// <inheritdoc />
        public NetworkAddress SelectPeerToConnectTo(PeerIntroductionType peerIntroductionType)
        {
            if (this.Peers.Tried(peerIntroductionType).Any() == true &&
                (this.Peers.New(peerIntroductionType).Any() == false || GetRandomInteger(2) == 0))
            {
                return(this.Peers.Tried(peerIntroductionType).Random().NetworkAddress);
            }

            if (this.Peers.New(peerIntroductionType).Any() == true)
            {
                return(this.Peers.New(peerIntroductionType).Random().NetworkAddress);
            }

            return(null);
        }
Exemple #8
0
        private IPeerConnector CreatePeerConnector(
            NodeConnectionParameters parameters,
            NodeServices requiredServices,
            Func <IPEndPoint, byte[]> peerSelector,
            PeerIntroductionType peerIntroductionType,
            int?maximumNodeConnections = 8)
        {
            this.logger.LogTrace("({0}:{1})", nameof(requiredServices), requiredServices);

            var nodeRequirement = new NodeRequirement
            {
                MinVersion       = this.NodeSettings.ProtocolVersion,
                RequiredServices = requiredServices,
            };

            var peerConnector = new PeerConnector(this.Network, this.nodeLifetime, parameters, nodeRequirement, peerSelector, this.asyncLoopFactory, this.peerAddressManager, peerIntroductionType)
            {
                MaximumNodeConnections = maximumNodeConnections.Value
            };

            this.logger.LogTrace("(-)");

            return(peerConnector);
        }
        /// <summary>
        /// Return peers where they have have had connection attempts, successful or not.
        /// </summary>
        public static IEnumerable <PeerAddress> Tried(this ConcurrentDictionary <IPEndPoint, PeerAddress> peers, PeerIntroductionType peerIntroductionType)
        {
            var tried = peers.Skip(0)
                        .Where(p => p.Value.PeerIntroductionType == peerIntroductionType)
                        .Where(p => !p.Value.IsNew).Select(p => p.Value);

            return(tried);
        }