コード例 #1
0
        public void ConnectTo(NodeAddress address)
        {
            if (cancellationTokenSource.IsCancellationRequested)
            {
                return;
            }

            //todo: The construction of BitcoinEndpoint and BitcoinConnection is confusing. Both can use host and port.
            BitcoinEndpoint endpoint = new BitcoinEndpoint(HandleMessage);
            try
            {
                endpoint.Connect(address.Address.ToString(), address.Port);
            }
            catch (BitcoinNetworkException)
            {
                addressCollection.Reject(address);
                //todo: log error?
                return;
            }

            //todo: check nonce to avoid connection to self
            //todo: check if peer is banned
            //todo: send ping messages to check if connection is alive

            if ((endpoint.PeerInfo.VersionMessage.Services & VersionMessage.ServiceNodeNetwork) == 0)
            {
                // outgoing connections ignore non-full nodes
                addressCollection.Reject(address);
                endpoint.Dispose();
                return;
            }

            addressCollection.Confirm(address);

            if (!connectionCollection.Add(NodeConnectionDirection.Outgoing, endpoint))
            {
                endpoint.Dispose();
                return;
            }

            services.OnNodeConnected(endpoint);
            //todo: make sure that OnNodeConnected is always called before OnNodeDisconnected and before message processing
            endpoint.Disconnected += () => services.OnNodeDisconnected(endpoint);
        }
コード例 #2
0
 public void ProcessMessage(BitcoinEndpoint endpoint, IBitcoinMessage message)
 {
     //todo: add test and catch maybe catch other exceptions
     try
     {
         foreach (NodeServiceInfo serviceInfo in services)
         {
             serviceInfo.Service.ProcessMessage(endpoint, message);
         }
     }
     catch (BitcoinProtocolViolationException e)
     {
         logger.Error(e, "Remote node violated protecol rules ({0}).", endpoint.PeerInfo.IpEndpoint);
         //todo: is this a correct way to disconnect node?
         //todo: ban node ?
         endpoint.Dispose();
     }
 }
コード例 #3
0
        private void HandleIncomingConnection(BitcoinConnection connection)
        {
            BitcoinEndpoint endpoint = new BitcoinEndpoint(HandleMessage);
            endpoint.Connect(connection);

            if (!connectionCollection.Add(NodeConnectionDirection.Incoming, endpoint))
            {
                endpoint.Dispose();
                return;
            }

            services.OnNodeConnected(endpoint);
            //todo: make sure that OnNodeConnected is always called before OnNodeDisconnected and before message processing
            endpoint.Disconnected += () => services.OnNodeDisconnected(endpoint);
        }