From eebbc263b0f3b8ed0e1021b9c801d58ef0d305f0 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Thu, 10 Jan 2013 17:10:15 +0100 Subject: [PATCH] TickLoop thread --- src/classes/PocketMinecraftServer.class.php | 14 ++++++-- src/classes/UDPSocket.class.php | 9 +++-- src/classes/Utils.class.php | 2 +- src/misc/utils/TickLoop.php | 40 +++++++++++++++++++++ 4 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 src/misc/utils/TickLoop.php diff --git a/src/classes/PocketMinecraftServer.class.php b/src/classes/PocketMinecraftServer.class.php index 06df959e1..968613560 100644 --- a/src/classes/PocketMinecraftServer.class.php +++ b/src/classes/PocketMinecraftServer.class.php @@ -27,7 +27,7 @@ the Free Software Foundation, either version 3 of the License, or class PocketMinecraftServer{ var $invisible, $api, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $timePerSecond, $responses, $spawn, $entities, $mapDir, $mapName, $map, $level, $tileEntities; - private $database, $interface, $evCnt, $handCnt, $events, $handlers, $version, $serverType, $lastTick; + private $database, $interface, $evCnt, $handCnt, $events, $handlers, $version, $serverType, $lastTick, $ticker; function __construct($name, $gamemode = 1, $seed = false, $port = 19132, $serverID = false){ $this->port = (int) $port; //19132 - 19135 $vNumber = new VersionString(); @@ -79,6 +79,10 @@ class PocketMinecraftServer{ console("[INFO] Protocol Version: ".CURRENT_PROTOCOL); $this->stop = false; } + + public function run(){ + + } public function getTPS(){ $v = array_values($this->tickMeasure); @@ -157,6 +161,7 @@ class PocketMinecraftServer{ public function close($reason = "stop"){ if($this->stop !== true){ $this->chat(false, "Stopping server..."); + $this->ticker->stop = true; $this->save(true); $this->stop = true; $this->trigger("server.close", $reason); @@ -317,6 +322,8 @@ class PocketMinecraftServer{ } console("[INFO] Loading events..."); $this->loadEvents(); + $this->ticker = new TickLoop; + $this->ticker->start(); declare(ticks=15); register_tick_function(array($this, "tick")); register_shutdown_function(array($this, "dumpError")); @@ -347,12 +354,13 @@ class PocketMinecraftServer{ } public function tick(){ - $time = microtime(true); - if($this->lastTick <= ($time - 0.05)){ + if($this->ticker->isWaiting() === true){ + $time = microtime(true); array_shift($this->tickMeasure); $this->tickMeasure[] = $this->lastTick = $time; $this->tickerFunction($time); $this->trigger("server.tick", $time); + $this->ticker->notify(); } } diff --git a/src/classes/UDPSocket.class.php b/src/classes/UDPSocket.class.php index f87842123..bb703798c 100644 --- a/src/classes/UDPSocket.class.php +++ b/src/classes/UDPSocket.class.php @@ -27,10 +27,9 @@ the Free Software Foundation, either version 3 of the License, or -class UDPSocket{ +class UDPSocket extends Thread{ private $encrypt; var $buffer, $connected, $errors, $sock, $server; - function __construct($server, $port, $listen = false, $socket = false){ $this->errors = array_fill(88,(125 - 88) + 1, true); $this->server = $server; @@ -57,8 +56,12 @@ class UDPSocket{ } } } + + public function run(){ + + } - function listenSocket(){ + public function listenSocket(){ $sock = @socket_accept($this->sock); if($sock !== false){ $sock = new Socket(false, false, false, $sock); diff --git a/src/classes/Utils.class.php b/src/classes/Utils.class.php index 136b98074..f5107c45d 100644 --- a/src/classes/Utils.class.php +++ b/src/classes/Utils.class.php @@ -35,7 +35,7 @@ define("BIG_ENDIAN", 0x00); define("LITTLE_ENDIAN", 0x01); define("ENDIANNESS", (pack("d", 1) === "\77\360\0\0\0\0\0\0" ? BIG_ENDIAN:LITTLE_ENDIAN)); -abstract class Utils{ +class Utils{ public static function getOS(){ $uname = strtoupper(php_uname("s")); diff --git a/src/misc/utils/TickLoop.php b/src/misc/utils/TickLoop.php new file mode 100644 index 000000000..65b86b904 --- /dev/null +++ b/src/misc/utils/TickLoop.php @@ -0,0 +1,40 @@ +stop !== true){ + $time = microtime(true); + if($this->lastTick <= ($time - 0.05)){ + $this->lastTick = $time; + $this->wait(); + } + } + exit(0); + } +} \ No newline at end of file