Exemplo n.º 1
0
        /// <summary>
        /// Listen the blockchain network.
        /// </summary>
        public void ListenBlockchainNetworkWallet()
        {
            if (ThreadListenBlockchainNetwork != null && (ThreadListenBlockchainNetwork.IsAlive || ThreadListenBlockchainNetwork != null))
            {
                ThreadListenBlockchainNetwork.Abort();
                GC.SuppressFinalize(ThreadListenBlockchainNetwork);
            }
            ThreadListenBlockchainNetwork = new Thread(async delegate()
            {
                while (SeedNodeConnector.ReturnStatus())
                {
                    string packetWallet = await WalletConnector.ListenPacketWalletAsync(CertificateConnection, true);
                    if (packetWallet.Contains("*")) // Character separator.
                    {
                        var splitPacket = packetWallet.Split(new[] { "*" }, StringSplitOptions.None);
                        foreach (var packetEach in splitPacket)
                        {
                            if (packetEach != null)
                            {
                                if (!string.IsNullOrEmpty(packetEach))
                                {
                                    if (packetEach.Length > 1)
                                    {
                                        if (packetEach == ClassAlgoErrorEnumeration.AlgoError)
                                        {
                                            WalletCreateResult = ClassWalletCreatorEnumeration.WalletCreatorError;
                                            break;
                                        }

                                        await Task.Factory.StartNew(() => HandlePacketBlockchainNetworkWallet(packetEach.Replace("*", "")), CancellationToken.None, TaskCreationOptions.RunContinuationsAsynchronously, TaskScheduler.Current).ConfigureAwait(false);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (packetWallet == ClassAlgoErrorEnumeration.AlgoError)
                        {
                            WalletCreateResult = ClassWalletCreatorEnumeration.WalletCreatorError;
                            break;
                        }

                        await Task.Factory.StartNew(() => HandlePacketBlockchainNetworkWallet(packetWallet), CancellationToken.None, TaskCreationOptions.RunContinuationsAsynchronously, TaskScheduler.Current).ConfigureAwait(false);
                    }
                }
            });
            ThreadListenBlockchainNetwork.Start();
        }
Exemplo n.º 2
0
        private static void CheckBlockchainConnection()
        {
            _lastPacketReceivedFromBlockchain = DateTimeOffset.Now.ToUnixTimeSeconds();
            var threadCheckConnection = new Thread(async delegate()
            {
                while (!Program.Exit)
                {
                    Thread.Sleep(1000);
                    if (!IsConnected || !classSeedNodeConnector.ReturnStatus())
                    {
                        if (ThreadListenBlockchain != null && (ThreadListenBlockchain.IsAlive || ThreadListenBlockchain != null))
                        {
                            ThreadListenBlockchain.Abort();
                            GC.SuppressFinalize(ThreadListenBlockchain);
                        }
                        if (ThreadAskMiningMethod != null && (ThreadAskMiningMethod.IsAlive || ThreadAskMiningMethod != null))
                        {
                            ThreadAskMiningMethod.Abort();
                            GC.SuppressFinalize(ThreadAskMiningMethod);
                        }
                        ClassMiningPoolGlobalStats.CurrentBlockTemplate = string.Empty;
                        ClassMiningPoolGlobalStats.CurrentBlockId       = string.Empty;
                        IsConnected   = false;
                        LoginAccepted = false;
                        while (!await ConnectToBlockchainAsync())
                        {
                            ClassLog.ConsoleWriteLog("Can't connect to the network, retry in 5 seconds..", ClassLogEnumeration.IndexPoolGeneralErrorLog, ClassLogConsoleEnumeration.IndexPoolConsoleRedLog, true);
                            Thread.Sleep(5000);
                        }
                        ClassLog.ConsoleWriteLog("Connection success, generate dynamic certificate for the network.", ClassLogEnumeration.IndexPoolGeneralErrorLog, ClassLogConsoleEnumeration.IndexPoolConsoleYellowLog, true);
                        Program.Certificate = ClassUtils.GenerateCertificate();
                        ClassLog.ConsoleWriteLog("Certificate generate, send to the network..", ClassLogEnumeration.IndexPoolGeneralErrorLog, ClassLogConsoleEnumeration.IndexPoolConsoleYellowLog, true);
                        if (!await SendPacketToNetworkBlockchain(Program.Certificate, false))
                        {
                            ClassLog.ConsoleWriteLog("Can't send certificate, reconnect now..", ClassLogEnumeration.IndexPoolGeneralErrorLog, ClassLogConsoleEnumeration.IndexPoolConsoleRedLog, true);
                            IsConnected = false;
                        }
                        else
                        {
                            Thread.Sleep(1000);
                            ClassLog.ConsoleWriteLog("Certificate sent, start to login..", ClassLogEnumeration.IndexPoolGeneralErrorLog, ClassLogConsoleEnumeration.IndexPoolConsoleYellowLog, true);
                            ListenBlockchain();
                            if (!await SendPacketToNetworkBlockchain(ClassConnectorSettingEnumeration.MinerLoginType + "|" + MiningPoolSetting.MiningPoolWalletAddress, true))
                            {
                                ClassLog.ConsoleWriteLog("Can't login to the network, reconnect now.", ClassLogEnumeration.IndexPoolGeneralErrorLog, ClassLogConsoleEnumeration.IndexPoolConsoleRedLog, true);
                                IsConnected = false;
                            }
                            else
                            {
                                ClassLog.ConsoleWriteLog("Login successfully sent, waiting confirmation.. (Wait 5 seconds maximum.)", ClassLogEnumeration.IndexPoolGeneralErrorLog, ClassLogConsoleEnumeration.IndexPoolConsoleGreenLog, true);
                                IsConnected = true;
                                Thread.Sleep(ClassConnectorSetting.MaxTimeoutConnect);
                                if (!LoginAccepted)
                                {
                                    IsConnected = false;
                                }
                            }
                        }
                    }
                }
            });

            threadCheckConnection.Start();
        }