mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-28 16:10:28 -05:00
think I finished reworking the event system
added http log reading support for debugging remotely started working on unit test framework
This commit is contained in:
@ -6,11 +6,11 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace IW4MAdmin.Application.IO
|
||||
{
|
||||
class GameLogEvent
|
||||
class GameLogEventDetection
|
||||
{
|
||||
Server Server;
|
||||
long PreviousFileSize;
|
||||
GameLogReader Reader;
|
||||
IGameLogReader Reader;
|
||||
readonly string GameLogFile;
|
||||
|
||||
class EventState
|
||||
@ -19,14 +19,22 @@ namespace IW4MAdmin.Application.IO
|
||||
public string ServerId { get; set; }
|
||||
}
|
||||
|
||||
public GameLogEvent(Server server, string gameLogPath, string gameLogName)
|
||||
public GameLogEventDetection(Server server, string gameLogPath, string gameLogName)
|
||||
{
|
||||
GameLogFile = gameLogPath;
|
||||
Reader = new GameLogReader(gameLogPath, server.EventParser);
|
||||
// todo: abtract this more
|
||||
if (gameLogPath.StartsWith("http"))
|
||||
{
|
||||
Reader = new GameLogReaderHttp(gameLogPath, server.EventParser);
|
||||
}
|
||||
else
|
||||
{
|
||||
Reader = new GameLogReader(gameLogPath, server.EventParser);
|
||||
}
|
||||
Server = server;
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
{
|
||||
while (!server.Manager.ShutdownRequested())
|
||||
{
|
||||
if ((server.Manager as ApplicationManager).IsInitialized)
|
||||
@ -44,7 +52,7 @@ namespace IW4MAdmin.Application.IO
|
||||
|
||||
private void OnEvent(object state)
|
||||
{
|
||||
long newLength = new FileInfo(GameLogFile).Length;
|
||||
long newLength = Reader.Length;
|
||||
|
||||
try
|
||||
{
|
@ -7,11 +7,15 @@ using System.Text;
|
||||
|
||||
namespace IW4MAdmin.Application.IO
|
||||
{
|
||||
class GameLogReader
|
||||
class GameLogReader : IGameLogReader
|
||||
{
|
||||
IEventParser Parser;
|
||||
readonly string LogFile;
|
||||
|
||||
public long Length => new FileInfo(LogFile).Length;
|
||||
|
||||
public int UpdateInterval => 100;
|
||||
|
||||
public GameLogReader(string logFile, IEventParser parser)
|
||||
{
|
||||
LogFile = logFile;
|
||||
|
84
Application/IO/GameLogReaderHttp.cs
Normal file
84
Application/IO/GameLogReaderHttp.cs
Normal file
@ -0,0 +1,84 @@
|
||||
using SharedLibraryCore;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
|
||||
namespace IW4MAdmin.Application.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// provides capibility of reading log files over HTTP
|
||||
/// </summary>
|
||||
class GameLogReaderHttp : IGameLogReader
|
||||
{
|
||||
readonly IEventParser Parser;
|
||||
readonly string LogFile;
|
||||
|
||||
public GameLogReaderHttp(string logFile, IEventParser parser)
|
||||
{
|
||||
LogFile = logFile;
|
||||
Parser = parser;
|
||||
}
|
||||
|
||||
public long Length
|
||||
{
|
||||
get
|
||||
{
|
||||
using (var cl = new HttpClient())
|
||||
{
|
||||
using (var re = cl.GetAsync($"{LogFile}?length=1").Result)
|
||||
{
|
||||
using (var content = re.Content)
|
||||
{
|
||||
return Convert.ToInt64(content.ReadAsStringAsync().Result ?? "0");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int UpdateInterval => 1000;
|
||||
|
||||
public ICollection<GameEvent> EventsFromLog(Server server, long fileSizeDiff, long startPosition)
|
||||
{
|
||||
string log;
|
||||
using (var cl = new HttpClient())
|
||||
{
|
||||
using (var re = cl.GetAsync($"{LogFile}?start={fileSizeDiff}").Result)
|
||||
{
|
||||
using (var content = re.Content)
|
||||
{
|
||||
log = content.ReadAsStringAsync().Result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<GameEvent> events = new List<GameEvent>();
|
||||
|
||||
// parse each line
|
||||
foreach (string eventLine in log.Split(Environment.NewLine))
|
||||
{
|
||||
if (eventLine.Length > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
// todo: catch elsewhere
|
||||
events.Add(Parser.GetEvent(server, eventLine));
|
||||
}
|
||||
|
||||
catch (Exception e)
|
||||
{
|
||||
Program.ServerManager.GetLogger().WriteWarning("Could not properly parse event line");
|
||||
Program.ServerManager.GetLogger().WriteDebug(e.Message);
|
||||
Program.ServerManager.GetLogger().WriteDebug(eventLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return events;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user