mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-24 14:10:29 -05:00
moved event API stuff around
finally fixed threading issue (which actually had to do with IW4x log outputs being out of sync (not an issue with my code). What a lot of headache over something that wasn't my fault.
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
using SharedLibraryCore;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IW4MAdmin.Application.IO
|
||||
@ -31,44 +33,36 @@ namespace IW4MAdmin.Application.IO
|
||||
{
|
||||
Reader = new GameLogReader(gameLogPath, server.EventParser);
|
||||
}
|
||||
|
||||
Server = server;
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
while (!server.Manager.ShutdownRequested())
|
||||
{
|
||||
if ((server.Manager as ApplicationManager).IsInitialized)
|
||||
{
|
||||
OnEvent(new EventState()
|
||||
{
|
||||
Log = server.Manager.GetLogger(),
|
||||
ServerId = server.ToString()
|
||||
});
|
||||
}
|
||||
await Task.Delay(100);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void OnEvent(object state)
|
||||
public void PollForChanges()
|
||||
{
|
||||
long newLength = Reader.Length;
|
||||
|
||||
try
|
||||
while (!Server.Manager.ShutdownRequested())
|
||||
{
|
||||
UpdateLogEvents(newLength);
|
||||
}
|
||||
if ((Server.Manager as ApplicationManager).IsInitialized)
|
||||
{
|
||||
try
|
||||
{
|
||||
UpdateLogEvents();
|
||||
}
|
||||
|
||||
catch (Exception e)
|
||||
{
|
||||
((EventState)state).Log.WriteWarning($"Failed to update log event for {((EventState)state).ServerId}");
|
||||
((EventState)state).Log.WriteDebug($"Exception: {e.Message}");
|
||||
((EventState)state).Log.WriteDebug($"StackTrace: {e.StackTrace}");
|
||||
catch (Exception e)
|
||||
{
|
||||
Server.Logger.WriteWarning($"Failed to update log event for {Server.GetHashCode()}");
|
||||
Server.Logger.WriteDebug($"Exception: {e.Message}");
|
||||
Server.Logger.WriteDebug($"StackTrace: {e.StackTrace}");
|
||||
}
|
||||
}
|
||||
Thread.Sleep(Reader.UpdateInterval);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateLogEvents(long fileSize)
|
||||
private void UpdateLogEvents()
|
||||
{
|
||||
long fileSize = Reader.Length;
|
||||
|
||||
if (PreviousFileSize == 0)
|
||||
PreviousFileSize = fileSize;
|
||||
|
||||
@ -79,9 +73,12 @@ namespace IW4MAdmin.Application.IO
|
||||
|
||||
PreviousFileSize = fileSize;
|
||||
|
||||
var events = Reader.EventsFromLog(Server, fileDiff, 0);
|
||||
var events = Reader.ReadEventsFromLog(Server, fileDiff, 0);
|
||||
|
||||
foreach (var ev in events)
|
||||
{
|
||||
Server.Manager.GetEventHandler().AddEvent(ev);
|
||||
}
|
||||
|
||||
PreviousFileSize = fileSize;
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ namespace IW4MAdmin.Application.IO
|
||||
|
||||
public long Length => new FileInfo(LogFile).Length;
|
||||
|
||||
public int UpdateInterval => 100;
|
||||
public int UpdateInterval => 300;
|
||||
|
||||
public GameLogReader(string logFile, IEventParser parser)
|
||||
{
|
||||
@ -22,7 +22,7 @@ namespace IW4MAdmin.Application.IO
|
||||
Parser = parser;
|
||||
}
|
||||
|
||||
public ICollection<GameEvent> EventsFromLog(Server server, long fileSizeDiff, long startPosition)
|
||||
public ICollection<GameEvent> ReadEventsFromLog(Server server, long fileSizeDiff, long startPosition)
|
||||
{
|
||||
// allocate the bytes for the new log lines
|
||||
List<string> logLines = new List<string>();
|
||||
|
@ -29,11 +29,12 @@ namespace IW4MAdmin.Application.IO
|
||||
{
|
||||
using (var cl = new HttpClient())
|
||||
{
|
||||
using (var re = cl.GetAsync($"{LogFile}?length=1").Result)
|
||||
using (var re = cl.GetAsync($"{LogFile}&length=1").Result)
|
||||
{
|
||||
using (var content = re.Content)
|
||||
{
|
||||
return Convert.ToInt64(content.ReadAsStringAsync().Result ?? "0");
|
||||
string response = content.ReadAsStringAsync().Result ?? "0";
|
||||
return Convert.ToInt64(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -42,12 +43,15 @@ namespace IW4MAdmin.Application.IO
|
||||
|
||||
public int UpdateInterval => 1000;
|
||||
|
||||
public ICollection<GameEvent> EventsFromLog(Server server, long fileSizeDiff, long startPosition)
|
||||
public ICollection<GameEvent> ReadEventsFromLog(Server server, long fileSizeDiff, long startPosition)
|
||||
{
|
||||
#if DEBUG == true
|
||||
server.Logger.WriteDebug($"Begin reading {fileSizeDiff} from http log");
|
||||
#endif
|
||||
string log;
|
||||
using (var cl = new HttpClient())
|
||||
{
|
||||
using (var re = cl.GetAsync($"{LogFile}?start={fileSizeDiff}").Result)
|
||||
using (var re = cl.GetAsync($"{LogFile}&start={fileSizeDiff}").Result)
|
||||
{
|
||||
using (var content = re.Content)
|
||||
{
|
||||
@ -55,18 +59,29 @@ namespace IW4MAdmin.Application.IO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG == true
|
||||
server.Logger.WriteDebug($"retrieved events from http log");
|
||||
#endif
|
||||
List<GameEvent> events = new List<GameEvent>();
|
||||
string[] lines = log.Split(Environment.NewLine);
|
||||
|
||||
#if DEBUG == true
|
||||
server.Logger.WriteDebug($"Begin parse of {lines.Length} lines from http log");
|
||||
#endif
|
||||
|
||||
// parse each line
|
||||
foreach (string eventLine in log.Split(Environment.NewLine))
|
||||
foreach (string eventLine in lines)
|
||||
{
|
||||
if (eventLine.Length > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
// todo: catch elsewhere
|
||||
events.Add(Parser.GetEvent(server, eventLine));
|
||||
var e = Parser.GetEvent(server, eventLine);
|
||||
#if DEBUG == true
|
||||
server.Logger.WriteDebug($"Parsed event with id {e.Id} from http");
|
||||
#endif
|
||||
events.Add(e);
|
||||
}
|
||||
|
||||
catch (Exception e)
|
||||
|
Reference in New Issue
Block a user