2018-08-28 17:32:59 -04:00
|
|
|
|
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())
|
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
using (var re = cl.GetAsync($"{LogFile}&length=1").Result)
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
|
|
|
|
using (var content = re.Content)
|
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
string response = content.ReadAsStringAsync().Result ?? "0";
|
|
|
|
|
return Convert.ToInt64(response);
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int UpdateInterval => 1000;
|
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
public ICollection<GameEvent> ReadEventsFromLog(Server server, long fileSizeDiff, long startPosition)
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
#if DEBUG == true
|
|
|
|
|
server.Logger.WriteDebug($"Begin reading {fileSizeDiff} from http log");
|
|
|
|
|
#endif
|
2018-08-28 17:32:59 -04:00
|
|
|
|
string log;
|
|
|
|
|
using (var cl = new HttpClient())
|
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
using (var re = cl.GetAsync($"{LogFile}&start={fileSizeDiff}").Result)
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
|
|
|
|
using (var content = re.Content)
|
|
|
|
|
{
|
|
|
|
|
log = content.ReadAsStringAsync().Result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-30 21:53:00 -04:00
|
|
|
|
#if DEBUG == true
|
|
|
|
|
server.Logger.WriteDebug($"retrieved events from http log");
|
|
|
|
|
#endif
|
2018-08-28 17:32:59 -04:00
|
|
|
|
List<GameEvent> events = new List<GameEvent>();
|
2018-08-30 21:53:00 -04:00
|
|
|
|
string[] lines = log.Split(Environment.NewLine);
|
|
|
|
|
|
|
|
|
|
#if DEBUG == true
|
|
|
|
|
server.Logger.WriteDebug($"Begin parse of {lines.Length} lines from http log");
|
|
|
|
|
#endif
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
|
|
|
|
// parse each line
|
2018-08-30 21:53:00 -04:00
|
|
|
|
foreach (string eventLine in lines)
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
|
|
|
|
if (eventLine.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// todo: catch elsewhere
|
2018-08-30 21:53:00 -04:00
|
|
|
|
var e = Parser.GetEvent(server, eventLine);
|
|
|
|
|
#if DEBUG == true
|
|
|
|
|
server.Logger.WriteDebug($"Parsed event with id {e.Id} from http");
|
|
|
|
|
#endif
|
|
|
|
|
events.Add(e);
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|