Esempio n. 1
0
        public async Task <bool> SerializeAsync(
            IRemoteNode receiver,
            object message,
            StartSerializeAsync start,
            CancellationToken cancellationToken)
        {
            if (receiver == null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (start == null)
            {
                throw new ArgumentNullException(nameof(start));
            }

            if (message is FooMessage)
            {
                var output = await start(FooMessage.Id, FooMessage.Size, cancellationToken);

                WriteFooPayload(output, ((FooMessage)message).Value1);
            }
            else
            {
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        public void DispatchTransaction(BinaryReader reader, IRemoteNode remoteNode)
        {
            TransactionType       type      = (TransactionType)reader.ReadByte();
            ITransactionProcessor processor = _transactionProcessors[type];

            processor.ProcessTransaction(reader, remoteNode);
        }
Esempio n. 3
0
        public bool TryAddNewConnectedPeer(IRemoteNode peer)
        {
            if (Equals(peer.ListenerEndpoint.Address, LocalNode.LocalAddress.Address) &&
                peer.ListenerEndpoint.Port == (_clientSettings.BlockChainNetSettings?.ListenPortOverride ?? _clientSettings.ListenPort))
            {
                return(false);
            }

            lock (_notConnectedPeers)
            {
                var unConnectedEntry = _notConnectedPeers.FirstOrDefault(x => Equals(x.Address, peer.ListenerEndpoint.Address) && (x.Port == peer.ListenerEndpoint.Port));
                if (unConnectedEntry != null)
                {
                    _notConnectedPeers.Remove(unConnectedEntry);
                }
            }

            lock (_connectedPeers)
            {
                if (!_connectedPeers.ContainsKey($"{peer.ListenerEndpoint.Address}:{peer.ListenerEndpoint.Port}"))
                {
                    _connectedPeers[$"{peer.ListenerEndpoint.Address}:{peer.ListenerEndpoint.Port}"] = peer;
                    return(true);
                }
                return(false);
            }
        }
Esempio n. 4
0
        public void ProcessMessage(RequestPeerAddressesMessage message, IRemoteNode remoteNode)
        {
            PeerAddressesPayloadMessage response = new PeerAddressesPayloadMessage();

            response.NotConnectedIpEndPoints = _peerTracker.GetNotConnectedEndPoints(Int32.MaxValue, 0);
            response.ConnectedIpEndPoints    = _peerTracker.GetConnectedEndPoints(Int32.MaxValue, 0);
            remoteNode.QueueMessage(response);
        }
Esempio n. 5
0
        public bool TryRemovePeer(IPEndPoint endPoint, out IRemoteNode outPeer)
        {
            if (_trackedPeers.TryRemove($"{endPoint.Address}:{endPoint.Port}", out outPeer))
            {
                return(true);
            }

            return(false);
        }
Esempio n. 6
0
        public IRemoteNode RemovePeer(IRemoteNode peer)
        {
            lock (peer)
            {
                IRemoteNode outPeer;
                if (_connectedPeers.TryRemove($"{peer.ListenerEndpoint.Address}:{peer.ListenerEndpoint.Port}", out outPeer))
                {
                    return(outPeer);
                }

                return(null);
            }
        }
Esempio n. 7
0
        public Task <object> DeserializeAsync(
            IRemoteNode sender,
            Guid messageId,
            int payloadSize,
            Stream payload,
            CancellationToken cancellationToken)
        {
            object message;

            if (sender == null)
            {
                throw new ArgumentNullException(nameof(sender));
            }

            if (payloadSize < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(payloadSize));
            }

            if (payload == null)
            {
                throw new ArgumentNullException(nameof(payload));
            }

            if (messageId == FooMessage.Id)
            {
                if (payloadSize != FooMessage.Size)
                {
                    throw new InvalidMessageException();
                }

                using (var reader = new BinaryReader(payload, Encoding.UTF8, true))
                {
                    var value1 = reader.ReadInt32();
                    if (value1 < 0)
                    {
                        throw new InvalidMessageException();
                    }

                    message = new FooMessage(value1);
                }
            }
            else
            {
                message = null;
            }

            return(Task.FromResult(message));
        }
        public virtual INode GetNode(IId id)
        {
            if (nodes.ContainsKey(id))
            {
                logger.Trace("Node was found with id {0}", id);
                return(nodes[id]);
            }

            // create a new node, and add it to the context for reuse
            logger.Trace("Creating remote node for {0} id.", id);
            IRemoteNode rn = this.EntityFactory.CreateRemoteNode(id);

            AddNode(rn);

            return(rn);
        }
Esempio n. 9
0
        public void ProcessMessage(PeerAddressesPayloadMessage message, IRemoteNode remoteNode)
        {
            foreach (var responseIpEndPoint in message.NotConnectedIpEndPoints)
            {
                if (_peerTracker.TryAddNotConnectedIpEndPoint(responseIpEndPoint))
                {
                    Console.WriteLine($"Added new potential peer at: {responseIpEndPoint.Address}:{responseIpEndPoint.Port}");
                }
            }

            foreach (var responseIpEndPoint in message.ConnectedIpEndPoints)
            {
                if (_peerTracker.TryAddNotConnectedIpEndPoint(responseIpEndPoint))
                {
                    Console.WriteLine($"Added new verified connected peer at: {responseIpEndPoint.Address}:{responseIpEndPoint.Port}");
                }
            }
        }
Esempio n. 10
0
        public async Task <bool> SerializeAsync(
            IRemoteNode receiver,
            object message,
            StartSerializeAsync start,
            CancellationToken cancellationToken)
        {
            if (receiver == null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (start == null)
            {
                throw new ArgumentNullException(nameof(start));
            }

            if (message is BarMessage)
            {
                byte[] payload;

                using (var buffer = new MemoryStream())
                    using (var writer = new BinaryWriter(buffer, Encoding.UTF8, true))
                    {
                        writer.Write(((BarMessage)message).Value1);

                        payload = buffer.ToArray();
                    }

                var output = await start(BarMessage.Id, payload.Length, cancellationToken);

                await output.WriteAsync(payload, 0, payload.Length, cancellationToken);
            }
            else
            {
                return(false);
            }

            return(true);
        }
Esempio n. 11
0
        public async Task <bool> SerializeAsync(
            IRemoteNode receiver,
            object message,
            StartSerializeAsync start,
            CancellationToken cancellationToken)
        {
            if (receiver == null)
            {
                throw new ArgumentNullException(nameof(receiver));
            }

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (start == null)
            {
                throw new ArgumentNullException(nameof(start));
            }

            if (message is BazMessage)
            {
                var output = await start(BazMessage.Id, 8, cancellationToken);

                using (var writer = new BinaryWriter(output, Encoding.UTF8, true))
                {
                    writer.Write(((BazMessage)message).Value1);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
Esempio n. 12
0
        public void ProcessHandShake(BinaryReader reader, IRemoteNode remoteNode)
        {
            IntroductionMessage messageToSend = new IntroductionMessage();

            messageToSend.BlockHeight = 100;
            messageToSend.Version     = new Version(_clientSettings.Version);
            messageToSend.Port        = _clientSettings.BlockChainNetSettings.ListenPortOverride ?? _clientSettings.ListenPort;

            remoteNode.QueueMessage(messageToSend);

            MessageType msgType = (MessageType)reader.ReadByte();

            if (msgType != MessageType.Introduction)
            {
                throw new ProtocolViolationException("Invalid version response.");
            }

            IntroductionMessage receivedMessage = new IntroductionMessage();

            receivedMessage.ReadFromStream(reader);

            if (receivedMessage.Version != new Version(_clientSettings.Version))
            {
                throw new ProtocolViolationException("Version mismatch.");
            }

            remoteNode.ListenerEndpoint = new IPEndPoint(remoteNode.RemoteEndPoint.Address, receivedMessage.Port);

            if (_peerTracker.TryAddNewConnectedPeer(remoteNode))
            {
                Console.WriteLine("Successfully Received Introduction Request!");
                Console.WriteLine("Starting Protocol With New Peer.");

                Task.Run(() => remoteNode.ExecuteProtocolLoop());
            }
        }
Esempio n. 13
0
        private void OnNodeConnectionEstablished(TcpClient client)
        {
            IRemoteNode newNode = _remoteNodeFactory.Invoke(client);

            newNode.ExecuteProtocolHandShake();
        }
Esempio n. 14
0
        public void DispatchProtocolMessage(MessageType messageType, BinaryReader reader, IRemoteNode remoteNode)
        {
            switch (messageType)
            {
            case MessageType.GetPeerAddresses:
                _messageProcessors[MessageType.GetPeerAddresses].ProcessMessage(reader, remoteNode);
                break;

            case MessageType.PeerAddressesPayload:
                _messageProcessors[MessageType.PeerAddressesPayload].ProcessMessage(reader, remoteNode);
                break;

            case MessageType.Transaction:
                _transactionDispatcher.DispatchTransaction(reader, remoteNode);
                break;
            }
        }
Esempio n. 15
0
 public void ProcessMessage(BinaryReader reader, IRemoteNode remoteNode)
 {
 }
Esempio n. 16
0
 public void ProcessTransaction(BinaryReader reader, IRemoteNode remoteNode)
 {
     ProcessTransaction(_objectReader.ReadObject <TransferTransaction>(reader), remoteNode);
 }
Esempio n. 17
0
 public void ProcessTransaction(TransferTransaction transaction, IRemoteNode remoteNode)
 {
     Console.WriteLine("Received a transfer transaction!");
 }
Esempio n. 18
0
 public void ProcessMessage(BinaryReader reader, IRemoteNode remoteNode)
 {
     ProcessMessage(_objectReader.ReadObject <RequestPeerAddressesMessage>(reader), remoteNode);
 }
Esempio n. 19
0
 public void ProcessMessage(TransactionMessage message, IRemoteNode remoteNode)
 {
 }