private async Task KeepSending() { while (!source.Token.IsCancellationRequested) { while (!readyForWork) { byte[] message = { (byte)PacketType.ConnectToParent }; await udpClient.SendAsync(message, message.Length, parentNode).ConfigureAwait(false); await Task.Delay(IterationsDelay, source.Token).ConfigureAwait(false); } while (true) { foreach (var node in neighbours) { if (MaxAllowedAttempts < node.Value.LastPinged.AttemptsAlready) { DisconnectNeighbour(node.Key); continue; } foreach (var message in node.Value.LastAttempts) { if (MaxAllowedAttempts < message.Value.AttemptsAlready) { DisconnectNeighbour(node.Key); break; } if (DateTime.Now - message.Value.LastAttempt > RetryInterval) { await SendMessage(message.Key, node.Key).ConfigureAwait(false); var newInfo = new AttemptsInfo(message.Value.AttemptsAlready + 1, DateTime.Now); node.Value.LastAttempts[message.Key] = newInfo; node.Value.LastPinged = newInfo; } } if (DateTime.Now - node.Value.LastPinged.LastAttempt > RetryInterval) { var command = (byte)(PacketType.AreYouAlive); byte[] message = { command }; await udpClient.SendAsync(message, 1, node.Key).ConfigureAwait(false); var newInfo = new AttemptsInfo(node.Value.LastPinged.AttemptsAlready + 1, DateTime.Now); node.Value.LastPinged = newInfo; } } await Task.Delay(IterationsDelay, source.Token).ConfigureAwait(false); } } }
public void ResetAttempts() { LastPinged = new AttemptsInfo(0, DateTime.Now); }