mirror of
https://github.com/xPaw/PHP-Source-Query.git
synced 2026-06-11 00:23:15 +02:00
Bring in stricter CS Fixer rules.
This commit is contained in:
@@ -5,8 +5,8 @@ declare(strict_types=1);
|
||||
/**
|
||||
* @author Pavel Djundik
|
||||
*
|
||||
* @link https://xpaw.me
|
||||
* @link https://github.com/xPaw/PHP-Source-Query
|
||||
* @see https://xpaw.me
|
||||
* @see https://github.com/xPaw/PHP-Source-Query
|
||||
*
|
||||
* @license GNU Lesser General Public License, version 2.1
|
||||
*
|
||||
@@ -22,14 +22,8 @@ use xPaw\SourceQuery\Exception\SocketException;
|
||||
|
||||
abstract class AbstractSocket implements SocketInterface
|
||||
{
|
||||
/**
|
||||
* @var string $address
|
||||
*/
|
||||
public string $address = '';
|
||||
|
||||
/**
|
||||
* @var int $port
|
||||
*/
|
||||
public int $port = 0;
|
||||
|
||||
/**
|
||||
@@ -39,39 +33,30 @@ abstract class AbstractSocket implements SocketInterface
|
||||
*/
|
||||
public $socket;
|
||||
|
||||
/**
|
||||
* @var int $timeout
|
||||
*/
|
||||
public int $timeout = 0;
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
* 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 resource
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return resource
|
||||
*/
|
||||
public function getSocket()
|
||||
{
|
||||
@@ -82,30 +67,23 @@ abstract class AbstractSocket implements SocketInterface
|
||||
return $this->socket;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTimeout(): int
|
||||
{
|
||||
return $this->timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $address
|
||||
* @param int $port
|
||||
* @param int $timeout
|
||||
*
|
||||
* @throws SocketException
|
||||
*/
|
||||
public function open(string $address, int $port, int $timeout): void
|
||||
{
|
||||
$this->timeout = $timeout;
|
||||
$this->port = $port;
|
||||
$this->port = $port;
|
||||
$this->address = $address;
|
||||
|
||||
$socket = @fsockopen('udp://' . $address, $port, $errNo, $errStr, $timeout);
|
||||
|
||||
if ($errNo || $socket === false) {
|
||||
if ($errNo || false === $socket) {
|
||||
throw new SocketException('Could not create socket: ' . $errStr, SocketException::COULD_NOT_CREATE_SOCKET);
|
||||
}
|
||||
|
||||
@@ -115,7 +93,7 @@ abstract class AbstractSocket implements SocketInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Close
|
||||
* Close.
|
||||
*/
|
||||
public function close(): void
|
||||
{
|
||||
@@ -129,13 +107,10 @@ abstract class AbstractSocket implements SocketInterface
|
||||
/**
|
||||
* Reads from socket and returns Buffer.
|
||||
*
|
||||
* @param int $length
|
||||
*
|
||||
* @return Buffer Buffer
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
* @throws SocketException
|
||||
*
|
||||
* @return Buffer Buffer
|
||||
*/
|
||||
public function read(int $length = 1400): Buffer
|
||||
{
|
||||
@@ -152,17 +127,12 @@ abstract class AbstractSocket implements SocketInterface
|
||||
|
||||
$buffer->set($data);
|
||||
|
||||
$this->readInternal($buffer, $length, [ $this, 'sherlock' ]);
|
||||
$this->readInternal($buffer, $length, [$this, 'sherlock']);
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $header
|
||||
* @param string $string
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
*/
|
||||
public function write(int $header, string $string = ''): bool
|
||||
@@ -172,17 +142,12 @@ abstract class AbstractSocket implements SocketInterface
|
||||
}
|
||||
|
||||
$command = pack('ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $header, $string);
|
||||
$length = strlen($command);
|
||||
$length = strlen($command);
|
||||
|
||||
return $length === fwrite($this->socket, $command, $length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Buffer $buffer
|
||||
* @param int $length
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
*/
|
||||
public function sherlock(Buffer $buffer, int $length): bool
|
||||
@@ -203,18 +168,11 @@ abstract class AbstractSocket implements SocketInterface
|
||||
|
||||
$buffer->set($data);
|
||||
|
||||
return $buffer->getLong() === -2;
|
||||
return -2 === $buffer->getLong();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 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,
|
||||
@@ -225,12 +183,6 @@ abstract class AbstractSocket implements SocketInterface
|
||||
): void;
|
||||
|
||||
/**
|
||||
* @param Buffer $buffer
|
||||
* @param int $length
|
||||
* @param callable $sherlockFunction
|
||||
*
|
||||
* @return Buffer
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
*/
|
||||
protected function readInternal(Buffer $buffer, int $length, callable $sherlockFunction): Buffer
|
||||
@@ -242,11 +194,11 @@ abstract class AbstractSocket implements SocketInterface
|
||||
$header = $buffer->getLong();
|
||||
|
||||
// Single packet, do nothing.
|
||||
if ($header === -1) {
|
||||
if (-1 === $header) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
if ($header === -2) { // Split packet
|
||||
if (-2 === $header) { // Split packet
|
||||
$packets = [];
|
||||
$packetCount = 0;
|
||||
$packetNumber = 0;
|
||||
@@ -269,7 +221,7 @@ abstract class AbstractSocket implements SocketInterface
|
||||
$readMore = $packetCount > count($packets);
|
||||
} while ($readMore && $sherlockFunction($buffer, $length));
|
||||
|
||||
$data = implode($packets);
|
||||
$data = implode('', $packets);
|
||||
|
||||
// TODO: Test this
|
||||
if ($isCompressed) {
|
||||
|
||||
@@ -5,8 +5,8 @@ declare(strict_types=1);
|
||||
/**
|
||||
* @author Pavel Djundik
|
||||
*
|
||||
* @link https://xpaw.me
|
||||
* @link https://github.com/xPaw/PHP-Source-Query
|
||||
* @see https://xpaw.me
|
||||
* @see https://github.com/xPaw/PHP-Source-Query
|
||||
*
|
||||
* @license GNU Lesser General Public License, version 2.1
|
||||
*
|
||||
@@ -19,21 +19,11 @@ 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,
|
||||
|
||||
@@ -5,8 +5,8 @@ declare(strict_types=1);
|
||||
/**
|
||||
* @author Pavel Djundik
|
||||
*
|
||||
* @link https://xpaw.me
|
||||
* @link https://github.com/xPaw/PHP-Source-Query
|
||||
* @see https://xpaw.me
|
||||
* @see https://github.com/xPaw/PHP-Source-Query
|
||||
*
|
||||
* @license GNU Lesser General Public License, version 2.1
|
||||
*
|
||||
@@ -18,20 +18,12 @@ namespace xPaw\SourceQuery\Socket;
|
||||
use xPaw\SourceQuery\Buffer;
|
||||
|
||||
/**
|
||||
* Base socket interface
|
||||
*
|
||||
* @package xPaw\SourceQuery\Socket
|
||||
* Base socket interface.
|
||||
*/
|
||||
interface SocketInterface
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAddress(): string;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPort(): int;
|
||||
|
||||
/**
|
||||
@@ -39,9 +31,6 @@ interface SocketInterface
|
||||
*/
|
||||
public function getSocket();
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTimeout(): int;
|
||||
|
||||
/**
|
||||
@@ -49,30 +38,14 @@ interface SocketInterface
|
||||
*/
|
||||
public function getType(): int;
|
||||
|
||||
/**
|
||||
* @param string $address
|
||||
* @param int $port
|
||||
* @param int $timeout
|
||||
*/
|
||||
public function open(string $address, int $port, int $timeout): void;
|
||||
|
||||
/**
|
||||
* Close
|
||||
* Close.
|
||||
*/
|
||||
public function close(): void;
|
||||
|
||||
/**
|
||||
* @param int $length
|
||||
*
|
||||
* @return Buffer
|
||||
*/
|
||||
public function read(int $length = 1400): Buffer;
|
||||
|
||||
/**
|
||||
* @param int $header
|
||||
* @param string $string
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function write(int $header, string $string = ''): bool;
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ declare(strict_types=1);
|
||||
/**
|
||||
* @author Pavel Djundik
|
||||
*
|
||||
* @link https://xpaw.me
|
||||
* @link https://github.com/xPaw/PHP-Source-Query
|
||||
* @see https://xpaw.me
|
||||
* @see https://github.com/xPaw/PHP-Source-Query
|
||||
*
|
||||
* @license GNU Lesser General Public License, version 2.1
|
||||
*
|
||||
|
||||
@@ -5,8 +5,8 @@ declare(strict_types=1);
|
||||
/**
|
||||
* @author Pavel Djundik
|
||||
*
|
||||
* @link https://xpaw.me
|
||||
* @link https://github.com/xPaw/PHP-Source-Query
|
||||
* @see https://xpaw.me
|
||||
* @see https://github.com/xPaw/PHP-Source-Query
|
||||
*
|
||||
* @license GNU Lesser General Public License, version 2.1
|
||||
*
|
||||
@@ -20,21 +20,12 @@ 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(
|
||||
|
||||
@@ -5,8 +5,8 @@ declare(strict_types=1);
|
||||
/**
|
||||
* @author Pavel Djundik
|
||||
*
|
||||
* @link https://xpaw.me
|
||||
* @link https://github.com/xPaw/PHP-Source-Query
|
||||
* @see https://xpaw.me
|
||||
* @see https://github.com/xPaw/PHP-Source-Query
|
||||
*
|
||||
* @license GNU Lesser General Public License, version 2.1
|
||||
*
|
||||
@@ -25,15 +25,10 @@ final class TestableSocket extends AbstractSocket
|
||||
*/
|
||||
private array $packetQueue;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private int $type;
|
||||
|
||||
/**
|
||||
* TestableSocket constructor.
|
||||
*
|
||||
* @param int $type
|
||||
*/
|
||||
public function __construct(int $type)
|
||||
{
|
||||
@@ -41,31 +36,20 @@ final class TestableSocket extends AbstractSocket
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getType(): int
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data
|
||||
*/
|
||||
public function queue(string $data): void
|
||||
{
|
||||
$this->packetQueue[] = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $address
|
||||
* @param int $port
|
||||
* @param int $timeout
|
||||
*/
|
||||
public function open(string $address, int $port, int $timeout): void
|
||||
{
|
||||
$this->timeout = $timeout;
|
||||
$this->port = $port;
|
||||
$this->port = $port;
|
||||
$this->address = $address;
|
||||
}
|
||||
|
||||
@@ -77,10 +61,6 @@ final class TestableSocket extends AbstractSocket
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $length
|
||||
*
|
||||
* @return Buffer
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
*/
|
||||
public function read(int $length = 1400): Buffer
|
||||
@@ -95,48 +75,31 @@ final class TestableSocket extends AbstractSocket
|
||||
|
||||
$buffer->set($packet);
|
||||
|
||||
$this->readInternal($buffer, $length, [ $this, 'sherlock' ]);
|
||||
$this->readInternal($buffer, $length, [$this, 'sherlock']);
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $header
|
||||
* @param string $string
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function write(int $header, string $string = ''): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Buffer $buffer
|
||||
* @param int $length
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
*/
|
||||
public function sherlock(Buffer $buffer, int $length): bool
|
||||
{
|
||||
if (count($this->packetQueue) === 0) {
|
||||
if (0 === count($this->packetQueue)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$buffer->set(array_shift($this->packetQueue));
|
||||
|
||||
return $buffer->getLong() === -2;
|
||||
return -2 === $buffer->getLong();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Buffer $buffer
|
||||
* @param int $count
|
||||
* @param int $number
|
||||
* @param bool $isCompressed
|
||||
* @param int|null $checksum
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
*/
|
||||
protected function readInternalPacketData(
|
||||
@@ -172,12 +135,6 @@ final class TestableSocket extends AbstractSocket
|
||||
|
||||
/**
|
||||
* Same as GoldSourceSocket::readInternalPacketData.
|
||||
*
|
||||
* @param Buffer $buffer
|
||||
* @param int $count
|
||||
* @param int $number
|
||||
* @param bool $isCompressed
|
||||
* @param int|null $checksum
|
||||
*/
|
||||
private function readInternalPacketDataGoldSource(
|
||||
Buffer $buffer,
|
||||
@@ -195,12 +152,6 @@ final class TestableSocket extends AbstractSocket
|
||||
/**
|
||||
* Same as SourceSocket::readInternalPacketData.
|
||||
*
|
||||
* @param Buffer $buffer
|
||||
* @param int $count
|
||||
* @param int $number
|
||||
* @param bool $isCompressed
|
||||
* @param int|null $checksum
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
*/
|
||||
private function readInternalPacketDataSource(
|
||||
|
||||
Reference in New Issue
Block a user