/// <summary> /// Task to read response in loop. /// </summary> /// <param name="call"></param> /// <returns></returns> private Task ReadResponse(AsyncDuplexStreamingCall <RequestBlockInfo, TResponse> call) { var responseReaderTask = Task.Run(async() => { while (await call.ResponseStream.MoveNext()) { var response = call.ResponseStream.Current; // request failed or useless response if (!response.Success) { _realInterval = AdjustInterval(); continue; } if (response.Height != _next || !ToBeIndexedInfoQueue.TryAdd(response.BlockInfoResult)) { continue; } _next++; _realInterval = _interval; _logger?.Trace($"Received response from chain {response.BlockInfoResult.ChainId} at height {response.Height}"); } }); return(responseReaderTask); }
/// <summary> /// Start to request only once but response many (one by one) /// </summary> /// <param name="next"></param> /// <returns></returns> public async Task StartServerStreamingCall(ulong next) { _next = Math.Max(next, ToBeIndexedInfoQueue.Last()?.Height ?? -1 + 1); try { var request = new RequestBlockInfo { ChainId = Hash.LoadHex(ChainConfig.Instance.ChainId), NextHeight = ToBeIndexedInfoQueue.Count == 0 ? _next : ToBeIndexedInfoQueue.Last().Height + 1 }; using (var call = Call(request)) { while (await call.ResponseStream.MoveNext()) { var response = call.ResponseStream.Current; // request failed or useless response if (!response.Success || response.Height != _next) { continue; } if (ToBeIndexedInfoQueue.TryAdd(response.BlockInfoResult)) { _next++; } } } } catch (RpcException e) { _logger.Error(e); throw; } }