From 801c2736bace3a44e0b2df760a911089c7e478a6 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Wed, 12 Dec 2012 20:03:06 +0100 Subject: [PATCH] Socket cleanup --- classes/MinecraftInterface.class.php | 30 ++++++++++------------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/classes/MinecraftInterface.class.php b/classes/MinecraftInterface.class.php index 8714ff342..fc0b461c7 100644 --- a/classes/MinecraftInterface.class.php +++ b/classes/MinecraftInterface.class.php @@ -26,10 +26,10 @@ the Free Software Foundation, either version 3 of the License, or */ class MinecraftInterface{ - var $pstruct, $name, $server, $protocol, $client, $buffer, $dataName; - + var $pstruct, $name, $protocol, $client, $buffer, $dataName; + private $socket; function __construct($server, $protocol = CURRENT_PROTOCOL, $port = 25565, $listen = false, $client = true){ - $this->server = new UDPSocket($server, $port, (bool) $listen); + $this->socket = new UDPSocket($server, $port, (bool) $listen); $this->protocol = (int) $protocol; require("pstruct/RakNet.php"); require("pstruct/packetName.php"); @@ -44,7 +44,7 @@ class MinecraftInterface{ } public function close(){ - return $this->server->close(); + return $this->socket->close(); } protected function getStruct($pid){ @@ -74,30 +74,22 @@ class MinecraftInterface{ if($p !== false){ return $p; } - if($this->server->connected === false){ - //return array("pid" => "ff", "data" => array(0 => 'Connection error')); + if($this->socket->connected === false){ + return false; } - $data = $this->server->read(); + $data = $this->socket->read(); if($data[3] === false){ return false; } $pid = ord($data[0]); - /*if($pid === 0x84){ - $data[0] = substr($data[0], 10); - $pid = ord($data[0]); - }*/ $struct = $this->getStruct($pid); if($struct === false){ - console("[ERROR] Bad packet id 0x".Utils::strTohex(chr($pid)), true, true, 0); + console("[ERROR] Unknown Packet ID 0x".Utils::strToHex(chr($pid)), true, true, 0); $p = "[".(microtime(true) - $this->start)."] [".((($origin === "client" and $this->client === true) or ($origin === "server" and $this->client === false)) ? "CLIENT->SERVER":"SERVER->CLIENT")." ".$ip.":".$port."]: Error, bad packet id 0x".Utils::strTohex(chr($pid))." [length ".strlen($raw)."]".PHP_EOL; $p .= Utils::hexdump($data[0]); $p .= PHP_EOL; - logg($p, "packets", true, 2); - + logg($p, "packets", true, 2); $this->buffer = ""; - //$this->server->recieve("\xff".Utils::writeString('Bad packet id '.$pid.'')); - //$this->writePacket("ff", array(0 => 'Bad packet id '.$pid.'')); - //return array("pid" => "ff", "data" => array(0 => 'Bad packet id '.$pid.'')); return false; } @@ -131,10 +123,10 @@ class MinecraftInterface{ $packet->protocol = $this->protocol; $packet->data = $data; $packet->create(); - $write = $this->server->write($packet->raw, $dest, $port); + $write = $this->socket->write($packet->raw, $dest, $port); $this->writeDump($pid, $packet->raw, $data, "client", $dest, $port); }else{ - $write = $this->server->write($data, $dest, $port); + $write = $this->socket->write($data, $dest, $port); $this->writeDump($pid, $data, false, "client", $dest, $port); } return true;