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;
|
2018-04-28 01:22:18 -04:00
|
|
|
|
using System.Collections.Concurrent;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
using System.Collections.Generic;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application
|
|
|
|
|
{
|
|
|
|
|
class GameEventHandler : IEventHandler
|
|
|
|
|
{
|
2018-08-27 18:07:54 -04:00
|
|
|
|
private readonly IManager Manager;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
static long NextEventId = 1;
|
|
|
|
|
private readonly SortedList<long, GameEvent> OutOfOrderEvents;
|
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-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
public bool 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-02 17:59:27 -04:00
|
|
|
|
// GameEvent sortedEvent = null;
|
|
|
|
|
// lock (OutOfOrderEvents)
|
|
|
|
|
// {
|
|
|
|
|
// sortedEvent = OutOfOrderEvents.Values.FirstOrDefault();
|
2018-08-31 23:35:51 -04:00
|
|
|
|
|
2018-09-02 17:59:27 -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-02 17:59:27 -04:00
|
|
|
|
// AddEvent(sortedEvent);
|
|
|
|
|
// sortedEvent = OutOfOrderEvents.Values.FirstOrDefault();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2018-08-30 21:53:00 -04:00
|
|
|
|
|
2018-09-02 17:59:27 -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))
|
|
|
|
|
// {
|
|
|
|
|
//#if DEBUG == true
|
|
|
|
|
// Manager.GetLogger().WriteDebug($"sent event with id {gameEvent.Id} to be processed");
|
|
|
|
|
//#endif
|
2018-08-30 21:53:00 -04:00
|
|
|
|
((Manager as ApplicationManager).OnServerEvent)(this, new GameEventArgs(null, false, gameEvent));
|
2018-09-02 17:59:27 -04:00
|
|
|
|
return true;
|
|
|
|
|
// Interlocked.Increment(ref NextEventId);
|
|
|
|
|
// }
|
2018-08-30 21:53:00 -04:00
|
|
|
|
|
2018-09-02 17:59:27 -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-02 17:59:27 -04:00
|
|
|
|
// // this prevents multiple threads from adding simultaneously
|
|
|
|
|
// lock (OutOfOrderEvents)
|
|
|
|
|
// {
|
|
|
|
|
// if (!OutOfOrderEvents.TryGetValue(gameEvent.Id, out GameEvent discard))
|
|
|
|
|
// {
|
|
|
|
|
// OutOfOrderEvents.Add(gameEvent.Id, gameEvent);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return true;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|