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(){
@mkdir(DATA_PATH."logs/", 0755, true);
@mkdir(DATA_PATH."players/", 0755);
@mkdir(DATA_PATH."worlds/", 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{
private $server;
private $recoveryQueue = array();
private $receiveQueue = array();
private $resendQueue = array();
private $ackQueue = array();
private $receiveCount = -1;
private $buffer = "";
private $nextBuffer = 0;
public $recovery = array();
private $evid = array();
private $lastMovement = 0;
private $timeout;
@ -43,7 +47,6 @@ class Player{
private $iusername;
private $eid = false;
private $startAction = false;
private $queue = array();
public $data;
public $entity = false;
public $auth = false;
@ -99,7 +102,7 @@ class Player{
$this->level = $this->server->api->level->getDefault();
$this->slot = 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"));
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"));
}
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(){
if($this->entity instanceof Entity){
$this->data->set("position", array(
@ -247,14 +236,13 @@ class Player{
$this->eventHandler(new Container("You have been kicked. Reason: ".$reason), "server.chat");
$this->directDataPacket(MC_DISCONNECT);
$this->sendBuffer();
$this->connected = false;
$this->level->freeAllChunks($this);
$this->buffer = null;
unset($this->buffer);
$this->recovery = null;
unset($this->recovery);
$this->queue = null;
unset($this->queue);
$this->connected = false;
$this->recoveryQueue = array();
$this->receiveQueue = array();
$this->resendQueue = array();
$this->server->interface->stopChunked($this->CID);
if($msg === true and $this->username != ""){
$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,
), 0x00, false);
));
}
}
@ -828,57 +816,109 @@ class Player{
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){
$this->timeout = microtime(true) + 20;
switch($pid){
case 0xa0: //NACK
foreach($data[0] as $count){
if(isset($this->recovery[$count])){
$this->lag[] = microtime(true) - $this->recovery[$count]["sendtime"];
$this->directDataPacket($this->recovery[$count]["id"], $this->recovery[$count], $this->recovery[$count]["pid"]);
++$this->packetStats[1];
$this->recovery[$count] = null;
unset($this->recovery[$count]);
if(isset($this->recoveryQueue[$count])){
$this->resendQueue[$count] =& $this->recoveryQueue[$count];
$this->lag[] = microtime(true) - $this->recoveryQueue[$count]["sendtime"];
unset($this->recoveryQueue[$count]);
$this->packetStats[1]++;
}
}
break;
case 0xc0: //ACK
foreach($data[0] as $count){
if($count > $this->counter[2]){
$this->counter[2] = $count;
}
if(isset($this->recovery[$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]);
if(isset($this->recoveryQueue[$count])){
$this->lag[] = microtime(true) - $this->recoveryQueue[$count]["sendtime"];
unset($this->recoveryQueue[$count]);
unset($this->resendQueue[$count]);
}
}
break;
case 0x07:
if($this->loggedIn === true){
break;
}
$this->send(0x08, array(
RAKNET_MAGIC,
$this->server->serverID,
$this->port,
$data[3],
0,
));
break;
case 0x80:
case 0x80: //Data Packet
case 0x81:
case 0x82:
case 0x83:
@ -894,22 +934,18 @@ class Player{
case 0x8d:
case 0x8e:
case 0x8f:
if(isset($data[0])){
$this->send(0xc0, array(array($data[0])));
$diff = $data[0] - $this->counter[1];
if($diff > 1){ //Packet recovery
$this->queue[$data[0]] = array($pid, $data);
$this->ackQueue[] = $data[0];
$this->receiveQueue[$data[0]] = array();
foreach($data["packets"] as $packet){
$this->receiveQueue[$data[0]][] = $packet[1];
}
break;
}else{
$this->counter[1] = $data[0];
++$this->packetStats[0];
}
}
}
if(!isset($data["id"])){
break;
}
switch($data["id"]){
public function handleDataPacket($pid, $data){
switch($pid){
case 0x01:
break;
case MC_PONG:
@ -1088,12 +1124,9 @@ class Player{
$this->server->schedule(5, array($this->entity, "update"), array(), true);
$this->sendArmor();
$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"){
$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->sendSettings();
@ -1419,7 +1452,6 @@ class Player{
COOKIE => 2,
COOKED_FISH => 5,
RAW_FISH => 2,
);
$slot = $this->getSlot($this->slot);
if($this->entity->getHealth() < 20 and isset($items[$slot->getID()])){
@ -1599,28 +1631,9 @@ class Player{
}
break;
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;
}
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){
@ -1682,15 +1695,7 @@ class Player{
foreach($buffer as $i => $buf){
$data["raw"] = Utils::writeShort(strlen($buf) << 3).strrev(Utils::writeTriad($this->counter[3]++)).$h.Utils::writeInt($i).$buf;
$count = $this->counter[0]++;
if(count($this->recovery) >= PLAYER_RECOVERY_BUFFER){
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->recoveryQueue[$count] = $data;
$this->send(0x80, array(
$count,
0x50, //0b01010000
@ -1709,17 +1714,8 @@ class Player{
$data["sendtime"] = microtime(true);
$count = $this->counter[0]++;
if($recover !== false){
if(count($this->recovery) >= PLAYER_RECOVERY_BUFFER){
reset($this->recovery);
$k = key($this->recovery);
$this->recovery[$k] = null;
unset($this->recovery[$k]);
end($this->recovery);
$this->recoveryQueue[$count] = $data;
}
$this->recovery[$count] = $data;
}
$this->send(0x80, array(
$count,
$pid,

View File

@ -379,10 +379,14 @@ class PocketMinecraftServer{
}
$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";
ob_start();
phpinfo();
$dump .= "\r\nphpinfo(): \r\n".base64_encode(gzdeflate(ob_get_contents(), 9))."\r\n";
ob_end_clean();
$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);
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(){
@ -404,7 +408,7 @@ class PocketMinecraftServer{
$data =& $packet["data"];
$CID = PocketMinecraftServer::clientID($packet["ip"], $packet["port"]);
if(isset($this->clients[$CID])){
$this->clients[$CID]->handle($packet["pid"], $data);
$this->clients[$CID]->handlePacket($packet["pid"], $data);
}else{
if($this->handle("server.noauthpacket", $packet) === false){
return;
@ -464,7 +468,13 @@ class PocketMinecraftServer{
$MTU = $data[3];
$clientID = $data[4];
$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;
}
}

View File

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

View File

@ -262,7 +262,7 @@ function logg($message, $name, $EOL = true, $level = 2, $close = false){
$fpointers = array();
}
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);
if($close === true){

View File

@ -57,26 +57,6 @@ class MinecraftInterface{
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(){
$pk = $this->popPacket();
if($this->socket->connected === false){
@ -104,13 +84,7 @@ class MinecraftInterface{
"ip" => $source,
"port" => $port
)) !== true){
if(LOG === true and DEBUG >= 3){
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;
}
@ -251,15 +225,6 @@ class MinecraftInterface{
$p = each($this->data);
unset($this->data[$p[0]]);
$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 false;
@ -279,7 +244,6 @@ class MinecraftInterface{
$write = strlen($packet->raw);
}else{
$write = $this->socket->write($packet->raw, $dest, $port);
$this->writeDump($pid, $packet->raw, $data, "client", $dest, $port);
}
}else{
if($force === false and $this->isChunked($CID)){
@ -290,7 +254,6 @@ class MinecraftInterface{
$write = strlen($data);
}else{
$write = $this->socket->write($data, $dest, $port);
$this->writeDump($pid, $data, false, "client", $dest, $port);
}
}
return $write;

View File

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

View File

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

View File

@ -67,7 +67,7 @@ class UDPSocket{
if($this->connected === 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){