2018-04-26 02:13:04 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
using System.Threading;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.IO
|
|
|
|
|
{
|
2018-08-28 17:32:59 -04:00
|
|
|
|
class GameLogEventDetection
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
private long previousFileSize;
|
|
|
|
|
private readonly Server _server;
|
|
|
|
|
private readonly IGameLogReader _reader;
|
|
|
|
|
private readonly string _gameLogFile;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-04-28 01:22:18 -04:00
|
|
|
|
class EventState
|
|
|
|
|
{
|
|
|
|
|
public ILogger Log { get; set; }
|
|
|
|
|
public string ServerId { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 16:35:13 -05:00
|
|
|
|
public GameLogEventDetection(Server server, string gameLogPath, Uri gameLogServerUri)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_gameLogFile = gameLogPath;
|
|
|
|
|
_reader = gameLogServerUri != null ? new GameLogReaderHttp(gameLogServerUri, gameLogPath, server.EventParser) : _reader = new GameLogReader(gameLogPath, server.EventParser);
|
|
|
|
|
_server = server;
|
2018-04-28 01:22:18 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-10-25 09:14:39 -04:00
|
|
|
|
public async Task PollForChanges()
|
2018-04-28 01:22:18 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
while (!_server.Manager.CancellationToken.IsCancellationRequested)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
if (_server.IsInitialized)
|
2018-08-30 21:53:00 -04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-25 09:14:39 -04:00
|
|
|
|
await UpdateLogEvents();
|
2018-08-30 21:53:00 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_server.Logger.WriteWarning($"Failed to update log event for {_server.EndPoint}");
|
|
|
|
|
_server.Logger.WriteDebug($"Exception: {e.Message}");
|
|
|
|
|
_server.Logger.WriteDebug($"StackTrace: {e.StackTrace}");
|
2018-08-30 21:53:00 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-08 21:34:17 -04:00
|
|
|
|
|
|
|
|
|
await Task.Delay(_reader.UpdateInterval, _server.Manager.CancellationToken);
|
2018-04-28 01:22:18 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 09:14:39 -04:00
|
|
|
|
private async Task UpdateLogEvents()
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
long fileSize = _reader.Length;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
if (previousFileSize == 0)
|
|
|
|
|
previousFileSize = fileSize;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
long fileDiff = fileSize - previousFileSize;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-09-06 14:25:58 -04:00
|
|
|
|
// this makes the http log get pulled
|
|
|
|
|
if (fileDiff < 1 && fileSize != -1)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
return;
|
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
previousFileSize = fileSize;
|
2018-04-26 16:26:03 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
var events = await _reader.ReadEventsFromLog(_server, fileDiff, 0);
|
2018-08-30 21:53:00 -04:00
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
foreach (var ev in events)
|
2018-08-30 21:53:00 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_server.Manager.GetEventHandler().AddEvent(ev);
|
|
|
|
|
await ev.WaitAsync(Utilities.DefaultCommandTimeout, _server.Manager.CancellationToken);
|
2018-08-30 21:53:00 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
previousFileSize = fileSize;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|