async Task ConnectAlways() { while (true) { try { if (isDisposed) { return; } if (channel.State == ChannelState.Shutdown) { return; } await channel.ConnectAsync(); if (isDisposed) { return; } var connectionId = (useSameId) ? this.connectionId : connectionIdFactory(); var client = new HeartbeatClient(channel, connectionId); latestStreamingResult.Dispose(); latestStreamingResult = client.Connect(); // wait connect complete. await latestStreamingResult.ResponseStream.MoveNext(); this.connectionId = connectionId; currentRetryCount = 0; waitConnectComplete.TrySetResult(new object()); try { // now channelstate is ready and wait changed. await Task.WhenAny(channel.WaitForStateChangedAsync(ChannelState.Ready), latestStreamingResult.ResponseStream.MoveNext()).ConfigureAwait(false); } finally { waitConnectComplete = new TaskCompletionSource <object>(); foreach (var action in disconnectedActions) { action(); } } } catch (Exception ex) { GrpcEnvironment.Logger.Error(ex, "Reconnect Failed, Retrying:" + currentRetryCount++); } } }
IEnumerator ConnectAlways() { while (true) { if (isDisposed) { yield break; } if (channel.State == ChannelState.Shutdown) { yield break; } var conn = channel.ConnectAsync().ToYieldInstruction(false); yield return(conn); if (isDisposed) { yield break; } if (conn.HasError) { GrpcEnvironment.Logger.Error(conn.Error, "Reconnect Failed, Retrying:" + currentRetryCount++); continue; } var connectionId = (useSameId) ? this.connectionId : connectionIdFactory(); var client = new HeartbeatClient(channel, connectionId); latestStreamingResult.Dispose(); var heartBeatConnect = client.Connect().ToYieldInstruction(false); yield return(heartBeatConnect); if (heartBeatConnect.HasError) { GrpcEnvironment.Logger.Error(heartBeatConnect.Error, "Reconnect Failed, Retrying:" + currentRetryCount++); continue; } else { latestStreamingResult = heartBeatConnect.Result; } var connectCheck = heartBeatConnect.Result.ResponseStream.MoveNext().ToYieldInstruction(false); yield return(connectCheck); if (connectCheck.HasError) { GrpcEnvironment.Logger.Error(heartBeatConnect.Error, "Reconnect Failed, Retrying:" + currentRetryCount++); continue; } this.connectionId = connectionId; currentRetryCount = 0; waitConnectComplete.OnNext(Unit.Default); waitConnectComplete.OnCompleted(); var waitForDisconnect = Observable.Amb(channel.WaitForStateChangedAsync(ChannelState.Ready), heartBeatConnect.Result.ResponseStream.MoveNext()).ToYieldInstruction(false); yield return(waitForDisconnect); try { waitConnectComplete = new AsyncSubject <Unit>(); foreach (var action in disconnectedActions) { action(); } } catch (Exception ex) { GrpcEnvironment.Logger.Error(ex, "Reconnect Failed, Retrying:" + currentRetryCount++); } if (waitForDisconnect.HasError) { GrpcEnvironment.Logger.Error(waitForDisconnect.Error, "Reconnect Failed, Retrying:" + currentRetryCount++); } } }
async Task ConnectAlways() { while (true) { try { if (isDisposed) { return; } if (channel.State == ChannelState.Shutdown) { return; } await channel.ConnectAsync(); if (isDisposed) { return; } var connectionId = (useSameId) ? this.connectionId : connectionIdFactory(); var client = new HeartbeatClient(channel, connectionId); latestStreamingResult.Dispose(); latestStreamingResult = client.Connect(); // wait connect complete. await latestStreamingResult.ResponseStream.MoveNext(); this.connectionId = connectionId; currentRetryCount = 0; waitConnectComplete.TrySetResult(new object()); bool isFinished = false; try { var heartbeat = Task.Run(async() => { try { while (true) { if (isFinished) { return; } await Task.Delay(TimeSpan.FromSeconds(pingSecond)).ConfigureAwait(false); if (isFinished) { return; } await latestStreamingResult.RequestStream.WriteAsync(Nil.Default).ConfigureAwait(false); } } catch { } }); await Task.WhenAny(channel.WaitForStateChangedAsync(ChannelState.Ready), latestStreamingResult.ResponseStream.MoveNext(), heartbeat).ConfigureAwait(false); } finally { isFinished = true; waitConnectComplete = new TaskCompletionSource <object>(); foreach (var action in disconnectedActions) { action(); } } } catch (Exception ex) { GrpcEnvironment.Logger.Error(ex, "Reconnect Failed, Retrying:" + currentRetryCount++); } } }