Partial ChunkData sending, NACK handling, Queues

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-13 22:32:28 +01:00
parent 063f44c330
commit 6ef600942b
4 changed files with 140 additions and 186 deletions

View File

@ -44,9 +44,9 @@ class LevelAPI{
} }
public function getChunk($X, $Z){ public function getChunk($X, $Z){
/*if($this->check() and isset($this->map->map[$X][$Z])){ if($this->check() and isset($this->map->map[$X][$Z])){
return $this->map->map[$X][$Z]; return $this->map->map[$X][$Z];
}*/ }
return false; return false;
} }
@ -62,11 +62,11 @@ class LevelAPI{
for($j = 0; $j < $columnsPerPacket; ++$j){ for($j = 0; $j < $columnsPerPacket; ++$j){
$ordered[$i] .= "\xff"; $ordered[$i] .= "\xff";
for($k = 0; $k < 8; ++$k){ for($k = 0; $k < 8; ++$k){
$ordered[$i] .= substr($c[$i][0], $k << 4, 16); //Block data $ordered[$i] .= substr($c[0][$i+$j], $k << 4, 16); //Block data
$ordered[$i] .= substr($c[$i][1], $k << 3, 8); //Meta data $ordered[$i] .= substr($c[1][$i+$j], $k << 3, 8); //Meta data
} }
++$i;
} }
$i += $columnsPerPacket;
} }
return $ordered; return $ordered;
} }

View File

