public DerelictTimer(IMyCubeGrid grid) { m_Grid = grid; m_TimerInfo = null; m_Timer = null; CompletedPhase = DT_INFO.PHASE.NONE; m_Logger = new Logger(m_Grid.EntityId.ToString(), "DerelictTimer"); }
public void Wait() { m_timer.AddElapsed(MyTimeSpan.FromMilliseconds(-m_delta)); var currentTicks = m_timer.ElapsedTicks; // Wait for correct frame start m_targetTicks += TickPerFrame; if ((currentTicks > m_targetTicks + TickPerFrame * 5) || EnableMaxSpeed) { // We're more behind than 5 frames, don't try to catch up // (or we just do not want to wait in EnableMaxSpeed mode) m_targetTicks = currentTicks; } else { // For until correct tick comes if (EnableUpdateWait) { var remaining = MyTimeSpan.FromTicks(m_targetTicks - currentTicks); int waitMs = (int)(remaining.Milliseconds - 0.1); // To handle up to 0.1ms inaccuracy of timer if (waitMs > 0) { m_waiter.Reset(); MyTimer.StartOneShot(waitMs, m_handler); m_waiter.Wait(17 + m_delta); // Never wait more than 17ms //Debug.Assert(MyPerformanceCounter.ElapsedTicks < m_targetTicks); //VRageRender.MyRenderStats.Write("WaitRemaining", (float)MyPerformanceCounter.TicksToMs(m_targetTicks - MyPerformanceCounter.ElapsedTicks), VRageRender.MyStatTypeEnum.MinMaxAvg, 300, 3); } } // Sanity check to prevent freezing in the loop, wait at most 1 and 1/4 of frame if (m_targetTicks < (m_timer.ElapsedTicks + TickPerFrame + TickPerFrame / 4)) { while (m_timer.ElapsedTicks < m_targetTicks) { } // Busy wait for the precise moment } else // Something went terribly wrong, reset target ticks { m_targetTicks = m_timer.ElapsedTicks; } } m_delta = 0; }
public override void unloadData() { log("Unloading", "unloadData"); GridEnforcer.OnPlacementViolation -= eventPlacementViolation; GridEnforcer.OnCleanupViolation -= eventCleanupViolation; GridEnforcer.OnCleanupTimerStart -= eventCleanupTimerStart; GridEnforcer.OnCleanupTimerEnd -= eventCleanupTimerEnd; ControlPoint.OnRewardsDistributed -= notifyPlayersOfCPResults; if (m_LocalReceiver != null) { m_MailMan.localMsgSent -= m_LocalReceiver.incomming; m_LocalReceiver.unload(); m_LocalReceiver = null; } m_MailMan.unload(); if (!MyAPIGateway.Utilities.IsDedicated) m_CmdProc.shutdown(); m_RoundTimer.Dispose(); m_RoundTimer = null; m_SaveTimer.Dispose(); m_SaveTimer = null; s_Logger = null; }
/// <summary> /// Starts up the core on the server /// </summary> public override void initialize() { if (MyAPIGateway.Session == null || m_Initialized) return; if (s_Logger == null) s_Logger = new Logger("Conquest Core", "Server"); log("Conquest core (Server) started"); s_Settings = ConquestSettings.getInstance(); s_DelayedSettingWrite = s_Settings.WriteFailed; // Start round timer m_RoundTimer = new MyTimer(s_Settings.CPPeriod * 1000, roundEnd); m_RoundTimer.Start(); log("Round timer started with " + s_Settings.CPPeriod + " seconds"); // Start save timer m_SaveTimer = new MyTimer(Constants.SaveInterval * 1000, saveTimer); m_SaveTimer.Start(); log("Save timer started"); m_MailMan = new RequestProcessor(); // If the server is a player (non-dedicated) they also need to receive notifications if (!MyAPIGateway.Utilities.IsDedicated) { m_LocalReceiver = new ResponseProcessor(false); m_MailMan.localMsgSent += m_LocalReceiver.incomming; m_CmdProc = new CommandProcessor(m_LocalReceiver); m_CmdProc.initialize(); m_LocalReceiver.requestSettings(); } // Subscribe events GridEnforcer.OnPlacementViolation += eventPlacementViolation; GridEnforcer.OnCleanupViolation += eventCleanupViolation; GridEnforcer.OnCleanupTimerStart += eventCleanupTimerStart; GridEnforcer.OnCleanupTimerEnd += eventCleanupTimerEnd; ControlPoint.OnRewardsDistributed += notifyPlayersOfCPResults; m_Initialized = true; }
public void Dispose() { if (m_timer != null) { lock (m_timer) { Packet packet; while (m_receiveQueue.Count > 0) { if (m_receiveQueue.TryDequeue(out packet)) { packet.Delete(); } } m_timer.Dispose(); m_timer = null; m_timerAction = null; } } if (!m_peer.IsNull) { m_peer.Shutdown(300); m_peer.Delete(); } GC.SuppressFinalize(this); }
protected StartupResultEnum Startup(uint maxConnections, ushort port, string host) { StartupResultEnum result = m_peer.Startup(maxConnections, port, host); if (result == StartupResultEnum.RAKNET_STARTED) { m_timerAction = new Action(ReceiveMessageInternal); m_timer = new MyTimer(1, m_timerAction); m_timer.Start(); } return result; }
public MyReceiveQueue(int channel, Mode readMode = Mode.Synchronized, int defaultMessageCount = 1, Func<TimeSpan> timestampProvider = null) { #if !XB1 Trace.Assert(readMode != Mode.Spin, "Spin mode should be used only for testing purposes, it keeps CPU under heavy load!"); #else // XB1 System.Diagnostics.Debug.Assert(readMode != Mode.Spin, "Spin mode should be used only for testing purposes, it keeps CPU under heavy load!"); #endif // XB1 Disposed = false; Channel = channel; ReadMode = readMode; m_messagePool = new MyConcurrentPool<Message>(defaultMessageCount, true); m_receiveQueue = new MyConcurrentQueue<Message>(defaultMessageCount); m_timestampProvider = timestampProvider; if (readMode == Mode.Spin) { m_readThread = new Thread(ReceiveThread); m_readThread.CurrentCulture = CultureInfo.InvariantCulture; m_readThread.CurrentUICulture = CultureInfo.InvariantCulture; m_readThread.Start(); } else if (readMode == Mode.Timer) { m_timerAction = new Action(ReceiveTimer); m_timer = new MyTimer(1, m_timerAction); m_timer.Start(); } }
/// <summary> /// Cancels the dereliction timer /// </summary> /// <returns>False if there was no timer to cancel</returns> public bool cancel() { if (m_Timer == null) return false; StateTracker.getInstance().removeDerelictTimer(m_TimerInfo.GridID); m_Timer.Dispose(); m_Timer = null; m_TimerInfo = null; TimerExpired = false; log("Timer cancelled", "cancel"); return true; }
private void timerExpired() { if (m_Timer == null) return; log("Dereliction timer has expired", "timerExpired"); StateTracker.getInstance().removeDerelictTimer(m_TimerInfo.GridID); m_Timer.Dispose(); m_Timer = null; CompletedPhase = m_TimerInfo.Phase; m_TimerInfo = null; TimerExpired = true; }
/// <summary> /// Starts or resumes the derelict timer /// </summary> /// <returns>Returns false if dereliction is disabled</returns> public bool start() { int seconds = ConquestSettings.getInstance().CleanupPeriod; int settingsTimerLength = seconds * 1000; log("Starting timer with settings start value of " + seconds + " seconds.", "start", Logger.severity.TRACE); if (seconds < 0) { log("Dereliction timers disabled. No timer started.", "start"); return false; } // Check if there is a timer to resume for this entity DT_INFO existing = StateTracker.getInstance().findActiveDerelictTimer(m_Grid.EntityId); if (existing != null) { // Resuming an existing timer log("Resuming existing timer", "start"); m_TimerInfo = existing; // If the settings Timer Length has changed, update this timer accordingly if (m_TimerInfo.TimerLength != settingsTimerLength) { log("Timer length has changed from " + m_TimerInfo.TimerLength + "ms to " + settingsTimerLength + "ms", "start"); int savedMillis = m_TimerInfo.MillisRemaining; decimal lengthRatio = (decimal)settingsTimerLength / (decimal)m_TimerInfo.TimerLength; int correctedMillis = (int)(savedMillis * lengthRatio); log("Changing this timer from " + savedMillis + "ms to " + correctedMillis + "ms using ratio " + lengthRatio, "start"); m_TimerInfo.MillisRemaining = correctedMillis; m_TimerInfo.TimerLength = settingsTimerLength; } m_Timer = new MyTimer(m_TimerInfo.MillisRemaining, timerExpired); m_Timer.Start(); log("Timer resumed with " + m_TimerInfo.MillisRemaining + "ms", "start"); } else { // Starting a new timer log("Starting new timer", "start"); m_TimerInfo = new DT_INFO(); m_TimerInfo.GridID = m_Grid.EntityId; m_TimerInfo.Phase = DT_INFO.PHASE.INITIAL; m_TimerInfo.TimerLength = seconds * 1000; m_TimerInfo.MillisRemaining = m_TimerInfo.TimerLength; m_TimerInfo.LastUpdated = DateTime.UtcNow; m_Timer = new MyTimer(m_TimerInfo.MillisRemaining, timerExpired); m_Timer.Start(); log("Timer started with " + m_TimerInfo.MillisRemaining + "ms", "start"); StateTracker.getInstance().addNewDerelictTimer(m_TimerInfo); } return true; }
/// <summary> /// Starts up the core on the server /// </summary> public override void initialize() { if (MyAPIGateway.Session == null || m_Initialized) return; if (s_Logger == null) s_Logger = new Logger("Conquest Core", "Server"); log("Conquest core (Server) started"); s_TokenBuilder = new MyObjectBuilder_Component() { SubtypeName = "ShipLicense" }; s_TokenDef = new VRage.ObjectBuilders.SerializableDefinitionId( typeof(MyObjectBuilder_InventoryItem), "ShipLicense"); s_Sorter = new GridSorter(); s_Settings = ConquestSettings.getInstance(); s_DelayedSettingWrite = s_Settings.WriteFailed; // Start round timer m_RoundTimer = new MyTimer(s_Settings.CPPeriod * 1000, roundEnd); m_RoundTimer.Start(); log("Round timer started"); // Start save timer m_SaveTimer = new MyTimer(Constants.SaveInterval * 1000, saveTimer); m_SaveTimer.Start(); log("Save timer started"); m_MailMan = new RequestProcessor(); // If the server is a player (non-dedicated) they also need to receive notifications if (!MyAPIGateway.Utilities.IsDedicated) { m_LocalReceiver = new ResponseProcessor(false); m_MailMan.localMsgSent += m_LocalReceiver.incomming; m_CmdProc = new CommandProcessor(m_LocalReceiver); m_CmdProc.initialize(); m_LocalReceiver.requestSettings(); } // Subscribe events GridEnforcer.OnPlacementViolation += eventPlacementViolation; GridEnforcer.OnCleanupViolation += eventCleanupViolation; GridEnforcer.OnCleanupTimerStart += eventCleanupTimerStart; GridEnforcer.OnCleanupTimerEnd += eventCleanupTimerEnd; m_Initialized = true; }