public void Scheduler_AddTimer_RetrieveTask() { var scheduler = new SimpleScheduler(); var task = scheduler.Start(() => 0, 10); Assert.IsTrue(task.Active, "Task should be active after Scheduler Insertion..."); }
public void Scheduler_StopTimer_TaskInactive() { var scheduler = new SimpleScheduler(); var task = scheduler.Start(() => 1, 1); task.Stop(); Assert.IsFalse(task.Active, "Task should be inactive after Scheduler Stop..."); }
public void Scheduler_StopTimerLatency_TaskNotRun() { var scheduler = new SimpleScheduler(); var run = false; var task = scheduler.Start(() => { run = true; return 0; }, 100); System.Threading.Thread.Sleep(50); task.Stop(); System.Threading.Thread.Sleep(100); Assert.IsFalse(run, "Task should not have run when Stopping before delay..."); }
/// <summary> /// Starts the server /// </summary> /// <returns>True if the server was successfully started</returns> public override bool Start() { try { if (log.IsDebugEnabled) log.DebugFormat("Starting Server, Memory is {0}MB", GC.GetTotalMemory(false)/1024/1024); m_status = eGameServerStatus.GSS_Closed; Thread.CurrentThread.Priority = ThreadPriority.Normal; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; //--------------------------------------------------------------- //Try to compile the Scripts if (!InitComponent(CompileScripts(), "Script compilation")) return false; //--------------------------------------------------------------- //Try to init Server Properties if (!InitComponent(Properties.InitProperties, "Server Properties Lookup")) return false; //--------------------------------------------------------------- //Try loading the commands if (!InitComponent(ScriptMgr.LoadCommands(), "Loading Commands")) return false; //--------------------------------------------------------------- //Check and update the database if needed if (!UpdateDatabase()) return false; //--------------------------------------------------------------- //Try to init the server port if (!InitComponent(InitSocket(), "InitSocket()")) return false; //--------------------------------------------------------------- //Packet buffers if (!InitComponent(AllocatePacketBuffers(), "AllocatePacketBuffers()")) return false; //--------------------------------------------------------------- //Try to start the udp port if (!InitComponent(StartUDP(), "StartUDP()")) return false; //--------------------------------------------------------------- //Try to init the RSA key /* No Cryptlib currently if (log.IsInfoEnabled) log.Info("Generating RSA key, may take a minute, please wait..."); if (!InitComponent(CryptLib168.GenerateRSAKey(), "RSA key generation")) return false; */ //--------------------------------------------------------------- //Try to initialize the Scheduler if (!InitComponent(() => Scheduler = new Scheduler.SimpleScheduler(), "Scheduler Initialization")) return false; //--------------------------------------------------------------- //Try to initialize the WorldManager if (!InitComponent(() => WorldManager = new WorldManager(this), "Instancied World Manager Initialization")) return false; //--------------------------------------------------------------- //Try to initialize the PlayerManager if (!InitComponent(() => PlayerManager = new PlayerManager(this), "Player Manager Initialization")) return false; //--------------------------------------------------------------- //Try to initialize the NpcManager if (!InitComponent(() => NpcManager = new NpcManager(this), "NPC Manager Initialization")) return false; //--------------------------------------------------------------- //Try to start the Language Manager if (!InitComponent(LanguageMgr.Init(), "Multi Language Initialization")) return false; //Init the mail manager InitComponent(MailMgr.Init(), "Mail Manager Initialization"); //--------------------------------------------------------------- //Try to initialize the WorldMgr in early state RegionData[] regionsData; if (!InitComponent(WorldMgr.EarlyInit(out regionsData), "World Manager PreInitialization")) return false; //--------------------------------------------------------------- //Try to initialize the script components if (!InitComponent(StartScriptComponents(), "Script components")) return false; //--------------------------------------------------------------- //Load all faction managers if (!InitComponent(FactionMgr.Init(), "Faction Managers")) return false; //--------------------------------------------------------------- //Load artifact manager InitComponent(ArtifactMgr.Init(), "Artifact Manager"); //--------------------------------------------------------------- //Load all calculators if (!InitComponent(GameLiving.LoadCalculators(), "GameLiving.LoadCalculators()")) return false; //--------------------------------------------------------------- //Try to start the npc equipment if (!InitComponent(GameNpcInventoryTemplate.Init(), "Npc Equipment")) return false; //--------------------------------------------------------------- //Try to start the Npc Templates Manager if (!InitComponent(NpcTemplateMgr.Init(), "Npc Templates Manager")) return false; //--------------------------------------------------------------- //Load the house manager if (!InitComponent(HouseMgr.Start(), "House Manager")) return false; //--------------------------------------------------------------- //Create the market cache if (!InitComponent(MarketCache.Initialize(), "Market Cache")) return false; //--------------------------------------------------------------- //Load the region managers if (!InitComponent(WorldMgr.StartRegionMgrs(), "Region Managers")) return false; //--------------------------------------------------------------- //Load the area manager if (!InitComponent(AreaMgr.LoadAllAreas(), "Areas")) return false; //--------------------------------------------------------------- //Enable Worldsave timer now if (m_timer != null) { m_timer.Change(Timeout.Infinite, Timeout.Infinite); m_timer.Dispose(); } m_timer = new Timer(SaveTimerProc, null, SaveInterval*MINUTE_CONV, Timeout.Infinite); if (log.IsInfoEnabled) log.Info("World save timer: true"); //--------------------------------------------------------------- //Load all boats if (!InitComponent(BoatMgr.LoadAllBoats(), "Boat Manager")) return false; //--------------------------------------------------------------- //Load all guilds if (!InitComponent(GuildMgr.LoadAllGuilds(), "Guild Manager")) return false; //--------------------------------------------------------------- //Load the keep manager if (!InitComponent(StartKeepManager(), "Keep Manager")) return false; //--------------------------------------------------------------- //Load the door manager if (!InitComponent(DoorMgr.Init(), "Door Manager")) return false; //--------------------------------------------------------------- //Try to initialize the WorldMgr if (!InitComponent(WorldMgr.Init(regionsData), "World Manager Initialization")) return false; regionsData = null; //--------------------------------------------------------------- //Load the relic manager if (!InitComponent(RelicMgr.Init(), "Relic Manager")) return false; //--------------------------------------------------------------- //Load all crafting managers if (!InitComponent(CraftingMgr.Init(), "Crafting Managers")) return false; //--------------------------------------------------------------- //Load player titles manager if (!InitComponent(PlayerTitleMgr.Init(), "Player Titles Manager")) return false; //--------------------------------------------------------------- //Load behaviour manager if (!InitComponent(BehaviourMgr.Init(), "Behaviour Manager")) return false; //Load the quest managers if enabled if (Properties.LOAD_QUESTS) { if (!InitComponent(QuestMgr.Init(), "Quest Manager")) return false; } else { log.InfoFormat("Not Loading Quest Manager : Obeying Server Property <load_quests> - {0}", Properties.LOAD_QUESTS); } //--------------------------------------------------------------- //Notify our scripts that everything went fine! GameEventMgr.Notify(ScriptEvent.Loaded); //--------------------------------------------------------------- //Set the GameServer StartTick m_startTick = Environment.TickCount; //--------------------------------------------------------------- //Notify everyone that the server is now started! GameEventMgr.Notify(GameServerEvent.Started, this); //--------------------------------------------------------------- //Try to start the base server (open server port for connections) if (!InitComponent(base.Start(), "base.Start()")) return false; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); //--------------------------------------------------------------- //Open the server, players can now connect! m_status = eGameServerStatus.GSS_Open; if (log.IsInfoEnabled) log.Info("GameServer is now open for connections!"); //INIT WAS FINE! return true; } catch (Exception e) { if (log.IsErrorEnabled) log.Error("Failed to start the server", e); return false; } }
public void Scheduler_TimerException_LogDisplay() { var scheduler = new SimpleScheduler(); var task = scheduler.Start(() => { throw new Exception(); }, 1); System.Threading.Thread.Sleep(100); Assert.IsFalse(task.Active, "Task should have been scheduled to Test Exception..."); }
public void Scheduler_MultiInsertStopTest_AllTasksFinished() { var scheduler = new SimpleScheduler(); // Long Interval Tasks var longTasks = Enumerable.Range(0, 10).Select(i => scheduler.Start(() => 0, 1000)).ToArray(); System.Threading.Thread.Sleep(100); // Short Interval Tasks var shortTasks = Enumerable.Range(0, 10).Select(i => scheduler.Start(() => 0, 1)).ToArray(); System.Threading.Thread.Sleep(100); CollectionAssert.AreEqual(Enumerable.Range(0, 10).Select(i => false), shortTasks.Select(t => t.Active)); System.Threading.Thread.Sleep(900); CollectionAssert.AreEqual(Enumerable.Range(0, 10).Select(i => false), longTasks.Select(t => t.Active)); }
public void Scheduler_MultiInsertInterval_FixedCountRun() { var scheduler = new SimpleScheduler(); // Long Interval Tasks var longCount = Enumerable.Range(0, 10).Select(i => 0).ToArray(); var longTasks = Enumerable.Range(0, 10).Select(i => scheduler.Start(() => { longCount[i]++; return longCount[i] == 5 ? 0 : 500; }, 500)).ToArray(); System.Threading.Thread.Sleep(100); // Short Interval Tasks var shortCount = Enumerable.Range(0, 10).Select(i => 0).ToArray(); var shortTasks = Enumerable.Range(0, 10).Select(i => scheduler.Start(() => { shortCount[i]++; return shortCount[i] == 10 ? 0 : 100; }, 1)).ToArray(); System.Threading.Thread.Sleep(1100); CollectionAssert.AreEqual(Enumerable.Range(0, 10).Select(i => 10), shortCount); CollectionAssert.AreEqual(Enumerable.Range(0, 10).Select(i => false), shortTasks.Select(t => t.Active)); System.Threading.Thread.Sleep(1900); CollectionAssert.AreEqual(Enumerable.Range(0, 10).Select(i => 5), longCount); CollectionAssert.AreEqual(Enumerable.Range(0, 10).Select(i => false), longTasks.Select(t => t.Active)); }
public void Scheduler_TimerStartLatency_LowerThan() { var scheduler = new SimpleScheduler(); var start = SimpleScheduler.Ticks; var finished = long.MaxValue; var task = scheduler.Start(() => { finished = SimpleScheduler.Ticks; return 0; }, 1); System.Threading.Thread.Sleep(100); Assert.Less(finished - start, 100, "Scheduler Task Latency is higher than 100ms!"); Assert.IsFalse(task.Active, "Task Should be inactive after Scheduling..."); }
public void Scheduler_IntervalTimerFixedCount_CountRun() { var scheduler = new SimpleScheduler(); int count = 0; var task = scheduler.Start(() => { count++; return count == 10 ? 0 : 1; }, 1); System.Threading.Thread.Sleep(200); Assert.AreEqual(10, count, "Task Should have been Scheduled 10 times..."); Assert.IsFalse(task.Active, "Task should be Inactive after Fixed Schedule count..."); }
public void Scheduler_StartTimerOnce_TaskRunOnce() { var scheduler = new SimpleScheduler(); int count = 0; var task = scheduler.Start(() => { count++; return 0; }, 1); System.Threading.Thread.Sleep(100); Assert.AreEqual(count, 1, "Task Should have been Scheduled once with no Interval..."); Assert.IsFalse(task.Active, "Task Should be inactive after one Scheduling..."); }
public void Scheduler_StartIntervalTimer_TaskRunMultipleTime() { var scheduler = new SimpleScheduler(); int count = 0; var task = scheduler.Start(() => { count++; return 1; }, 1); System.Threading.Thread.Sleep(100); var intermediate = count; Assert.Greater(intermediate, 0, "Task Should have been Scheduled multiple time with an Interval of 1ms..."); System.Threading.Thread.Sleep(100); task.Stop(); Assert.Greater(count, intermediate, "Task should have been scheduled again before stopping..."); }
/// <summary> /// Create a new Instance of <see cref="WeatherManager"/> /// </summary> public WeatherManager(SimpleScheduler Scheduler) { this.Scheduler = Scheduler; RegionsWeather = new Dictionary<ushort, RegionWeather>(); RegionsTasks = new Dictionary<ushort, ScheduledTask>(); GameEventMgr.AddHandler(RegionEvent.RegionStart, OnRegionStart); GameEventMgr.AddHandler(RegionEvent.RegionStop, OnRegionStop); GameEventMgr.AddHandler(RegionEvent.PlayerEnter, OnPlayerEnter); }