コード例 #1
0
        /// <inheritdoc/>
        public async Task <INetworkPeer> CreateConnectedNetworkPeerAsync(
            IPEndPoint peerEndPoint,
            NetworkPeerConnectionParameters parameters = null,
            NetworkPeerDisposer networkPeerDisposer    = null)
        {
            Guard.NotNull(peerEndPoint, nameof(peerEndPoint));

            Action <INetworkPeer> onDisconnected = null;

            if (networkPeerDisposer != null)
            {
                onDisconnected = networkPeerDisposer.OnPeerDisconnectedHandler;
            }

            var peer = new NetworkPeer(peerEndPoint, this.network, parameters, this, this.dateTimeProvider, this.loggerFactory, this.selfEndpointTracker, this.asyncProvider, onDisconnected, this.onSendingMessage);

            try
            {
                await peer.ConnectAsync(peer.ConnectionParameters.ConnectCancellation).ConfigureAwait(false);

                networkPeerDisposer?.AddPeer(peer);
            }
            catch
            {
                peer.Dispose();
                throw;
            }

            return(peer);
        }
コード例 #2
0
        /// <inheritdoc/>
        public async Task <INetworkPeer> CreateConnectedNetworkPeerAsync(
            string endPoint,
            uint myVersion,
            bool isRelay = true,
            CancellationToken cancellation          = default(CancellationToken),
            NetworkPeerDisposer networkPeerDisposer = null)
        {
            Guard.NotNull(endPoint, nameof(endPoint));

            IPEndPoint ipEndPoint = Utils.ParseIpEndpoint(endPoint, this.network.DefaultPort);
            var        parameters = new NetworkPeerConnectionParameters()
            {
                ConnectCancellation = cancellation,
                IsRelay             = isRelay,
                Version             = myVersion,
                Services            = NetworkPeerServices.Nothing,
            };

            return(await this.CreateConnectedNetworkPeerAsync(ipEndPoint, parameters, networkPeerDisposer).ConfigureAwait(false));
        }
コード例 #3
0
        /// <summary>
        /// Initializes instance of a network peer server.
        /// </summary>
        /// <param name="network">Specification of the network the node runs on - regtest/testnet/mainnet.</param>
        /// <param name="localEndPoint">IP address and port to listen on.</param>
        /// <param name="externalEndPoint">IP address and port that the server is reachable from the Internet on.</param>
        /// <param name="version">Version of the network protocol that the server should run.</param>
        /// <param name="loggerFactory">Factory for creating loggers.</param>
        /// <param name="networkPeerFactory">Factory for creating P2P network peers.</param>
        /// <param name="initialBlockDownloadState">Provider of IBD state.</param>
        /// <param name="connectionManagerSettings">Configuration related to incoming and outgoing connections.</param>
        public NetworkPeerServer(Network network,
                                 IPEndPoint localEndPoint,
                                 IPEndPoint externalEndPoint,
                                 uint version,
                                 ILoggerFactory loggerFactory,
                                 INetworkPeerFactory networkPeerFactory,
                                 IInitialBlockDownloadState initialBlockDownloadState,
                                 ConnectionManagerSettings connectionManagerSettings,
                                 IAsyncProvider asyncProvider,
                                 IPeerAddressManager peerAddressManager,
                                 IDateTimeProvider dateTimeProvider)
        {
            this.logger                    = loggerFactory.CreateLogger(this.GetType().FullName);
            this.signals                   = asyncProvider.Signals;
            this.networkPeerFactory        = networkPeerFactory;
            this.networkPeerDisposer       = new NetworkPeerDisposer(loggerFactory, asyncProvider);
            this.initialBlockDownloadState = initialBlockDownloadState;
            this.connectionManagerSettings = connectionManagerSettings;
            this.peerAddressManager        = peerAddressManager;
            this.dateTimeProvider          = dateTimeProvider;

            this.InboundNetworkPeerConnectionParameters = new NetworkPeerConnectionParameters();

            this.LocalEndpoint    = Utils.EnsureIPv6(localEndPoint);
            this.ExternalEndpoint = Utils.EnsureIPv6(externalEndPoint);

            this.Network = network;
            this.Version = version;

            this.serverCancel = new CancellationTokenSource();

            this.tcpListener = new TcpListener(this.LocalEndpoint);
            this.tcpListener.Server.LingerState = new LingerOption(true, 0);
            this.tcpListener.Server.NoDelay     = true;
            this.tcpListener.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);

            this.acceptTask = Task.CompletedTask;

            this.logger.LogDebug("Network peer server ready to listen on '{0}'.", this.LocalEndpoint);
        }
コード例 #4
0
        /// <inheritdoc/>
        public INetworkPeer CreateNetworkPeer(TcpClient client, NetworkPeerConnectionParameters parameters = null, NetworkPeerDisposer networkPeerDisposer = null)
        {
            Guard.NotNull(client, nameof(client));

            Action <INetworkPeer> onDisconnected = null;

            if (networkPeerDisposer != null)
            {
                onDisconnected = networkPeerDisposer.OnPeerDisconnectedHandler;
            }

            var peer = new NetworkPeer((IPEndPoint)client.Client.RemoteEndPoint, this.network, parameters, client, this.dateTimeProvider, this, this.loggerFactory, this.selfEndpointTracker, this.asyncProvider, onDisconnected, this.onSendingMessage);

            networkPeerDisposer?.AddPeer(peer);

            return(peer);
        }