Skip to content

Commit f89720e

Browse files
committed
implemented reading multi-packet responses
1 parent ed46208 commit f89720e

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/Rcon.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Rcon
2626

2727
const PACKET_AUTHORIZE = 5;
2828
const PACKET_COMMAND = 6;
29+
const PACKET_COMMAND_ENDING = 7;
2930

3031
const SERVERDATA_AUTH = 3;
3132
const SERVERDATA_AUTH_RESPONSE = 2;
@@ -117,15 +118,21 @@ public function sendCommand($command)
117118
// send command packet
118119
$this->writePacket(self::PACKET_COMMAND, self::SERVERDATA_EXECCOMMAND, $command);
119120

121+
// send additional packet to determine last response packet later
122+
$this->writePacket(self::PACKET_COMMAND_ENDING, self::SERVERDATA_EXECCOMMAND, 'ping');
123+
120124
// get response
125+
$response = '';
121126
$response_packet = $this->readPacket();
122-
if ($response_packet['id'] == self::PACKET_COMMAND) {
123-
if ($response_packet['type'] == self::SERVERDATA_RESPONSE_VALUE) {
124-
$this->lastResponse = $response_packet['body'];
125-
126-
return $response_packet['body'];
127-
}
127+
while ($response_packet['id'] == self::PACKET_COMMAND && $response_packet['type'] == self::SERVERDATA_RESPONSE_VALUE) {
128+
$response .= $response_packet['body'];
129+
$response_packet = $this->readPacket();
128130
}
131+
$response = substr($response, 0, -3);
132+
if ($response != '') {
133+
$this->lastResponse = $response_packet['body'];
134+
return $response;
135+
}
129136

130137
return false;
131138
}

0 commit comments

Comments
 (0)