NetworkSession: some exception handling cleanup

This commit is contained in:
Dylan K. Taylor 2019-02-28 16:36:03 +00:00
parent 13b9fd7b66
commit f680a239f7

View File

@ -207,12 +207,18 @@ class NetworkSession{
while(!$stream->feof() and $this->connected){ while(!$stream->feof() and $this->connected){
try{ try{
$buf = $stream->getString(); $pk = PacketPool::getPacket($stream->getString());
}catch(BinaryDataException $e){ }catch(BinaryDataException $e){
$this->server->getLogger()->debug("Packet batch from " . $this->getDisplayName() . ": " . bin2hex($stream->getBuffer())); $this->server->getLogger()->debug("Packet batch from " . $this->getDisplayName() . ": " . bin2hex($stream->getBuffer()));
throw new BadPacketException("Packet batch decode error: " . $e->getMessage(), 0, $e); throw new BadPacketException("Packet batch decode error: " . $e->getMessage(), 0, $e);
} }
$this->handleDataPacket(PacketPool::getPacket($buf));
try{
$this->handleDataPacket($pk);
}catch(BadPacketException $e){
$this->server->getLogger()->debug($pk->getName() . " from " . $this->getDisplayName() . ": " . bin2hex($pk->getBuffer()));
throw new BadPacketException("Error processing " . $pk->getName() . ": " . $e->getMessage(), 0, $e);
}
} }
} }
@ -223,7 +229,7 @@ class NetworkSession{
*/ */
public function handleDataPacket(Packet $packet) : void{ public function handleDataPacket(Packet $packet) : void{
if(!($packet instanceof ServerboundPacket)){ if(!($packet instanceof ServerboundPacket)){
throw new BadPacketException("Unexpected non-serverbound packet " . $packet->getName()); throw new BadPacketException("Unexpected non-serverbound packet");
} }
$timings = Timings::getReceiveDataPacketTimings($packet); $timings = Timings::getReceiveDataPacketTimings($packet);
@ -231,22 +237,19 @@ class NetworkSession{
try{ try{
$packet->decode(); $packet->decode();
}catch(BadPacketException $e){ if(!$packet->feof() and !$packet->mayHaveUnreadBytes()){
$this->server->getLogger()->debug($packet->getName() . " from " . $this->getDisplayName() . ": " . bin2hex($packet->getBuffer())); $remains = substr($packet->getBuffer(), $packet->getOffset());
throw $e; $this->server->getLogger()->debug("Still " . strlen($remains) . " bytes unread in " . $packet->getName() . ": " . bin2hex($remains));
} }
if(!$packet->feof() and !$packet->mayHaveUnreadBytes()){
$remains = substr($packet->getBuffer(), $packet->getOffset());
$this->server->getLogger()->debug("Still " . strlen($remains) . " bytes unread in " . $packet->getName() . ": " . bin2hex($remains));
}
$ev = new DataPacketReceiveEvent($this->player, $packet); $ev = new DataPacketReceiveEvent($this->player, $packet);
$ev->call(); $ev->call();
if(!$ev->isCancelled() and !$packet->handle($this->handler)){ if(!$ev->isCancelled() and !$packet->handle($this->handler)){
$this->server->getLogger()->debug("Unhandled " . $packet->getName() . " received from " . $this->getDisplayName() . ": " . bin2hex($packet->getBuffer())); $this->server->getLogger()->debug("Unhandled " . $packet->getName() . " received from " . $this->getDisplayName() . ": " . bin2hex($packet->getBuffer()));
}
}finally{
$timings->stopTiming();
} }
$timings->stopTiming();
} }
public function sendDataPacket(ClientboundPacket $packet, bool $immediate = false) : bool{ public function sendDataPacket(ClientboundPacket $packet, bool $immediate = false) : bool{