public void Test()
        {
            byte[] hello = new byte[]
                           {
                               0x62, 0xea, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0xb2, 0xd0, 0x50,
                               0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                               0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x2e, 0xb3, 0x5d, 0x8c, 0xe6, 0x17, 0x65,
                               0x0f, 0x2f, 0x53, 0x61, 0x74, 0x6f, 0x73, 0x68, 0x69, 0x3a, 0x30, 0x2e, 0x37, 0x2e, 0x32, 0x2f,
                               0xc0, 0x3e, 0x03, 0x00
                           };

            using (BitcoinConnection conn = new BitcoinConnection())
            {
                conn.Connect("localhost", 8333);

                conn.WriteMessage(new BitcoinMessage("version", hello));

                BitcoinMessage incVersionMessage = conn.ReadMessage();
                Assert.That(incVersionMessage.Command, Is.EqualTo(VersionMessage.Command));

                conn.WriteMessage(new BitcoinMessage(VerAckMessage.Command, new byte[0]));

                BitcoinMessage incVerAckMessage = conn.ReadMessage();
                Assert.That(incVerAckMessage.Command, Is.EqualTo(VerAckMessage.Command));

                BitcoinMessage incPing = conn.ReadMessage();
                Assert.That(incPing.Command, Is.EqualTo(PingMessage.Command));
            }
        }
 private void Listen()
 {
     while (running)
     {
         TcpClient tcpClient;
         try
         {
             tcpClient = tcpListener.AcceptTcpClient();
         }
         catch (SocketException)
         {
             //todo: this happens when listener is stopped, can it happen for other reasons?
             return;
         }
         BitcoinConnection conn = new BitcoinConnection(tcpClient);
         connectionHandler(conn);
     }
 }
        private void DeadlockTestConnectionHandler(BitcoinConnection connection)
        {
            using (BitcoinEndpoint endpoint = new BitcoinEndpoint(DeadlockTestMessageHandler))
            {
                endpoint.Connect(connection);

                SendDeadlockTestSequence(endpoint, "server");

                //let other peer to send remaining data
                Thread.Sleep(1000);
            }
        }
        private void ConnectionHandler(BitcoinConnection connection)
        {
            using (BitcoinEndpoint endpoint = new BitcoinEndpoint(LoggingMessageHandler))
            {
                endpoint.Connect(connection);

                Thread.Sleep(1000);
                endpoint.WriteMessage(new BitcoinMessage(InvMessage.Command, new byte[0]));
            }
        }
Пример #5
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);
        }
        /// <summary>
        /// Connects to a remote host using an established connection and sends a version handshake.
        /// </summary>
        /// <param name="connection">The established network connection.</param>
        /// <exception cref="BitcoinNetworkException">Connection failed.</exception>
        public void Connect(BitcoinConnection connection)
        {
            if (conn != null)
            {
                throw new BitcoinNetworkException("Connection was already established.");
            }

            conn = connection;

            Start();
        }
        /// <summary>
        /// Connects to a remote host and sends a version handshake.
        /// </summary>
        /// <param name="host">The DNS name of the remote host.</param>
        /// <param name="port">The port number of the remote host.</param>
        /// <exception cref="BitcoinNetworkException">Connection failed.</exception>
        public void Connect(string host, int port)
        {
            if (conn != null)
            {
                throw new BitcoinNetworkException("Connection was already established.");
            }

            conn = new BitcoinConnection();
            conn.Connect(host, port);

            Start();
        }