Esempio n. 1
0
        public IDisposable RegisterPeerTableRepository(PeerTableRepository peerTableRepository)
        {
            var poll = new EventLoopMessageListener <object>(o =>
            {
                var message = o as IncomingMessage;
                if (message != null)
                {
                    if (message.Message.Payload is AddrPayload)
                    {
                        peerTableRepository.WritePeers(((AddrPayload)message.Message.Payload).Addresses
                                                       .Where(a => a.Endpoint.Address.IsRoutable(AllowLocalPeers))
                                                       .Select(a => new Peer(PeerOrigin.Addr, a)));
                    }
                }
                var peer = o as Peer;
                if (peer != null)
                {
                    if (peer.NetworkAddress.Endpoint.Address.IsRoutable(AllowLocalPeers))
                    {
                        peerTableRepository.WritePeer(peer);
                    }
                }
            });

            if (peerTableRepository != _PeerTable)
            {
                _InternalMessageProducer.PushMessages(peerTableRepository.GetPeers());
            }
            return(new CompositeDisposable(AllMessages.AddMessageListener(poll), _InternalMessageProducer.AddMessageListener(poll), OwnResource(poll)));
        }
Esempio n. 2
0
 public void RespondToHandShake(CancellationToken cancellation = default(CancellationToken))
 {
     using (TraceCorrelation.Open())
     {
         var listener = new PollMessageListener <IncomingMessage>();
         using (MessageProducer.AddMessageListener(listener))
         {
             NodeServerTrace.Information("Responding to handshake");
             SendMessage(MyVersion);
             listener.ReceiveMessage().AssertPayload <VerAckPayload>();
             SendMessage(new VerAckPayload());
             _State = NodeState.HandShaked;
         }
     }
 }
Esempio n. 3
0
        public TPayload RecieveMessage <TPayload>(CancellationToken cancellationToken = default(CancellationToken)) where TPayload : Payload
        {
            var listener = new PollMessageListener <IncomingMessage>();

            using (MessageProducer.AddMessageListener(listener))
            {
                while (true)
                {
                    var message = listener.RecieveMessage(cancellationToken);
                    if (message.Message.Payload is TPayload)
                    {
                        return((TPayload)message.Message.Payload);
                    }
                }
            }
            throw new InvalidProgramException("Bug in Node.RecieveMessage");
        }
Esempio n. 4
0
        public void VersionHandshake(CancellationToken cancellationToken = default(CancellationToken))
        {
            var listener = new PollMessageListener <IncomingMessage>();

            using (MessageProducer.AddMessageListener(listener))
            {
                using (TraceCorrelation.Open())
                {
                    var myVersion = CreateVersionPayload();
                    SendMessage(myVersion);

                    var version = listener.RecieveMessage(cancellationToken).AssertPayload <VersionPayload>();
                    _FullVersion = version;
                    Version      = version.Version;
                    if (version.Nonce == NodeServer.Nonce)
                    {
                        NodeServerTrace.ConnectionToSelfDetected();
                        Disconnect();
                        throw new InvalidOperationException("Impossible to connect to self");
                    }

                    if (!version.AddressReciever.Address.Equals(ExternalEndpoint.Address))
                    {
                        NodeServerTrace.Warning("Different external address detected by the node " + version.AddressReciever.Address + " instead of " + ExternalEndpoint.Address);
                    }
                    NodeServer.ExternalAddressDetected(version.AddressReciever.Address);
                    if (version.Version < ProtocolVersion.MIN_PEER_PROTO_VERSION)
                    {
                        NodeServerTrace.Warning("Outdated version " + version.Version + " disconnecting");
                        Disconnect();
                        return;
                    }
                    SendMessage(new VerAckPayload());
                    listener.RecieveMessage(cancellationToken).AssertPayload <VerAckPayload>();
                    State = NodeState.HandShaked;
                    if (NodeServer.AdvertizeMyself)
                    {
                        AdvertiseMyself();
                    }
                }
            }
        }