LocationChecks and PositionUpdate now share a scheduler thread.

This commit is contained in:
UnknownShadow200 2017-05-31 11:03:01 +10:00
parent 16e3545551
commit 09e10eb75b
10 changed files with 63 additions and 61 deletions

View File

@ -161,7 +161,7 @@ namespace MCGalaxy {
void UpdatePosition() { void UpdatePosition() {
if (movement) { if (movement) {
double scale = Math.Ceiling(Server.updateTimer.Interval / 25.0); double scale = Math.Ceiling(Server.PositionInterval / 25.0);
int steps = movementSpeed * (int)scale; int steps = movementSpeed * (int)scale;
for (int i = 0; i < steps; i++) for (int i = 0; i < steps; i++)
DoMove(); DoMove();

View File

@ -33,11 +33,11 @@ namespace MCGalaxy {
if (text[0] == '@') text = text.Remove(0, 1).Trim(); if (text[0] == '@') text = text.Remove(0, 1).Trim();
if (p == null || p.whisperTo == "") { if (p == null || p.whisperTo == "") {
int pos = text.IndexOf(' '); int sepIndex = text.IndexOf(' ');
if ( pos != -1 ) { if (sepIndex != -1) {
string to = text.Substring(0, pos); string target = text.Substring(0, sepIndex);
string msg = text.Substring(pos + 1); text = text.Substring(sepIndex + 1);
HandleWhisper(p, to, msg); HandleWhisper(p, target, text);
} else { } else {
Player.Message(p, "No message entered"); Player.Message(p, "No message entered");
} }

View File

@ -48,7 +48,7 @@ namespace MCGalaxy.Commands.Info {
up.Shorten(true), Server.SoftwareName, Server.VersionString); up.Shorten(true), Server.SoftwareName, Server.VersionString);
Player.Message(p, "Player positions are updated every &b" Player.Message(p, "Player positions are updated every &b"
+ Server.updateTimer.Interval + " %Smilliseconds."); + Server.PositionInterval + " %Smilliseconds.");
string owner = Server.server_owner; string owner = Server.server_owner;
if (!owner.CaselessEq("Notch")) if (!owner.CaselessEq("Notch"))
Player.Message(p, "Owner is &3{0}. %SConsole state: &3{1}", owner, Server.ZallState); Player.Message(p, "Owner is &3{0}. %SConsole state: &3{1}", owner, Server.ZallState);

View File

@ -23,7 +23,7 @@ namespace MCGalaxy.Commands.Maintenance {
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } } public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
public override void Use(Player p, string message) { public override void Use(Player p, string message) {
if (message == "" && Server.updateTimer.Interval > 1000) { if (message == "" && Server.PositionInterval > 1000) {
Server.PositionInterval = 100; Server.PositionInterval = 100;
Chat.MessageGlobal("&dLow lag %Sturned &cOFF %S- positions update every &b100 %Sms."); Chat.MessageGlobal("&dLow lag %Sturned &cOFF %S- positions update every &b100 %Sms.");
} else if (message == "") { } else if (message == "") {
@ -36,7 +36,6 @@ namespace MCGalaxy.Commands.Maintenance {
Server.PositionInterval = interval; Server.PositionInterval = interval;
Chat.MessageGlobal("Positions now update every &b{0} %Smilliseconds.", interval); Chat.MessageGlobal("Positions now update every &b{0} %Smilliseconds.", interval);
} }
Server.updateTimer.Interval = Server.PositionInterval;
SrvProperties.Save(); SrvProperties.Save();
} }

View File

@ -47,12 +47,10 @@ namespace MCGalaxy {
ZombieGameProps.SaveSettings(); ZombieGameProps.SaveSettings();
ZombieGameProps.LoadSettings(); ZombieGameProps.LoadSettings();
Database.Backend = Server.useMySQL ? Database.Backend = Server.useMySQL ? MySQLBackend.Instance : SQLiteBackend.Instance;
MySQLBackend.Instance : SQLiteBackend.Instance;
if (!Directory.Exists(Server.backupLocation)) if (!Directory.Exists(Server.backupLocation))
Server.backupLocation = Path.Combine(Utils.FolderPath, "levels/backups"); Server.backupLocation = Path.Combine(Utils.FolderPath, "levels/backups");
Server.updateTimer.Interval = Server.PositionInterval;
Save(givenPath); Save(givenPath);
} }

View File

@ -177,7 +177,7 @@ namespace MCGalaxy {
} catch { } catch {
} }
if (Server.updateTimer.Interval > 1000) if (Server.PositionInterval > 1000)
SendMessage("Lowlag mode is currently &aON."); SendMessage("Lowlag mode is currently &aON.");
if (String.IsNullOrEmpty(appName)) { if (String.IsNullOrEmpty(appName)) {

View File

@ -70,9 +70,7 @@ namespace MCGalaxy {
// URL hash for connecting to the server // URL hash for connecting to the server
public static string Hash = String.Empty, URL = String.Empty; public static string Hash = String.Empty, URL = String.Empty;
public static Socket listen; public static Socket listen;
public static System.Timers.Timer updateTimer = new System.Timers.Timer(100);
//Chatrooms //Chatrooms
public static List<string> Chatrooms = new List<string>(); public static List<string> Chatrooms = new List<string>();
@ -123,6 +121,7 @@ namespace MCGalaxy {
public static Scheduler MainScheduler = new Scheduler("MCG_MainScheduler"); public static Scheduler MainScheduler = new Scheduler("MCG_MainScheduler");
public static Scheduler Background = new Scheduler("MCG_BackgroundScheduler"); public static Scheduler Background = new Scheduler("MCG_BackgroundScheduler");
public static Scheduler Critical = new Scheduler("MCG_CriticalScheduler");
public static Server s; public static Server s;
public const byte version = 7; public const byte version = 7;

View File

@ -102,33 +102,29 @@ namespace MCGalaxy {
} }
void InitTimers() { void InitTimers() {
updateTimer.Elapsed += delegate {
Entities.GlobalUpdate();
PlayerBot.GlobalUpdatePosition();
};
updateTimer.Start();
if (File.Exists(Paths.AnnouncementsFile)) { if (File.Exists(Paths.AnnouncementsFile)) {
string[] lines = File.ReadAllLines(Paths.AnnouncementsFile); string[] lines = File.ReadAllLines(Paths.AnnouncementsFile);
messages = new List<string>(lines); messages = new List<string>(lines);
} else { } else {
using (File.Create(Paths.AnnouncementsFile)) {} 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() { void InitRest() {
IRC = new IRCBot(); IRC = new IRCBot();
if (Server.irc) IRC.Connect(); if (Server.irc) IRC.Connect();
locationChecker = new Thread(ServerTasks.LocationChecks);
locationChecker.Name = "MCG_LocationCheck";
locationChecker.Start();
InitZombieSurvival(); InitZombieSurvival();
InitLavaSurvival(); InitLavaSurvival();
MainScheduler.QueueRepeat(BlockQueue.Loop, null, MainScheduler.QueueRepeat(BlockQueue.Loop, null,
TimeSpan.FromMilliseconds(BlockQueue.time)); TimeSpan.FromMilliseconds(BlockQueue.time));
Critical.QueueRepeat(ServerTasks.LocationChecks, null,
TimeSpan.FromMilliseconds(20));
Log("Finished setting up server, finding classicube.net url.."); Log("Finished setting up server, finding classicube.net url..");
ServerSetupFinished = true; ServerSetupFinished = true;

View File

@ -26,11 +26,11 @@ using MCGalaxy.Maths;
namespace MCGalaxy.Tasks { namespace MCGalaxy.Tasks {
internal static class ServerTasks { internal static class ServerTasks {
internal static void LocationChecks() { internal static void LocationChecks(SchedulerTask task) {
while (true) {
Player[] players = PlayerInfo.Online.Items; Player[] players = PlayerInfo.Online.Items;
Thread.Sleep(players.Length == 0 ? 20 : 10);
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++) { for (int i = 0; i < players.Length; i++) {
try { try {
@ -64,6 +64,11 @@ namespace MCGalaxy.Tasks {
} }
} }
} }
internal static void UpdateEntityPositions(SchedulerTask task) {
Entities.GlobalUpdate();
PlayerBot.GlobalUpdatePosition();
task.Delay = TimeSpan.FromMilliseconds(Server.PositionInterval);
} }
internal static void CheckState(SchedulerTask task) { internal static void CheckState(SchedulerTask task) {

View File

@ -77,21 +77,26 @@ namespace MCGalaxy.Tasks {
SchedulerTask GetNextTask() { SchedulerTask GetNextTask() {
DateTime now = DateTime.UtcNow; DateTime minTime = DateTime.UtcNow;
SchedulerTask minTask = null;
lock (taskLock) { lock (taskLock) {
foreach (SchedulerTask task in tasks) { 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) { void DoTask(SchedulerTask task) {
try { try {
task.Callback(task); task.Callback(task);
} catch (Exception ex) { } catch (Exception ex) {
MCGalaxy.Server.ErrorLog(ex); MCGalaxy.Server.ErrorLog(ex);
} }
lastTask = task;
if (task.Repeating) { if (task.Repeating) {
task.NextRun = DateTime.UtcNow.Add(task.Delay); task.NextRun = DateTime.UtcNow.Add(task.Delay);