Updated packet handling to eliminate loops

This commit is contained in:
Shoghi Cervantes 2014-02-08 23:01:22 +01:00
parent 3fed63b248
commit 7f0693e7e7
2 changed files with 6 additions and 17 deletions

View File

@ -560,7 +560,7 @@ class PocketMinecraftServer{
$lastLoop = 0; $lastLoop = 0;
while($this->stop === false){ while($this->stop === false){
$packet = $this->interface->readPacket(); $packet = $this->interface->readPacket();
if($packet !== false){ if($packet instanceof Packet){
$this->packetHandler($packet); $this->packetHandler($packet);
$lastLoop = 0; $lastLoop = 0;
}else{ }else{

View File

@ -41,20 +41,18 @@ class MinecraftInterface{
} }
public function readPacket(){ public function readPacket(){
$pk = $this->popPacket();
if($this->socket->connected === false){ if($this->socket->connected === false){
return $pk; return false;
} }
$buf = ""; $buf = "";
$source = false; $source = false;
$port = 1; $port = 1;
$len = $this->socket->read($buf, $source, $port); $len = $this->socket->read($buf, $source, $port);
if($len === false or $len === 0){ if($len === false or $len === 0){
return $pk; return false;
} }
$this->bandwidth[0] += $len; $this->bandwidth[0] += $len;
$this->parsePacket($buf, $source, $port); return $this->parsePacket($buf, $source, $port);
return ($pk !== false ? $pk : $this->popPacket());
} }
private function parsePacket($buffer, $source, $port){ private function parsePacket($buffer, $source, $port){
@ -64,8 +62,9 @@ class MinecraftInterface{
if($parser->packet !== false){ if($parser->packet !== false){
$parser->packet->ip = $source; $parser->packet->ip = $source;
$parser->packet->port = $port; $parser->packet->port = $port;
$this->packets[] = $parser->packet; return $parser->packet;
} }
return false;
}else{ }else{
$packet = new Packet(); $packet = new Packet();
$packet->ip = $source; $packet->ip = $source;
@ -76,16 +75,6 @@ class MinecraftInterface{
} }
return false; return false;
} }
return true;
}
public function popPacket(){
if(count($this->packets) > 0){
$p = each($this->packets);
unset($this->packets[$p[0]]);
return $p[1];
}
return false;
} }
public function writePacket(Packet $packet){ public function writePacket(Packet $packet){