diff --git a/MCGalaxy/Bots/PlayerBot.cs b/MCGalaxy/Bots/PlayerBot.cs index 54a5e43c9..7ad723bf8 100644 --- a/MCGalaxy/Bots/PlayerBot.cs +++ b/MCGalaxy/Bots/PlayerBot.cs @@ -161,7 +161,7 @@ namespace MCGalaxy { void UpdatePosition() { if (movement) { - double scale = Math.Ceiling(Server.updateTimer.Interval / 25.0); + double scale = Math.Ceiling(Server.PositionInterval / 25.0); int steps = movementSpeed * (int)scale; for (int i = 0; i < steps; i++) DoMove(); diff --git a/MCGalaxy/Chat/ChatModes.cs b/MCGalaxy/Chat/ChatModes.cs index c0fc67d6b..f85bdfbe7 100644 --- a/MCGalaxy/Chat/ChatModes.cs +++ b/MCGalaxy/Chat/ChatModes.cs @@ -33,11 +33,11 @@ namespace MCGalaxy { if (text[0] == '@') text = text.Remove(0, 1).Trim(); if (p == null || p.whisperTo == "") { - int pos = text.IndexOf(' '); - if ( pos != -1 ) { - string to = text.Substring(0, pos); - string msg = text.Substring(pos + 1); - HandleWhisper(p, to, msg); + int sepIndex = text.IndexOf(' '); + if (sepIndex != -1) { + string target = text.Substring(0, sepIndex); + text = text.Substring(sepIndex + 1); + HandleWhisper(p, target, text); } else { Player.Message(p, "No message entered"); } diff --git a/MCGalaxy/Commands/Information/CmdServerInfo.cs b/MCGalaxy/Commands/Information/CmdServerInfo.cs index ef0901f87..5cf48b5bb 100644 --- a/MCGalaxy/Commands/Information/CmdServerInfo.cs +++ b/MCGalaxy/Commands/Information/CmdServerInfo.cs @@ -48,7 +48,7 @@ namespace MCGalaxy.Commands.Info { up.Shorten(true), Server.SoftwareName, Server.VersionString); Player.Message(p, "Player positions are updated every &b" - + Server.updateTimer.Interval + " %Smilliseconds."); + + Server.PositionInterval + " %Smilliseconds."); string owner = Server.server_owner; if (!owner.CaselessEq("Notch")) Player.Message(p, "Owner is &3{0}. %SConsole state: &3{1}", owner, Server.ZallState); diff --git a/MCGalaxy/Commands/Maintenance/CmdLowlag.cs b/MCGalaxy/Commands/Maintenance/CmdLowlag.cs index 1ab09ddd4..d0a503fe7 100644 --- a/MCGalaxy/Commands/Maintenance/CmdLowlag.cs +++ b/MCGalaxy/Commands/Maintenance/CmdLowlag.cs @@ -23,7 +23,7 @@ namespace MCGalaxy.Commands.Maintenance { public override LevelPermission defaultRank { get { return LevelPermission.Operator; } } public override void Use(Player p, string message) { - if (message == "" && Server.updateTimer.Interval > 1000) { + if (message == "" && Server.PositionInterval > 1000) { Server.PositionInterval = 100; Chat.MessageGlobal("&dLow lag %Sturned &cOFF %S- positions update every &b100 %Sms."); } else if (message == "") { @@ -36,7 +36,6 @@ namespace MCGalaxy.Commands.Maintenance { Server.PositionInterval = interval; Chat.MessageGlobal("Positions now update every &b{0} %Smilliseconds.", interval); } - Server.updateTimer.Interval = Server.PositionInterval; SrvProperties.Save(); } diff --git a/MCGalaxy/Config/ServerProperties.cs b/MCGalaxy/Config/ServerProperties.cs index 893fc3279..f28733dfa 100644 --- a/MCGalaxy/Config/ServerProperties.cs +++ b/MCGalaxy/Config/ServerProperties.cs @@ -47,12 +47,10 @@ namespace MCGalaxy { ZombieGameProps.SaveSettings(); ZombieGameProps.LoadSettings(); - Database.Backend = Server.useMySQL ? - MySQLBackend.Instance : SQLiteBackend.Instance; + Database.Backend = Server.useMySQL ? MySQLBackend.Instance : SQLiteBackend.Instance; if (!Directory.Exists(Server.backupLocation)) Server.backupLocation = Path.Combine(Utils.FolderPath, "levels/backups"); - Server.updateTimer.Interval = Server.PositionInterval; Save(givenPath); } diff --git a/MCGalaxy/Player/Player.Login.cs b/MCGalaxy/Player/Player.Login.cs index 0aac66b68..98aac1e44 100644 --- a/MCGalaxy/Player/Player.Login.cs +++ b/MCGalaxy/Player/Player.Login.cs @@ -177,7 +177,7 @@ namespace MCGalaxy { } catch { } - if (Server.updateTimer.Interval > 1000) + if (Server.PositionInterval > 1000) SendMessage("Lowlag mode is currently &aON."); if (String.IsNullOrEmpty(appName)) { diff --git a/MCGalaxy/Server/Server.Fields.cs b/MCGalaxy/Server/Server.Fields.cs index 2a104e00a..39c0ffcac 100644 --- a/MCGalaxy/Server/Server.Fields.cs +++ b/MCGalaxy/Server/Server.Fields.cs @@ -70,9 +70,7 @@ namespace MCGalaxy { // URL hash for connecting to the server public static string Hash = String.Empty, URL = String.Empty; - public static Socket listen; - public static System.Timers.Timer updateTimer = new System.Timers.Timer(100); //Chatrooms public static List Chatrooms = new List(); @@ -123,6 +121,7 @@ namespace MCGalaxy { public static Scheduler MainScheduler = new Scheduler("MCG_MainScheduler"); public static Scheduler Background = new Scheduler("MCG_BackgroundScheduler"); + public static Scheduler Critical = new Scheduler("MCG_CriticalScheduler"); public static Server s; public const byte version = 7; diff --git a/MCGalaxy/Server/Server.Init.cs b/MCGalaxy/Server/Server.Init.cs index 6635ec772..e3c5ceb6b 100644 --- a/MCGalaxy/Server/Server.Init.cs +++ b/MCGalaxy/Server/Server.Init.cs @@ -102,33 +102,29 @@ namespace MCGalaxy { } void InitTimers() { - updateTimer.Elapsed += delegate { - Entities.GlobalUpdate(); - PlayerBot.GlobalUpdatePosition(); - }; - updateTimer.Start(); - if (File.Exists(Paths.AnnouncementsFile)) { string[] lines = File.ReadAllLines(Paths.AnnouncementsFile); messages = new List(lines); } else { using (File.Create(Paths.AnnouncementsFile)) {} } - Server.MainScheduler.QueueRepeat(RandomMessage, null, TimeSpan.FromMinutes(5)); + + MainScheduler.QueueRepeat(RandomMessage, null, + TimeSpan.FromMinutes(5)); + Critical.QueueRepeat(ServerTasks.UpdateEntityPositions, null, + TimeSpan.FromMilliseconds(PositionInterval)); } void InitRest() { IRC = new IRCBot(); if (Server.irc) IRC.Connect(); - - locationChecker = new Thread(ServerTasks.LocationChecks); - locationChecker.Name = "MCG_LocationCheck"; - locationChecker.Start(); InitZombieSurvival(); InitLavaSurvival(); MainScheduler.QueueRepeat(BlockQueue.Loop, null, TimeSpan.FromMilliseconds(BlockQueue.time)); + Critical.QueueRepeat(ServerTasks.LocationChecks, null, + TimeSpan.FromMilliseconds(20)); Log("Finished setting up server, finding classicube.net url.."); ServerSetupFinished = true; diff --git a/MCGalaxy/Server/Tasks/ServerTasks.cs b/MCGalaxy/Server/Tasks/ServerTasks.cs index 110cae09b..2db9d8cbc 100644 --- a/MCGalaxy/Server/Tasks/ServerTasks.cs +++ b/MCGalaxy/Server/Tasks/ServerTasks.cs @@ -26,46 +26,51 @@ using MCGalaxy.Maths; namespace MCGalaxy.Tasks { internal static class ServerTasks { - internal static void LocationChecks() { - while (true) { - Player[] players = PlayerInfo.Online.Items; - Thread.Sleep(players.Length == 0 ? 20 : 10); - players = PlayerInfo.Online.Items; - - for (int i = 0; i < players.Length; i++) { - try { - Player p = players[i]; + internal static void LocationChecks(SchedulerTask task) { + Player[] players = PlayerInfo.Online.Items; + players = PlayerInfo.Online.Items; + int delay = players.Length == 0 ? 100 : 20; + task.Delay = TimeSpan.FromMilliseconds(delay); + + for (int i = 0; i < players.Length; i++) { + try { + Player p = players[i]; - if (p.following != "") { - Player who = PlayerInfo.FindExact(p.following); - if (who == null || who.level != p.level) { - p.following = ""; - if (!p.canBuild) - p.canBuild = true; - if (who != null && who.possess == p.name) - who.possess = ""; - continue; - } - - p.SendPos(Entities.SelfID, who.Pos, who.Rot); - } else if (p.possess != "") { - Player who = PlayerInfo.FindExact(p.possess); - if (who == null || who.level != p.level) - p.possess = ""; + if (p.following != "") { + Player who = PlayerInfo.FindExact(p.following); + if (who == null || who.level != p.level) { + p.following = ""; + if (!p.canBuild) + p.canBuild = true; + if (who != null && who.possess == p.name) + who.possess = ""; + continue; } - Vec3U16 P = (Vec3U16)p.Pos.BlockCoords; - if (p.level.Death) - p.CheckSurvival(P.X, P.Y, P.Z); - p.CheckBlock(); - p.oldIndex = p.level.PosToInt(P.X, P.Y, P.Z); - } catch (Exception e) { - Server.ErrorLog(e); + p.SendPos(Entities.SelfID, who.Pos, who.Rot); + } else if (p.possess != "") { + Player who = PlayerInfo.FindExact(p.possess); + if (who == null || who.level != p.level) + p.possess = ""; } + + Vec3U16 P = (Vec3U16)p.Pos.BlockCoords; + if (p.level.Death) + p.CheckSurvival(P.X, P.Y, P.Z); + p.CheckBlock(); + p.oldIndex = p.level.PosToInt(P.X, P.Y, P.Z); + } catch (Exception e) { + Server.ErrorLog(e); } } } + internal static void UpdateEntityPositions(SchedulerTask task) { + Entities.GlobalUpdate(); + PlayerBot.GlobalUpdatePosition(); + task.Delay = TimeSpan.FromMilliseconds(Server.PositionInterval); + } + internal static void CheckState(SchedulerTask task) { Player[] players = PlayerInfo.Online.Items; foreach (Player p in players) { diff --git a/MCGalaxy/util/Threading/Scheduler.cs b/MCGalaxy/util/Threading/Scheduler.cs index 407b3d963..298c4681e 100644 --- a/MCGalaxy/util/Threading/Scheduler.cs +++ b/MCGalaxy/util/Threading/Scheduler.cs @@ -77,21 +77,26 @@ namespace MCGalaxy.Tasks { SchedulerTask GetNextTask() { - DateTime now = DateTime.UtcNow; + DateTime minTime = DateTime.UtcNow; + SchedulerTask minTask = null; + lock (taskLock) { foreach (SchedulerTask task in tasks) { - if (task.NextRun < now) return task; + if (task.NextRun > minTime) continue; + minTime = task.NextRun; minTask = task; } } - return null; + return minTask; } + SchedulerTask lastTask = null; void DoTask(SchedulerTask task) { try { task.Callback(task); } catch (Exception ex) { MCGalaxy.Server.ErrorLog(ex); } + lastTask = task; if (task.Repeating) { task.NextRun = DateTime.UtcNow.Add(task.Delay);