internal void ChangeHeight(float v) { var msg = new HeightMsg(); msg.EntityId = Entity.EntityId; msg.Height = v; Sync.Layer.SendMessageToAllAndSelf(ref msg); }
private void sendHeartbeat(string address, int port) { var msg = new HeightMsg(); msg.Height = this.LocalHeight; msg.BlockTime = this.LocalLatestBlockTime; var command = P2PCommand.CreateCommand(this.Identity.ToString(), CommandNames.P2P.Heartbeat, msg); p2pComponent.SendCommand(address, port, command); }
private void receivedGetHeight(string address, int port, int nonce) { var height = this.blockComponent.GetLatestHeight(); var block = this.blockComponent.GetBlockMsgByHeight(height); if (block != null) { var payload = new HeightMsg(); payload.Height = height; payload.BlockTime = block.Header.Timestamp; var cmd = P2PCommand.CreateCommand(this.Identity.ToString(), CommandNames.Block.Height, nonce, payload); this.p2pComponent.SendCommand(address, port, cmd); } }
private void receivedHeightMessage(string address, int port, HeightMsg msg) { //var localHeight = this.blockComponent.GetLatestHeight(); //if(localHeight < msg.Height) //{ // var //} var nodes = this.p2pComponent.GetNodes(); var node = nodes.Where(n => n.IP == address && n.Port == port).FirstOrDefault(); if (node != null) { node.LatestHeight = msg.Height; node.LatestBlockTime = msg.BlockTime; } }
private void heartbeatMsgHandle(P2PState state) { var peer = this.p2pComponent.GetNodes().Where(p => p.IP == state.IP && p.Port == state.Port).FirstOrDefault(); if (peer != null && peer.IsConnected) { var payload = new HeightMsg(); int index = 0; try { payload.Deserialize(state.Command.Payload, ref index); peer.LatestHeight = payload.Height; peer.LatestBlockTime = payload.BlockTime; } catch { } peer.LastHeartbeat = Time.EpochTime; } }
//private void processLongTimeCommand() //{ // LogHelper.Warn("Thread : In to thread threadProcessLongTimeCommand's process action : processLongTimeCommand"); // P2PState state = default(P2PState); // //System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); // while (this.isRunning) // { // if (longTimeCommandQueue.TryDequeue(out state)) // { // if(state != null) // { // try // { // //stopwatch.Reset(); // //stopwatch.Start(); // int index = 0; // switch (state.Command.CommandName) // { // case CommandNames.Block.GetHeaders: // var getHeadersMsg = new GetHeadersMsg(); // getHeadersMsg.Deserialize(state.Command.Payload, ref index); // this.receivedGetHeaders(state.IP, state.Port, getHeadersMsg, state.Command.Nonce); // break; // case CommandNames.Block.GetBlocks: // var getBlocksMsg = new GetBlocksMsg(); // getBlocksMsg.Deserialize(state.Command.Payload, ref index); // this.receivedGetBlocks(state.IP, state.Port, getBlocksMsg, state.Command.Nonce); // break; // default: // break; // } // //stopwatch.Stop(); // //LogHelper.Warn($"processLongTimeCommand : {state.Command.CommandName} -- {stopwatch.ElapsedMilliseconds} , longTimeCommandQueue count = {longTimeCommandQueue.Count()}" ); // } // catch (Exception ex) // { // LogHelper.Error(ex.ToString()); // //LogHelper.Warn($"processLongTimeCommand : {state.Command.CommandName} -- {stopwatch.ElapsedMilliseconds} , longTimeCommandQueue count = {longTimeCommandQueue.Count()} -- Exception msg: {ex.ToString()} "); // } // } // Thread.Sleep(10); // } // else // { // Thread.Sleep(50); // } // } // LogHelper.Warn("Thread : out of thread threadProcessLongTimeCommand's process action : processLongTimeCommand"); //} #endregion private void dataReceived(P2PState state) { int index = 0; switch (state.Command.CommandName) { case CommandNames.Transaction.GetTxPool: this.receivedGetTransactionPool(state.IP, state.Port, state.Command.Nonce); break; case CommandNames.Transaction.TxPool: var txPoolMsg = new TxPoolMsg(); txPoolMsg.Deserialize(state.Command.Payload, ref index); this.receivedTransacitonPoolMessage(state.IP, state.Port, txPoolMsg); break; case CommandNames.Transaction.GetTx: var getTxMsg = new GetTxsMsg(); getTxMsg.Deserialize(state.Command.Payload, ref index); this.receivedGetTransaction(state.IP, state.Port, getTxMsg, state.Command.Nonce); break; case CommandNames.Transaction.Tx: var txsMsg = new TxsMsg(); txsMsg.Deserialize(state.Command.Payload, ref index); this.receivedTransactionMessage(state.IP, state.Port, txsMsg); break; case CommandNames.Transaction.NewTx: var newTxMsg = new NewTxMsg(); newTxMsg.Deserialize(state.Command.Payload, ref index); this.receivedNewTransactionMessage(state.IP, state.Port, newTxMsg); break; case CommandNames.Block.GetHeight: this.receivedGetHeight(state.IP, state.Port, state.Command.Nonce); break; case CommandNames.Block.Height: var heightMsg = new HeightMsg(); heightMsg.Deserialize(state.Command.Payload, ref index); this.receivedHeightMessage(state.IP, state.Port, heightMsg); break; case CommandNames.Block.GetHeaders: var getHeadersMsg = new GetHeadersMsg(); getHeadersMsg.Deserialize(state.Command.Payload, ref index); this.receivedGetHeaders(state.IP, state.Port, getHeadersMsg, state.Command.Nonce); //longTimeCommandQueue.Enqueue(state); break; case CommandNames.Block.Headers: var headersMsg = new HeadersMsg(); headersMsg.Deserialize(state.Command.Payload, ref index); this.receivedHeadersMessage(state.IP, state.Port, headersMsg); break; case CommandNames.Block.GetBlocks: var getBlocksMsg = new GetBlocksMsg(); getBlocksMsg.Deserialize(state.Command.Payload, ref index); this.receivedGetBlocks(state.IP, state.Port, getBlocksMsg, state.Command.Nonce); //longTimeCommandQueue.Enqueue(state); break; case CommandNames.Block.Blocks: var blocksMsg = new BlocksMsg(); blocksMsg.Deserialize(state.Command.Payload, ref index); this.receivedBlocksMessage(state.IP, state.Port, blocksMsg); break; case CommandNames.Block.NewBlock: var newBlockMsg = new NewBlockMsg(); newBlockMsg.Deserialize(state.Command.Payload, ref index); this.receivedNewBlockMessage(state.IP, state.Port, newBlockMsg, state.Command.Nonce); break; case CommandNames.MiningPool.GetMiningPools: this.receivedGetMiningPoolsMessage(state.IP, state.Port); break; case CommandNames.MiningPool.MiningPools: var miningPoolMsg = new MiningPoolMsg(); miningPoolMsg.Deserialize(state.Command.Payload, ref index); receivedMiningPoolsMessage(miningPoolMsg); break; case CommandNames.MiningPool.NewMiningPool: var newMiningPoolMsg = new NewMiningPoolMsg(); newMiningPoolMsg.Deserialize(state.Command.Payload, ref index); this.receivedNewMiningPoolMessage(state, newMiningPoolMsg); break; case CommandNames.Other.Reject: case CommandNames.Other.NotFound: default: break; } }
private void peerCheckTimer_Elapsed(object sender, ElapsedEventArgs e) { for (int i = Peers.Count - 1; i >= 0; i--) { var peer = Peers[i]; var nowTime = Time.EpochTime; //check unconnected peers if (!peer.IsConnected) { if ((peer.ConnectedTime > nowTime || (nowTime - peer.ConnectedTime) > 1 * 60 * 1000)) { Peers.RemoveAt(i); this.receivedMessageBuffer.Remove(peer.IP + ":" + peer.Port); } } else { //check long time not received heartbeat peers if (peer.LastHeartbeat < nowTime && (nowTime - peer.LastHeartbeat) > 10 * 60 * 1000) { peer.IsConnected = false; this.raiseNodeConnectionStateChanged(peer); Peers.RemoveAt(i); this.receivedMessageBuffer.Remove(peer.IP + ":" + peer.Port); } else if (this.isTrackerNode) { var msg = new HeightMsg(); msg.Height = this.LastBlockHeight; msg.BlockTime = this.LastBlockTime; var command = P2PCommand.CreateCommand(CommandNames.P2P.Heartbeat, msg); this.Send(command, peer.IP, peer.Port); } } } if (!this.isTrackerNode) { //reconnect with tracker nodes if (this.Peers.Count == 0 || this.Peers.Where(p => p.IsTrackerServer && p.IsConnected).Count() == 0) { var trackerNode = this.Peers.Where(t => t.IsTrackerServer).FirstOrDefault(); if (trackerNode != null) { this.Peers.Remove(trackerNode); this.receivedMessageBuffer.Remove(trackerNode.IP + ":" + trackerNode.Port); } string trackerIp = GlobalParameters.IsTestnet ? Resource.TestnetTrackerServer : Resource.MainnetTrackerServer; int trackerPort = int.Parse(GlobalParameters.IsTestnet ? Resource.TestnetPort : Resource.MainnetPort); this.ConnectToNewPeer(trackerIp, trackerPort, true); } //get new node address from tracker else if (this.Peers.Count() < this.maxConnections) { var tracker = this.Peers.Where(p => p.IsTrackerServer && p.IsConnected).FirstOrDefault(); if (tracker != null) { var payload = new GetAddrMsg(); payload.Count = this.maxConnections; var command = P2PCommand.CreateCommand(CommandNames.P2P.GetAddr, payload); this.Send(command, tracker.IP, tracker.Port); } } } }
private void dataReceived(P2PState state) { int index = 0; switch (state.Command.CommandName) { //case CommandNames.P2P.GetAddr: // this.getAddrMsgHandle(state); // break; //case CommandNames.P2P.Addr: // this.addrMsgHandle(state); // break; //case CommandNames.P2P.Heartbeat: // this.heartbeatMsgHandle(state); // break; case CommandNames.Transaction.GetTxPool: this.receivedGetTransactionPool(state.IP, state.Port, state.Command.Nonce); break; case CommandNames.Transaction.TxPool: var txPoolMsg = new TxPoolMsg(); txPoolMsg.Deserialize(state.Command.Payload, ref index); this.receivedTransacitonPoolMessage(state.IP, state.Port, txPoolMsg); break; case CommandNames.Transaction.GetTx: var getTxMsg = new GetTxsMsg(); getTxMsg.Deserialize(state.Command.Payload, ref index); this.receivedGetTransaction(state.IP, state.Port, getTxMsg, state.Command.Nonce); break; case CommandNames.Transaction.Tx: var txsMsg = new TxsMsg(); txsMsg.Deserialize(state.Command.Payload, ref index); this.receivedTransactionMessage(state.IP, state.Port, txsMsg); break; case CommandNames.Transaction.NewTx: var newTxMsg = new NewTxMsg(); newTxMsg.Deserialize(state.Command.Payload, ref index); this.receivedNewTransactionMessage(state.IP, state.Port, newTxMsg); break; case CommandNames.Block.GetHeight: this.receivedGetHeight(state.IP, state.Port, state.Command.Nonce); break; case CommandNames.Block.Height: var heightMsg = new HeightMsg(); heightMsg.Deserialize(state.Command.Payload, ref index); this.receivedHeightMessage(state.IP, state.Port, heightMsg); break; case CommandNames.Block.GetHeaders: var getHeadersMsg = new GetHeadersMsg(); getHeadersMsg.Deserialize(state.Command.Payload, ref index); this.receivedGetHeaders(state.IP, state.Port, getHeadersMsg, state.Command.Nonce); break; case CommandNames.Block.Headers: var headersMsg = new HeadersMsg(); headersMsg.Deserialize(state.Command.Payload, ref index); this.receivedHeadersMessage(state.IP, state.Port, headersMsg); break; case CommandNames.Block.GetBlocks: var getBlocksMsg = new GetBlocksMsg(); getBlocksMsg.Deserialize(state.Command.Payload, ref index); this.receivedGetBlocks(state.IP, state.Port, getBlocksMsg, state.Command.Nonce); break; case CommandNames.Block.Blocks: var blocksMsg = new BlocksMsg(); blocksMsg.Deserialize(state.Command.Payload, ref index); this.receivedBlocksMessage(state.IP, state.Port, blocksMsg); break; case CommandNames.Block.NewBlock: var newBlockMsg = new NewBlockMsg(); newBlockMsg.Deserialize(state.Command.Payload, ref index); this.receivedNewBlockMessage(state.IP, state.Port, newBlockMsg, state.Command.Nonce); break; case CommandNames.Other.Reject: break; case CommandNames.Other.NotFound: break; default: break; } }
static void OnChangeHeight(MySyncMotorSuspension sync, ref HeightMsg msg, MyNetworkClient sender) { sync.Entity.Height = msg.Height; }