1
0
mirror of https://github.com/xPaw/PHP-Source-Query.git synced 2026-06-11 00:23: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
+19 -8
View File
@@ -2,6 +2,17 @@
declare(strict_types=1);
/**
* @author Pavel Djundik
*
* @link https://xpaw.me
* @link https://github.com/xPaw/PHP-Source-Query
*
* @license GNU Lesser General Public License, version 2.1
*
* @internal
*/
namespace xPaw\SourceQuery\Rcon;
use xPaw\SourceQuery\Buffer;
@@ -10,14 +21,6 @@ use xPaw\SourceQuery\Exception\InvalidPacketException;
abstract class AbstractRcon implements RconInterface
{
/**
* @param int|null $header
* @param string $string
*
* @return bool
*/
abstract protected function write(?int $header, string $string = ''): bool;
/**
* @throws AuthenticationException
* @throws InvalidPacketException
@@ -25,4 +28,12 @@ abstract class AbstractRcon implements RconInterface
* @return Buffer
*/
abstract protected function read(): Buffer;
/**
* @param int|null $header
* @param string $string
*
* @return bool
*/
abstract protected function write(?int $header, string $string = ''): bool;
}
+39 -47
View File
@@ -21,14 +21,6 @@ use xPaw\SourceQuery\Exception\InvalidPacketException;
use xPaw\SourceQuery\Socket\SocketInterface;
use xPaw\SourceQuery\SourceQuery;
/**
* Class GoldSourceRcon
*
* @package xPaw\SourceQuery
*
* @uses AuthenticationException
* @uses InvalidPacketException
*/
final class GoldSourceRcon extends AbstractRcon
{
/**
@@ -56,6 +48,13 @@ final class GoldSourceRcon extends AbstractRcon
$this->socket = $socket;
}
/**
* Open
*/
public function open(): void
{
}
/**
* Close
*/
@@ -66,10 +65,22 @@ final class GoldSourceRcon extends AbstractRcon
}
/**
* Open
* @param string $password
*
* @throws AuthenticationException
*/
public function open(): void
public function authorize(string $password): void
{
$this->rconPassword = $password;
$this->write(null, 'challenge rcon');
$buffer = $this->socket->read();
if ($buffer->get(14) !== 'challenge rcon') {
throw new AuthenticationException('Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD);
}
$this->rconChallenge = trim($buffer->get());
}
/**
@@ -92,39 +103,6 @@ final class GoldSourceRcon extends AbstractRcon
return $buffer->get();
}
/**
* @param string $password
*
* @throws AuthenticationException
*/
public function authorize(string $password): void
{
$this->rconPassword = $password;
$this->write(null, 'challenge rcon');
$buffer = $this->socket->read();
if ($buffer->get(14) !== 'challenge rcon') {
throw new AuthenticationException('Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD);
}
$this->rconChallenge = trim($buffer->get());
}
/**
* @param int|null $header
* @param string $string
*
* @return bool
*/
protected function write(?int $header, string $string = ''): bool
{
$command = pack('cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $string);
$length = strlen($command);
return $length === fwrite($this->socket->getSocket(), $command, $length);
}
/**
* @throws AuthenticationException
* @throws InvalidPacketException
@@ -133,14 +111,14 @@ final class GoldSourceRcon extends AbstractRcon
*/
protected function read(): Buffer
{
// GoldSource RCON has same structure as Query
// GoldSource RCON has same structure as Query.
$buffer = $this->socket->read();
$stringBuffer = '';
// There is no indentifier of the end, so we just need to continue reading
// There is no identifier of the end, so we just need to continue reading.
do {
$readMore = $buffer->remaining() > 0;
$readMore = !$buffer->isEmpty();
if ($readMore) {
if ($buffer->getByte() !== SourceQuery::S2A_RCON) {
@@ -151,7 +129,7 @@ final class GoldSourceRcon extends AbstractRcon
$stringBuffer .= $packet;
//$stringBuffer .= SubStr( $packet, 0, -2 );
// Let's assume if this packet is not long enough, there are no more after this one
// Let's assume if this packet is not long enough, there are no more after this one.
$readMore = strlen($packet) > 1000; // use 1300?
if ($readMore) {
@@ -172,4 +150,18 @@ final class GoldSourceRcon extends AbstractRcon
return $buffer;
}
/**
* @param int|null $header
* @param string $string
*
* @return bool
*/
protected function write(?int $header, string $string = ''): bool
{
$command = pack('cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $string);
$length = strlen($command);
return $length === fwrite($this->socket->getSocket(), $command, $length);
}
}
+20 -9
View File
@@ -2,6 +2,17 @@
declare(strict_types=1);
/**
* @author Pavel Djundik
*
* @link https://xpaw.me
* @link https://github.com/xPaw/PHP-Source-Query
*
* @license GNU Lesser General Public License, version 2.1
*
* @internal
*/
namespace xPaw\SourceQuery\Rcon;
use xPaw\SourceQuery\Exception\AuthenticationException;
@@ -15,15 +26,22 @@ interface RconInterface
*/
public function __construct(SocketInterface $socket);
/**
* Open
*/
public function open(): void;
/**
* Close
*/
public function close(): void;
/**
* Open
* @param string $password
*
* @throws AuthenticationException
*/
public function open(): void;
public function authorize(string $password): void;
/**
* @param string $command
@@ -34,11 +52,4 @@ interface RconInterface
* @throws InvalidPacketException
*/
public function command(string $command): string;
/**
* @param string $password
*
* @throws AuthenticationException
*/
public function authorize(string $password): void;
}
+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);
}
}