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

Split up Socket and Rcon classes to be engine specific extensions.

This commit is contained in:
Anthony Birkett
2021-05-31 11:12:21 +01:00
parent a2f7834ef5
commit b01c1f643f
20 changed files with 1520 additions and 1156 deletions
+285
View File
@@ -0,0 +1,285 @@
<?php
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\Socket;
use xPaw\SourceQuery\Buffer;
use xPaw\SourceQuery\Exception\InvalidArgumentException;
use xPaw\SourceQuery\Exception\InvalidPacketException;
use xPaw\SourceQuery\Exception\SocketException;
/**
* Base socket
*
* @package xPaw\SourceQuery
*
* @uses InvalidPacketException
* @uses SocketException
*/
abstract class AbstractSocket implements SocketInterface
{
/**
* @var resource|null
*/
public $socket;
/**
* @var int
*/
public int $engine = SocketType::SOURCE;
/**
* @var string $address
*/
public string $address = '';
/**
* @var int $port
*/
public int $port = 0;
/**
* @var int $timeout
*/
public int $timeout = 0;
/**
* Destructor
*/
public function __destruct()
{
$this->close();
}
/**
* @return string
*/
public function getAddress(): string
{
return $this->address;
}
/**
* @return int
*/
public function getPort(): int
{
return $this->port;
}
/**
* @return int
*/
public function getTimeout(): int
{
return $this->timeout;
}
/**
* @return resource
*
* @throws InvalidArgumentException
*/
public function getSocket()
{
if (!$this->socket) {
throw new InvalidArgumentException('Socket not open.');
}
return $this->socket;
}
/**
* Close
*/
public function close(): void
{
if ($this->socket) {
fclose($this->socket);
$this->socket = null;
}
}
/**
* @param string $address
* @param int $port
* @param int $timeout
* @param int $engine
*
* @throws SocketException
*/
public function open(string $address, int $port, int $timeout, int $engine): void
{
$this->timeout = $timeout;
$this->engine = $engine;
$this->port = $port;
$this->address = $address;
$socket = @fsockopen('udp://' . $address, $port, $errNo, $errStr, $timeout);
if ($errNo || $socket === false) {
throw new SocketException('Could not create socket: ' . $errStr, SocketException::COULD_NOT_CREATE_SOCKET);
}
$this->socket = $socket;
stream_set_timeout($this->socket, $timeout);
stream_set_blocking($this->socket, true);
}
/**
* @param int $header
* @param string $string
*
* @return bool
*/
public function write(int $header, string $string = ''): bool
{
$command = pack('ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $header, $string);
$length = strlen($command);
return $length === fwrite($this->socket, $command, $length);
}
/**
* Reads from socket and returns Buffer.
*
* @param int $length
*
* @return Buffer Buffer
*
* @throws InvalidPacketException
* @throws SocketException
*
*/
public function read(int $length = 1400): Buffer
{
$buffer = new Buffer();
$data = fread($this->socket, $length);
if (!$data) {
throw new SocketException('Failed to open socket.');
}
$buffer->set($data);
$this->readInternal($buffer, $length, [ $this, 'sherlock' ]);
return $buffer;
}
/**
* @param Buffer $buffer
* @param int $length
*
* @return bool
*
* @throws InvalidPacketException
*/
public function sherlock(Buffer $buffer, int $length): bool
{
$data = fread($this->socket, $length);
if (strlen($data) < 4) {
return false;
}
$buffer->set($data);
return $buffer->getLong() === -2;
}
/**
*
* Get packet data (count, number, checksum) from the buffer. Different for goldsrc/src.
*
* @param Buffer $buffer
* @param int $count
* @param int $number
* @param bool $isCompressed
* @param int|null $checksum
*/
abstract protected function readInternalPacketData(
Buffer $buffer,
int &$count,
int &$number,
bool &$isCompressed,
?int &$checksum
): void;
/**
* @param Buffer $buffer
* @param int $length
* @param callable $sherlockFunction
*
* @return Buffer
*
* @throws InvalidPacketException
*/
protected function readInternal(Buffer $buffer, int $length, callable $sherlockFunction): Buffer
{
if ($buffer->remaining() === 0) {
throw new InvalidPacketException('Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY);
}
$header = $buffer->getLong();
// Single packet, do nothing.
if ($header === -1) {
return $buffer;
}
if ($header === -2) { // Split packet
$packets = [];
$packetCount = 0;
$packetNumber = 0;
$packetChecksum = null;
do {
$requestId = $buffer->getLong();
$isCompressed = ($requestId & 0x80000000) !== 0;
$this->readInternalPacketData(
$buffer,
$packetCount,
$packetNumber,
$isCompressed,
$packetChecksum
);
$packets[$packetNumber] = $buffer->get();
$readMore = $packetCount > count($packets);
} while ($readMore && $sherlockFunction($buffer, $length));
$data = implode($packets);
// TODO: Test this
if ($isCompressed) {
$data = bzdecompress($data);
if (!is_string($data) || crc32($data) !== $packetChecksum) {
throw new InvalidPacketException('CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH);
}
}
$buffer->set(substr($data, 4));
} else {
throw new InvalidPacketException('Socket read: Raw packet header mismatch. (0x' . dechex($header) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
}
return $buffer;
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace xPaw\SourceQuery\Socket;
use xPaw\SourceQuery\Buffer;
final class GoldSourceSocket extends AbstractSocket
{
/**
* @return int
*/
public function getType(): int
{
return SocketType::GOLDSOURCE;
}
/**
* @param Buffer $buffer
* @param int $count
* @param int $number
* @param bool $isCompressed
* @param int|null $checksum
*/
protected function readInternalPacketData(
Buffer $buffer,
int &$count,
int &$number,
bool &$isCompressed,
?int &$checksum
): void {
$packetCountAndNumber = $buffer->getByte();
$count = $packetCountAndNumber & 0xF;
$number = $packetCountAndNumber >> 4;
$isCompressed = false;
}
}
+79
View File
@@ -0,0 +1,79 @@
<?php
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\Socket;
use xPaw\SourceQuery\Buffer;
/**
* Base socket interface
*
* @package xPaw\SourceQuery\Socket
*/
interface SocketInterface
{
/**
* @return string
*/
public function getAddress(): string;
/**
* @return int
*/
public function getPort(): int;
/**
* @return int
*/
public function getTimeout(): int;
/**
* @return resource
*/
public function getSocket();
/**
* Get the socket type (goldsrc/src).
*/
public function getType(): int;
/**
* Close
*/
public function close(): void;
/**
* @param string $address
* @param int $port
* @param int $timeout
* @param int $engine
*/
public function open(string $address, int $port, int $timeout, int $engine): void;
/**
* @param int $header
* @param string $string
*
* @return bool
*/
public function write(int $header, string $string = ''): bool;
/**
* @param int $length
*
* @return Buffer
*/
public function read(int $length = 1400): Buffer;
}
+11
View File
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace xPaw\SourceQuery\Socket;
abstract class SocketType
{
public const GOLDSOURCE = 0;
public const SOURCE = 1;
}
+47
View File
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace xPaw\SourceQuery\Socket;
use xPaw\SourceQuery\Buffer;
use xPaw\SourceQuery\Exception\InvalidPacketException;
final class SourceSocket extends AbstractSocket
{
/**
* @return int
*/
public function getType(): int
{
return SocketType::SOURCE;
}
/**
* @param Buffer $buffer
* @param int $count
* @param int $number
* @param bool $isCompressed
* @param int|null $checksum
*
* @throws InvalidPacketException
*/
protected function readInternalPacketData(
Buffer $buffer,
int &$count,
int &$number,
bool &$isCompressed,
?int &$checksum
): void {
$count = $buffer->getByte();
$number = $buffer->getByte() + 1;
if ($isCompressed) {
$buffer->getLong(); // Split size
$checksum = $buffer->getUnsignedLong();
} else {
$buffer->getShort(); // Split size
}
}
}
+211
View File
@@ -0,0 +1,211 @@
<?php
declare(strict_types=1);
namespace xPaw\SourceQuery\Socket;
use SplQueue;
use SplDoublyLinkedList;
use xPaw\SourceQuery\Buffer;
use xPaw\SourceQuery\Exception\InvalidPacketException;
final class TestableSocket extends AbstractSocket
{
/**
* @var SplQueue<string>
*/
private SplQueue $packetQueue;
/**
* @var int
*/
private int $type;
/**
* TestableSocket constructor.
*
* @param int $type
*/
public function __construct(int $type)
{
$this->packetQueue = new SplQueue();
$this->packetQueue->setIteratorMode(SplDoublyLinkedList::IT_MODE_DELETE);
$this->type = $type;
}
/**
* @return int
*/
public function getType(): int
{
return $this->type;
}
/**
* @param string $data
*/
public function queue(string $data): void
{
$this->packetQueue->push($data);
}
/**
* Close.
*/
public function close(): void
{
}
/**
* @param string $address
* @param int $port
* @param int $timeout
* @param int $engine
*/
public function open(string $address, int $port, int $timeout, int $engine): void
{
$this->timeout = $timeout;
$this->engine = $engine;
$this->port = $port;
$this->address = $address;
}
/**
* @param int $header
* @param string $string
*
* @return bool
*/
public function write(int $header, string $string = ''): bool
{
return true;
}
/**
* @param int $length
*
* @return Buffer
*
* @throws InvalidPacketException
*/
public function read(int $length = 1400): Buffer
{
$buffer = new Buffer();
$buffer->set($this->packetQueue->shift());
$this->readInternal($buffer, $length, [ $this, 'sherlock' ]);
return $buffer;
}
/**
* @param Buffer $buffer
* @param int $length
*
* @return bool
*
* @throws InvalidPacketException
*/
public function sherlock(Buffer $buffer, int $length): bool
{
if ($this->packetQueue->isEmpty()) {
return false;
}
$buffer->set($this->packetQueue->shift());
return $buffer->getLong() === -2;
}
/**
* @param Buffer $buffer
* @param int $count
* @param int $number
* @param bool $isCompressed
* @param int|null $checksum
*
* @throws InvalidPacketException
*/
protected function readInternalPacketData(
Buffer $buffer,
int &$count,
int &$number,
bool &$isCompressed,
?int &$checksum
): void {
switch ($this->type) {
case SocketType::GOLDSOURCE:
$this->readInternalPacketDataGoldSource(
$buffer,
$count,
$number,
$isCompressed,
$checksum
);
break;
case SocketType::SOURCE:
default:
$this->readInternalPacketDataSource(
$buffer,
$count,
$number,
$isCompressed,
$checksum
);
}
}
/**
* Same as GoldSourceSocket::readInternalPacketData.
*
* @param Buffer $buffer
* @param int $count
* @param int $number
* @param bool $isCompressed
* @param int|null $checksum
*/
protected function readInternalPacketDataGoldSource(
Buffer $buffer,
int &$count,
int &$number,
bool &$isCompressed,
?int &$checksum
): void {
$packetCountAndNumber = $buffer->getByte();
$count = $packetCountAndNumber & 0xF;
$number = $packetCountAndNumber >> 4;
$isCompressed = false;
}
/**
* Same as SourceSocket::readInternalPacketData.
*
* @param Buffer $buffer
* @param int $count
* @param int $number
* @param bool $isCompressed
* @param int|null $checksum
*
* @throws InvalidPacketException
*/
protected function readInternalPacketDataSource(
Buffer $buffer,
int &$count,
int &$number,
bool &$isCompressed,
?int &$checksum
): void {
$count = $buffer->getByte();
$number = $buffer->getByte() + 1;
if ($isCompressed) {
$buffer->getLong(); // Split size
$checksum = $buffer->getUnsignedLong();
} else {
$buffer->getShort(); // Split size
}
}
}