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() {
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();

View File

@ -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");
}

View File

@ -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);

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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)) {

View File

@ -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<string> Chatrooms = new List<string>();
@ -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;

View File

@ -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<string>(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;

View File

@ -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) {

View File

@ -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);