IW4M-Admin/Application/IO/GameLogEventDetection.cs

78 lines
2.3 KiB
C#
Raw Normal View History

using SharedLibraryCore;
using SharedLibraryCore.Interfaces;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace IW4MAdmin.Application.IO
{
class GameLogEventDetection
{
Server Server;
long PreviousFileSize;
IGameLogReader Reader;
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.ShutdownRequested())
{
if ((Server.Manager as ApplicationManager).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}");
}
}
Thread.Sleep(Reader.UpdateInterval);
}
}
private async Task UpdateLogEvents()
{
long fileSize = Reader.Length;
if (PreviousFileSize == 0)
PreviousFileSize = fileSize;
long fileDiff = fileSize - PreviousFileSize;
2018-09-06 14:25:58 -04:00
// 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);
2018-11-07 21:30:11 -05:00
await ev.WaitAsync();
}
PreviousFileSize = fileSize;
}
}
}