/// <summary>Attempts to connect to a random peer.</summary> internal async Task ConnectAsync(PeerAddress peerAddress) { if (this.selfEndpointTracker.IsSelf(peerAddress.Endpoint)) { this.logger.LogTrace("{0} is self. Therefore not connecting.", peerAddress.Endpoint); return; } // Connect if local, ip range filtering disabled or ip range filtering enabled and peer in a different group. if (peerAddress.Endpoint.Address.IsRoutable(false) && this.ConnectionSettings.IpRangeFiltering && this.PeerIsPartOfExistingGroup(peerAddress)) { this.logger.LogTrace("(-)[RANGE_FILTERED]"); return; } INetworkPeer peer = null; try { using (CancellationTokenSource timeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(this.nodeLifetime.ApplicationStopping)) { this.peerAddressManager.PeerAttempted(peerAddress.Endpoint, this.dateTimeProvider.GetUtcNow()); NetworkPeerConnectionParameters clonedConnectParamaters = this.CurrentParameters.Clone(); timeoutTokenSource.CancelAfter(5000); clonedConnectParamaters.ConnectCancellation = timeoutTokenSource.Token; peer = await this.networkPeerFactory.CreateConnectedNetworkPeerAsync(peerAddress.Endpoint, clonedConnectParamaters, this.networkPeerDisposer).ConfigureAwait(false); await peer.VersionHandshakeAsync(this.Requirements, timeoutTokenSource.Token).ConfigureAwait(false); this.AddPeer(peer); } } catch (OperationCanceledException) { if (this.nodeLifetime.ApplicationStopping.IsCancellationRequested) { this.logger.LogDebug("Peer {0} connection canceled because application is stopping.", peerAddress.Endpoint); peer?.Disconnect("Application stopping"); } else { this.logger.LogDebug("Peer {0} connection timeout.", peerAddress.Endpoint); peerAddress.SetHandshakeAttempted(this.dateTimeProvider.GetUtcNow()); peer?.Disconnect("Connection timeout"); } } catch (NBitcoin.Protocol.ProtocolException) { this.logger.LogDebug("Handshake rejected by peer '{0}'.", peerAddress.Endpoint); peerAddress.SetHandshakeAttempted(this.dateTimeProvider.GetUtcNow()); peer?.Disconnect("Error while handshaking"); } catch (Exception exception) { this.logger.LogTrace("Exception occurred while connecting: {0}", exception.ToString()); peerAddress.SetHandshakeAttempted(this.dateTimeProvider.GetUtcNow()); peer?.Disconnect("Error while connecting", exception); } }
/// <summary>Attempts to connect to a random peer.</summary> public async Task ConnectAsync(PeerAddress peerAddress) { if (this.selfEndpointTracker.IsSelf(peerAddress.Endpoint)) { this.logger.LogDebug("Connect aborted: {0} is self.", peerAddress.Endpoint); return; } if (this.IsPeerConnected(peerAddress.Endpoint)) { this.logger.LogDebug("Connect aborted: {0} is already connected.", peerAddress.Endpoint); return; } if (peerAddress.IsBanned(this.dateTimeProvider.GetUtcNow())) { this.logger.LogDebug("Connect aborted: {0} is banned until {1}.", peerAddress.Endpoint, peerAddress.BanUntil); return; } INetworkPeer peer = null; try { using (CancellationTokenSource timeoutTokenSource = CancellationTokenSource.CreateLinkedTokenSource(this.NodeLifetime.ApplicationStopping)) { this.PeerAddressManager.PeerAttempted(peerAddress.Endpoint, this.dateTimeProvider.GetUtcNow()); NetworkPeerConnectionParameters clonedConnectParameters = this.CurrentParameters.Clone(); timeoutTokenSource.CancelAfter(5000); clonedConnectParameters.ConnectCancellation = timeoutTokenSource.Token; peer = await this.networkPeerFactory.CreateConnectedNetworkPeerAsync(peerAddress.Endpoint, clonedConnectParameters, this.networkPeerDisposer).ConfigureAwait(false); await peer.VersionHandshakeAsync(this.Requirements, timeoutTokenSource.Token).ConfigureAwait(false); this.AddPeer(peer); } } catch (OperationCanceledException) { if (this.NodeLifetime.ApplicationStopping.IsCancellationRequested) { this.logger.LogDebug("Peer {0} connection canceled because application is stopping.", peerAddress.Endpoint); peer?.Disconnect("Application stopping"); } else { this.logger.LogDebug("Peer {0} connection timeout.", peerAddress.Endpoint); peerAddress.SetHandshakeAttempted(this.dateTimeProvider.GetUtcNow()); peer?.Disconnect("Connection timeout"); } } catch (NBitcoin.Protocol.ProtocolException) { this.logger.LogDebug("Handshake rejected by peer '{0}'.", peerAddress.Endpoint); peerAddress.SetHandshakeAttempted(this.dateTimeProvider.GetUtcNow()); peer?.Disconnect("Error while handshaking"); } catch (Exception exception) { this.logger.LogDebug("Exception occurred while connecting: {0}", exception is SocketException ? exception.Message : exception.ToString()); peerAddress.SetHandshakeAttempted(this.dateTimeProvider.GetUtcNow()); peer?.Disconnect("Error while connecting", exception); } }