From 3eb6e870192746e822c1321f4792fc0a72ec8cad Mon Sep 17 00:00:00 2001 From: dbrownidau Date: Mon, 10 Feb 2020 00:38:52 +1100 Subject: [PATCH 1/8] Moved main loop into FixedRate scheduler. Added sync lock on runnable. --- .../src/com/rebotted/GameEngine.java | 114 +++++++++++------- 1 file changed, 73 insertions(+), 41 deletions(-) diff --git a/2006Redone Server/src/com/rebotted/GameEngine.java b/2006Redone Server/src/com/rebotted/GameEngine.java index 89551064..bb39bb62 100644 --- a/2006Redone Server/src/com/rebotted/GameEngine.java +++ b/2006Redone Server/src/com/rebotted/GameEngine.java @@ -3,6 +3,11 @@ package com.rebotted; import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import org.apache.mina.common.IoAcceptor; import org.apache.mina.transport.socket.nio.SocketAcceptor; @@ -73,6 +78,8 @@ public class GameEngine { public static FightCaves fightCaves = new FightCaves(); private static PestControl pestControl = new PestControl(); public static Trawler trawler = new Trawler(); + private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + private final static Lock lock = new ReentrantLock(); /** * Port and Cycle rate. @@ -147,51 +154,76 @@ public class GameEngine { /** * Main Server Tick + * + * This scheduler will tick once every 600ms. If the previous tick takes + * 300ms to execute, this scheduler will wait 300ms only before the next + * tick. + * + * Each tick, it will attempt to get a lock on the main server thread. + * Without locking this runnable, multiple ticks could occur at once. */ - try { - while (!GameEngine.shutdownServer) { - Thread.sleep(600); - itemHandler.process(); - playerHandler.process(); - npcHandler.process(); - shopHandler.process(); - objectManager.process(); - CastleWars.process(); - FightPits.process(); - pestControl.process(); - CycleEventHandler.getSingleton().process(); - PlayersOnlineWebsite.addUpdatePlayersOnlineTask(); - RegisteredAccsWebsite.addUpdateRegisteredUsersTask(); - DiscordActivity.updateActivity(); - if (System.currentTimeMillis() - lastMassSave > 300000) { - for (Player p : PlayerHandler.players) { - if (p == null) { - continue; + scheduler.scheduleAtFixedRate(new Runnable() { + public void run() { + if (lock.tryLock()) { + synchronized (lock) { + /** + * Main Server Tick + */ + try { + if (GameEngine.shutdownServer) { + scheduler.shutdown(); + } + itemHandler.process(); + playerHandler.process(); + npcHandler.process(); + shopHandler.process(); + objectManager.process(); + CastleWars.process(); + FightPits.process(); + pestControl.process(); + CycleEventHandler.getSingleton().process(); + PlayersOnlineWebsite.addUpdatePlayersOnlineTask(); + RegisteredAccsWebsite + .addUpdateRegisteredUsersTask(); + DiscordActivity.updateActivity(); + if (System.currentTimeMillis() + - lastMassSave > 300000) { + for (Player p : PlayerHandler.players) { + if (p == null) { + continue; + } + PlayerSave.saveGame((Client) p); + System.out.println("Saved game for " + p.playerName + "."); + lastMassSave = System.currentTimeMillis(); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + System.out.println("A fatal exception has been thrown!"); + for (Player p : PlayerHandler.players) { + if (p == null) { + continue; + } + if (p.inTrade) { + ((Client) p).getTrading().declineTrade(); + } + if (p.duelStatus == 6) { + ((Client) p).getDueling().claimStakedItems(); + } + PlayerSave.saveGame((Client) p); + System.out.println("Saved game for " + p.playerName + "."); + scheduler.shutdown(); // Kills the tickloop thread if Exception is thrown. + } } - PlayerSave.saveGame((Client) p); - System.out.println("Saved game for " + p.playerName - + "."); - lastMassSave = System.currentTimeMillis(); - } - } - } - } catch (Exception ex) { - ex.printStackTrace(); - System.out.println("A fatal exception has been thrown!"); - for (Player p : PlayerHandler.players) { - if (p == null) { - continue; + } + } else { + System.out.println("Can't Keep up! Did the system time change or is the server overloaded?"); } - if (p.inTrade) { - ((Client)p).getTrading().declineTrade(); - } - if(p.duelStatus == 6) { - ((Client)p).getDueling().claimStakedItems(); - } - PlayerSave.saveGame((Client) p); - System.out.println("Saved game for " + p.playerName + "."); } - } + }, 0, GameConstants.CYCLE_TIME, TimeUnit.MILLISECONDS); + + + acceptor = null; connectionHandler = null; sac = null; From fb1321c8e0009dada331a0b9641cd2aec03c6367 Mon Sep 17 00:00:00 2001 From: dbrownidau Date: Mon, 10 Feb 2020 01:29:57 +1100 Subject: [PATCH 2/8] Synchronization lock not required for scheduleAtFixedRate(new Runnable(){}) https://stackoverflow.com/a/35498230 --- 2006Redone Server/src/com/rebotted/GameEngine.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/2006Redone Server/src/com/rebotted/GameEngine.java b/2006Redone Server/src/com/rebotted/GameEngine.java index bb39bb62..6803ecac 100644 --- a/2006Redone Server/src/com/rebotted/GameEngine.java +++ b/2006Redone Server/src/com/rebotted/GameEngine.java @@ -164,8 +164,6 @@ public class GameEngine { */ scheduler.scheduleAtFixedRate(new Runnable() { public void run() { - if (lock.tryLock()) { - synchronized (lock) { /** * Main Server Tick */ @@ -215,10 +213,6 @@ public class GameEngine { scheduler.shutdown(); // Kills the tickloop thread if Exception is thrown. } } - } - } else { - System.out.println("Can't Keep up! Did the system time change or is the server overloaded?"); - } } }, 0, GameConstants.CYCLE_TIME, TimeUnit.MILLISECONDS); From 00efcbbde96c7af9c63beb831fbe01e5846d90a9 Mon Sep 17 00:00:00 2001 From: dbrownidau Date: Mon, 10 Feb 2020 01:31:12 +1100 Subject: [PATCH 3/8] Adjust formatting --- .../src/com/rebotted/GameEngine.java | 92 +++++++++---------- 1 file changed, 45 insertions(+), 47 deletions(-) diff --git a/2006Redone Server/src/com/rebotted/GameEngine.java b/2006Redone Server/src/com/rebotted/GameEngine.java index 6803ecac..847aa98e 100644 --- a/2006Redone Server/src/com/rebotted/GameEngine.java +++ b/2006Redone Server/src/com/rebotted/GameEngine.java @@ -164,55 +164,53 @@ public class GameEngine { */ scheduler.scheduleAtFixedRate(new Runnable() { public void run() { - /** - * Main Server Tick - */ - try { - if (GameEngine.shutdownServer) { - scheduler.shutdown(); - } - itemHandler.process(); - playerHandler.process(); - npcHandler.process(); - shopHandler.process(); - objectManager.process(); - CastleWars.process(); - FightPits.process(); - pestControl.process(); - CycleEventHandler.getSingleton().process(); - PlayersOnlineWebsite.addUpdatePlayersOnlineTask(); - RegisteredAccsWebsite - .addUpdateRegisteredUsersTask(); - DiscordActivity.updateActivity(); - if (System.currentTimeMillis() - - lastMassSave > 300000) { - for (Player p : PlayerHandler.players) { - if (p == null) { - continue; - } - PlayerSave.saveGame((Client) p); - System.out.println("Saved game for " + p.playerName + "."); - lastMassSave = System.currentTimeMillis(); - } - } - } catch (Exception ex) { - ex.printStackTrace(); - System.out.println("A fatal exception has been thrown!"); - for (Player p : PlayerHandler.players) { - if (p == null) { - continue; - } - if (p.inTrade) { - ((Client) p).getTrading().declineTrade(); - } - if (p.duelStatus == 6) { - ((Client) p).getDueling().claimStakedItems(); - } - PlayerSave.saveGame((Client) p); - System.out.println("Saved game for " + p.playerName + "."); - scheduler.shutdown(); // Kills the tickloop thread if Exception is thrown. + /** + * Main Server Tick + */ + try { + if (GameEngine.shutdownServer) { + scheduler.shutdown(); + } + itemHandler.process(); + playerHandler.process(); + npcHandler.process(); + shopHandler.process(); + objectManager.process(); + CastleWars.process(); + FightPits.process(); + pestControl.process(); + CycleEventHandler.getSingleton().process(); + PlayersOnlineWebsite.addUpdatePlayersOnlineTask(); + RegisteredAccsWebsite.addUpdateRegisteredUsersTask(); + DiscordActivity.updateActivity(); + if (System.currentTimeMillis() - lastMassSave > 300000) { + for (Player p : PlayerHandler.players) { + if (p == null) { + continue; } + PlayerSave.saveGame((Client) p); + System.out.println("Saved game for " + p.playerName + "."); + lastMassSave = System.currentTimeMillis(); } + } + } catch (Exception ex) { + ex.printStackTrace(); + System.out.println("A fatal exception has been thrown!"); + for (Player p : PlayerHandler.players) { + if (p == null) { + continue; + } + if (p.inTrade) { + ((Client) p).getTrading().declineTrade(); + } + if (p.duelStatus == 6) { + ((Client) p).getDueling().claimStakedItems(); + } + PlayerSave.saveGame((Client) p); + System.out.println("Saved game for " + p.playerName + "."); + scheduler.shutdown(); // Kills the tickloop thread if Exception is thrown. + } + } } }, 0, GameConstants.CYCLE_TIME, TimeUnit.MILLISECONDS); From b855a98842b4172ee74bc3733a4af901de086499 Mon Sep 17 00:00:00 2001 From: dbrownidau Date: Mon, 10 Feb 2020 01:35:40 +1100 Subject: [PATCH 4/8] Fix doco --- 2006Redone Server/src/com/rebotted/GameEngine.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/2006Redone Server/src/com/rebotted/GameEngine.java b/2006Redone Server/src/com/rebotted/GameEngine.java index 847aa98e..6bd717b4 100644 --- a/2006Redone Server/src/com/rebotted/GameEngine.java +++ b/2006Redone Server/src/com/rebotted/GameEngine.java @@ -159,8 +159,7 @@ public class GameEngine { * 300ms to execute, this scheduler will wait 300ms only before the next * tick. * - * Each tick, it will attempt to get a lock on the main server thread. - * Without locking this runnable, multiple ticks could occur at once. + * scheduleAtFixedRate() does not invoke concurrent Runnables. */ scheduler.scheduleAtFixedRate(new Runnable() { public void run() { From b665504c4b20d3874799e3c733f956ea24c96a67 Mon Sep 17 00:00:00 2001 From: dbrownidau Date: Mon, 10 Feb 2020 10:35:19 +1100 Subject: [PATCH 5/8] Added block on main thread until tick scheduler shutdown. --- 2006Redone Server/src/com/rebotted/GameEngine.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/2006Redone Server/src/com/rebotted/GameEngine.java b/2006Redone Server/src/com/rebotted/GameEngine.java index 6bd717b4..2d8b215d 100644 --- a/2006Redone Server/src/com/rebotted/GameEngine.java +++ b/2006Redone Server/src/com/rebotted/GameEngine.java @@ -213,7 +213,14 @@ public class GameEngine { } }, 0, GameConstants.CYCLE_TIME, TimeUnit.MILLISECONDS); - + try { + while (!scheduler.awaitTermination(60, TimeUnit.SECONDS)) { + // TODO + // Cleanup? + } + } catch (InterruptedException e) { + e.printStackTrace(); + } acceptor = null; connectionHandler = null; From 1d95da80b72bb70a9ff661846801dc45e603f15e Mon Sep 17 00:00:00 2001 From: dbrownidau Date: Mon, 10 Feb 2020 14:09:56 +1100 Subject: [PATCH 6/8] Move shutdown call outside for loop. --- 2006Redone Server/src/com/rebotted/GameEngine.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2006Redone Server/src/com/rebotted/GameEngine.java b/2006Redone Server/src/com/rebotted/GameEngine.java index 2d8b215d..ca6fa259 100644 --- a/2006Redone Server/src/com/rebotted/GameEngine.java +++ b/2006Redone Server/src/com/rebotted/GameEngine.java @@ -207,8 +207,8 @@ public class GameEngine { } PlayerSave.saveGame((Client) p); System.out.println("Saved game for " + p.playerName + "."); - scheduler.shutdown(); // Kills the tickloop thread if Exception is thrown. } + scheduler.shutdown(); // Kills the tickloop thread if Exception is thrown. } } }, 0, GameConstants.CYCLE_TIME, TimeUnit.MILLISECONDS); From 4a134a819eda1ba1e6b89c34502a9d0b8ac64b41 Mon Sep 17 00:00:00 2001 From: dginovker Date: Tue, 11 Feb 2020 18:53:00 -0500 Subject: [PATCH 7/8] Easier setup --- .idea/modules.xml | 3 + .idea/workspace.xml | 172 ++++++++++++------ .../classes/com/rebotted/GameEngine$1.class | Bin 0 -> 3614 bytes .../classes/com/rebotted/GameEngine.class | Bin 7527 -> 6889 bytes .../META-INF/2006rebotted.kotlin_module | Bin 16 -> 0 bytes .../rebotted/game/items/ItemDefinition.class | Bin 6752 -> 0 bytes 6 files changed, 116 insertions(+), 59 deletions(-) create mode 100644 2006Redone Server/out/production/classes/com/rebotted/GameEngine$1.class delete mode 100644 CompiledServer/production/2006rebotted/META-INF/2006rebotted.kotlin_module delete mode 100644 CompiledServer/production/2006rebotted/com/rebotted/game/items/ItemDefinition.class diff --git a/.idea/modules.xml b/.idea/modules.xml index 5c106462..80847077 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,6 +2,9 @@ + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index aad92893..f4a8d94e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,7 +7,6 @@ - @@ -37,12 +36,24 @@ - - + + - + + + + + + + + + + + + + @@ -79,53 +90,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +