TickLoop thread

This commit is contained in:
Shoghi Cervantes Pueyo 2013-01-10 17:10:15 +01:00
parent d5d319fb7c
commit eebbc263b0
4 changed files with 58 additions and 7 deletions

View File

@ -27,7 +27,7 @@ the Free Software Foundation, either version 3 of the License, or
class PocketMinecraftServer{ 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; 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){ function __construct($name, $gamemode = 1, $seed = false, $port = 19132, $serverID = false){
$this->port = (int) $port; //19132 - 19135 $this->port = (int) $port; //19132 - 19135
$vNumber = new VersionString(); $vNumber = new VersionString();
@ -80,6 +80,10 @@ class PocketMinecraftServer{
$this->stop = false; $this->stop = false;
} }
public function run(){
}
public function getTPS(){ public function getTPS(){
$v = array_values($this->tickMeasure); $v = array_values($this->tickMeasure);
$tps = 40 / ($v[39] - $v[0]); $tps = 40 / ($v[39] - $v[0]);
@ -157,6 +161,7 @@ class PocketMinecraftServer{
public function close($reason = "stop"){ public function close($reason = "stop"){
if($this->stop !== true){ if($this->stop !== true){
$this->chat(false, "Stopping server..."); $this->chat(false, "Stopping server...");
$this->ticker->stop = true;
$this->save(true); $this->save(true);
$this->stop = true; $this->stop = true;
$this->trigger("server.close", $reason); $this->trigger("server.close", $reason);
@ -317,6 +322,8 @@ class PocketMinecraftServer{
} }
console("[INFO] Loading events..."); console("[INFO] Loading events...");
$this->loadEvents(); $this->loadEvents();
$this->ticker = new TickLoop;
$this->ticker->start();
declare(ticks=15); declare(ticks=15);
register_tick_function(array($this, "tick")); register_tick_function(array($this, "tick"));
register_shutdown_function(array($this, "dumpError")); register_shutdown_function(array($this, "dumpError"));
@ -347,12 +354,13 @@ class PocketMinecraftServer{
} }
public function tick(){ public function tick(){
$time = microtime(true); if($this->ticker->isWaiting() === true){
if($this->lastTick <= ($time - 0.05)){ $time = microtime(true);
array_shift($this->tickMeasure); array_shift($this->tickMeasure);
$this->tickMeasure[] = $this->lastTick = $time; $this->tickMeasure[] = $this->lastTick = $time;
$this->tickerFunction($time); $this->tickerFunction($time);
$this->trigger("server.tick", $time); $this->trigger("server.tick", $time);
$this->ticker->notify();
} }
} }

View File

@ -27,10 +27,9 @@ the Free Software Foundation, either version 3 of the License, or
class UDPSocket{ class UDPSocket extends Thread{
private $encrypt; private $encrypt;
var $buffer, $connected, $errors, $sock, $server; var $buffer, $connected, $errors, $sock, $server;
function __construct($server, $port, $listen = false, $socket = false){ function __construct($server, $port, $listen = false, $socket = false){
$this->errors = array_fill(88,(125 - 88) + 1, true); $this->errors = array_fill(88,(125 - 88) + 1, true);
$this->server = $server; $this->server = $server;
@ -58,7 +57,11 @@ class UDPSocket{
} }
} }
function listenSocket(){ public function run(){
}
public function listenSocket(){
$sock = @socket_accept($this->sock); $sock = @socket_accept($this->sock);
if($sock !== false){ if($sock !== false){
$sock = new Socket(false, false, false, $sock); $sock = new Socket(false, false, false, $sock);

View File

@ -35,7 +35,7 @@ define("BIG_ENDIAN", 0x00);
define("LITTLE_ENDIAN", 0x01); define("LITTLE_ENDIAN", 0x01);
define("ENDIANNESS", (pack("d", 1) === "\77\360\0\0\0\0\0\0" ? BIG_ENDIAN:LITTLE_ENDIAN)); 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(){ public static function getOS(){
$uname = strtoupper(php_uname("s")); $uname = strtoupper(php_uname("s"));

View File

@ -0,0 +1,40 @@
<?php
/*
-
/ \
/ \
/ PocketMine \
/ MP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
class TickLoop extends Thread{
var $stop = false, $lastTick = 0;
public function run(){
while($this->stop !== true){
$time = microtime(true);
if($this->lastTick <= ($time - 0.05)){
$this->lastTick = $time;
$this->wait();
}
}
exit(0);
}
}