IW4M-Admin/Integrations/Source/SourceRConConnection.cs

188 lines
6.3 KiB
C#
Raw Permalink Normal View History

2021-06-11 12:52:30 -04:00
using System;
using System.Linq;
2021-07-11 18:26:30 -04:00
using System.Net;
2021-06-03 11:51:03 -04:00
using System.Net.Sockets;
2021-06-11 12:52:30 -04:00
using System.Threading;
2021-06-03 11:51:03 -04:00
using System.Threading.Tasks;
2021-06-11 12:52:30 -04:00
using Integrations.Source.Extensions;
2021-06-03 11:51:03 -04:00
using Integrations.Source.Interfaces;
using Microsoft.Extensions.Logging;
using RconSharp;
using Serilog.Context;
using SharedLibraryCore;
using SharedLibraryCore.Exceptions;
using SharedLibraryCore.Interfaces;
using SharedLibraryCore.RCon;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace Integrations.Source
{
public class SourceRConConnection : IRConConnection
{
private readonly ILogger _logger;
private readonly string _password;
2021-07-11 18:26:30 -04:00
private readonly IPEndPoint _ipEndPoint;
2021-06-03 11:51:03 -04:00
private readonly IRConClientFactory _rconClientFactory;
2021-06-11 12:52:30 -04:00
private readonly SemaphoreSlim _activeQuery;
2021-06-03 11:51:03 -04:00
2021-06-11 12:52:30 -04:00
private static readonly TimeSpan FloodDelay = TimeSpan.FromMilliseconds(250);
private static readonly TimeSpan ConnectionTimeout = TimeSpan.FromSeconds(30);
2021-06-11 12:52:30 -04:00
private DateTime _lastQuery = DateTime.Now;
2021-06-03 11:51:03 -04:00
private RconClient _rconClient;
private bool _authenticated;
private bool _needNewSocket = true;
2021-06-03 11:51:03 -04:00
public SourceRConConnection(ILogger<SourceRConConnection> logger, IRConClientFactory rconClientFactory,
2021-07-11 18:26:30 -04:00
IPEndPoint ipEndPoint, string password)
2021-06-03 11:51:03 -04:00
{
_rconClientFactory = rconClientFactory;
_password = password;
_logger = logger;
2021-07-11 18:26:30 -04:00
_ipEndPoint = ipEndPoint;
2021-06-11 12:52:30 -04:00
_activeQuery = new SemaphoreSlim(1, 1);
2021-06-03 11:51:03 -04:00
}
2021-06-11 12:52:30 -04:00
~SourceRConConnection()
2021-06-03 11:51:03 -04:00
{
2021-06-11 12:52:30 -04:00
_activeQuery.Dispose();
}
2021-06-03 11:51:03 -04:00
public async Task<string[]> SendQueryAsync(StaticHelpers.QueryType type, string parameters = "", CancellationToken token = default)
2021-06-11 12:52:30 -04:00
{
2021-06-03 11:51:03 -04:00
try
{
await _activeQuery.WaitAsync(token);
await WaitForAvailable(token);
2021-06-11 12:52:30 -04:00
if (_needNewSocket)
2021-06-11 12:52:30 -04:00
{
try
2021-06-11 12:52:30 -04:00
{
_rconClient?.Disconnect();
2021-06-11 12:52:30 -04:00
}
catch
2021-06-11 12:52:30 -04:00
{
// ignored
2021-06-11 12:52:30 -04:00
}
await Task.Delay(ConnectionTimeout, token);
2021-07-11 18:26:30 -04:00
_rconClient = _rconClientFactory.CreateClient(_ipEndPoint);
_authenticated = false;
_needNewSocket = false;
2021-06-11 12:52:30 -04:00
}
2021-07-11 18:26:30 -04:00
using (LogContext.PushProperty("Server", _ipEndPoint.ToString()))
2021-06-03 11:51:03 -04:00
{
_logger.LogDebug("Connecting to RCon socket");
2021-06-03 11:51:03 -04:00
}
await TryConnectAndAuthenticate().WithTimeout(ConnectionTimeout);
var multiPacket = false;
2021-06-11 12:52:30 -04:00
if (type == StaticHelpers.QueryType.COMMAND_STATUS)
{
parameters = "status";
multiPacket = true;
2021-06-11 12:52:30 -04:00
}
parameters = parameters.ReplaceUnfriendlyCharacters();
parameters = parameters.StripColors();
2021-06-03 11:51:03 -04:00
2021-07-11 18:26:30 -04:00
using (LogContext.PushProperty("Server", _ipEndPoint.ToString()))
2021-06-03 11:51:03 -04:00
{
2021-06-11 12:52:30 -04:00
_logger.LogDebug("Sending query {Type} with parameters \"{Parameters}\"", type, parameters);
2021-06-03 11:51:03 -04:00
}
var response = await _rconClient.ExecuteCommandAsync(parameters, multiPacket)
.WithTimeout(ConnectionTimeout);
2021-06-03 11:51:03 -04:00
2021-07-11 18:26:30 -04:00
using (LogContext.PushProperty("Server", $"{_ipEndPoint}"))
2021-06-11 12:52:30 -04:00
{
_logger.LogDebug("Received RCon response {Response}", response);
}
var split = response.TrimEnd('\n').Split('\n');
return split.Take(split.Length - 1).ToArray();
2021-06-03 11:51:03 -04:00
}
catch (TaskCanceledException)
{
_needNewSocket = true;
throw new NetworkException("Timeout while attempting to communicate with server");
}
catch (SocketException ex)
{
2021-07-11 18:26:30 -04:00
using (LogContext.PushProperty("Server", _ipEndPoint.ToString()))
{
_logger.LogError(ex, "Socket exception encountered while attempting to communicate with server");
}
_needNewSocket = true;
throw new NetworkException("Socket exception encountered while attempting to communicate with server");
}
2021-06-11 12:52:30 -04:00
catch (Exception ex) when (ex.GetType() != typeof(NetworkException) &&
ex.GetType() != typeof(ServerException))
2021-06-03 11:51:03 -04:00
{
2021-07-11 18:26:30 -04:00
using (LogContext.PushProperty("Server", _ipEndPoint.ToString()))
2021-06-11 12:52:30 -04:00
{
_logger.LogError(ex, "Could not execute RCon query {Parameters}", parameters);
}
2021-06-03 11:51:03 -04:00
2021-06-11 12:52:30 -04:00
throw new NetworkException("Unable to communicate with server");
}
2021-06-03 11:51:03 -04:00
2021-06-11 12:52:30 -04:00
finally
2021-06-03 11:51:03 -04:00
{
2021-06-11 12:52:30 -04:00
if (_activeQuery.CurrentCount == 0)
{
_activeQuery.Release();
}
2021-06-03 11:51:03 -04:00
2021-06-11 12:52:30 -04:00
_lastQuery = DateTime.Now;
}
2021-06-03 11:51:03 -04:00
}
private async Task WaitForAvailable(CancellationToken token)
{
var diff = DateTime.Now - _lastQuery;
if (diff < FloodDelay)
{
await Task.Delay(FloodDelay - diff, token);
}
}
private async Task TryConnectAndAuthenticate()
{
if (!_authenticated)
{
2021-07-11 18:26:30 -04:00
using (LogContext.PushProperty("Server", _ipEndPoint.ToString()))
{
_logger.LogDebug("Authenticating to RCon socket");
}
await _rconClient.ConnectAsync().WithTimeout(ConnectionTimeout);
_authenticated = await _rconClient.AuthenticateAsync(_password).WithTimeout(ConnectionTimeout);
if (!_authenticated)
{
2021-07-11 18:26:30 -04:00
using (LogContext.PushProperty("Server", _ipEndPoint.ToString()))
{
_logger.LogError("Could not login to server");
}
throw new ServerException("Could not authenticate to server with provided password");
}
}
}
2021-06-03 11:51:03 -04:00
public void SetConfiguration(IRConParser config)
{
}
}
2022-01-19 10:58:25 -05:00
}