Esempio n. 1
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,
                                 ProtocolVersion version,
                                 ILoggerFactory loggerFactory,
                                 INetworkPeerFactory networkPeerFactory,
                                 IInitialBlockDownloadState initialBlockDownloadState,
                                 ConnectionManagerSettings connectionManagerSettings)
        {
            this.logger = loggerFactory.CreateLogger(this.GetType().FullName, $"[{localEndPoint}] ");

            this.networkPeerFactory        = networkPeerFactory;
            this.networkPeerDisposer       = new NetworkPeerDisposer(loggerFactory);
            this.initialBlockDownloadState = initialBlockDownloadState;
            this.connectionManagerSettings = connectionManagerSettings;

            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.LogTrace("Network peer server ready to listen on '{0}'.", this.LocalEndpoint);
        }
Esempio n. 2
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, onDisconnected, this.onSendingMessage);

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

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

            return(peer);
        }
        /// <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>
        public NetworkPeerServer(Network network,
                                 IPEndPoint localEndPoint,
                                 IPEndPoint externalEndPoint,
                                 ProtocolVersion version,
                                 ILoggerFactory loggerFactory,
                                 INetworkPeerFactory networkPeerFactory)
        {
            this.logger = loggerFactory.CreateLogger(this.GetType().FullName, $"[{localEndPoint}] ");
            this.logger.LogTrace("({0}:{1},{2}:{3},{4}:{5})", nameof(network), network, nameof(localEndPoint), localEndPoint, nameof(externalEndPoint), externalEndPoint, nameof(version), version);

            this.networkPeerFactory  = networkPeerFactory;
            this.networkPeerDisposer = new NetworkPeerDisposer(loggerFactory);

            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.LogTrace("Network peer server ready to listen on '{0}'.", this.LocalEndpoint);

            this.logger.LogTrace("(-)");
        }
Esempio n. 4
0
        /// <inheritdoc/>
        public async Task <INetworkPeer> CreateConnectedNetworkPeerAsync(
            string endPoint,
            ProtocolVersion myVersion = ProtocolVersion.PROTOCOL_VERSION,
            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));
        }
Esempio n. 5
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, onDisconnected, this.onSendingMessage);

            networkPeerDisposer?.AddPeer(peer);

            return(peer);
        }