public void Service() { //Just do one a tick. if (EventQueue.Any()) { if (EventQueue.TryDequeue(out var action)) { action?.Invoke(); } } foreach (var tickable in CurrentGameTickables) { tickable.Tick(); } long currentMilliseconds = TimeService.CurrentTimeInMilliseconds(); if (LastMilliseconds == -1) { LastMilliseconds = currentMilliseconds; } else if (currentMilliseconds - 600 >= LastMilliseconds) { //It's been 600ms, time for another tick. LastMilliseconds = currentMilliseconds; foreach (var fixedTickable in CurrentGameFixedTickables) { fixedTickable.OnGameFixedTick(); } } }
public void Publish() { lock (_lockObject) { if (_inProcess) { return; } _inProcess = true; } do { EventQueue localQueue = null; lock (_lockObject) { localQueue = _eventQueue; _eventQueue = new EventQueue(localQueue.Scope); } if (!localQueue.Any()) { break; } ProcessQueue(localQueue); } while (true); lock (_lockObject) { _inProcess = false; } }
private void ServeEventQueue(bool predictive) { var cac = new Cac(predictive); while (EventQueue.Any()) { var @event = EventQueue.Dequeue(); if (@event.GetType() == typeof(CallStartedEvent)) { var evt = (CallStartedEvent)@event; cac.AdmitCall(evt); } else { var evt = (CallEndedEvent)@event; var mobileTerminal = HetNet.Instance.MobileTerminals .FirstOrDefault(x => x.MobileTerminalId == evt.MobileTerminalId); //Find if call was considered var call = HetNet.Instance.Rats .SelectMany(x => x.OngoingSessions) .SelectMany(x => x.ActiveCalls) .FirstOrDefault(x => x.CallId == evt.CallId); //if no session contains this call then it was never considered if (call is null) { //Log.Warning($"CallStartedEventCorresponding to {nameof(CallEndedEvent)} was rejected"); } else { Events.Add(evt); mobileTerminal.EndCall(evt); } } } }
/// <summary> /// This is costly, and should rarely be called. /// </summary> /// <param name="predicate"></param> protected void RemoveEventMatchingPredicate(Func <TEventArgs, bool> predicate) { if (predicate == null) { throw new ArgumentNullException(nameof(predicate)); } lock (SyncObj) { if (EventQueue.Count == 0) { return; } //If it doesn't have matching event then we don't need to touch the queue. if (!EventQueue.Any(predicate)) { return; } //All the events that DON'T match the predicate EventQueue = new Queue <TEventArgs>(EventQueue.Where(i => !predicate(i))); } }