First commit

This commit is contained in:
EnderIce2
2020-10-25 16:19:43 +02:00
parent 9d78d84acb
commit b91db81473
73 changed files with 7634 additions and 0 deletions

View File

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DiscordRPC.Helper
{
internal class BackoffDelay
{
/// <summary>
/// The maximum time the backoff can reach
/// </summary>
public int Maximum { get; private set; }
/// <summary>
/// The minimum time the backoff can start at
/// </summary>
public int Minimum { get; private set; }
/// <summary>
/// The current time of the backoff
/// </summary>
public int Current { get { return _current; } }
private int _current;
/// <summary>
/// The current number of failures
/// </summary>
public int Fails { get { return _fails; } }
private int _fails;
/// <summary>
/// The random generator
/// </summary>
public Random Random { get; set; }
private BackoffDelay() { }
public BackoffDelay(int min, int max) : this(min, max, new Random()) { }
public BackoffDelay(int min, int max, Random random)
{
this.Minimum = min;
this.Maximum = max;
this._current = min;
this._fails = 0;
this.Random = random;
}
/// <summary>
/// Resets the backoff
/// </summary>
public void Reset()
{
_fails = 0;
_current = Minimum;
}
public int NextDelay()
{
//Increment the failures
_fails++;
double diff = (Maximum - Minimum) / 100f;
_current = (int)Math.Floor(diff * _fails) + Minimum;
return Math.Min(Math.Max(_current, Minimum), Maximum);
}
}
}