2018-04-26 02:13:04 -04:00
|
|
|
|
using SharedLibraryCore;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
using SharedLibraryCore.Events;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System.Collections.Generic;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application
|
|
|
|
|
{
|
|
|
|
|
class GameEventHandler : IEventHandler
|
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
static long NextEventId = 1;
|
2018-09-07 23:29:42 -04:00
|
|
|
|
readonly IManager Manager;
|
|
|
|
|
readonly SortedList<long, GameEvent> OutOfOrderEvents;
|
|
|
|
|
readonly SemaphoreSlim IsProcessingEvent;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
|
|
|
|
public GameEventHandler(IManager mgr)
|
|
|
|
|
{
|
|
|
|
|
Manager = mgr;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
OutOfOrderEvents = new SortedList<long, GameEvent>();
|
2018-09-13 15:34:42 -04:00
|
|
|
|
IsProcessingEvent = new SemaphoreSlim(1, 1);
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 13:40:29 -04:00
|
|
|
|
public void AddEvent(GameEvent gameEvent)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
2018-08-30 21:53:00 -04:00
|
|
|
|
Manager.GetLogger().WriteDebug($"Got new event of type {gameEvent.Type} for {gameEvent.Owner} with id {gameEvent.Id}");
|
2018-04-26 02:13:04 -04:00
|
|
|
|
#endif
|
2018-09-04 13:40:29 -04:00
|
|
|
|
GameEvent sortedEvent = null;
|
|
|
|
|
lock (OutOfOrderEvents)
|
|
|
|
|
{
|
|
|
|
|
sortedEvent = OutOfOrderEvents.Values.FirstOrDefault();
|
2018-08-31 23:35:51 -04:00
|
|
|
|
|
2018-09-04 13:40:29 -04:00
|
|
|
|
while (sortedEvent?.Id == Interlocked.Read(ref NextEventId))
|
|
|
|
|
{
|
|
|
|
|
if (OutOfOrderEvents.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
OutOfOrderEvents.RemoveAt(0);
|
|
|
|
|
}
|
2018-08-31 23:35:51 -04:00
|
|
|
|
|
2018-09-04 13:40:29 -04:00
|
|
|
|
AddEvent(sortedEvent);
|
|
|
|
|
sortedEvent = OutOfOrderEvents.Values.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-30 21:53:00 -04:00
|
|
|
|
|
2018-09-04 13:40:29 -04:00
|
|
|
|
// both the gameEvent Id and the LastEventId are thread safe because we want to synchronize when the
|
|
|
|
|
// event occurs
|
|
|
|
|
if (gameEvent.Id == Interlocked.Read(ref NextEventId))
|
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
((Manager as ApplicationManager).OnServerEvent)(this, new GameEventArgs(null, false, gameEvent));
|
2018-09-23 20:45:54 -04:00
|
|
|
|
#if DEBUG == true
|
|
|
|
|
Manager.GetLogger().WriteDebug($"notified event {gameEvent.Type} for {gameEvent.Owner} with id {gameEvent.Id}");
|
|
|
|
|
#endif
|
2018-09-04 13:40:29 -04:00
|
|
|
|
Interlocked.Increment(ref NextEventId);
|
|
|
|
|
}
|
2018-08-30 21:53:00 -04:00
|
|
|
|
|
2018-09-04 13:40:29 -04:00
|
|
|
|
// a "newer" event has been added before and "older" one has been added (due to threads and context switching)
|
|
|
|
|
// so me must wait until the next expected one arrives
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG == true
|
|
|
|
|
Manager.GetLogger().WriteWarning("Incoming event is out of order");
|
|
|
|
|
Manager.GetLogger().WriteDebug($"Expected event Id is {Interlocked.Read(ref NextEventId)}, but got {gameEvent.Id} instead");
|
|
|
|
|
#endif
|
2018-08-30 21:53:00 -04:00
|
|
|
|
|
2018-09-04 13:40:29 -04:00
|
|
|
|
// this prevents multiple threads from adding simultaneously
|
|
|
|
|
lock (OutOfOrderEvents)
|
|
|
|
|
{
|
|
|
|
|
if (!OutOfOrderEvents.TryGetValue(gameEvent.Id, out GameEvent discard))
|
|
|
|
|
{
|
|
|
|
|
OutOfOrderEvents.Add(gameEvent.Id, gameEvent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|