public void Receive(TcpClient client) { while (listening) { BlockChain newChain = JsonConvert.DeserializeObject <BlockChain>(Chat.Recieve(client)); if (newChain.IsValid() && newChain.Chain.Count > Program.Blockchain.Chain.Count) { List <Transaction> newTransactions = new List <Transaction>(); newTransactions.AddRange(newChain.PendingTransactions); newTransactions.AddRange(Program.Blockchain.PendingTransactions); newChain.PendingTransactions = newTransactions; newChain.PrintExtends(Program.Blockchain); Program.Blockchain = newChain; } } }
public void FinishHandshake(TcpClient client) { string key = ToString(client.Client.RemoteEndPoint); if (!_wsDict.ContainsKey(key)) { string recieved = Chat.Recieve(client); if (recieved == "HISERVER") { Chat.PrintLog("Recieved handshake request!"); Chat.Send(client, "HICLIENT"); } recieved = Chat.Recieve(client); BlockChain newChain = JsonConvert.DeserializeObject <BlockChain>(recieved); if (newChain.IsValid() && newChain.Chain.Count > Program.Blockchain.Chain.Count) { List <Transaction> newTransactions = new List <Transaction>(); newTransactions.AddRange(newChain.PendingTransactions); newTransactions.AddRange(Program.Blockchain.PendingTransactions); newChain.PendingTransactions = newTransactions; newChain.PrintExtends(Program.Blockchain); Program.Blockchain = newChain; Chat.Send(client, "GOODCHAIN"); } else { Chat.Send(client, JsonConvert.SerializeObject(Program.Blockchain)); } _wsDict.Add(key, client); Task.Run(() => Receive(client)); } }