make connection attempts for CoD configurable as "ServerConnectionAttempts"

This commit is contained in:
RaidMax 2021-09-18 18:25:02 -05:00
parent d4fb75d07c
commit e80753a4d3
4 changed files with 13 additions and 10 deletions

View File

@ -7,6 +7,7 @@ using Integrations.Source;
using Integrations.Source.Interfaces; using Integrations.Source.Interfaces;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using SharedLibraryCore.Configuration;
namespace IW4MAdmin.Application.Factories namespace IW4MAdmin.Application.Factories
{ {
@ -33,8 +34,10 @@ namespace IW4MAdmin.Application.Factories
return rconEngine switch return rconEngine switch
{ {
"COD" => new CodRConConnection(ipEndpoint, password, "COD" => new CodRConConnection(ipEndpoint, password,
_serviceProvider.GetRequiredService<ILogger<CodRConConnection>>(), GameEncoding), _serviceProvider.GetRequiredService<ILogger<CodRConConnection>>(), GameEncoding,
"Source" => new SourceRConConnection(_serviceProvider.GetRequiredService<ILogger<SourceRConConnection>>(), _serviceProvider.GetRequiredService<ApplicationConfiguration>()?.ServerConnectionAttempts ?? 6),
"Source" => new SourceRConConnection(
_serviceProvider.GetRequiredService<ILogger<SourceRConConnection>>(),
_serviceProvider.GetRequiredService<IRConClientFactory>(), ipEndpoint, password), _serviceProvider.GetRequiredService<IRConClientFactory>(), ipEndpoint, password),
_ => throw new ArgumentException($"No supported RCon engine available for '{rconEngine}'") _ => throw new ArgumentException($"No supported RCon engine available for '{rconEngine}'")
}; };

View File

@ -31,13 +31,15 @@ namespace Integrations.Cod
private IRConParserConfiguration config; private IRConParserConfiguration config;
private readonly ILogger _log; private readonly ILogger _log;
private readonly Encoding _gameEncoding; private readonly Encoding _gameEncoding;
private readonly int _retryAttempts;
public CodRConConnection(IPEndPoint ipEndpoint, string password, ILogger<CodRConConnection> log, Encoding gameEncoding) public CodRConConnection(IPEndPoint ipEndpoint, string password, ILogger<CodRConConnection> log, Encoding gameEncoding, int retryAttempts)
{ {
RConPassword = password; RConPassword = password;
_gameEncoding = gameEncoding; _gameEncoding = gameEncoding;
_log = log; _log = log;
Endpoint = ipEndpoint; Endpoint = ipEndpoint;
_retryAttempts = retryAttempts;
} }
public void SetConfiguration(IRConParser parser) public void SetConfiguration(IRConParser parser)
@ -137,7 +139,7 @@ namespace Integrations.Cod
_log.LogInformation( _log.LogInformation(
"Retrying RCon message ({connectionAttempts}/{allowedConnectionFailures} attempts) with parameters {payload}", "Retrying RCon message ({connectionAttempts}/{allowedConnectionFailures} attempts) with parameters {payload}",
connectionState.ConnectionAttempts, connectionState.ConnectionAttempts,
StaticHelpers.AllowedConnectionFails, parameters); _retryAttempts, parameters);
} }
} }
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
@ -155,7 +157,7 @@ namespace Integrations.Cod
bool exceptionCaught = false; bool exceptionCaught = false;
_log.LogDebug("Sending {payloadLength} bytes to [{endpoint}] ({connectionAttempts}/{allowedConnectionFailures})", _log.LogDebug("Sending {payloadLength} bytes to [{endpoint}] ({connectionAttempts}/{allowedConnectionFailures})",
payload.Length, Endpoint, connectionState.ConnectionAttempts, StaticHelpers.AllowedConnectionFails); payload.Length, Endpoint, connectionState.ConnectionAttempts, _retryAttempts);
try try
{ {
@ -173,7 +175,7 @@ namespace Integrations.Cod
catch catch
{ {
// we want to retry with a delay // we want to retry with a delay
if (connectionState.ConnectionAttempts < StaticHelpers.AllowedConnectionFails) if (connectionState.ConnectionAttempts < _retryAttempts)
{ {
exceptionCaught = true; exceptionCaught = true;
await Task.Delay(StaticHelpers.SocketTimeout(connectionState.ConnectionAttempts)); await Task.Delay(StaticHelpers.SocketTimeout(connectionState.ConnectionAttempts));

View File

@ -150,6 +150,8 @@ namespace SharedLibraryCore.Configuration
[ConfigurationIgnore] [ConfigurationIgnore]
public TimeSpan ServerDataCollectionInterval { get; set; } = TimeSpan.FromMinutes(5); public TimeSpan ServerDataCollectionInterval { get; set; } = TimeSpan.FromMinutes(5);
public int ServerConnectionAttempts { get; set; } = 6;
[ConfigurationIgnore] [ConfigurationIgnore]
public Dictionary<Permission, string> OverridePermissionLevelNames { get; set; } = Enum public Dictionary<Permission, string> OverridePermissionLevelNames { get; set; } = Enum

View File

@ -64,9 +64,5 @@ namespace SharedLibraryCore.RCon
/// interval in milliseconds to wait before sending the next RCon request /// interval in milliseconds to wait before sending the next RCon request
/// </summary> /// </summary>
public static readonly int FloodProtectionInterval = 750; public static readonly int FloodProtectionInterval = 750;
/// <summary>
/// how many failed connection attempts before aborting connection
/// </summary>
public static readonly int AllowedConnectionFails = 5;
} }
} }