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); }
public void DispatchTransaction(BinaryReader reader, IRemoteNode remoteNode) { TransactionType type = (TransactionType)reader.ReadByte(); ITransactionProcessor processor = _transactionProcessors[type]; processor.ProcessTransaction(reader, remoteNode); }
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); } }
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); }
public bool TryRemovePeer(IPEndPoint endPoint, out IRemoteNode outPeer) { if (_trackedPeers.TryRemove($"{endPoint.Address}:{endPoint.Port}", out outPeer)) { return(true); } return(false); }
public IRemoteNode RemovePeer(IRemoteNode peer) { lock (peer) { IRemoteNode outPeer; if (_connectedPeers.TryRemove($"{peer.ListenerEndpoint.Address}:{peer.ListenerEndpoint.Port}", out outPeer)) { return(outPeer); } return(null); } }
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); }
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}"); } } }
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); }
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); }
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()); } }
private void OnNodeConnectionEstablished(TcpClient client) { IRemoteNode newNode = _remoteNodeFactory.Invoke(client); newNode.ExecuteProtocolHandShake(); }
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; } }
public void ProcessMessage(BinaryReader reader, IRemoteNode remoteNode) { }
public void ProcessTransaction(BinaryReader reader, IRemoteNode remoteNode) { ProcessTransaction(_objectReader.ReadObject <TransferTransaction>(reader), remoteNode); }
public void ProcessTransaction(TransferTransaction transaction, IRemoteNode remoteNode) { Console.WriteLine("Received a transfer transaction!"); }
public void ProcessMessage(BinaryReader reader, IRemoteNode remoteNode) { ProcessMessage(_objectReader.ReadObject <RequestPeerAddressesMessage>(reader), remoteNode); }
public void ProcessMessage(TransactionMessage message, IRemoteNode remoteNode) { }