Player packet handling rewrite, fix #350

This commit is contained in:
Shoghi Cervantes 2013-06-09 05:56:48 +02:00
parent 715b92b681
commit 9b88a4a73f
9 changed files with 823 additions and 861 deletions

View File

@ -50,7 +50,6 @@ class ServerAPI{
} }
public function load(){ public function load(){
@mkdir(DATA_PATH."logs/", 0755, true);
@mkdir(DATA_PATH."players/", 0755); @mkdir(DATA_PATH."players/", 0755);
@mkdir(DATA_PATH."worlds/", 0755); @mkdir(DATA_PATH."worlds/", 0755);
@mkdir(DATA_PATH."plugins/", 0755); @mkdir(DATA_PATH."plugins/", 0755);

View File

@ -28,9 +28,13 @@ the Free Software Foundation, either version 3 of the License, or
class Player{ class Player{
private $server; private $server;
private $recoveryQueue = array();
private $receiveQueue = array();
private $resendQueue = array();
private $ackQueue = array();
private $receiveCount = -1;
private $buffer = ""; private $buffer = "";
private $nextBuffer = 0; private $nextBuffer = 0;
public $recovery = array();
private $evid = array(); private $evid = array();
private $lastMovement = 0; private $lastMovement = 0;
private $timeout; private $timeout;
@ -43,7 +47,6 @@ class Player{
private $iusername; private $iusername;
private $eid = false; private $eid = false;
private $startAction = false; private $startAction = false;
private $queue = array();
public $data; public $data;
public $entity = false; public $entity = false;
public $auth = false; public $auth = false;
@ -99,7 +102,7 @@ class Player{
$this->level = $this->server->api->level->getDefault(); $this->level = $this->server->api->level->getDefault();
$this->slot = 0; $this->slot = 0;
$this->packetStats = array(0,0); $this->packetStats = array(0,0);
$this->server->schedule(2, array($this, "onTick"), array(), true); $this->server->schedule(1, array($this, "handlePacketQueues"), 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);
} }
@ -191,20 +194,6 @@ class Player{
$this->server->schedule(1, array($this, "getNextChunk")); $this->server->schedule(1, array($this, "getNextChunk"));
} }
public function onTick(){
if($this->connected === false){
return false;
}
$time = microtime(true);
if($time > $this->timeout){
$this->close("timeout");
}else{
if($this->nextBuffer <= $time and strlen($this->buffer) > 0){
$this->sendBuffer();
}
}
}
public function save(){ public function save(){
if($this->entity instanceof Entity){ if($this->entity instanceof Entity){
$this->data->set("position", array( $this->data->set("position", array(
@ -247,14 +236,13 @@ class Player{
$this->eventHandler(new Container("You have been kicked. Reason: ".$reason), "server.chat"); $this->eventHandler(new Container("You have been kicked. Reason: ".$reason), "server.chat");
$this->directDataPacket(MC_DISCONNECT); $this->directDataPacket(MC_DISCONNECT);
$this->sendBuffer(); $this->sendBuffer();
$this->connected = false;
$this->level->freeAllChunks($this); $this->level->freeAllChunks($this);
$this->buffer = null; $this->buffer = null;
unset($this->buffer); unset($this->buffer);
$this->recovery = null; $this->recoveryQueue = array();
unset($this->recovery); $this->receiveQueue = array();
$this->queue = null; $this->resendQueue = array();
unset($this->queue);
$this->connected = false;
$this->server->interface->stopChunked($this->CID); $this->server->interface->stopChunked($this->CID);
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");
@ -564,9 +552,9 @@ class Player{
} }
} }
} }
$this->directdataPacket(MC_CHAT, array( $this->dataPacket(MC_CHAT, array(
"message" => $m, "message" => $m,
), 0x00, false); ));
} }
} }
@ -828,57 +816,109 @@ class Player{
return array_sum($this->bandwidthStats) / max(1, count($this->bandwidthStats)); return array_sum($this->bandwidthStats) / max(1, count($this->bandwidthStats));
} }
public function handle($pid, $data){ public function handlePacketQueues(){
if($this->connected === false){
return false;
}
$time = microtime(true);
if($time > $this->timeout){
$this->close("timeout");
return false;
}
if(($ackCnt = count($this->ackQueue)) > 0){
rsort($this->ackQueue);
$safeCount = (int) (($this->MTU - 1) / 4);
$packetCnt = (int) ($ackCnt / $safeCount + 1);
for($p = 0; $p < $packetCnt; ++$p){
$acks = array();
for($c = 0; $c < $safeCount; ++$c){
if(($k = array_pop($this->ackQueue)) === null){
break;
}
$acks[] = $k;
}
$this->send(0xc0, array($acks));
}
$this->ackQueue = array();
}
if(($receiveCnt = count($this->receiveQueue)) > 0){
ksort($this->receiveQueue);
foreach($this->receiveQueue as $count => $packets){
unset($this->receiveQueue[$count]);
foreach($packets as $p){
if(isset($p["counter"])){
console($p["counter"]);
if($p["counter"] > $this->receiveCount){
$this->receiveCount = $p["counter"];
}elseif($p["counter"] !== 0){
switch($p["id"]){
case 0x01:
case MC_PONG:
case MC_PING:
case MC_MOVE_PLAYER:
case MC_REQUEST_CHUNK:
case MC_ANIMATE:
case MC_SET_HEALTH:
continue;
}
}
}
$this->handleDataPacket($p["id"], $p);
}
}
}
if($this->nextBuffer <= $time and strlen($this->buffer) > 0){
$this->sendBuffer();
}
$limit = $time - 8; //max lag
foreach($this->recoveryQueue as $count => $data){
if($data["sendtime"] > $limit){
break;
}
unset($this->recoveryQueue[$count]);
$this->resendQueue[$count] = $data;
}
if(($resendCnt = count($this->resendQueue)) > 0){
foreach($this->resendQueue as $count => $data){
unset($this->resendQueue[$count]);
$this->packetStats[1]++;
$this->lag[] = microtime(true) - $data["sendtime"];
$this->directDataPacket($data["id"], $data, $data["pid"], false);
}
}
}
public function handlePacket($pid, $data){
if($this->connected === true){ if($this->connected === true){
$this->timeout = microtime(true) + 20; $this->timeout = microtime(true) + 20;
switch($pid){ switch($pid){
case 0xa0: //NACK case 0xa0: //NACK
foreach($data[0] as $count){ foreach($data[0] as $count){
if(isset($this->recovery[$count])){ if(isset($this->recoveryQueue[$count])){
$this->lag[] = microtime(true) - $this->recovery[$count]["sendtime"]; $this->resendQueue[$count] =& $this->recoveryQueue[$count];
$this->directDataPacket($this->recovery[$count]["id"], $this->recovery[$count], $this->recovery[$count]["pid"]); $this->lag[] = microtime(true) - $this->recoveryQueue[$count]["sendtime"];
++$this->packetStats[1]; unset($this->recoveryQueue[$count]);
$this->recovery[$count] = null; $this->packetStats[1]++;
unset($this->recovery[$count]);
} }
} }
break; break;
case 0xc0: //ACK case 0xc0: //ACK
foreach($data[0] as $count){ foreach($data[0] as $count){
if($count > $this->counter[2]){ if(isset($this->recoveryQueue[$count])){
$this->counter[2] = $count; $this->lag[] = microtime(true) - $this->recoveryQueue[$count]["sendtime"];
} unset($this->recoveryQueue[$count]);
if(isset($this->recovery[$count])){ unset($this->resendQueue[$count]);
$this->lag[] = microtime(true) - $this->recovery[$count]["sendtime"];
$this->recovery[$count] = null;
unset($this->recovery[$count]);
}
}
$limit = microtime(true) - 6; //max lag
foreach($this->recovery as $count => $d){
$diff = $this->counter[2] - $count;
if($diff > 16 and $d["sendtime"] < $limit){
$this->lag[] = microtime(true) - $d["sendtime"];
$this->directDataPacket($d["id"], $d, $d["pid"]);
++$this->packetStats[1];
$this->recovery[$count] = null;
unset($this->recovery[$count]);
} }
} }
break; break;
case 0x07:
if($this->loggedIn === true){ case 0x80: //Data Packet
break;
}
$this->send(0x08, array(
RAKNET_MAGIC,
$this->server->serverID,
$this->port,
$data[3],
0,
));
break;
case 0x80:
case 0x81: case 0x81:
case 0x82: case 0x82:
case 0x83: case 0x83:
@ -894,22 +934,18 @@ class Player{
case 0x8d: case 0x8d:
case 0x8e: case 0x8e:
case 0x8f: case 0x8f:
if(isset($data[0])){ $this->ackQueue[] = $data[0];
$this->send(0xc0, array(array($data[0]))); $this->receiveQueue[$data[0]] = array();
$diff = $data[0] - $this->counter[1]; foreach($data["packets"] as $packet){
if($diff > 1){ //Packet recovery $this->receiveQueue[$data[0]][] = $packet[1];
$this->queue[$data[0]] = array($pid, $data); }
break; break;
}else{ }
$this->counter[1] = $data[0];
++$this->packetStats[0];
} }
} }
if(!isset($data["id"])){ public function handleDataPacket($pid, $data){
break; switch($pid){
}
switch($data["id"]){
case 0x01: case 0x01:
break; break;
case MC_PONG: case MC_PONG:
@ -1088,12 +1124,9 @@ class Player{
$this->server->schedule(5, array($this->entity, "update"), array(), true); $this->server->schedule(5, array($this->entity, "update"), array(), true);
$this->sendArmor(); $this->sendArmor();
$this->eventHandler(new Container($this->server->motd), "server.chat"); $this->eventHandler(new Container($this->server->motd), "server.chat");
if($this->MTU <= 548){
$this->eventHandler("Your connection is bad, you may experience lag and slow map loading.", "server.chat");
}
if($this->iusername === "steve" or $this->iusername === "stevie"){ if($this->iusername === "steve" or $this->iusername === "stevie"){
$this->eventHandler("You're using the default username. Please change it on the Minecraft PE settings.", "server.chat"); $this->sendChat("You're using the default username. Please change it on the Minecraft PE settings.\n");
} }
$this->sendInventory(); $this->sendInventory();
$this->sendSettings(); $this->sendSettings();
@ -1419,7 +1452,6 @@ class Player{
COOKIE => 2, COOKIE => 2,
COOKED_FISH => 5, COOKED_FISH => 5,
RAW_FISH => 2, RAW_FISH => 2,
); );
$slot = $this->getSlot($this->slot); $slot = $this->getSlot($this->slot);
if($this->entity->getHealth() < 20 and isset($items[$slot->getID()])){ if($this->entity->getHealth() < 20 and isset($items[$slot->getID()])){
@ -1599,28 +1631,9 @@ class Player{
} }
break; break;
default: default:
console("[DEBUG] Unhandled 0x".dechex($data["id"])." Data Packet for Client ID ".$this->clientID.": ".print_r($data, true), true, true, 2); console("[DEBUG] Unhandled 0x".dechex($pid)." Data Packet for Client ID ".$this->clientID.": ".print_r($data, true), true, true, 2);
break; break;
} }
if(isset($this->queue[$this->counter[1] + 1])){
$d = $this->queue[$this->counter[1] + 1];
$this->queue[$this->counter[1] + 1] = null;
unset($this->queue[$this->counter[1] + 1]);
$this->handle($d[0], $d[1]);
}elseif(($cnt = count($this->queue)) > 25){
if($cnt >= 256){
$this->close("end of stream");
break;
}
$q = array_shift($this->queue);
$this->counter[1] = $q[1][0];
$this->handle($q[0], $q[1]);
}
break;
}
}
} }
public function sendArmor($player = false){ public function sendArmor($player = false){
@ -1682,15 +1695,7 @@ class Player{
foreach($buffer as $i => $buf){ foreach($buffer as $i => $buf){
$data["raw"] = Utils::writeShort(strlen($buf) << 3).strrev(Utils::writeTriad($this->counter[3]++)).$h.Utils::writeInt($i).$buf; $data["raw"] = Utils::writeShort(strlen($buf) << 3).strrev(Utils::writeTriad($this->counter[3]++)).$h.Utils::writeInt($i).$buf;
$count = $this->counter[0]++; $count = $this->counter[0]++;
if(count($this->recovery) >= PLAYER_RECOVERY_BUFFER){ $this->recoveryQueue[$count] = $data;
reset($this->recovery);
$k = key($this->recovery);
$this->recovery[$k] = null;
unset($this->recovery[$k]);
unset($this->recovery[$k]);
end($this->recovery);
}
$this->recovery[$count] = $data;
$this->send(0x80, array( $this->send(0x80, array(
$count, $count,
0x50, //0b01010000 0x50, //0b01010000
@ -1709,17 +1714,8 @@ class Player{
$data["sendtime"] = microtime(true); $data["sendtime"] = microtime(true);
$count = $this->counter[0]++; $count = $this->counter[0]++;
if($recover !== false){ if($recover !== false){
if(count($this->recovery) >= PLAYER_RECOVERY_BUFFER){ $this->recoveryQueue[$count] = $data;
reset($this->recovery);
$k = key($this->recovery);
$this->recovery[$k] = null;
unset($this->recovery[$k]);
end($this->recovery);
} }
$this->recovery[$count] = $data;
}
$this->send(0x80, array( $this->send(0x80, array(
$count, $count,
$pid, $pid,

View File

@ -379,10 +379,14 @@ class PocketMinecraftServer{
} }
$dump .= "Loaded Modules: ".var_export(get_loaded_extensions(), true)."\r\n"; $dump .= "Loaded Modules: ".var_export(get_loaded_extensions(), true)."\r\n";
$dump .= "Memory Usage Tracking: \r\n".base64_encode(gzdeflate(implode(";", $this->memoryStats), 9))."\r\n"; $dump .= "Memory Usage Tracking: \r\n".base64_encode(gzdeflate(implode(";", $this->memoryStats), 9))."\r\n";
ob_start();
phpinfo();
$dump .= "\r\nphpinfo(): \r\n".base64_encode(gzdeflate(ob_get_contents(), 9))."\r\n";
ob_end_clean();
$dump .= "\r\n```"; $dump .= "\r\n```";
$name = "error_dump_".time(); $name = "Error_Dump_".date("D_M_j-H:i:s-T_Y");
logg($dump, $name, true, 0, true); logg($dump, $name, true, 0, true);
console("[ERROR] Please submit the \"logs/{$name}.log\" file to the Bug Reporting page. Give as much info as you can.", true, true, 0); console("[ERROR] Please submit the \"{$name}.log\" file to the Bug Reporting page. Give as much info as you can.", true, true, 0);
} }
public function tick(){ public function tick(){
@ -404,7 +408,7 @@ class PocketMinecraftServer{
$data =& $packet["data"]; $data =& $packet["data"];
$CID = PocketMinecraftServer::clientID($packet["ip"], $packet["port"]); $CID = PocketMinecraftServer::clientID($packet["ip"], $packet["port"]);
if(isset($this->clients[$CID])){ if(isset($this->clients[$CID])){
$this->clients[$CID]->handle($packet["pid"], $data); $this->clients[$CID]->handlePacket($packet["pid"], $data);
}else{ }else{
if($this->handle("server.noauthpacket", $packet) === false){ if($this->handle("server.noauthpacket", $packet) === false){
return; return;
@ -464,7 +468,13 @@ class PocketMinecraftServer{
$MTU = $data[3]; $MTU = $data[3];
$clientID = $data[4]; $clientID = $data[4];
$this->clients[$CID] = new Player($clientID, $packet["ip"], $packet["port"], $MTU); //New Session! $this->clients[$CID] = new Player($clientID, $packet["ip"], $packet["port"], $MTU); //New Session!
$this->clients[$CID]->handle(0x07, $data); $this->send(0x08, array(
RAKNET_MAGIC,
$this->serverID,
$this->port,
$data[3],
0,
), false, $packet["ip"], $packet["port"]);
break; break;
} }
} }

View File

@ -37,7 +37,6 @@ define("VIEWER", 3);
//Players //Players
define("PLAYER_RECOVERY_BUFFER", 2048);
define("PLAYER_MAX_PACKET_LOSS", 0.20); define("PLAYER_MAX_PACKET_LOSS", 0.20);
define("PLAYER_SURVIVAL_SLOTS", 36); define("PLAYER_SURVIVAL_SLOTS", 36);

View File

@ -262,7 +262,7 @@ function logg($message, $name, $EOL = true, $level = 2, $close = false){
$fpointers = array(); $fpointers = array();
} }
if(!isset($fpointers[$name]) or $fpointers[$name] === false){ if(!isset($fpointers[$name]) or $fpointers[$name] === false){
$fpointers[$name] = @fopen(DATA_PATH."logs/".$name.".log", "ab"); $fpointers[$name] = @fopen(DATA_PATH."/".$name.".log", "ab");
} }
@fwrite($fpointers[$name], $message); @fwrite($fpointers[$name], $message);
if($close === true){ if($close === true){

View File

@ -57,26 +57,6 @@ class MinecraftInterface{
return false; return false;
} }
protected function writeDump($pid, $raw, $data, $origin = "client", $ip = "", $port = 0){
if(LOG === true and DEBUG >= 3){
$p = "[".(microtime(true) - $this->start)."] [".((($origin === "client" and $this->client === true) or ($origin === "server" and $this->client === false)) ? "CLIENT->SERVER":"SERVER->CLIENT")." ".$ip.":".$port."]: ".(isset($data["id"]) ? "MC Packet ".Protocol::$dataName[$pid]:Protocol::$packetName[$pid])." (0x".Utils::strTohex(chr($pid)).") [length ".strlen($raw)."]".PHP_EOL;
$p .= Utils::hexdump($raw);
if(is_array($data)){
foreach($data as $i => $d){
if(!isset(Protocol::$raknet[$pid][$i])){
$ty = "special";
}else{
$ty = Protocol::$raknet[$pid][$i];
}
$p .= $i ." => ".(!is_array($d) ? $ty."(".(($ty === "magic" or substr($ty, 0, 7) === "special" or is_int($ty)) ? Utils::strToHex($d):Utils::printable((string) $d)).")":$ty."(\"".serialize(array_map("Utils::printable", $d))."\")").PHP_EOL;
}
}
$p .= PHP_EOL;
logg($p, "packets", false);
}
}
public function readPacket(){ public function readPacket(){
$pk = $this->popPacket(); $pk = $this->popPacket();
if($this->socket->connected === false){ if($this->socket->connected === false){
@ -104,13 +84,7 @@ class MinecraftInterface{
"ip" => $source, "ip" => $source,
"port" => $port "port" => $port
)) !== true){ )) !== true){
if(LOG === true and DEBUG >= 3){
console("[ERROR] Unknown Packet ID 0x".Utils::strToHex(chr($pid)), true, true, 2); console("[ERROR] Unknown Packet ID 0x".Utils::strToHex(chr($pid)), true, true, 2);
$p = "[".(microtime(true) - $this->start)."] [CLIENT->SERVER ".$source.":".$port."]: Error, bad packet id 0x".Utils::strToHex(chr($pid))." [length ".strlen($buf)."]".PHP_EOL;
$p .= Utils::hexdump($buf);
$p .= PHP_EOL;
logg($p, "packets", true, 2);
}
} }
return false; return false;
} }
@ -251,15 +225,6 @@ class MinecraftInterface{
$p = each($this->data); $p = each($this->data);
unset($this->data[$p[0]]); unset($this->data[$p[0]]);
$p = $p[1]; $p = $p[1];
if(isset($p[1]["packets"]) and is_array($p[1]["packets"])){
foreach($p[1]["packets"] as $d){
$this->data[] = array($p[0], $d[1], $d[2], $p[3], $p[4]);
}
}
$c = (isset($p[1]["id"]) ? true:false);
$p[2] = $c ? chr($p[1]["id"]).$p[2]:$p[2];
$this->writeDump(($c ? $p[1]["id"]:$p[0]), $p[2], $p[1], "server", $p[3], $p[4]);
return array("pid" => $p[0], "data" => $p[1], "raw" => $p[2], "ip" => $p[3], "port" => $p[4]); return array("pid" => $p[0], "data" => $p[1], "raw" => $p[2], "ip" => $p[3], "port" => $p[4]);
} }
return false; return false;
@ -279,7 +244,6 @@ class MinecraftInterface{
$write = strlen($packet->raw); $write = strlen($packet->raw);
}else{ }else{
$write = $this->socket->write($packet->raw, $dest, $port); $write = $this->socket->write($packet->raw, $dest, $port);
$this->writeDump($pid, $packet->raw, $data, "client", $dest, $port);
} }
}else{ }else{
if($force === false and $this->isChunked($CID)){ if($force === false and $this->isChunked($CID)){
@ -290,7 +254,6 @@ class MinecraftInterface{
$write = strlen($data); $write = strlen($data);
}else{ }else{
$write = $this->socket->write($data, $dest, $port); $write = $this->socket->write($data, $dest, $port);
$this->writeDump($pid, $data, false, "client", $dest, $port);
} }
} }
return $write; return $write;

View File

@ -51,10 +51,6 @@ class Packet{
$this->addRaw($this->data[$field]); $this->addRaw($this->data[$field]);
continue; continue;
} }
if(is_int($type)){
$this->addRaw($this->data[$field]);
continue;
}
switch($type){ switch($type){
case "special1": case "special1":
switch($this->pid){ switch($this->pid){
@ -191,13 +187,12 @@ class Packet{
public function parse(){ public function parse(){
foreach($this->struct as $field => $type){ foreach($this->struct as $field => $type){
if(is_int($type)){
$this->data[] = $this->get($type);
continue;
}
switch($type){ switch($type){
case "special1": case "special1":
switch($this->pid){ switch($this->pid){
case 0x07:
$this->data[] = $this->get(5);
break;
case 0x99: case 0x99:
if($this->data[0] >= 2){ // if($this->data[0] >= 2){ //
$messageID = Utils::readShort($this->get(2), false); $messageID = Utils::readShort($this->get(2), false);

View File

@ -208,7 +208,7 @@ class Protocol{
0x07 => array( 0x07 => array(
"magic", "magic",
5, //Security Cookie (idk why it's sent here) "special1", //Security Cookie
"short", //Server UDP Port "short", //Server UDP Port
"short", //MTU Size "short", //MTU Size
"long", //Client GUID "long", //Client GUID

View File

@ -67,7 +67,7 @@ class UDPSocket{
if($this->connected === false){ if($this->connected === false){
return false; return false;
} }
return @socket_recvfrom($this->sock, $buf, 1500, 0, $source, $port); return @socket_recvfrom($this->sock, $buf, 65535, 0, $source, $port);
} }
public function write($data, $dest = false, $port = false){ public function write($data, $dest = false, $port = false){