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
+30 -45
View File
@@ -20,26 +20,8 @@ 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
*/
@@ -50,6 +32,11 @@ abstract class AbstractSocket implements SocketInterface
*/
public int $port = 0;
/**
* @var resource|null
*/
public $socket;
/**
* @var int $timeout
*/
@@ -79,14 +66,6 @@ abstract class AbstractSocket implements SocketInterface
return $this->port;
}
/**
* @return int
*/
public function getTimeout(): int
{
return $this->timeout;
}
/**
* @return resource
*
@@ -102,29 +81,23 @@ abstract class AbstractSocket implements SocketInterface
}
/**
* Close
* @return int
*/
public function close(): void
public function getTimeout(): int
{
if ($this->socket) {
fclose($this->socket);
$this->socket = null;
}
return $this->timeout;
}
/**
* @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
public function open(string $address, int $port, int $timeout): void
{
$this->timeout = $timeout;
$this->engine = $engine;
$this->port = $port;
$this->address = $address;
@@ -140,17 +113,15 @@ abstract class AbstractSocket implements SocketInterface
}
/**
* @param int $header
* @param string $string
*
* @return bool
* Close
*/
public function write(int $header, string $string = ''): bool
public function close(): void
{
$command = pack('ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $header, $string);
$length = strlen($command);
if ($this->socket) {
fclose($this->socket);
return $length === fwrite($this->socket, $command, $length);
$this->socket = null;
}
}
/**
@@ -180,6 +151,20 @@ abstract class AbstractSocket implements SocketInterface
return $buffer;
}
/**
* @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);
}
/**
* @param Buffer $buffer
* @param int $length
@@ -230,7 +215,7 @@ abstract class AbstractSocket implements SocketInterface
*/
protected function readInternal(Buffer $buffer, int $length, callable $sherlockFunction): Buffer
{
if ($buffer->remaining() === 0) {
if ($buffer->isEmpty()) {
throw new InvalidPacketException('Failed to read any data from socket', InvalidPacketException::BUFFER_EMPTY);
}
+11
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\Socket;
use xPaw\SourceQuery\Buffer;
+16 -17
View File
@@ -34,33 +34,39 @@ interface SocketInterface
*/
public function getPort(): int;
/**
* @return int
*/
public function getTimeout(): int;
/**
* @return resource
*/
public function getSocket();
/**
* @return int
*/
public function getTimeout(): int;
/**
* Get the socket type (goldsrc/src).
*/
public function getType(): int;
/**
* @param string $address
* @param int $port
* @param int $timeout
*/
public function open(string $address, int $port, int $timeout): void;
/**
* Close
*/
public function close(): void;
/**
* @param string $address
* @param int $port
* @param int $timeout
* @param int $engine
* @param int $length
*
* @return Buffer
*/
public function open(string $address, int $port, int $timeout, int $engine): void;
public function read(int $length = 1400): Buffer;
/**
* @param int $header
@@ -69,11 +75,4 @@ interface SocketInterface
* @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
@@ -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\Socket;
abstract class SocketType
+13 -2
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\Socket;
use xPaw\SourceQuery\Buffer;
@@ -37,11 +48,11 @@ final class SourceSocket extends AbstractSocket
$number = $buffer->getByte() + 1;
if ($isCompressed) {
$buffer->getLong(); // Split size
$buffer->getLong(); // Split size.
$checksum = $buffer->getUnsignedLong();
} else {
$buffer->getShort(); // Split size
$buffer->getShort(); // Split size.
}
}
}
+29 -20
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\Socket;
use SplQueue;
@@ -49,36 +60,23 @@ final class TestableSocket extends AbstractSocket
$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
public function open(string $address, int $port, int $timeout): void
{
$this->timeout = $timeout;
$this->engine = $engine;
$this->port = $port;
$this->address = $address;
}
/**
* @param int $header
* @param string $string
*
* @return bool
* Close.
*/
public function write(int $header, string $string = ''): bool
public function close(): void
{
return true;
}
/**
@@ -98,6 +96,17 @@ final class TestableSocket extends AbstractSocket
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
@@ -166,7 +175,7 @@ final class TestableSocket extends AbstractSocket
* @param bool $isCompressed
* @param int|null $checksum
*/
protected function readInternalPacketDataGoldSource(
private function readInternalPacketDataGoldSource(
Buffer $buffer,
int &$count,
int &$number,
@@ -190,7 +199,7 @@ final class TestableSocket extends AbstractSocket
*
* @throws InvalidPacketException
*/
protected function readInternalPacketDataSource(
private function readInternalPacketDataSource(
Buffer $buffer,
int &$count,
int &$number,
@@ -201,11 +210,11 @@ final class TestableSocket extends AbstractSocket
$number = $buffer->getByte() + 1;
if ($isCompressed) {
$buffer->getLong(); // Split size
$buffer->getLong(); // Split size.
$checksum = $buffer->getUnsignedLong();
} else {
$buffer->getShort(); // Split size
$buffer->getShort(); // Split size.
}
}
}