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);

File diff suppressed because it is too large Load Diff

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);
}
console("[ERROR] Unknown Packet ID 0x".Utils::strToHex(chr($pid)), true, 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);
@ -238,7 +233,7 @@ class Packet{
$this->data["packets"] = array();
while($len > $offset){
$pid = ord($raw{$offset});
++$offset;
++$offset;
$reliability = ($pid & 0b11100000) >> 5;
$hasSplit = ($pid & 0b00010000) >> 4;
$length = Utils::readShort(substr($raw, $offset, 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){