Remove duplicated code in the TestableSocket.

pull/150/head
Anthony Birkett 4 years ago
parent 5d54dc8929
commit 723ed0589c

@ -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;
}
}

@ -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.
}
}
}

@ -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.
}
}
}

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*
* @see https://xpaw.me
* @see https://github.com/xPaw/PHP-Source-Query
*
* @license GNU Lesser General Public License, version 2.1
*
* @internal
*/
namespace xPaw\SourceQuery\Socket\Traits;
use xPaw\SourceQuery\Buffer;
trait GoldSourcePacketDataTrait
{
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;
}
}

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*
* @see https://xpaw.me
* @see https://github.com/xPaw/PHP-Source-Query
*
* @license GNU Lesser General Public License, version 2.1
*
* @internal
*/
namespace xPaw\SourceQuery\Socket\Traits;
use xPaw\SourceQuery\Buffer;
use xPaw\SourceQuery\Exception\InvalidPacketException;
trait SourcePacketDataTrait
{
/**
* @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.
}
}
}
Loading…
Cancel
Save