public static string QueryStringSync(string url, HttpMessage request, float timeout = 1) { string jsonModel = JsonHelper.ToJson(request.map); var timepass = new TimePass(timeout); using (WebClient wc = new WebClient()) { //发送到服务端并获得返回值 var awaiter = wc.UploadDataTaskAsync(url, System.Text.Encoding.UTF8.GetBytes(jsonModel)).ConfigureAwait(false).GetAwaiter(); while (!awaiter.IsCompleted) { System.Threading.Thread.Sleep(10); if (timepass.IsPassOnce()) { return(null); } } var str = System.Text.Encoding.UTF8.GetString(awaiter.GetResult()); //把服务端返回的信息转成字符串 return(str); } }
public async void Run() { await Task.Delay(2000); long.TryParse(levelDBStore.Get("UndoHeight"), out transferHeight); Log.Debug($"Consensus.Run at height {transferHeight}"); // 恢复到停机前的高度 ApplyBlockChain(); // 算力统计 calculatePower.Clear(); for (long ii = Math.Max(1, transferHeight - calculatePower.statistic); ii <= transferHeight; ii++) { calculatePower.Insert(BlockChainHelper.GetMcBlock(ii)); } int blockIndex = 0; while (true) { try { if (newBlocks.Count > 0) { for (blockIndex = blockIndex % newBlocks.Count; blockIndex < newBlocks.Count; blockIndex++) { if (newBlocks[blockIndex] == null) { continue; } var ipEndPoint = newBlocks[blockIndex].ipEndPoint; Block otherMcBlk = JsonHelper.FromJson <Block>(newBlocks[blockIndex].block); if (Check(otherMcBlk)) { if (await SyncHeight(otherMcBlk, newBlocks[blockIndex].ipEndPoint)) { newBlocks.RemoveAll((x) => { return(x.ipEndPoint == ipEndPoint); }); break; } //if (await SyncHeight(otherMcBlk, newBlocks[ii].ipEndPoint)) ApplyBlockChain(); } //break; } } ApplyBlockChain(); lock (this) { runAction?.Invoke(); runAction = null; } if (bifurcatedReportTime.IsPassOnce() && bifurcatedReport != "") { Log.Info(bifurcatedReport); bifurcatedReport = ""; } await Task.Delay(1000); } catch (Exception e) { newBlocks.Clear(); Log.Error(e); await Task.Delay(1000); } } }