From 22ef626c4f182702bc27a422544cf16c1df8b4b2 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Fri, 14 Dec 2012 14:27:15 +0100 Subject: [PATCH] Fixed ChunkData, faster, action queue, NACK --- classes/API/ConsoleAPI.php | 2 +- classes/API/LevelAPI.php | 2 +- classes/ChunkParser.class.php | 1 - classes/Session.class.php | 65 ++++++++++++++++++++++++++++------- 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/classes/API/ConsoleAPI.php b/classes/API/ConsoleAPI.php index 0d056ece0..9b90144d6 100644 --- a/classes/API/ConsoleAPI.php +++ b/classes/API/ConsoleAPI.php @@ -165,7 +165,7 @@ class ConsoleAPI{ break; case "block": foreach($this->server->clients as $client){ - $b = $this->server->map->getBlock($client->entity->position["x"], $client->entity->position["y"] - 2, $client->entity->position["z"]); + $b = $this->server->map->getBlock(round($client->entity->position["x"] - 0.5), round($client->entity->position["y"] - 1), round($client->entity->position["z"] - 0.5)); console("[INFO] EID ".$client->eid." is over block ".$b[0].":".$b[1]); } break; diff --git a/classes/API/LevelAPI.php b/classes/API/LevelAPI.php index 9a9ba1f6e..212e4c77f 100644 --- a/classes/API/LevelAPI.php +++ b/classes/API/LevelAPI.php @@ -60,7 +60,7 @@ class LevelAPI{ for($i = 0;$i < 0xff; ){ $ordered[$i] = str_repeat("\x00", $i); for($j = 0; $j < $columnsPerPacket; ++$j){ - if(($i + $j) >= 0xff){ + if(($i + $j) > 0xff){ break; } $ordered[$i] .= "\xff"; diff --git a/classes/ChunkParser.class.php b/classes/ChunkParser.class.php index 3f890b5b5..51fa8d257 100644 --- a/classes/ChunkParser.class.php +++ b/classes/ChunkParser.class.php @@ -164,7 +164,6 @@ class ChunkParser{ $aZ = $z - ($Z << 4); $index = $aX + ($aZ << 4); console("[DEBUG] $x $y $z | $X $Z $index", true, true, 2); - var_dump($this->map[$X][$Z][0][$index]); $block = ord($this->map[$X][$Z][0][$index]{$y}); //$meta = $this->getOffset($X, $Z) + 4 + (($x << 6) + $y + ($z << 10)); return array($block, 0); diff --git a/classes/Session.class.php b/classes/Session.class.php index c9ae58406..49db0d202 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -27,10 +27,11 @@ the Free Software Foundation, either version 3 of the License, or class Session{ - private $server, $timeout, $connected, $evid, $queue; + private $server, $timeout, $connected, $evid, $queue, $buffer; var $clientID, $ip, $port, $counter, $username, $eid, $data, $entity, $auth, $CID, $MTU; function __construct($server, $clientID, $eid, $ip, $port, $MTU){ $this->queue = array(); + $this->buffer = array(); $this->MTU = $MTU; $this->server = $server; $this->clientID = $clientID; @@ -55,10 +56,21 @@ class Session{ $this->close("timeout"); }else{ if(count($this->queue) > 0){ - $limit = $time + 0.1; - while($limit > microtime(true)){ + $cnt = 0; + while($cnt < 4){ $p = array_shift($this->queue); - $this->dataPacket($p[0], $p[1]); + if($p === null){ + break; + } + switch($p[0]){ + case 0: + $this->dataPacket($p[1], $p[2], false, $p[3]); + break; + case 1: + eval($p[1]); + break; + } + ++$cnt; } } } @@ -191,6 +203,22 @@ class Session{ if($this->connected === true){ $this->timeout = microtime(true) + 25; switch($pid){ + case 0xa0: //NACK + if(isset($this->buffer[$data[2]])){ + array_unshift($this->queue, array(0, $this->buffer[$data[2]][0], $this->buffer[$data[2]][1], $data[2])); + } + if(isset($data[3])){ + if(isset($this->buffer[$data[3]])){ + array_unshift($this->queue, array(0, $this->buffer[$data[3]][0], $this->buffer[$data[3]][1], $data[3])); + } + } + break; + case 0xc0: //ACK + unset($this->buffer[$data[2]]); + if(isset($data[3])){ + unset($this->buffer[$data[3]]); + } + break; case 0x07: $this->send(0x08, array( MAGIC, @@ -321,16 +349,18 @@ class Session{ console("[DEBUG] EID ".$this->eid." has now ".$data["block"].":".$data["meta"]." in their hands!", true, true, 2); break; case MC_REQUEST_CHUNK: + $this->actionQueue(' $max = floor(($this->MTU - 16 - 255) / 192); - $chunk = $this->server->api->level->getOrderedChunk($data["x"], $data["z"], $max); + $chunk = $this->server->api->level->getOrderedChunk('.$data["x"].', '.$data["z"].', $max); foreach($chunk as $d){ $this->dataPacket(MC_CHUNK_DATA, array( - "x" => $data["x"], - "z" => $data["z"], + "x" => '.$data["x"].', + "z" => '.$data["z"].', "data" => $d, ), true); } - console("[DEBUG] Chunk X ".$data["x"]." Z ".$data["z"]." requested", true, true, 2); + '); + console("[DEBUG] Chunk X ".$data["x"]." Z ".$data["z"]." requested", true, true, 2); break; case MC_REMOVE_BLOCK: console("[DEBUG] EID ".$this->eid." broke block at X ".$data["x"]." Y ".$data["y"]." Z ".$data["z"], true, true, 2); @@ -383,17 +413,28 @@ class Session{ } } - public function dataPacket($id, $data = array(), $queue = false){ + public function actionQueue($code){ + $this->queue[] = array(1, $code); + } + + public function dataPacket($id, $data = array(), $queue = false, $count = false){ if($queue === true){ - $this->queue[] = array($id, $data); + $this->queue[] = array(0, $id, $data, $count); }else{ + if($count === false){ + $count = $this->counter[0]; + ++$this->counter[0]; + if(count($this->buffer) >= 512){ + array_shift($this->buffer); + } + $this->buffer[$count] = array($id, $data); + } $data["id"] = $id; $this->send(0x84, array( - $this->counter[0], + $count, 0x00, $data, )); - ++$this->counter[0]; } }