add network game log reader ex: net.tcp://ip:port

This commit is contained in:
RaidMax 2022-02-11 15:33:05 -06:00
parent 4dec284b31
commit f3c6b10a35
3 changed files with 109 additions and 5 deletions

View File

@ -18,14 +18,22 @@ namespace IW4MAdmin.Application.Factories
public IGameLogReader CreateGameLogReader(Uri[] logUris, IEventParser eventParser)
{
var baseUri = logUris[0];
if (baseUri.Scheme == Uri.UriSchemeHttp)
if (baseUri.Scheme == Uri.UriSchemeHttp || baseUri.Scheme == Uri.UriSchemeHttps)
{
return new GameLogReaderHttp(logUris, eventParser, _serviceProvider.GetRequiredService<ILogger<GameLogReaderHttp>>());
return new GameLogReaderHttp(logUris, eventParser,
_serviceProvider.GetRequiredService<ILogger<GameLogReaderHttp>>());
}
else if (baseUri.Scheme == Uri.UriSchemeFile)
if (baseUri.Scheme == Uri.UriSchemeFile)
{
return new GameLogReader(baseUri.LocalPath, eventParser, _serviceProvider.GetRequiredService<ILogger<GameLogReader>>());
return new GameLogReader(baseUri.LocalPath, eventParser,
_serviceProvider.GetRequiredService<ILogger<GameLogReader>>());
}
if (baseUri.Scheme == Uri.UriSchemeNetTcp)
{
return new NetworkGameLogReader(logUris, eventParser,
_serviceProvider.GetRequiredService<ILogger<NetworkGameLogReader>>());
}
throw new NotImplementedException($"No log reader implemented for Uri scheme \"{baseUri.Scheme}\"");

View File

@ -0,0 +1,96 @@
using SharedLibraryCore;
using SharedLibraryCore.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace IW4MAdmin.Application.IO
{
/// <summary>
/// provides capability of reading log files over HTTP
/// </summary>
class NetworkGameLogReader : IGameLogReader
{
private readonly IEventParser _eventParser;
private readonly UdpClient _udpClient;
private readonly ILogger _logger;
public NetworkGameLogReader(Uri[] uris, IEventParser parser, ILogger<NetworkGameLogReader> logger)
{
_eventParser = parser;
try
{
var endPoint = new IPEndPoint(Dns.GetHostAddresses(uris[0].Host).First(), uris[0].Port);
_udpClient = new UdpClient(endPoint);
}
catch (Exception ex)
{
logger.LogError(ex, "Could setup {LogReader}", nameof(NetworkGameLogReader));
}
_logger = logger;
}
public long Length => -1;
public int UpdateInterval => 500;
public async Task<IEnumerable<GameEvent>> ReadEventsFromLog(long fileSizeDiff, long startPosition)
{
if (_udpClient == null)
{
return Enumerable.Empty<GameEvent>();
}
byte[] buffer;
try
{
buffer = (await _udpClient.ReceiveAsync()).Buffer;
}
catch (Exception ex)
{
_logger.LogError(ex, "Could receive lines for {LogReader}", nameof(NetworkGameLogReader));
return Enumerable.Empty<GameEvent>();
}
if (!buffer.Any())
{
return Enumerable.Empty<GameEvent>();
}
var logData = Utilities.EncodingType.GetString(buffer);
if (string.IsNullOrWhiteSpace(logData))
{
return Enumerable.Empty<GameEvent>();
}
var lines = logData
.Split('\n')
.Where(line => line.Length > 0);
var events = new List<GameEvent>();
foreach (var eventLine in lines)
{
try
{
// this trim end should hopefully fix the nasty runaway regex
var gameEvent = _eventParser.GenerateGameEvent(eventLine.TrimEnd('\r'));
events.Add(gameEvent);
}
catch (Exception ex)
{
_logger.LogError(ex, "Could not properly parse event line from http {eventLine}", eventLine);
}
}
return events;
}
}
}

View File

@ -334,7 +334,7 @@ const pollForEvents = server => {
if (event.subType === 'Meta') {
const metaService = _serviceResolver.ResolveService('IMetaService');
const meta = metaService.GetPersistentMeta(event.data, client).GetAwaiter().GetResult().Value;
const meta = metaService.GetPersistentMeta(event.data, client).GetAwaiter().GetResult();
data[event.data] = meta === null ? '' : meta.Value;
} else {
data = {