From bbe6760cb8c5a6e53a56b3d8207e65eac8c32227 Mon Sep 17 00:00:00 2001 From: piotrskibapl Date: Wed, 17 Mar 2021 19:55:57 +0100 Subject: [PATCH] implemented reading multi-packet responses --- src/Rcon.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Rcon.php b/src/Rcon.php index 604f60b..f2b6e15 100644 --- a/src/Rcon.php +++ b/src/Rcon.php @@ -26,6 +26,7 @@ class Rcon const PACKET_AUTHORIZE = 5; const PACKET_COMMAND = 6; + const PACKET_COMMAND_ENDING = 7; const SERVERDATA_AUTH = 3; const SERVERDATA_AUTH_RESPONSE = 2; @@ -117,14 +118,20 @@ public function sendCommand($command) // send command packet $this->writePacket(self::PACKET_COMMAND, self::SERVERDATA_EXECCOMMAND, $command); + // send additional packet to determine last response packet later + $this->writePacket(self::PACKET_COMMAND_ENDING, self::SERVERDATA_EXECCOMMAND, 'ping'); + // get response + $response = ''; $response_packet = $this->readPacket(); - if ($response_packet['id'] == self::PACKET_COMMAND) { - if ($response_packet['type'] == self::SERVERDATA_RESPONSE_VALUE) { - $this->lastResponse = $response_packet['body']; - - return $response_packet['body']; - } + while ($response_packet['id'] == self::PACKET_COMMAND && $response_packet['type'] == self::SERVERDATA_RESPONSE_VALUE) { + $response .= $response_packet['body']; + $response_packet = $this->readPacket(); + } + $response = substr($response, 0, -3); + if ($response != '') { + $this->lastResponse = $response_packet['body']; + return $response; } return false;