Performance improvements - removed Player action queue

This commit is contained in:
Shoghi Cervantes 2013-05-31 14:43:53 +02:00
parent 66fc483156
commit a890cdc023
3 changed files with 25 additions and 51 deletions

View File

@ -92,12 +92,12 @@ class TileEntityAPI{
)); ));
} }
public function spawnTo($id, $player, $queue = false){ public function spawnTo($id, $player){
$t = $this->getByID($id); $t = $this->getByID($id);
if($t === false){ if($t === false){
return false; return false;
} }
$t->spawn($player, $queue); $t->spawn($player);
} }
public function spawnToAll(Level $level, $id){ public function spawnToAll(Level $level, $id){

View File

@ -28,7 +28,6 @@ the Free Software Foundation, either version 3 of the License, or
class Player{ class Player{
private $server; private $server;
private $queue = array();
private $buffer = ""; private $buffer = "";
private $nextBuffer = 0; private $nextBuffer = 0;
private $recovery = array(); private $recovery = array();
@ -92,7 +91,7 @@ class Player{
$this->level = $this->server->api->level->getDefault(); $this->level = $this->server->api->level->getDefault();
$this->equipment = BlockAPI::getItem(AIR); $this->equipment = BlockAPI::getItem(AIR);
$this->packetStats = array(0,0); $this->packetStats = array(0,0);
$this->evid[] = $this->server->event("server.tick", array($this, "onTick")); $this->server->schedule(2, array($this, "onTick"), array(), true);
$this->evid[] = $this->server->event("server.close", array($this, "close")); $this->evid[] = $this->server->event("server.close", array($this, "close"));
console("[DEBUG] New Session started with ".$ip.":".$port.". MTU ".$this->MTU.", Client ID ".$this->clientID, true, true, 2); console("[DEBUG] New Session started with ".$ip.":".$port.". MTU ".$this->MTU.", Client ID ".$this->clientID, true, true, 2);
} }
@ -181,28 +180,14 @@ class Player{
$this->server->schedule(1, array($this, "getNextChunk")); $this->server->schedule(1, array($this, "getNextChunk"));
} }
public function onTick($time, $event){ public function onTick(){
if($event !== "server.tick" or $this->connected === false){ if($this->connected === false){
return; return;
} }
$time = microtime(true);
if($time > $this->timeout){ if($time > $this->timeout){
$this->close("timeout"); $this->close("timeout");
}else{ }else{
if(!empty($this->queue)){
$maxtime = $time + 0.0025;
while(microtime(true) < $maxtime and ($p = each($this->queue)) !== false){
unset($this->queue[$p[0]]);
switch($p[1][0]){
case 0:
$this->dataPacket($p[1][1]["id"], $p[1][1], false);
break;
case 1:
eval($p[1][1]);
break;
}
}
}
if($this->nextBuffer <= $time and strlen($this->buffer) > 0){ if($this->nextBuffer <= $time and strlen($this->buffer) > 0){
$this->sendBuffer(); $this->sendBuffer();
} }
@ -247,8 +232,6 @@ class Player{
unset($this->buffer); unset($this->buffer);
$this->recovery = null; $this->recovery = null;
unset($this->recovery); unset($this->recovery);
$this->queue = null;
unset($this->queue);
$this->connected = false; $this->connected = false;
if($msg === true and $this->username != ""){ if($msg === true and $this->username != ""){
$this->server->api->chat->broadcast($this->username." left the game"); $this->server->api->chat->broadcast($this->username." left the game");
@ -1386,10 +1369,6 @@ class Player{
++$this->packetStats[0]; ++$this->packetStats[0];
} }
} }
public function actionQueue($code){
$this->queue[] = array(1, $code);
}
public function sendBuffer(){ public function sendBuffer(){
if(strlen($this->buffer) > 0){ if(strlen($this->buffer) > 0){
@ -1454,31 +1433,26 @@ class Player{
)); ));
} }
public function dataPacket($id, $data = array(), $queue = false){ public function dataPacket($id, $data = array()){
$data["id"] = $id; $data["id"] = $id;
if($queue === true){ if($id === false){
$this->queue[] = array(0, $data); $raw = $data["raw"];
}else{ }else{
if($id === false){ $data = new CustomPacketHandler($id, "", $data, true);
$raw = $data["raw"]; $raw = chr($id).$data->raw;
}else{
$data = new CustomPacketHandler($id, "", $data, true);
$raw = chr($id).$data->raw;
}
$len = strlen($raw);
$MTU = $this->MTU - 24;
if($len > $MTU){
$this->directBigRawPacket(false, $raw);
return;
}
if((strlen($this->buffer) + $len) >= $MTU){
$this->sendBuffer();
}
$this->buffer .= ($this->buffer === "" ? "":"\x40").Utils::writeShort($len << 3).strrev(Utils::writeTriad($this->counter[3]++)).$raw;
} }
$len = strlen($raw);
$MTU = $this->MTU - 24;
if($len > $MTU){
$this->directBigRawPacket(false, $raw);
return;
}
if((strlen($this->buffer) + $len) >= $MTU){
$this->sendBuffer();
}
$this->buffer .= ($this->buffer === "" ? "":"\x40").Utils::writeShort($len << 3).strrev(Utils::writeTriad($this->counter[3]++)).$raw;
} }
function __toString(){ function __toString(){

View File

@ -196,7 +196,7 @@ class TileEntity extends Position{
return true; return true;
} }
public function spawn($player, $queue = false){ public function spawn($player){
if($this->closed){ if($this->closed){
return false; return false;
} }
@ -214,7 +214,7 @@ class TileEntity extends Position{
"line1" => $this->data["Text2"], "line1" => $this->data["Text2"],
"line2" => $this->data["Text3"], "line2" => $this->data["Text3"],
"line3" => $this->data["Text4"], "line3" => $this->data["Text4"],
), $queue); ));
break; break;
} }
} }