1
0
mirror of https://github.com/xPaw/PHP-Source-Query.git synced 2026-06-11 01:33:15 +02:00

Remove engine from the Sockets.

Docblock changes.
Reorder methods to be logical.
This commit is contained in:
Anthony Birkett
2021-05-31 11:52:18 +01:00
parent b01c1f643f
commit d9bab8aa25
17 changed files with 369 additions and 408 deletions
+63 -72
View File
@@ -22,15 +22,6 @@ use xPaw\SourceQuery\Exception\SocketException;
use xPaw\SourceQuery\Socket\SocketInterface;
use xPaw\SourceQuery\SourceQuery;
/**
* Class SourceRcon
*
* @package xPaw\SourceQuery
*
* @uses AuthenticationException
* @uses InvalidPacketException
* @uses SocketException
*/
final class SourceRcon extends AbstractRcon
{
/**
@@ -56,20 +47,6 @@ final class SourceRcon extends AbstractRcon
$this->socket = $socket;
}
/**
* Close
*/
public function close(): void
{
if ($this->rconSocket) {
fclose($this->rconSocket);
$this->rconSocket = null;
}
$this->rconRequestId = 0;
}
/**
* @throws SocketException
*/
@@ -94,6 +71,49 @@ final class SourceRcon extends AbstractRcon
}
}
/**
* Close
*/
public function close(): void
{
if ($this->rconSocket) {
fclose($this->rconSocket);
$this->rconSocket = null;
}
$this->rconRequestId = 0;
}
/**
* @param string $password
*
* @throws AuthenticationException
* @throws InvalidPacketException
*/
public function authorize(string $password): void
{
$this->write(SourceQuery::SERVERDATA_AUTH, $password);
$buffer = $this->read();
$requestId = $buffer->getLong();
$type = $buffer->getLong();
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again.
// More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments
if ($type === SourceQuery::SERVERDATA_RESPONSE_VALUE) {
$buffer = $this->read();
$requestId = $buffer->getLong();
$type = $buffer->getLong();
}
if ($requestId === -1 || $type !== SourceQuery::SERVERDATA_AUTH_RESPONSE) {
throw new AuthenticationException('RCON authorization failed.', AuthenticationException::BAD_PASSWORD);
}
}
/**
* @param string $command
*
@@ -119,7 +139,7 @@ final class SourceRcon extends AbstractRcon
$data = $buffer->get();
// We do this stupid hack to handle split packets
// We do this stupid hack to handle split packets.
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
if (strlen($data) >= 4000) {
$this->write(SourceQuery::SERVERDATA_REQUESTVALUE);
@@ -127,7 +147,7 @@ final class SourceRcon extends AbstractRcon
do {
$buffer = $this->read();
$buffer->getLong(); // RequestID
$buffer->getLong(); // RequestID.
if ($buffer->getLong() !== SourceQuery::SERVERDATA_RESPONSE_VALUE) {
break;
@@ -146,53 +166,6 @@ final class SourceRcon extends AbstractRcon
return rtrim($data, "\0");
}
/**
* @param string $password
*
* @throws AuthenticationException
* @throws InvalidPacketException
*/
public function authorize(string $password): void
{
$this->write(SourceQuery::SERVERDATA_AUTH, $password);
$buffer = $this->read();
$requestId = $buffer->getLong();
$type = $buffer->getLong();
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again
// More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments
if ($type === SourceQuery::SERVERDATA_RESPONSE_VALUE) {
$buffer = $this->read();
$requestId = $buffer->getLong();
$type = $buffer->getLong();
}
if ($requestId === -1 || $type !== SourceQuery::SERVERDATA_AUTH_RESPONSE) {
throw new AuthenticationException('RCON authorization failed.', AuthenticationException::BAD_PASSWORD);
}
}
/**
* @param int|null $header
* @param string $string
*
* @return bool
*/
protected function write(?int $header, string $string = ''): bool
{
// Pack the packet together
$command = pack('VV', ++$this->rconRequestId, $header) . $string . "\x00\x00";
// Prepend packet length
$command = pack('V', strlen($command)) . $command;
$length = strlen($command);
return $length === fwrite($this->rconSocket, $command, $length);
}
/**
* @return Buffer
*
@@ -232,4 +205,22 @@ final class SourceRcon extends AbstractRcon
return $buffer;
}
/**
* @param int|null $header
* @param string $string
*
* @return bool
*/
protected function write(?int $header, string $string = ''): bool
{
// Pack the packet together.
$command = pack('VV', ++$this->rconRequestId, $header) . $string . "\x00\x00";
// Prepend packet length.
$command = pack('V', strlen($command)) . $command;
$length = strlen($command);
return $length === fwrite($this->rconSocket, $command, $length);
}
}