I have a simple event that the client sends to the server, the server can then respond saying if it was successful, or if it failed for some reason.
The client has to send a auth code, which is inside their config class. Below I attempt to throttle it, allowing 2 attempts, then reloading the config after the 2nd failed attempt.
If it still fails 2 more attempts after reloading the config, I throttle it for 60 seconds. If it fails after reloading the config 3 or more times (12 requests), then I just refuse to resend it.
Please note that I understand just leaving it and not warning the user why it failed to connect to the server is wrong, but I haven't finished with that code yet, and will adapt later on.
I'm just looking to see if its okay, and if I can improve it in any way.
public class HandshakeFailedEvent : IIncomingPacketEvent
{
private static int ATTEMPTS_BEFORE_CONFIG_RELOAD = 2;
private static int ATTEMPTS_BEFORE_THROTTLE = 4;
private static int SECONDS_TO_THROTTLE = 60;
private int _timesFailed;
private int _timesSinceReset;
public void Process(ICoreContext coreContext, IIncomingPacket packet)
{
if (packet.GetInteger("error_code") == 1) // WRONG_PASSWORD
{
CheckForThrottle(coreContext);
}
// Other error codes may need fixing, not throttling, handle here.
coreContext.SocketHandler.SendPacket(new HandshakeRequestComposer(coreContext.ConfigHandler));
}
private void CheckForThrottle(ICoreContext coreContext)
{
_timesFailed++;
_timesSinceReset++;
if (_timesFailed >= ATTEMPTS_BEFORE_THROTTLE * 3)
{
// Application.Restart();
return;
}
if (_timesSinceReset >= ATTEMPTS_BEFORE_CONFIG_RELOAD && _timesSinceReset < ATTEMPTS_BEFORE_THROTTLE)
{
coreContext.ConfigHandler.Download();
}
else if (_timesSinceReset >= ATTEMPTS_BEFORE_THROTTLE)
{
Task.Delay(SECONDS_TO_THROTTLE * 1000).ContinueWith((t) => { _timesSinceReset = 0; });
}
}
}
TimeSpaninstead of integers forTask.Delayetc.: docs.microsoft.com/en-us/dotnet/api/… \$\endgroup\$