public MatchManager(GameModel gameModel, TimerQueue timedQ, EventBus eventBus, EventFactory eventFactory, List<string> levelPaths) { _pointsPerCity = 100; _pointsPerRemainingShots = 10; _currentLevelIndex = 0; _userScore = 0; _timerQ = timedQ; _gameModel = gameModel; _levels = new List<Level>(); _eventFactory = eventFactory; _eventBus = eventBus; foreach (string path in levelPaths) { try { _levels.Add(new Level(path)); } catch (Exception e) { Console.WriteLine("ERROR caught exception loading level from path: "+path+e.Message+e.StackTrace); } } //initiaize the first level _loadLevel(_levels[0]); }
private void CreateTimerQueue() { Debug.Assert( TimerQueue == null ); TimerQueue = new TimerQueue(); _timerCompletionQueue = new FastLinkedList<TimerCompletionEntry>(); _timerSyncRoot = new object(); }
private void DestroyTimerQueue() { lock( _timerSyncRoot ) _timerCompletionQueue.Clear(); Debug.Assert( TimerQueue != null ); TimerQueue.Dispose(); TimerQueue = null; }
public RepeatingTimerHandle(TimerQueue queue, TimeSpan interval, TimerCallback callback, object state, bool started) { _queue = queue; _interval = interval; _callback = callback; _state = state; if (started) Start(); }
public void Setup() { _queue = new TimerQueue(); _dequeuedValues = new List <int>(); _now = DateTime.UtcNow; }
public TimerGroup() { waitableTimer = new WaitableTimer(); timerQueue = new TimerQueue(); }
static TimerQueue() { Default = new TimerQueue(IntPtr.Zero); }
internal TimerQueueTimer( TimerQueue queue, TimerCallback cb, object state, uint dueTime, uint period, TimerQueueTimerFlags flags) { MyQueue = queue; Callback = cb; win32WaitOrTimerCallback = TimerCallback; UserState = state; bool rslt = TQTimerWin32.CreateTimerQueueTimer( out Handle, MyQueue.Handle, win32WaitOrTimerCallback, IntPtr.Zero, dueTime, period, flags); if (!rslt) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Error creating timer."); } }
/// <summary> /// Determines if level is complete by checking if there are any pending /// events, or if any enemy objects still exist. /// </summary> /// <param name="gameObjects"></param> /// <param name="timerQ"></param> /// <returns></returns> bool _isLevelComplete(List<IGameObject> gameObjects, TimerQueue timerQ) { bool isComplete = false; //level not complete if any timed events remain if (timerQ.Count() <= 0) { if (!_enemiesExist(gameObjects)) { isComplete = true; } } return isComplete; }
private void Timer_Elapsed(object sender, ElapsedEventArgs e) { TimerQueue.Enqueue(() => { AhemStorage.Save(); }); }
public override void OnRegister() { Log.DebugFormat("TEXAS MACHINE IS ON REGISTERED"); base.OnRegister(); Queue = new TimerQueue(20); Queue.Initialize(); States = new StateMachine <StateCode, TexasTrigger>(StateCode.WaitingNewGame); _updateSlotTrigger = States.SetTriggerParameters <Slot>(TexasTrigger.action_update_slot); _sitTrigger = States.SetTriggerParameters <TexasSitDown>(TexasTrigger.action_sit); _betTrigger = States.SetTriggerParameters <TexasBetRequest>(TexasTrigger.action_bet); _startGameTrigger = States.SetTriggerParameters <TexasStartGame>(TexasTrigger.state_pre_flop); States.Configure(StateCode.WaitingNewGame) .OnEntry(stateChanged) .PermitReentry(TexasTrigger.action_sit) .OnEntryFrom(_sitTrigger, sitPar => OnSit(sitPar)) .PermitReentry(TexasTrigger.action_update_slot) .OnEntryFrom(_updateSlotTrigger, slotPar => OnUpdateSlot(slotPar)) .Permit(TexasTrigger.state_start_game, StateCode.StartGame) // .Permit(TexasTrigger.state_waiting_new_game, StateCode.WaitingNewGame) //.OnEntryFrom(_startGameTrigger, startPar => OnStartGame(startPar)); ; States.Configure(StateCode.StartGame) // .OnEntry(stateChanged) //.OnEntryFrom(_startGameTrigger, startPar => OnStartGame(startPar)) .PermitReentry(TexasTrigger.action_sit) .OnEntryFrom(_sitTrigger, sitPar => OnSit(sitPar)) .PermitReentry(TexasTrigger.action_update_slot) .OnEntryFrom(_updateSlotTrigger, slotPar => OnUpdateSlot(slotPar)) .Permit(TexasTrigger.state_play, StateCode.Play) //.PermitReentry(TexasTrigger.action_sit) //.OnEntryFrom(_sitTrigger, sitPar => OnSit(sitPar)) //.OnEntryFrom(TexasTrigger.state_pre_flop, stateChanged) //.Permit(TexasTrigger.state_pre_flop, StateCode.PreFlop); ; States.Configure(StateCode.Play) .PermitReentry(TexasTrigger.action_sit) .OnEntryFrom(_sitTrigger, sitPar => OnSit(sitPar)) .PermitReentry(TexasTrigger.action_update_slot) .OnEntryFrom(_updateSlotTrigger, slotPar => OnUpdateSlot(slotPar)) .PermitReentry(TexasTrigger.action_bet) .OnEntryFrom(_betTrigger, betPar => OnBet(betPar)) //.OnEntry(stateChanged) ; States.Configure(StateCode.PreFlop) .PermitReentry(TexasTrigger.action_sit) .OnEntryFrom(_sitTrigger, sitPar => OnSit(sitPar)) .PermitReentry(TexasTrigger.action_update_slot) .OnEntryFrom(_updateSlotTrigger, slotPar => OnUpdateSlot(slotPar)) .PermitReentry(TexasTrigger.action_bet) .OnEntryFrom(_betTrigger, betPar => OnBet(betPar)) .OnEntry(stateChanged) .Permit(TexasTrigger.state_flop, StateCode.Flop); States.Configure(StateCode.Flop) .PermitReentry(TexasTrigger.action_sit) .OnEntryFrom(_sitTrigger, sitPar => OnSit(sitPar)) .PermitReentry(TexasTrigger.action_update_slot) .OnEntryFrom(_updateSlotTrigger, slotPar => OnUpdateSlot(slotPar)) .PermitReentry(TexasTrigger.action_bet) .OnEntryFrom(_betTrigger, betPar => OnBet(betPar)) .OnEntry(stateChanged) .Permit(TexasTrigger.state_turn, StateCode.Turn); States.Configure(StateCode.Turn) .PermitReentry(TexasTrigger.action_sit) .OnEntryFrom(_sitTrigger, sitPar => OnSit(sitPar)) .PermitReentry(TexasTrigger.action_update_slot) .OnEntryFrom(_updateSlotTrigger, slotPar => OnUpdateSlot(slotPar)) .PermitReentry(TexasTrigger.action_bet) .OnEntryFrom(_betTrigger, betPar => OnBet(betPar)) .OnEntry(stateChanged) .Permit(TexasTrigger.state_river, StateCode.River); States.Configure(StateCode.River) .PermitReentry(TexasTrigger.action_sit) .OnEntryFrom(_sitTrigger, sitPar => OnSit(sitPar)) .PermitReentry(TexasTrigger.action_update_slot) .OnEntryFrom(_updateSlotTrigger, slotPar => OnUpdateSlot(slotPar)) .PermitReentry(TexasTrigger.action_bet) .OnEntryFrom(_betTrigger, betPar => OnBet(betPar)) .OnEntry(stateChanged) .Permit(TexasTrigger.state_showdown, StateCode.ShowDown); States.Configure(StateCode.ShowDown) .PermitReentry(TexasTrigger.action_sit) .OnEntryFrom(_sitTrigger, sitPar => OnSit(sitPar)) .PermitReentry(TexasTrigger.action_update_slot) .OnEntryFrom(_updateSlotTrigger, slotPar => OnUpdateSlot(slotPar)) .PermitReentry(TexasTrigger.action_bet) .OnEntryFrom(_betTrigger, betPar => OnBet(betPar)) .OnEntry(stateChanged) .Permit(TexasTrigger.state_flip_card, StateCode.FlipCard); States.Configure(StateCode.FlipCard) .PermitReentry(TexasTrigger.action_sit) .OnEntryFrom(_sitTrigger, sitPar => OnSit(sitPar)) .PermitReentry(TexasTrigger.action_update_slot) .OnEntryFrom(_updateSlotTrigger, slotPar => OnUpdateSlot(slotPar)) .OnEntry(stateChanged) .Permit(TexasTrigger.state_calculating, StateCode.Calculating); States.Configure(StateCode.Calculating) .PermitReentry(TexasTrigger.action_sit) .OnEntryFrom(_sitTrigger, sitPar => OnSit(sitPar)) .PermitReentry(TexasTrigger.action_update_slot) .OnEntryFrom(_updateSlotTrigger, slotPar => OnUpdateSlot(slotPar)) .OnEntry(stateChanged) .Permit(TexasTrigger.state_done, StateCode.Done); States.Configure(StateCode.Done) .PermitReentry(TexasTrigger.action_sit) .OnEntryFrom(_sitTrigger, sitPar => OnSit(sitPar)) .PermitReentry(TexasTrigger.action_update_slot) .OnEntryFrom(_updateSlotTrigger, slotPar => OnUpdateSlot(slotPar)) .OnEntry(stateChanged) .Permit(TexasTrigger.state_waiting_new_game, StateCode.WaitingNewGame); States.OnUnhandledTrigger((state, trigger) => { }); // States.Fire(TexasTrigger.state_waiting_new_game); // WaitingNewGame(); SendNotification(TexasTrigger.state_waiting_new_game.ToString("G"), null); }
// The main server loop public void MainLoop() { basePeriod = 1; period = basePeriod; var timerQueue = new TimerQueue(); stopWatch.Start(); mainLoopTimer = timerQueue.CreateTimer(s => { RunLoop(); }, null, 0, period); while (Active) { are.WaitOne(-1); DoMainLoopStuff(); } /* TimerCallback tcb = RunLoop; var due = 1;// (long)ServerRate / 3; stopWatch.Start(); //Start the clock mainLoopTimer = new Timer(tcb, are, 0, due); are.WaitOne(-1);*/ }
internal Timer( TimerQueue queue, TimerMode mode, TimerExecutionContext context, bool isLongRunning, uint dueTime, uint period, TimerCallback callback ) { Debug.Assert( queue != null ); Debug.Assert( callback != null ); Queue = queue; Mode = mode; ExecutionContext = context; IsLongRunning = isLongRunning; Period = period; DueTime = dueTime; Callback = callback; }
public TimerGroup() { this.waitableTimer = new WaitableTimer(); this.waitableTimer.Set(long.MaxValue); this.timerQueue = new TimerQueue(); }
public TimerGroup() { WaitableTimer = new WaitableTimer(); WaitableTimer.Set(long.MaxValue); TimerQueue = new TimerQueue(); }
/// <summary> /// <para>Thread for the timer. Ignores all exceptions. If no activity occurs for a while, /// the thread will shut down.</para> /// </summary> private static void ThreadProc() { if (NetEventSource.IsEnabled) { NetEventSource.Enter(null); } #if DEBUG DebugThreadTracking.SetThreadSource(ThreadKinds.Timer); using (DebugThreadTracking.SetThreadKind(ThreadKinds.System | ThreadKinds.Async)) { #endif // Set this thread as a background thread. On AppDomain/Process shutdown, the thread will just be killed. Thread.CurrentThread.IsBackground = true; // Keep a permanent lock on s_Queues. This lets for example Shutdown() know when this thread isn't running. lock (s_Queues) { // If shutdown was recently called, abort here. if (Interlocked.CompareExchange(ref s_ThreadState, (int)TimerThreadState.Running, (int)TimerThreadState.Running) != (int)TimerThreadState.Running) { return; } bool running = true; while (running) { try { s_ThreadReadyEvent.Reset(); while (true) { // Copy all the new queues to the real queues. Since only this thread modifies the real queues, it doesn't have to lock it. if (s_NewQueues.Count > 0) { lock (s_NewQueues) { for (LinkedListNode <WeakReference> node = s_NewQueues.First; node != null; node = s_NewQueues.First) { s_NewQueues.Remove(node); s_Queues.AddLast(node); } } } int now = Environment.TickCount; int nextTick = 0; bool haveNextTick = false; for (LinkedListNode <WeakReference> node = s_Queues.First; node != null; /* node = node.Next must be done in the body */) { TimerQueue queue = (TimerQueue)node.Value.Target; if (queue == null) { LinkedListNode <WeakReference> next = node.Next; s_Queues.Remove(node); node = next; continue; } // Fire() will always return values that should be interpreted as later than 'now' (that is, even if 'now' is // returned, it is 0x100000000 milliseconds in the future). There's also a chance that Fire() will return a value // intended as > 0x100000000 milliseconds from 'now'. Either case will just cause an extra scan through the timers. int nextTickInstance; if (queue.Fire(out nextTickInstance) && (!haveNextTick || IsTickBetween(now, nextTick, nextTickInstance))) { nextTick = nextTickInstance; haveNextTick = true; } node = node.Next; } // Figure out how long to wait, taking into account how long the loop took. // Add 15 ms to compensate for poor TickCount resolution (want to guarantee a firing). int newNow = Environment.TickCount; int waitDuration = haveNextTick ? (int)(IsTickBetween(now, nextTick, newNow) ? Math.Min(unchecked ((uint)(nextTick - newNow)), (uint)(Int32.MaxValue - c_TickCountResolution)) + c_TickCountResolution : 0) : c_ThreadIdleTimeoutMilliseconds; if (NetEventSource.IsEnabled) { NetEventSource.Info(null, $"Waiting for {waitDuration}ms"); } int waitResult = WaitHandle.WaitAny(s_ThreadEvents, waitDuration, false); // 0 is s_ThreadShutdownEvent - die. if (waitResult == 0) { if (NetEventSource.IsEnabled) { NetEventSource.Info(null, "Awoke, cause: Shutdown"); } running = false; break; } if (NetEventSource.IsEnabled) { NetEventSource.Info(null, $"Awoke, cause {(waitResult == WaitHandle.WaitTimeout ? "Timeout" : "Prod")}"); } // If we timed out with nothing to do, shut down. if (waitResult == WaitHandle.WaitTimeout && !haveNextTick) { Interlocked.CompareExchange(ref s_ThreadState, (int)TimerThreadState.Idle, (int)TimerThreadState.Running); // There could have been one more prod between the wait and the exchange. Check, and abort if necessary. if (s_ThreadReadyEvent.WaitOne(0, false)) { if (Interlocked.CompareExchange(ref s_ThreadState, (int)TimerThreadState.Running, (int)TimerThreadState.Idle) == (int)TimerThreadState.Idle) { continue; } } running = false; break; } } } catch (Exception exception) { if (ExceptionCheck.IsFatal(exception)) { throw; } if (NetEventSource.IsEnabled) { NetEventSource.Error(null, exception); } // The only options are to continue processing and likely enter an error-loop, // shut down timers for this AppDomain, or shut down the AppDomain. Go with shutting // down the AppDomain in debug, and going into a loop in retail, but try to make the // loop somewhat slow. Note that in retail, this can only be triggered by OutOfMemory or StackOverflow, // or an exception thrown within TimerThread - the rest are caught in Fire(). #if !DEBUG Thread.Sleep(1000); #else throw; #endif } } } if (NetEventSource.IsEnabled) { NetEventSource.Info(null, "Stop"); } #if DEBUG } #endif }
private static void ThreadProc() { Thread.CurrentThread.IsBackground = true; lock (s_Queues) { if (Interlocked.CompareExchange(ref s_ThreadState, 1, 1) == 1) { bool flag = true; while (flag) { try { s_ThreadReadyEvent.Reset(); Label_0043: if (s_NewQueues.Count > 0) { lock (s_NewQueues) { for (LinkedListNode <WeakReference> node = s_NewQueues.First; node != null; node = s_NewQueues.First) { s_NewQueues.Remove(node); s_Queues.AddLast(node); } } } int tickCount = Environment.TickCount; int end = 0; bool flag3 = false; LinkedListNode <WeakReference> first = s_Queues.First; while (first != null) { TimerQueue target = (TimerQueue)first.Value.Target; if (target == null) { LinkedListNode <WeakReference> next = first.Next; s_Queues.Remove(first); first = next; } else { int num3; if (target.Fire(out num3) && (!flag3 || IsTickBetween(tickCount, end, num3))) { end = num3; flag3 = true; } first = first.Next; } } int comparand = Environment.TickCount; int millisecondsTimeout = flag3 ? (IsTickBetween(tickCount, end, comparand) ? (((int)Math.Min((uint)(end - comparand), 0x7ffffff0)) + 15) : 0) : 0x7530; int num6 = WaitHandle.WaitAny(s_ThreadEvents, millisecondsTimeout, false); if (num6 == 0) { flag = false; } else { if ((num6 != 0x102) || flag3) { goto Label_0043; } Interlocked.CompareExchange(ref s_ThreadState, 0, 1); if (s_ThreadReadyEvent.WaitOne(0, false) && (Interlocked.CompareExchange(ref s_ThreadState, 1, 0) == 0)) { goto Label_0043; } flag = false; } continue; } catch (Exception exception) { if (NclUtilities.IsFatal(exception)) { throw; } if (Logging.On) { Logging.PrintError(Logging.Web, "TimerThread#" + Thread.CurrentThread.ManagedThreadId.ToString(NumberFormatInfo.InvariantInfo) + "::ThreadProc() - Exception:" + exception.ToString()); } Thread.Sleep(0x3e8); continue; } } } } }