コード例 #1
0
ファイル: NodeServer.cs プロジェクト: pkahle/NBitcoin
        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)));
        }
コード例 #2
0
        public NodeServer(Network network, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION, int internalPort = -1)
        {
            this.AllowLocalPeers = true;
            this.InboundNodeConnectionParameters = new NodeConnectionParameters();

            internalPort       = internalPort == -1 ? network.DefaultPort : internalPort;
            this.localEndpoint = new IPEndPoint(IPAddress.Parse("0.0.0.0").MapToIPv6Ex(), internalPort);

            this.MaxConnections   = 125;
            this.Network          = network;
            this.externalEndpoint = new IPEndPoint(this.localEndpoint.Address, this.Network.DefaultPort);
            this.Version          = version;

            var listener = new EventLoopMessageListener <IncomingMessage>(ProcessMessage);

            this.messageProducer.AddMessageListener(listener);
            this.OwnResource(listener);

            this.ConnectedNodes          = new NodesCollection();
            this.ConnectedNodes.Added   += Nodes_NodeAdded;
            this.ConnectedNodes.Removed += Nodes_NodeRemoved;
            this.ConnectedNodes.MessageProducer.AddMessageListener(listener);

            this.AllMessages = new MessageProducer <IncomingMessage>();
            this.trace       = new TraceCorrelation(NodeServerTrace.Trace, "Node server listening on " + this.LocalEndpoint);
        }
コード例 #3
0
ファイル: Node.cs プロジェクト: jannickj/NBitcoin
 public void Dispose()
 {
     if (!_IsDisposed)
     {
         Utils.SafeCloseSocket(Socket);
         if (_PingListener != null)
         {
             Node.MessageProducer.RemoveMessageListener(_PingListener);
             _PingListener.Dispose();
             _PingListener = null;
         }
         _IsDisposed = true;
     }
 }
コード例 #4
0
ファイル: Node.cs プロジェクト: jannickj/NBitcoin
            public void BeginListen()
            {
                new Thread(() =>
                {
                    using (TraceCorrelation.Open(false))
                    {
                        NodeServerTrace.Information("Listening");
                        _PingListener = new EventLoopMessageListener <IncomingMessage>(MessageReceived);
                        Node.MessageProducer.AddMessageListener(_PingListener);
                        try
                        {
                            while (!Cancel.Token.IsCancellationRequested)
                            {
                                PerformanceCounter counter;
                                var message = Message.ReadNext(Socket, Node.Network, Node.Version, Cancel.Token, out counter);

                                Node.LastSeen = DateTimeOffset.UtcNow;
                                Node.MessageProducer.PushMessage(new IncomingMessage()
                                {
                                    Message = message,
                                    Socket  = Socket,
                                    Node    = Node
                                });
                                Node.Counter.Add(counter);
                            }
                        }
                        catch (OperationCanceledException)
                        {
                        }
                        catch (Exception ex)
                        {
                            if (Node.State != NodeState.Disconnecting)
                            {
                                Node.State = NodeState.Failed;
                                NodeServerTrace.Error("Connection to server stopped unexpectedly", ex);
                            }
                        }
                        NodeServerTrace.Information("Stop listening");
                        if (Node.State != NodeState.Failed)
                        {
                            Node.State = NodeState.Offline;
                        }
                        Dispose();

                        _Cancel.Cancel();
                        _Disconnected.Set();
                    }
                }).Start();
            }
コード例 #5
0
ファイル: NodeServer.cs プロジェクト: pkahle/NBitcoin
        public NodeServer(Network network, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION,
                          int internalPort = -1)
        {
            AdvertizeMyself   = false;
            internalPort      = internalPort == -1 ? network.DefaultPort : internalPort;
            _LocalEndpoint    = new IPEndPoint(IPAddress.Parse("0.0.0.0").MapToIPv6(), internalPort);
            _Network          = network;
            _ExternalEndpoint = new IPEndPoint(_LocalEndpoint.Address, Network.DefaultPort);
            _Version          = version;
            var listener = new EventLoopMessageListener <IncomingMessage>(ProcessMessage);

            _MessageProducer.AddMessageListener(listener);
            OwnResource(listener);
            RegisterPeerTableRepository(_PeerTable);
            _Nodes = new NodeSet();
            _Nodes.MessageProducer.AddMessageListener(listener);
            _Trace = new TraceCorrelation(NodeServerTrace.Trace, "Node server listening on " + LocalEndpoint);
        }
コード例 #6
0
        public NodeServer(NetworkInfo network, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION, int internalPort = -1)
        {
            AllowLocalPeers = true;
            InboundNodeConnectionParameters = new NodeConnectionParameters();
            internalPort   = internalPort == -1 ? network.DefaultPort : internalPort;
            _LocalEndpoint = new IPEndPoint(IPAddress.Parse("0.0.0.0").MapToIPv6Ex(), internalPort);
            _Network       = network;
            //_ExternalEndpoint = new IPEndPoint(_LocalEndpoint.Address, Network.DefaultPort); //TODO
            _Version = version;
            var listener = new EventLoopMessageListener <IncomingMessage>(ProcessMessage);

            _MessageProducer.AddMessageListener(listener);
            OwnResource(listener);
            _ConnectedNodes          = new NodesCollection();
            _ConnectedNodes.Added   += _Nodes_NodeAdded;
            _ConnectedNodes.Removed += _Nodes_NodeRemoved;
            _ConnectedNodes.MessageProducer.AddMessageListener(listener);
            _Trace = new TraceCorrelation(NodeServerTrace.Trace, "Node server listening on " + LocalEndpoint);
        }
コード例 #7
0
ファイル: NodeServer.cs プロジェクト: tohidkazemi/NBitcoin
        public NodeServer(Network network, uint?version = null,
                          int internalPort = -1)
        {
            AllowLocalPeers = true;
            InboundNodeConnectionParameters = new NodeConnectionParameters();
            internalPort      = internalPort == -1 ? network.DefaultPort : internalPort;
            _LocalEndpoint    = new IPEndPoint(IPAddress.Parse("0.0.0.0").MapToIPv6Ex(), internalPort);
            MaxConnections    = 125;
            _Network          = network;
            _ExternalEndpoint = new IPEndPoint(_LocalEndpoint.Address, Network.DefaultPort);
            _Version          = version == null ? network.MaxP2PVersion : version.Value;
            var listener = new EventLoopMessageListener <IncomingMessage>(ProcessMessage);

            _MessageProducer.AddMessageListener(listener);
            OwnResource(listener);
            _ConnectedNodes          = new NodesCollection();
            _ConnectedNodes.Added   += _Nodes_NodeAdded;
            _ConnectedNodes.Removed += _Nodes_NodeRemoved;
            _ConnectedNodes.MessageProducer.AddMessageListener(listener);
        }
コード例 #8
0
ファイル: NodeServer.cs プロジェクト: pkahle/NBitcoin
        public IDisposable RegisterBlockRepository(BlockRepository repository)
        {
            var listener = new EventLoopMessageListener <IncomingMessage>((m) =>
            {
                if (m.Node != null)
                {
                    if (m.Message.Payload is HeadersPayload)
                    {
                        foreach (var header in ((HeadersPayload)m.Message.Payload).Headers)
                        {
                            repository.WriteBlockHeader(header);
                        }
                    }
                    if (m.Message.Payload is BlockPayload)
                    {
                        repository.WriteBlock(((BlockPayload)m.Message.Payload).Object);
                    }
                }
            });

            return(new CompositeDisposable(AllMessages.AddMessageListener(listener), OwnResource(listener)));
        }