private async Task ReconnectAsync(bool isReconnect) { var connectAttempter = new Attempter(3); while (connectAttempter.Attempt()) { this.mpcConnectionReporter?.Connecting(isReconnect, connectAttempter.CurrentAttempt); await this.DisconnectAsync(false); this.tcpClient = new TcpClient(); await this.tcpClient.ConnectAsync(this.server.Address, this.server.Port); if (this.tcpClient.Connected) { this.mpcConnectionReporter?.ConnectionAccepted(isReconnect, connectAttempter.CurrentAttempt); break; } } this.networkStream = this.tcpClient.GetStream(); using (var reader = new StreamReader(this.networkStream, Encoding, true, 512, true)) { var firstLine = await reader.ReadLineAsync(); if (!firstLine.StartsWith(Constants.FirstLinePrefix)) { await this.DisconnectAsync(false); throw new MpcConnectException("Response of mpd does not start with \"" + Constants.FirstLinePrefix + "\"."); } this.Version = firstLine.Substring(Constants.FirstLinePrefix.Length); this.mpcConnectionReporter?.Connected(isReconnect, connectAttempter.CurrentAttempt, firstLine); } }
public void Attempt_Then_ResultsShouldEqualExpectedResults() { var expectedResults = new[] { true, true, false }; var testee = new Attempter(2); var results = new List <bool>(); foreach (var ignored in expectedResults) { results.Add(testee.Attempt()); } results.Should().Equal(expectedResults); }
private void RefreshBuildStatus(object state) { var attempter = new Attempter(5); var cancellationToken = this.cancellationTokenSource.Token; try { while (!cancellationToken.IsCancellationRequested) { try { var currentBuildStatus = this.GetStatus(this.buildTypeId); if (currentBuildStatus.HasPendingBuild != this.lastBuildStatus.HasPendingBuild || currentBuildStatus.LastBuildStatus != this.lastBuildStatus.LastBuildStatus) { this.BuildStatusChanged?.Invoke(this, currentBuildStatus); } this.lastBuildStatus = currentBuildStatus; attempter.Reset(5); this.manualResetEvent.WaitOne(this.refreshInterval); } catch (OperationCanceledException) { throw; } catch (Exception e) { if (!attempter.Attempt()) { this.teamCityBuildStatusProviderReporter?.RefreshBuildStatusFailed(attempter.MaxAttempts, e); this.manualResetEvent.WaitOne(TimeSpan.FromSeconds(20)); attempter.Reset(); } else { this.manualResetEvent.WaitOne(this.refreshInterval); } } } } catch (OperationCanceledException) { } }
/// <summary> /// Sends the command asynchronously. /// </summary> /// <typeparam name="TResponse">The response type.</typeparam> /// <param name="mpcCommand">The MPC command.</param> /// <returns> /// The send task. /// </returns> public async Task <IMpdMessage <TResponse> > SendAsync <TResponse>(IMpcCommand <TResponse> mpcCommand) { if (this.tcpClient == null) { await this.ReconnectAsync(true); } if (mpcCommand == null) { throw new CommandNullException(); } Exception lastException = null; IReadOnlyList <string> response = new List <string>(); var sendAttempter = new Attempter(3); var commandText = mpcCommand.Serialize(); this.mpcConnectionReporter?.Sending(commandText); while (sendAttempter.Attempt()) { try { using (var writer = new StreamWriter(this.networkStream, Encoding, 512, true) { NewLine = "\n" }) { await writer.WriteLineAsync(commandText); await writer.FlushAsync(); } response = await this.ReadResponseAsync(); if (response.Any()) { lastException = null; break; } throw new EmptyResponseException(commandText); } catch (Exception exception) { lastException = exception; this.mpcConnectionReporter?.SendException(commandText, sendAttempter.CurrentAttempt, exception); await this.ReconnectAsync(true); this.mpcConnectionReporter?.RetrySend(commandText, sendAttempter.CurrentAttempt); } } if (lastException != null) { try { await this.DisconnectAsync(false); } catch (Exception) { } return(new ErrorMpdMessage <TResponse>(mpcCommand, new ErrorMpdResponse <TResponse>(lastException))); } return(new MpdMessage <TResponse>(mpcCommand, true, response)); }