/// <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); }
/// <summary> /// Initializes the instance of the object. /// </summary> /// <param name="processMessageAsync"> /// User defined callback routine to be executed when a new message arrives to the /// listener. /// </param> public CallbackMessageListener(IAsyncProvider asyncProvider, ProcessMessageAsync <T> processMessageAsync, INetworkPeer peer) { var queuerName = $"{nameof(CallbackMessageListener<T>)}-{typeof(T).Name}-{peer.PeerEndPoint}"; this.asyncQueue = asyncProvider.CreateAndRunAsyncDelegateDequeuer(queuerName, new Func <T, CancellationToken, Task>(processMessageAsync)); }
/// <inheritdoc /> public NetworkPeerConnection CreateNetworkPeerConnection(INetworkPeer peer, TcpClient client, ProcessMessageAsync <IncomingMessage> processMessageAsync) { Guard.NotNull(peer, nameof(peer)); Guard.NotNull(client, nameof(client)); Guard.NotNull(processMessageAsync, nameof(processMessageAsync)); var id = Interlocked.Increment(ref this.lastClientId); return(new NetworkPeerConnection(this.network, peer, client, id, processMessageAsync, this.dateTimeProvider, this.loggerFactory, this.payloadProvider, this.asyncProvider)); }
/// <summary> /// Initializes the instance of the object. /// </summary> /// <param name="processMessageAsync">User defined callback routine to be executed when a new message arrives to the listener.</param> public CallbackMessageListener(ProcessMessageAsync <T> processMessageAsync) { this.asyncQueue = new AsyncQueue <T>(new AsyncQueue <T> .OnEnqueueAsync(processMessageAsync)); this.processMessageAsync = processMessageAsync; }
public UdpListener(ProcessMessageAsync processMessage, ushort port) { this.processMessageCallback = processMessage ?? throw new ArgumentNullException(nameof(processMessage)); this.listenPort = port; this.listener = new UdpClient(listenPort); }
/// <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); }