Exemplo n.º 1
0
        // Starts the Network Client Manager. First it connects to one of the seed nodes in order to fetch the Presence List.
        // Afterwards, it starts the reconnect and keepalive threads
        public static void start()
        {
            if (running)
            {
                return;
            }

            if (CoreConfig.preventNetworkOperations)
            {
                Logging.warn("Not starting NetworkClientManager thread due to preventNetworkOperations flag being set.");
                return;
            }

            running        = true;
            networkClients = new List <NetworkClient>();

            PeerStorage.readPeersFile();

            // Now add the seed nodes to the list
            foreach (string[] addr in CoreNetworkUtils.getSeedNodes(CoreConfig.isTestNet))
            {
                byte[] wallet_addr = null;
                if (addr[1] != null)
                {
                    wallet_addr = Base58Check.Base58CheckEncoding.DecodePlain(addr[1]);
                }
                PeerStorage.addPeerToPeerList(addr[0], wallet_addr, false);
            }

            // Connect to a random node first
            bool firstSeedConnected = false;

            while (firstSeedConnected == false && IxianHandler.forceShutdown == false)
            {
                Peer p = PeerStorage.getRandomMasterNodeAddress();
                if (p != null)
                {
                    firstSeedConnected = connectTo(p.hostname, p.walletAddress);
                }
                if (firstSeedConnected == false)
                {
                    Thread.Sleep(1000);
                }
            }

            // Start the reconnect thread
            TLC                  = new ThreadLiveCheck();
            reconnectThread      = new Thread(reconnectClients);
            reconnectThread.Name = "Network_Client_Manager_Reconnect";
            autoReconnect        = true;
            reconnectThread.Start();
        }
Exemplo n.º 2
0
        // Starts the Network Client Manager.
        // If connections_to_wait_for parameter is bigger than 0, it waits until it connects to the specified number of nodes.
        // Afterwards, it starts the reconnect and keepalive threads
        public static void start(int connections_to_wait_for = 0)
        {
            if (running)
            {
                return;
            }

            if (CoreConfig.preventNetworkOperations)
            {
                Logging.warn("Not starting NetworkClientManager thread due to preventNetworkOperations flag being set.");
                return;
            }

            running           = true;
            networkClients    = new List <NetworkClient>();
            connectingClients = new List <string>();

            PeerStorage.readPeersFile();

            // Now add the seed nodes to the list
            foreach (string[] addr in CoreNetworkUtils.getSeedNodes(IxianHandler.networkType))
            {
                byte[] wallet_addr = null;
                if (addr[1] != null)
                {
                    wallet_addr = Base58Check.Base58CheckEncoding.DecodePlain(addr[1]);
                }
                PeerStorage.addPeerToPeerList(addr[0], wallet_addr, Clock.getTimestamp(), 0, 1, 0, false);
            }

            if (connections_to_wait_for > 0)
            {
                Random rnd = new Random();
                // Connect to a random node first
                int i = 0;
                while (getConnectedClients(true).Count() < connections_to_wait_for && IxianHandler.forceShutdown == false)
                {
                    new Thread(() =>
                    {
                        reconnectClients(rnd);
                    }).Start();
                    i++;
                    if (i > 10)
                    {
                        i = 0;
                        Thread.Sleep(1000);
                    }
                    else
                    {
                        Thread.Sleep(200);
                    }
                    if (!running)
                    {
                        return;
                    }
                }
            }

            // Start the reconnect thread
            TLC = new ThreadLiveCheck();

            autoReconnect        = true;
            reconnectThread      = new Thread(reconnectLoop);
            reconnectThread.Name = "Network_Client_Manager_Reconnect";
            reconnectThread.Start();
        }