/// <summary>
        /// Initializes an instance of the object.
        /// </summary>
        /// <param name="network">Specification of the network the node runs on - regtest/testnet/mainnet.</param>
        /// <param name="peer">Network peer the node is connected to, or will connect to.</param>
        /// <param name="client">Initialized TCP client, which may or may not be already connected.</param>
        /// <param name="clientId">Unique identifier of the connection.</param>
        /// <param name="processMessageAsync">Callback to be called when a new message arrives from the peer.</param>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="loggerFactory">Factory for creating loggers.</param>
        public NetworkPeerConnection(Network network, INetworkPeer peer, TcpClient client, int clientId, ProcessMessageAsync <IncomingMessage> processMessageAsync, IDateTimeProvider dateTimeProvider, ILoggerFactory loggerFactory)
        {
            this.loggerFactory = loggerFactory;
            this.logger        = this.loggerFactory.CreateLogger(this.GetType().FullName, $"[{clientId}-{peer.PeerEndPoint}] ");

            this.network          = network;
            this.dateTimeProvider = dateTimeProvider;

            this.peer = peer;
            this.setPeerStateOnShutdown = NetworkPeerState.Offline;
            this.tcpClient = client;
            this.Id        = clientId;

            this.stream           = this.tcpClient.Connected ? this.tcpClient.GetStream() : null;
            this.ShutdownComplete = new TaskCompletionSource <bool>();
            this.DisposeComplete  = new TaskCompletionSource <bool>();

            this.shutdownLock = new object();
            this.writeLock    = new AsyncLock();

            this.CancellationSource = new CancellationTokenSource();

            this.MessageProducer             = new MessageProducer <IncomingMessage>();
            this.messageListener             = new CallbackMessageListener <IncomingMessage>(processMessageAsync);
            this.messageProducerRegistration = this.MessageProducer.AddMessageListener(this.messageListener);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Initializes an instance of the object.
        /// </summary>
        /// <param name="network">Specification of the network the node runs on - regtest/testnet/mainnet.</param>
        /// <param name="peer">Network peer the node is connected to, or will connect to.</param>
        /// <param name="client">Initialized TCP client, which may or may not be already connected.</param>
        /// <param name="clientId">Unique identifier of the connection.</param>
        /// <param name="processMessageAsync">Callback to be called when a new message arrives from the peer.</param>
        /// <param name="dateTimeProvider">Provider of time functions.</param>
        /// <param name="loggerFactory">Factory for creating loggers.</param>
        /// <param name="payloadProvider">A provider of network payload messages.</param>
        public NetworkPeerConnection(Network network, INetworkPeer peer, TcpClient client, int clientId,
                                     ProcessMessageAsync <IncomingMessage> processMessageAsync, IDateTimeProvider dateTimeProvider,
                                     ILoggerFactory loggerFactory, PayloadProvider payloadProvider, IAsyncProvider asyncProvider)
        {
            this.loggerFactory   = loggerFactory;
            this.payloadProvider = payloadProvider;
            this.asyncProvider   = Guard.NotNull(asyncProvider, nameof(asyncProvider));
            this.logger          = this.loggerFactory.CreateLogger(GetType().FullName, $"[{clientId}-{peer.PeerEndPoint}] ");

            this.network          = network;
            this.dateTimeProvider = dateTimeProvider;

            this.peer      = peer;
            this.tcpClient = client;
            this.Id        = clientId;

            this.stream = this.tcpClient.Connected ? this.tcpClient.GetStream() : null;

            this.writeLock = new AsyncLock();

            this.CancellationSource = new CancellationTokenSource();

            this.MessageProducer = new MessageProducer <IncomingMessage>();
            this.messageListener =
                new CallbackMessageListener <IncomingMessage>(asyncProvider, processMessageAsync, peer);
            this.messageProducerRegistration = this.MessageProducer.AddMessageListener(this.messageListener);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes the instance of the object and subscribes to the peer's message producer.
        /// </summary>
        /// <param name="peer">Connected network peer that we receive messages from.</param>
        public NetworkPeerListener(INetworkPeer peer, IAsyncProvider asyncProvider)
        {
            this.peer          = peer;
            this.asyncProvider = asyncProvider;
            this.asyncIncomingMessagesQueue = asyncProvider.CreateAsyncQueue <IncomingMessage>();

            this.messageProducerRegistration = peer.MessageProducer.AddMessageListener(this);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes the instance of the object and subscribes to the peer's message producer.
 /// </summary>
 /// <param name="peer">Connected network peer that we receive messages from.</param>
 public NetworkPeerListener(INetworkPeer peer)
 {
     this.asyncQueue = new AsyncQueue <IncomingMessage>();
     this.messageProducerRegistration = peer.MessageProducer.AddMessageListener(this);
     this.peer = peer;
 }