From 723ed0589c5521605c434b86da0f497576899afc Mon Sep 17 00:00:00 2001 From: Anthony Birkett Date: Mon, 31 May 2021 19:17:21 +0100 Subject: [PATCH] Remove duplicated code in the TestableSocket. --- SourceQuery/Socket/GoldSourceSocket.php | 17 ++---- SourceQuery/Socket/SourceSocket.php | 27 ++-------- SourceQuery/Socket/TestableSocket.php | 53 ++++--------------- .../Traits/GoldSourcePacketDataTrait.php | 34 ++++++++++++ .../Socket/Traits/SourcePacketDataTrait.php | 44 +++++++++++++++ 5 files changed, 94 insertions(+), 81 deletions(-) create mode 100644 SourceQuery/Socket/Traits/GoldSourcePacketDataTrait.php create mode 100644 SourceQuery/Socket/Traits/SourcePacketDataTrait.php diff --git a/SourceQuery/Socket/GoldSourceSocket.php b/SourceQuery/Socket/GoldSourceSocket.php index b5475ba..5a68848 100644 --- a/SourceQuery/Socket/GoldSourceSocket.php +++ b/SourceQuery/Socket/GoldSourceSocket.php @@ -15,25 +15,14 @@ declare(strict_types=1); namespace xPaw\SourceQuery\Socket; -use xPaw\SourceQuery\Buffer; +use xPaw\SourceQuery\Socket\Traits\GoldSourcePacketDataTrait; final class GoldSourceSocket extends AbstractSocket { + use GoldSourcePacketDataTrait; + public function getType(): int { return SocketType::GOLDSOURCE; } - - 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; - } } diff --git a/SourceQuery/Socket/SourceSocket.php b/SourceQuery/Socket/SourceSocket.php index 38824ce..27b66c0 100644 --- a/SourceQuery/Socket/SourceSocket.php +++ b/SourceQuery/Socket/SourceSocket.php @@ -15,35 +15,14 @@ declare(strict_types=1); namespace xPaw\SourceQuery\Socket; -use xPaw\SourceQuery\Buffer; -use xPaw\SourceQuery\Exception\InvalidPacketException; +use xPaw\SourceQuery\Socket\Traits\SourcePacketDataTrait; final class SourceSocket extends AbstractSocket { + use SourcePacketDataTrait; + public function getType(): int { return SocketType::SOURCE; } - - /** - * @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. - } - } } diff --git a/SourceQuery/Socket/TestableSocket.php b/SourceQuery/Socket/TestableSocket.php index 27a2ef1..17ef936 100644 --- a/SourceQuery/Socket/TestableSocket.php +++ b/SourceQuery/Socket/TestableSocket.php @@ -17,9 +17,19 @@ namespace xPaw\SourceQuery\Socket; use xPaw\SourceQuery\Buffer; use xPaw\SourceQuery\Exception\InvalidPacketException; +use xPaw\SourceQuery\Socket\Traits\GoldSourcePacketDataTrait; +use xPaw\SourceQuery\Socket\Traits\SourcePacketDataTrait; final class TestableSocket extends AbstractSocket { + use GoldSourcePacketDataTrait { + GoldSourcePacketDataTrait::readInternalPacketData as readInternalPacketDataGoldSource; + } + + use SourcePacketDataTrait { + SourcePacketDataTrait::readInternalPacketData as readInternalPacketDataSource; + } + /** * @var string[] */ @@ -99,9 +109,6 @@ final class TestableSocket extends AbstractSocket return -2 === $buffer->getLong(); } - /** - * @throws InvalidPacketException - */ protected function readInternalPacketData( Buffer $buffer, int &$count, @@ -132,44 +139,4 @@ final class TestableSocket extends AbstractSocket ); } } - - /** - * Same as GoldSourceSocket::readInternalPacketData. - */ - private 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. - * - * @throws InvalidPacketException - */ - private 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. - } - } } diff --git a/SourceQuery/Socket/Traits/GoldSourcePacketDataTrait.php b/SourceQuery/Socket/Traits/GoldSourcePacketDataTrait.php new file mode 100644 index 0000000..b07df6f --- /dev/null +++ b/SourceQuery/Socket/Traits/GoldSourcePacketDataTrait.php @@ -0,0 +1,34 @@ +getByte(); + $count = $packetCountAndNumber & 0xF; + $number = $packetCountAndNumber >> 4; + $isCompressed = false; + } +} diff --git a/SourceQuery/Socket/Traits/SourcePacketDataTrait.php b/SourceQuery/Socket/Traits/SourcePacketDataTrait.php new file mode 100644 index 0000000..c835659 --- /dev/null +++ b/SourceQuery/Socket/Traits/SourcePacketDataTrait.php @@ -0,0 +1,44 @@ +getByte(); + $number = $buffer->getByte() + 1; + + if ($isCompressed) { + $buffer->getLong(); // Split size. + + $checksum = $buffer->getUnsignedLong(); + } else { + $buffer->getShort(); // Split size. + } + } +}