@ -25,11 +25,8 @@ the Free Software Foundation, either version 3 of the License, or
*/ */
define("MAP_WIDTH", 256);
define("MAP_HEIGHT", 128);
class ChunkParser{ class ChunkParser{
private $raw = b""; private $location, $raw = b"";
var $sectorLenght = 4096; //16 * 16 * 16 var $sectorLenght = 4096; //16 * 16 * 16
var $chunkLenght = 86016; //21 * $sectorLenght var $chunkLenght = 86016; //21 * $sectorLenght
var $map; var $map;
@ -38,6 +35,29 @@ class ChunkParser{
$map = array(); $map = array();
} }
private function loadLocationTable(){
$this->location = array();
console("[DEBUG] Loading Chunk Location table...", true, true, 2);
$chunkCnt = 0;
for($offset = 0; $offset < 0x1000; $offset += 4){
$data = substr($this->raw, $offset, 4);
$sectors = ord($data{0});
if($sectors === 0){
continue;
}
$x = ord($data{1});
$z = ord($data{2});
$X = $chunkCnt % 16;
$Z = $chunkCnt >> 4;
//$unused = ord($data{3});
if(!isset($this->location[$X])){
$this->location[$X] = array();
}
$this->location[$X][$Z] = $this->getOffset($X, $Z, $sectors);
++$chunkCnt;
}
}
public function loadFile($file){ public function loadFile($file){
if(!file_exists($file)){ if(!file_exists($file)){
return false; return false;
@ -52,10 +72,10 @@ class ChunkParser{
return array(ord($data{0}), ord($data{1}), ord($data{2}), ord($data{3})); return array(ord($data{0}), ord($data{1}), ord($data{2}), ord($data{3}));
} }
private function getOffset($X, $Z){ private function getOffset($X, $Z, $sectors = 21){
//$info = $this->getOffsetPosition($X, $Z); //$info = $this->getOffsetPosition($X, $Z);
//return 4096 + (($info[1] * $info[0]) << 12) + (($info[2] * $data[0]) << 16); //return 4096 + (($info[1] * $info[0]) << 12) + (($info[2] * $data[0]) << 16);
return 4096 + (($X * 21) << 12) + (($Z * 21) << 16); return 0x1000 + (($X * $sectors) << 12) + (($Z * $sectors) << 16);
} }
public function getChunk($X, $Z, $header = true){ public function getChunk($X, $Z, $header = true){
@ -72,7 +92,7 @@ class ChunkParser{
public function parseChunk($X, $Z){ public function parseChunk($X, $Z){
$X = (int) $X; $X = (int) $X;
$Z = (int) $Z; $Z = (int) $Z;
$offset = $this->getOffset($X, $Z); $offset = $this->location[$X][$Z];
$len = Utils::readLInt(substr($this->raw, $offset, 4)); $len = Utils::readLInt(substr($this->raw, $offset, 4));
$offset += 4; $offset += 4;
$chunk = array( $chunk = array(
@ -122,6 +142,7 @@ class ChunkParser{
} }
public function loadMap(){ public function loadMap(){
$this->loadLocationTable();
console("[DEBUG] Loading chunks...", true, true, 2); console("[DEBUG] Loading chunks...", true, true, 2);
for($x = 0; $x < 16; ++$x){ for($x = 0; $x < 16; ++$x){
$this->map[$x] = array(); $this->map[$x] = array();

View File

@ -81,7 +81,7 @@ class PocketMinecraftServer extends stdClass{
$this->event("onPlayerAdd", "eventHandler", true); $this->event("onPlayerAdd", "eventHandler", true);
$this->event("onHealthChange", "eventHandler", true); $this->event("onHealthChange", "eventHandler", true);
$this->action(100000, '$this->time += ceil($this->timePerSecond / 10);$this->trigger("onTimeChange", $this->time);'); $this->action(500000, '$this->time += ceil($this->timePerSecond / 2);$this->trigger("onTimeChange", $this->time);');
$this->action(5000000, '$this->trigger("onHealthRegeneration", 1);'); $this->action(5000000, '$this->trigger("onHealthRegeneration", 1);');
$this->action(1000000 * 60, '$this->reloadConfig();'); $this->action(1000000 * 60, '$this->reloadConfig();');
$this->action(1000000 * 60 * 10, '$this->custom = array();'); $this->action(1000000 * 60 * 10, '$this->custom = array();');
@ -245,7 +245,6 @@ class PocketMinecraftServer extends stdClass{
declare(ticks=15); declare(ticks=15);
register_tick_function(array($this, "tick")); register_tick_function(array($this, "tick"));
$this->event("onTick", array($this, "tickerFunction")); $this->event("onTick", array($this, "tickerFunction"));
$this->event("onReceivedPacket", "packetHandler", true);
register_shutdown_function(array($this, "close")); register_shutdown_function(array($this, "close"));
console("[INFO] Server started!"); console("[INFO] Server started!");
$this->process(); $this->process();
@ -263,7 +262,7 @@ class PocketMinecraftServer extends stdClass{
return md5($pi . $port, true); return md5($pi . $port, true);
} }
public function packetHandler($packet, $event){ public function packetHandler($packet){
$data =& $packet["data"]; $data =& $packet["data"];
$CID = $this->clientID($packet["ip"], $packet["port"]); $CID = $this->clientID($packet["ip"], $packet["port"]);
if(isset($this->clients[$CID])){ if(isset($this->clients[$CID])){
@ -337,8 +336,6 @@ class PocketMinecraftServer extends stdClass{
} }
public function send($pid, $data = array(), $raw = false, $dest = false, $port = false){ public function send($pid, $data = array(), $raw = false, $dest = false, $port = false){
$this->trigger($pid, $data);
$this->trigger("onSentPacket", $data);
$this->interface->writePacket($pid, $data, $raw, $dest, $port); $this->interface->writePacket($pid, $data, $raw, $dest, $port);
} }
@ -346,8 +343,7 @@ class PocketMinecraftServer extends stdClass{
while($this->stop === false){ while($this->stop === false){
$packet = $this->interface->readPacket(); $packet = $this->interface->readPacket();
if($packet !== false){ if($packet !== false){
$this->trigger("onReceivedPacket", $packet); $this->packetHandler($packet);
$this->trigger($packet["pid"], $packet);
}else{ }else{
usleep(10000); usleep(10000);
} }
@ -359,7 +355,7 @@ class PocketMinecraftServer extends stdClass{
if($events === false or $events === true){ if($events === false or $events === true){
return; return;
} }
console("[INTERNAL] Event ". $event, true, true, 3); //console("[INTERNAL] Event ". $event, true, true, 3);
while($evn = $events->fetchArray(SQLITE3_ASSOC)){ while($evn = $events->fetchArray(SQLITE3_ASSOC)){
$ev = $this->events[$event][$evn["ID"]]; $ev = $this->events[$event][$evn["ID"]];
if(isset($ev[1]) and ($ev[1] === true or is_object($ev[1]))){ if(isset($ev[1]) and ($ev[1] === true or is_object($ev[1]))){

View File

@ -27,9 +27,10 @@ the Free Software Foundation, either version 3 of the License, or
class Session{ class Session{
private $server, $timeout, $connected, $evid; private $server, $timeout, $connected, $evid, $queue;
var $clientID, $ip, $port, $counter, $username, $eid, $data, $entity, $auth, $CID, $MTU; var $clientID, $ip, $port, $counter, $username, $eid, $data, $entity, $auth, $CID, $MTU;
function __construct($server, $clientID, $eid, $ip, $port, $MTU){ function __construct($server, $clientID, $eid, $ip, $port, $MTU){
$this->queue = array();
$this->MTU = $MTU; $this->MTU = $MTU;
$this->server = $server; $this->server = $server;
$this->clientID = $clientID; $this->clientID = $clientID;
@ -41,7 +42,7 @@ class Session{
$this->port = $port; $this->port = $port;
$this->timeout = microtime(true) + 25; $this->timeout = microtime(true) + 25;
$this->evid = array(); $this->evid = array();
$this->evid[] = array("onTick", $this->server->event("onTick", array($this, "checkTimeout"))); $this->evid[] = array("onTick", $this->server->event("onTick", array($this, "onTick")));
$this->evid[] = array("onClose", $this->server->event("onClose", array($this, "close"))); $this->evid[] = array("onClose", $this->server->event("onClose", 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);
$this->connected = true; $this->connected = true;
@ -49,9 +50,17 @@ class Session{
$this->counter = array(0, 0); $this->counter = array(0, 0);
} }
public function checkTimeout($time){ public function onTick($time){
if($time > $this->timeout){ if($time > $this->timeout){
$this->close("timeout"); $this->close("timeout");
}else{
if(count($this->queue) > 0){
$limit = $time + 0.1;
while($limit > microtime(true)){
$p = array_shift($this->queue);
$this->dataPacket($p[0], $p[1]);
}
}
} }
} }
@ -81,14 +90,7 @@ class Session{
$this->server->trigger("onChat", $this->username." left the game"); $this->server->trigger("onChat", $this->username." left the game");
} }
console("[INFO] Session with ".$this->ip.":".$this->port." Client ID ".$this->clientID." closed due to ".$reason); console("[INFO] Session with ".$this->ip.":".$this->port." Client ID ".$this->clientID." closed due to ".$reason);
$this->send(0x84, array( $this->dataPacket(MC_DISCONNECT);
$this->counter[0],
0x00,
array(
"id" => MC_DISCONNECT,
),
));
++$this->counter[0];
$this->server->api->player->remove($this->CID); $this->server->api->player->remove($this->CID);
} }
@ -103,40 +105,28 @@ class Session{
if($data["eid"] !== $this->eid){ if($data["eid"] !== $this->eid){
break; break;
} }
$this->send(0x84, array( $this->dataPacket(MC_MOVE_PLAYER, array(
$this->counter[0], "eid" => $data["eid"],
0x00, "x" => $data["x"],
array( "y" => $data["y"],
"id" => MC_MOVE_PLAYER, "z" => $data["z"],
"eid" => $data["eid"], "yaw" => 0,
"x" => $data["x"], "pitch" => 0,
"y" => $data["y"],
"z" => $data["z"],
"yaw" => 0,
"pitch" => 0,
),
)); ));
++$this->counter[0];
break; break;
case "onEntityMove": case "onEntityMove":
if($data === $this->eid){ if($data === $this->eid){
break; break;
} }
$entity = $this->server->entities[$data]; $entity = $this->server->entities[$data];
$this->send(0x84, array( $this->dataPacket(MC_MOVE_ENTITY_POSROT, array(
$this->counter[0], "eid" => $data,
0x00, "x" => $entity->position["x"],
array( "y" => $entity->position["y"],
"id" => MC_MOVE_ENTITY_POSROT, "z" => $entity->position["z"],
"eid" => $data, "yaw" => $entity->position["yaw"],
"x" => $entity->position["x"], "pitch" => $entity->position["pitch"],
"y" => $entity->position["y"],
"z" => $entity->position["z"],
"yaw" => $entity->position["yaw"],
"pitch" => $entity->position["pitch"],
),
)); ));
++$this->counter[0];
break; break;
case "onHealthRegeneration": case "onHealthRegeneration":
if($this->server->difficulty < 2){ if($this->server->difficulty < 2){
@ -145,15 +135,9 @@ class Session{
break; break;
case "onHealthChange": case "onHealthChange":
if($data["eid"] === $this->eid){ if($data["eid"] === $this->eid){
$this->send(0x84, array( $this->dataPacket(MC_SET_HEALTH, array(
$this->counter[0], "health" => $data["health"],
0x00,
array(
"id" => MC_SET_HEALTH,
"health" => $data["health"],
),
)); ));
++$this->counter[0];
$this->data["health"] = $data["health"]; $this->data["health"] = $data["health"];
if(is_object($this->entity)){ if(is_object($this->entity)){
$this->entity->setHealth($data["health"]); $this->entity->setHealth($data["health"]);
@ -164,71 +148,41 @@ class Session{
if($data["eid"] === $this->eid){ if($data["eid"] === $this->eid){
break; break;
} }
$this->send(0x84, array( $this->dataPacket(MC_ADD_PLAYER, array(
$this->counter[0], "clientID" => $data["clientID"],
0x00, "username" => $data["username"],
array( "eid" => $data["eid"],
"id" => MC_ADD_PLAYER, "x" => $data["x"],
"clientID" => $data["clientID"], "y" => $data["y"],
"username" => $data["username"], "z" => $data["z"],
"eid" => $data["eid"],
"x" => $data["x"],
"y" => $data["y"],
"z" => $data["z"],
),
)); ));
++$this->counter[0];
break; break;
case "onEntityRemove": case "onEntityRemove":
if($data === $this->eid){ if($data === $this->eid){
break; break;
} }
$this->send(0x84, array( $this->dataPacket(MC_ENTITY_REMOVE, array(
$this->counter[0], "eid" => $data,
0x00,
array(
"id" => MC_ENTITY_REMOVE,
"eid" => $data,
),
)); ));
++$this->counter[0];
break; break;
case "onTimeChange": case "onTimeChange":
$this->send(0x84, array( $this->dataPacket(MC_SET_TIME, array(
$this->counter[0], "time" => $data,
0x00,
array(
"id" => MC_SET_TIME,
"time" => $data,
),
)); ));
++$this->counter[0];
break; break;
case "onAnimate": case "onAnimate":
if($data["eid"] === $this->eid){ if($data["eid"] === $this->eid){
break; break;
} }
$this->send(0x84, array( $this->dataPacket(MC_ANIMATE, array(
$this->counter[0],
0x00,
array(
"id" => MC_ANIMATE,
"eid" => $data["eid"], "eid" => $data["eid"],
"action" => $data["action"], "action" => $data["action"],
), ));
));
++$this->counter[0];
break; break;
case "onChat": case "onChat":
$this->send(0x84, array( $this->dataPacket(MC_CHAT, array(
$this->counter[0], "message" => $data,
0x00,
array(
"id" => MC_CHAT,
"message" => $data,
),
)); ));
++$this->counter[0];
break; break;
} }
} }
@ -251,7 +205,15 @@ class Session{
case 0x88: case 0x88:
case 0x8c: case 0x8c:
if(isset($data[0])){ if(isset($data[0])){
$this->counter[1] = $data[0]; $diff = $data[0] - $this->counter[1];
if($diff > 1){ //Packet recovery
for($i = $this->counter[1]; $i < $data[0]; ++$i){
$this->send(0xa0, array(1, true, $i));
}
$this->counter[1] = $data[0];
}elseif($diff === 1){
$this->counter[1] = $data[0];
}
$this->send(0xc0, array(1, true, $data[0])); $this->send(0xc0, array(1, true, $data[0]));
} }
switch($data["id"]){ switch($data["id"]){
@ -259,17 +221,11 @@ class Session{
$this->close("client disconnect"); $this->close("client disconnect");
break; break;
case MC_CLIENT_CONNECT: case MC_CLIENT_CONNECT:
$this->send(0x84, array( $this->dataPacket(MC_SERVER_HANDSHAKE, array(
$this->counter[0], "port" => $this->port,
0x00, "session" => $data["session"],
array( "session2" => Utils::readLong("\x00\x00\x00\x00\x04\x44\x0b\xa9"),
"id" => MC_SERVER_HANDSHAKE,
"port" => $this->port,
"session" => $data["session"],
"session2" => Utils::readLong("\x00\x00\x00\x00\x04\x44\x0b\xa9"),
),
)); ));
++$this->counter[0];
break; break;
case MC_CLIENT_HANDSHAKE: case MC_CLIENT_HANDSHAKE:
@ -299,30 +255,18 @@ class Session{
$this->evid[] = array("onHealthRegeneration", $this->server->event("onHealthRegeneration", array($this, "eventHandler"))); $this->evid[] = array("onHealthRegeneration", $this->server->event("onHealthRegeneration", array($this, "eventHandler")));
$this->evid[] = array("onAnimate", $this->server->event("onAnimate", array($this, "eventHandler"))); $this->evid[] = array("onAnimate", $this->server->event("onAnimate", array($this, "eventHandler")));
$this->evid[] = array("onTeleport", $this->server->event("onTeleport", array($this, "eventHandler"))); $this->evid[] = array("onTeleport", $this->server->event("onTeleport", array($this, "eventHandler")));
$this->send(0x84, array( $this->dataPacket(MC_LOGIN_STATUS, array(
$this->counter[0], "status" => 0,
0x00,
array(
"id" => MC_LOGIN_STATUS,
"status" => 0,
),
)); ));
++$this->counter[0]; $this->dataPacket(MC_START_GAME, array(
$this->send(0x84, array( "seed" => $this->server->seed,
$this->counter[0], "x" => $this->data["spawn"]["x"],
0x00, "y" => $this->data["spawn"]["y"],
array( "z" => $this->data["spawn"]["z"],
"id" => MC_START_GAME, "unknown1" => 0,
"seed" => $this->server->seed, "gamemode" => $this->server->gamemode,
"x" => $this->data["spawn"]["x"], "eid" => $this->eid,
"y" => $this->data["spawn"]["y"],
"z" => $this->data["spawn"]["z"],
"unknown1" => 0,
"gamemode" => $this->server->gamemode,
"eid" => $this->eid,
),
)); ));
++$this->counter[0];
break; break;
case MC_READY: case MC_READY:
if(is_object($this->entity)){ if(is_object($this->entity)){
@ -355,24 +299,17 @@ class Session{
"z" => $entity->position["z"], "z" => $entity->position["z"],
), "onPlayerAdd"); ), "onPlayerAdd");
}else{ }else{
$this->send(0x84, array( $this->dataPacket(MC_ADD_MOB, array(
$this->counter[0], "eid" => $entity->eid,
0x00, "type" => $entity->type,
array( "x" => $entity->position["x"],
"id" => MC_ADD_MOB, "y" => $entity->position["y"],
"eid" => $entity->eid, "z" => $entity->position["z"],
"type" => $entity->type,
"x" => $entity->position["x"],
"y" => $entity->position["y"],
"z" => $entity->position["z"],
),
)); ));
++$this->counter[0];
} }
} }
} }
$this->eventHandler($this->server->motd, "onChat"); $this->eventHandler($this->server->motd, "onChat");
//$this->server->trigger("onChat", $this->username." joined the game");
break; break;
case MC_MOVE_PLAYER: case MC_MOVE_PLAYER:
if(is_object($this->entity)){ if(is_object($this->entity)){
@ -384,42 +321,28 @@ class Session{
console("[DEBUG] EID ".$this->eid." has now ".$data["block"].":".$data["meta"]." in their hands!", true, true, 2); console("[DEBUG] EID ".$this->eid." has now ".$data["block"].":".$data["meta"]." in their hands!", true, true, 2);
break; break;
case MC_REQUEST_CHUNK: case MC_REQUEST_CHUNK:
$chunk = $this->server->api->level->getOrderedChunk($data["x"], $data["z"]); $max = floor(($this->MTU - 16 - 255) / 192);
$chunk = $this->server->api->level->getOrderedChunk($data["x"], $data["z"], $max);
foreach($chunk as $d){ foreach($chunk as $d){
$this->send(0x84, array( $this->dataPacket(MC_CHUNK_DATA, array(
$this->counter[0], "x" => $data["x"],
0x00, "z" => $data["z"],
array( "data" => $d,
"id" => MC_CHUNK_DATA, ), true);
"x" => $data["x"],
"z" => $data["z"],
"data" => $d,
),
));
} }
++$this->counter[0];
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; break;
case MC_REMOVE_BLOCK: case MC_REMOVE_BLOCK:
//$this->eventHandler("Blocks broken will not be saved", "onChat");
console("[DEBUG] EID ".$this->eid." broke block at X ".$data["x"]." Y ".$data["y"]." Z ".$data["z"], true, true, 2); console("[DEBUG] EID ".$this->eid." broke block at X ".$data["x"]." Y ".$data["y"]." Z ".$data["z"], true, true, 2);
$this->send(0x84, array( $this->dataPacket(MC_ADD_ITEM_ENTITY, array(
$this->counter[0], "eid" => $this->server->eidCnt++,
0x00, "x" => $data["x"] + mt_rand(0, 100)/100,
array( "y" => $data["y"],
"id" => MC_ADD_ITEM_ENTITY, "z" => $data["z"] + mt_rand(0, 100)/100,
"eid" => $this->server->eidCnt++, "block" => 1,
"x" => $data["x"] + mt_rand(0, 100)/100, "meta" => 0,
"y" => $data["y"], "stack" => 1,
"z" => $data["z"] + mt_rand(0, 100)/100,
"block" => 1,
"meta" => 0,
"stack" => 1,
),
)); ));
++$this->counter[0];
/*$this->send(0x84, array( /*$this->send(0x84, array(
$this->counter[0], $this->counter[0],
0x00, 0x00,
@ -460,4 +383,18 @@ class Session{
} }
} }
public function dataPacket($id, $data = array(), $queue = false){
if($queue === true){
$this->queue[] = array($id, $data);
}else{
$data["id"] = $id;
$this->send(0x84, array(
$this->counter[0],
0x00,
$data,
));
++$this->counter[0];
}
}
} }