IW4M-Admin/Application/IO/GameLogEventDetection.cs
RaidMax 1dc0f5a240 fix aggregate issue with KDR on global top stats
refactor some of the main application code to have a cleaner code flow
add enviroment flag to opt out of .net core telemetry in start script
fixed "a moment" missing the "ago"
fixed case sensitive client searches on postgresql
clean up command code flow
Add missing map "mp_cairo" to default settings
2019-05-08 20:34:17 -05:00

79 lines
2.5 KiB
C#

using SharedLibraryCore;
using SharedLibraryCore.Interfaces;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace IW4MAdmin.Application.IO
{
class GameLogEventDetection
{
private long previousFileSize;
private readonly Server _server;
private readonly IGameLogReader _reader;
private readonly string _gameLogFile;
class EventState
{
public ILogger Log { get; set; }
public string ServerId { get; set; }
}
public GameLogEventDetection(Server server, string gameLogPath, Uri gameLogServerUri)
{
_gameLogFile = gameLogPath;
_reader = gameLogServerUri != null ? new GameLogReaderHttp(gameLogServerUri, gameLogPath, server.EventParser) : _reader = new GameLogReader(gameLogPath, server.EventParser);
_server = server;
}
public async Task PollForChanges()
{
while (!_server.Manager.CancellationToken.IsCancellationRequested)
{
if (_server.IsInitialized)
{
try
{
await UpdateLogEvents();
}
catch (Exception e)
{
_server.Logger.WriteWarning($"Failed to update log event for {_server.EndPoint}");
_server.Logger.WriteDebug($"Exception: {e.Message}");
_server.Logger.WriteDebug($"StackTrace: {e.StackTrace}");
}
}
await Task.Delay(_reader.UpdateInterval, _server.Manager.CancellationToken);
}
}
private async Task UpdateLogEvents()
{
long fileSize = _reader.Length;
if (previousFileSize == 0)
previousFileSize = fileSize;
long fileDiff = fileSize - previousFileSize;
// this makes the http log get pulled
if (fileDiff < 1 && fileSize != -1)
return;
previousFileSize = fileSize;
var events = await _reader.ReadEventsFromLog(_server, fileDiff, 0);
foreach (var ev in events)
{
_server.Manager.GetEventHandler().AddEvent(ev);
await ev.WaitAsync(Utilities.DefaultCommandTimeout, _server.Manager.CancellationToken);
}
previousFileSize = fileSize;
}
}
}