/// <summary> /// Listen Blockchain packet. /// </summary> public static void ListenBlockchain() { if (ThreadListenBlockchain != null && (ThreadListenBlockchain.IsAlive || ThreadListenBlockchain != null)) { ThreadListenBlockchain.Abort(); GC.SuppressFinalize(ThreadListenBlockchain); } ThreadListenBlockchain = new Thread(async delegate() { while (IsConnected && !Program.Exit) { try { string packet = await classSeedNodeConnector.ReceivePacketFromSeedNodeAsync(Program.Certificate, false, true).ConfigureAwait(false); if (packet == ClassSeedNodeStatus.SeedError) { ClassLog.ConsoleWriteLog("Connection to network lost, reconnect in 5 seconds..", ClassLogEnumeration.IndexPoolGeneralErrorLog, ClassLogConsoleEnumeration.IndexPoolConsoleRedLog, true); IsConnected = false; break; } _lastPacketReceivedFromBlockchain = DateTimeOffset.Now.ToUnixTimeSeconds(); if (packet.Contains("*")) { var splitPacket = packet.Split(new[] { "*" }, StringSplitOptions.None); if (splitPacket.Length > 1) { foreach (var packetEach in splitPacket) { if (packetEach != null) { if (!string.IsNullOrEmpty(packetEach)) { await HandlePacketNetworkAsync(packetEach.Replace("*", "")); } } } } else { await HandlePacketNetworkAsync(packet.Replace("*", "")); } } else { await HandlePacketNetworkAsync(packet); } } catch { ClassLog.ConsoleWriteLog("Connection to network lost, reconnect in 5 seconds..", ClassLogEnumeration.IndexPoolGeneralErrorLog, ClassLogConsoleEnumeration.IndexPoolConsoleRedLog, true); IsConnected = false; break; } } }); ThreadListenBlockchain.Start(); }
/// <summary> /// Receive packet from seed nodes for wallet. /// </summary> /// <returns></returns> public async Task <string> ListenPacketWalletAsync(string certificate, bool isEncrypted) { string packet = string.Empty; try { packet = await _seedNodeConnector.ReceivePacketFromSeedNodeAsync(certificate, false, isEncrypted); if (packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletSendRemoteNode) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultMaxSupply) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultCoinCirculating) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultNetworkDifficulty) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultNetworkHashrate) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultTotalBlockMined) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultTotalTransactionFee) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultTotalPendingTransaction) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultBlockPerId)) { return(packet); } if (WalletPhase != ClassWalletPhase.Create) { if (packet != ClassSeedNodeStatus.SeedNone && packet != ClassSeedNodeStatus.SeedError) { if (packet.Contains("*")) { var splitPacket = packet.Split(new[] { "*" }, StringSplitOptions.None); var packetCompleted = string.Empty; foreach (var packetEach in splitPacket) { if (packetEach != null) { if (!string.IsNullOrEmpty(packetEach)) { if (packetEach.Length > 1) { packetCompleted += DecryptPacketWallet(packetEach.Replace("*", "")) + "*"; } } } } return(packetCompleted); } return(DecryptPacketWallet(packet)); } } } catch { return(ClassSeedNodeStatus.SeedError); } return(packet); }
/// <summary> /// Receive packet from seed nodes for wallet. /// </summary> /// <returns></returns> public async Task <string> ListenPacketWalletAsync(string certificate, bool isEncrypted) { string packet = string.Empty; try { if (WalletPhase == ClassWalletPhase.Create || WalletPhase == string.Empty) // Not allow to create a wallet on non-permanent seed nodes. { if (ClassConnectorSetting.SeedNodeIp.ContainsKey(_seedNodeConnector.ReturnCurrentSeedNodeHost())) { if (!ClassConnectorSetting.SeedNodeIp[_seedNodeConnector.ReturnCurrentSeedNodeHost()].Item2) { return(ClassSeedNodeStatus.SeedError); } } else { return(ClassSeedNodeStatus.SeedError); } } packet = await _seedNodeConnector.ReceivePacketFromSeedNodeAsync(certificate, false, isEncrypted); if (packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletSendRemoteNode) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultMaxSupply) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultCoinCirculating) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultNetworkDifficulty) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultNetworkHashrate) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultTotalBlockMined) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultTotalTransactionFee) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultTotalPendingTransaction) || packet.Contains(ClassSeedNodeCommand.ClassReceiveSeedEnumeration.WalletResultBlockPerId)) { return(packet); } if (WalletPhase != ClassWalletPhase.Create) { if (packet != ClassSeedNodeStatus.SeedNone && packet != ClassSeedNodeStatus.SeedError) { if (packet.Contains(ClassConnectorSetting.PacketSplitSeperator)) { var splitPacket = packet.Split(new[] { ClassConnectorSetting.PacketSplitSeperator }, StringSplitOptions.None); var packetCompleted = string.Empty; foreach (var packetEach in splitPacket) { if (!string.IsNullOrEmpty(packetEach)) { if (packetEach.Length > 1) { if (packetEach.Replace(ClassConnectorSetting.PacketSplitSeperator, "") != ClassWalletCommand.ClassWalletReceiveEnumeration.WalletInvalidPacket) { packetCompleted += DecryptPacketWallet(packetEach.Replace(ClassConnectorSetting.PacketSplitSeperator, "")) + ClassConnectorSetting.PacketSplitSeperator; } else { packetCompleted += packetEach.Replace(ClassConnectorSetting.PacketSplitSeperator, "") + ClassConnectorSetting.PacketSplitSeperator; } } } } return(packetCompleted); } if (packet.Replace(ClassConnectorSetting.PacketSplitSeperator, "") != ClassWalletCommand.ClassWalletReceiveEnumeration.WalletInvalidPacket) { return(DecryptPacketWallet(packet)); } return(ClassWalletCommand.ClassWalletReceiveEnumeration.WalletInvalidPacket); } } } catch { return(ClassSeedNodeStatus.SeedError); } return(packet); }
/// <summary> /// Listen Blockchain packet. /// </summary> public static void ListenBlockchain() { if (ThreadListenBlockchain != null && (ThreadListenBlockchain.IsAlive || ThreadListenBlockchain != null)) { ThreadListenBlockchain.Abort(); GC.SuppressFinalize(ThreadListenBlockchain); } ThreadListenBlockchain = new Thread(async delegate() { while (IsConnected) { try { using (CancellationTokenSource cancellation = new CancellationTokenSource(100)) { string packet = await classSeedNodeConnector.ReceivePacketFromSeedNodeAsync(Program.NetworkCertificate, false, true); if (packet == ClassSeedNodeStatus.SeedError) { ConsoleLog.WriteLine("Connection to network lost, reconnect in 5 seconds..", ClassConsoleColorEnumeration.IndexConsoleRedLog); IsConnected = false; break; } _lastPacketReceivedFromBlockchain = DateTimeOffset.Now.ToUnixTimeSeconds(); if (packet.Contains("*")) { var splitPacket = packet.Split(new[] { "*" }, StringSplitOptions.None); if (splitPacket.Length > 1) { foreach (var packetEach in splitPacket) { if (packetEach != null) { if (!string.IsNullOrEmpty(packetEach)) { if (!await HandlePacketBlockchainAsync(packetEach.Replace("*", ""))) { IsConnected = false; } } } } } else { if (!await HandlePacketBlockchainAsync(packet.Replace("*", ""))) { IsConnected = false; } } } else { if (!await HandlePacketBlockchainAsync(packet)) { IsConnected = false; } } } } catch { ConsoleLog.WriteLine("Connection to network lost, reconnect in 5 seconds..", ClassConsoleColorEnumeration.IndexConsoleRedLog); IsConnected = false; break; } } }); ThreadListenBlockchain.Start(); }