mirror of
https://github.com/xPaw/PHP-Source-Query.git
synced 2026-06-10 22:43:16 +02:00
Remove engine from the Sockets.
Docblock changes. Reorder methods to be logical.
This commit is contained in:
+8
-16
@@ -6,29 +6,21 @@ require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use xPaw\SourceQuery\SourceQuery;
|
||||
use xPaw\SourceQuery\Socket\SourceSocket;
|
||||
use xPaw\SourceQuery\Socket\SocketType;
|
||||
|
||||
// For the sake of this example
|
||||
header('Content-Type: text/plain');
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
|
||||
// Edit this ->
|
||||
define('SQ_SERVER_ADDR', 'localhost');
|
||||
define('SQ_SERVER_PORT', 27015);
|
||||
define('SQ_TIMEOUT', 1);
|
||||
define('SQ_ENGINE', SocketType::SOURCE);
|
||||
// Edit this <-
|
||||
|
||||
$Query = new SourceQuery(new SourceSocket());
|
||||
$query = new SourceQuery(new SourceSocket());
|
||||
|
||||
try {
|
||||
$Query->connect(SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE);
|
||||
$query->connect('localhost', 27015, 1);
|
||||
|
||||
print_r($Query->getInfo());
|
||||
print_r($Query->getPlayers());
|
||||
print_r($Query->getRules());
|
||||
} catch (Exception $e) {
|
||||
echo $e->getMessage();
|
||||
print_r($query->getInfo());
|
||||
print_r($query->getPlayers());
|
||||
print_r($query->getRules());
|
||||
} catch (Exception $exception) {
|
||||
echo $exception->getMessage();
|
||||
} finally {
|
||||
$Query->disconnect();
|
||||
$query->disconnect();
|
||||
}
|
||||
|
||||
@@ -6,29 +6,21 @@ require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use xPaw\SourceQuery\SourceQuery;
|
||||
use xPaw\SourceQuery\Socket\SourceSocket;
|
||||
use xPaw\SourceQuery\Socket\SocketType;
|
||||
|
||||
// For the sake of this example
|
||||
header('Content-Type: text/plain');
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
|
||||
// Edit this ->
|
||||
define('SQ_SERVER_ADDR', 'localhost');
|
||||
define('SQ_SERVER_PORT', 27015);
|
||||
define('SQ_TIMEOUT', 1);
|
||||
define('SQ_ENGINE', SocketType::SOURCE);
|
||||
// Edit this <-
|
||||
|
||||
$Query = new SourceQuery(new SourceSocket());
|
||||
$query = new SourceQuery(new SourceSocket());
|
||||
|
||||
try {
|
||||
$Query->connect(SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE);
|
||||
$query->connect('localhost', 27015, 1);
|
||||
|
||||
$Query->setRconPassword('my_awesome_password');
|
||||
$query->setRconPassword('my_awesome_password');
|
||||
|
||||
var_dump($Query->rcon('say hello'));
|
||||
} catch (Exception $e) {
|
||||
echo $e->getMessage();
|
||||
var_dump($query->rcon('say hello'));
|
||||
} catch (Exception $exception) {
|
||||
echo $exception->getMessage();
|
||||
} finally {
|
||||
$Query->disconnect();
|
||||
$query->disconnect();
|
||||
}
|
||||
|
||||
+38
-46
@@ -6,38 +6,30 @@ require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use xPaw\SourceQuery\SourceQuery;
|
||||
use xPaw\SourceQuery\Socket\SourceSocket;
|
||||
use xPaw\SourceQuery\Socket\SocketType;
|
||||
|
||||
// Edit this ->
|
||||
define('SQ_SERVER_ADDR', 'localhost');
|
||||
define('SQ_SERVER_PORT', 27015);
|
||||
define('SQ_TIMEOUT', 3);
|
||||
define('SQ_ENGINE', SocketType::SOURCE);
|
||||
// Edit this <-
|
||||
$timer = microtime(true);
|
||||
|
||||
$Timer = microtime(true);
|
||||
$query = new SourceQuery(new SourceSocket());
|
||||
|
||||
$Query = new SourceQuery(new SourceSocket());
|
||||
|
||||
$Info = [];
|
||||
$Rules = [];
|
||||
$Players = [];
|
||||
$Exception = null;
|
||||
$info = [];
|
||||
$rules = [];
|
||||
$players = [];
|
||||
$exception = null;
|
||||
|
||||
try {
|
||||
$Query->connect(SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE);
|
||||
$query->connect('localhost', 27015, 3);
|
||||
//$Query->SetUseOldGetChallengeMethod( true ); // Use this when players/rules retrieval fails on games like Starbound
|
||||
|
||||
$Info = $Query->getInfo();
|
||||
$Players = $Query->getPlayers();
|
||||
$Rules = $Query->getRules();
|
||||
$info = $query->getInfo();
|
||||
$players = $query->getPlayers();
|
||||
$rules = $query->getRules();
|
||||
} catch (Exception $e) {
|
||||
$Exception = $e;
|
||||
$exception = $e;
|
||||
} finally {
|
||||
$Query->disconnect();
|
||||
$query->disconnect();
|
||||
}
|
||||
|
||||
$Timer = number_format(microtime(true) - $Timer, 4, '.', '');
|
||||
$timer = number_format(microtime(true) - $timer, 4, '.', '');
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
@@ -47,7 +39,7 @@ $Timer = number_format(microtime(true) - $Timer, 4, '.', '');
|
||||
<title>Source Query PHP Library</title>
|
||||
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
|
||||
<style type="text/css">
|
||||
<style>
|
||||
.table {
|
||||
table-layout: fixed;
|
||||
border-top-color: #428BCA;
|
||||
@@ -89,9 +81,9 @@ $Timer = number_format(microtime(true) - $Timer, 4, '.', '');
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<?php if ($Exception !== null): ?>
|
||||
<?php if ($exception !== null): ?>
|
||||
<div class="panel panel-error">
|
||||
<pre class="panel-body"><?php echo htmlspecialchars($Exception->__toString()); ?></pre>
|
||||
<pre class="panel-body"><?php echo htmlspecialchars($exception->__toString()); ?></pre>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<div class="row">
|
||||
@@ -100,27 +92,27 @@ $Timer = number_format(microtime(true) - $Timer, 4, '.', '');
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="info-column">Server Info</th>
|
||||
<th><span class="label label-<?php echo $Timer > 1.0 ? 'danger' : 'success'; ?>"><?php echo $Timer; ?>s</span></th>
|
||||
<th><span class="label label-<?php echo $timer > 1.0 ? 'danger' : 'success'; ?>"><?php echo $timer; ?>s</span></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if (!empty($Info)): ?>
|
||||
<?php foreach ($Info as $InfoKey => $InfoValue): ?>
|
||||
<?php if (!empty($info)): ?>
|
||||
<?php foreach ($info as $infoKey => $infoValue): ?>
|
||||
<tr>
|
||||
<td><?php echo htmlspecialchars($InfoKey); ?></td>
|
||||
<td><?php echo htmlspecialchars($infoKey); ?></td>
|
||||
<td><?php
|
||||
if (is_array($InfoValue)) {
|
||||
if (is_array($infoValue)) {
|
||||
echo "<pre>";
|
||||
print_r($InfoValue);
|
||||
print_r($infoValue);
|
||||
echo "</pre>";
|
||||
} elseif ($InfoValue === true) {
|
||||
} elseif ($infoValue === true) {
|
||||
echo 'true';
|
||||
} elseif ($InfoValue === false) {
|
||||
} elseif ($infoValue === false) {
|
||||
echo 'false';
|
||||
} elseif (is_int($InfoValue)) {
|
||||
echo $InfoValue;
|
||||
} elseif (is_int($infoValue)) {
|
||||
echo $infoValue;
|
||||
} else {
|
||||
echo htmlspecialchars($InfoValue);
|
||||
echo htmlspecialchars($infoValue);
|
||||
}
|
||||
?></td>
|
||||
</tr>
|
||||
@@ -137,18 +129,18 @@ $Timer = number_format(microtime(true) - $Timer, 4, '.', '');
|
||||
<table class="table table-bordered table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Player <span class="label label-info"><?php echo count($Players); ?></span></th>
|
||||
<th>Player <span class="label label-info"><?php echo count($players); ?></span></th>
|
||||
<th class="frags-column">Frags</th>
|
||||
<th class="frags-column">Time</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if (!empty($Players)): ?>
|
||||
<?php foreach ($Players as $Player): ?>
|
||||
<?php if (!empty($players)): ?>
|
||||
<?php foreach ($players as $player): ?>
|
||||
<tr>
|
||||
<td><?php echo htmlspecialchars($Player[ 'Name' ]); ?></td>
|
||||
<td><?php echo $Player[ 'Frags' ]; ?></td>
|
||||
<td><?php echo $Player[ 'TimeF' ]; ?></td>
|
||||
<td><?php echo htmlspecialchars($player[ 'Name' ]); ?></td>
|
||||
<td><?php echo $player[ 'Frags' ]; ?></td>
|
||||
<td><?php echo $player[ 'TimeF' ]; ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php else: ?>
|
||||
@@ -165,15 +157,15 @@ $Timer = number_format(microtime(true) - $Timer, 4, '.', '');
|
||||
<table class="table table-bordered table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th colspan="2">Rules <span class="label label-info"><?php echo count($Rules); ?></span></th>
|
||||
<th colspan="2">Rules <span class="label label-info"><?php echo count($rules); ?></span></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if (!empty($Rules)): ?>
|
||||
<?php foreach ($Rules as $Rule => $Value): ?>
|
||||
<?php if (!empty($rules)): ?>
|
||||
<?php foreach ($rules as $ruleKey => $ruleValue): ?>
|
||||
<tr>
|
||||
<td><?php echo htmlspecialchars($Rule); ?></td>
|
||||
<td><?php echo htmlspecialchars($Value); ?></td>
|
||||
<td><?php echo htmlspecialchars($ruleKey); ?></td>
|
||||
<td><?php echo htmlspecialchars($ruleValue); ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php else: ?>
|
||||
|
||||
@@ -17,13 +17,6 @@ namespace xPaw\SourceQuery;
|
||||
|
||||
use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
|
||||
/**
|
||||
* Class Buffer
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses InvalidPacketException
|
||||
*/
|
||||
final class Buffer
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -19,5 +19,5 @@ use Exception;
|
||||
|
||||
abstract class SourceQueryException extends Exception
|
||||
{
|
||||
// Base exception class
|
||||
// Base exception class.
|
||||
}
|
||||
|
||||
@@ -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\Rcon;
|
||||
|
||||
use xPaw\SourceQuery\Buffer;
|
||||
@@ -10,14 +21,6 @@ use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
|
||||
abstract class AbstractRcon implements RconInterface
|
||||
{
|
||||
/**
|
||||
* @param int|null $header
|
||||
* @param string $string
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract protected function write(?int $header, string $string = ''): bool;
|
||||
|
||||
/**
|
||||
* @throws AuthenticationException
|
||||
* @throws InvalidPacketException
|
||||
@@ -25,4 +28,12 @@ abstract class AbstractRcon implements RconInterface
|
||||
* @return Buffer
|
||||
*/
|
||||
abstract protected function read(): Buffer;
|
||||
|
||||
/**
|
||||
* @param int|null $header
|
||||
* @param string $string
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract protected function write(?int $header, string $string = ''): bool;
|
||||
}
|
||||
|
||||
@@ -21,14 +21,6 @@ use xPaw\SourceQuery\Exception\InvalidPacketException;
|
||||
use xPaw\SourceQuery\Socket\SocketInterface;
|
||||
use xPaw\SourceQuery\SourceQuery;
|
||||
|
||||
/**
|
||||
* Class GoldSourceRcon
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses AuthenticationException
|
||||
* @uses InvalidPacketException
|
||||
*/
|
||||
final class GoldSourceRcon extends AbstractRcon
|
||||
{
|
||||
/**
|
||||
@@ -56,6 +48,13 @@ final class GoldSourceRcon extends AbstractRcon
|
||||
$this->socket = $socket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open
|
||||
*/
|
||||
public function open(): void
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Close
|
||||
*/
|
||||
@@ -66,10 +65,22 @@ final class GoldSourceRcon extends AbstractRcon
|
||||
}
|
||||
|
||||
/**
|
||||
* Open
|
||||
* @param string $password
|
||||
*
|
||||
* @throws AuthenticationException
|
||||
*/
|
||||
public function open(): void
|
||||
public function authorize(string $password): void
|
||||
{
|
||||
$this->rconPassword = $password;
|
||||
|
||||
$this->write(null, 'challenge rcon');
|
||||
$buffer = $this->socket->read();
|
||||
|
||||
if ($buffer->get(14) !== 'challenge rcon') {
|
||||
throw new AuthenticationException('Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD);
|
||||
}
|
||||
|
||||
$this->rconChallenge = trim($buffer->get());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,39 +103,6 @@ final class GoldSourceRcon extends AbstractRcon
|
||||
return $buffer->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $password
|
||||
*
|
||||
* @throws AuthenticationException
|
||||
*/
|
||||
public function authorize(string $password): void
|
||||
{
|
||||
$this->rconPassword = $password;
|
||||
|
||||
$this->write(null, 'challenge rcon');
|
||||
$buffer = $this->socket->read();
|
||||
|
||||
if ($buffer->get(14) !== 'challenge rcon') {
|
||||
throw new AuthenticationException('Failed to get RCON challenge.', AuthenticationException::BAD_PASSWORD);
|
||||
}
|
||||
|
||||
$this->rconChallenge = trim($buffer->get());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $header
|
||||
* @param string $string
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function write(?int $header, string $string = ''): bool
|
||||
{
|
||||
$command = pack('cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $string);
|
||||
$length = strlen($command);
|
||||
|
||||
return $length === fwrite($this->socket->getSocket(), $command, $length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AuthenticationException
|
||||
* @throws InvalidPacketException
|
||||
@@ -133,14 +111,14 @@ final class GoldSourceRcon extends AbstractRcon
|
||||
*/
|
||||
protected function read(): Buffer
|
||||
{
|
||||
// GoldSource RCON has same structure as Query
|
||||
// GoldSource RCON has same structure as Query.
|
||||
$buffer = $this->socket->read();
|
||||
|
||||
$stringBuffer = '';
|
||||
|
||||
// There is no indentifier of the end, so we just need to continue reading
|
||||
// There is no identifier of the end, so we just need to continue reading.
|
||||
do {
|
||||
$readMore = $buffer->remaining() > 0;
|
||||
$readMore = !$buffer->isEmpty();
|
||||
|
||||
if ($readMore) {
|
||||
if ($buffer->getByte() !== SourceQuery::S2A_RCON) {
|
||||
@@ -151,7 +129,7 @@ final class GoldSourceRcon extends AbstractRcon
|
||||
$stringBuffer .= $packet;
|
||||
//$stringBuffer .= SubStr( $packet, 0, -2 );
|
||||
|
||||
// Let's assume if this packet is not long enough, there are no more after this one
|
||||
// Let's assume if this packet is not long enough, there are no more after this one.
|
||||
$readMore = strlen($packet) > 1000; // use 1300?
|
||||
|
||||
if ($readMore) {
|
||||
@@ -172,4 +150,18 @@ final class GoldSourceRcon extends AbstractRcon
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $header
|
||||
* @param string $string
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function write(?int $header, string $string = ''): bool
|
||||
{
|
||||
$command = pack('cccca*', 0xFF, 0xFF, 0xFF, 0xFF, $string);
|
||||
$length = strlen($command);
|
||||
|
||||
return $length === fwrite($this->socket->getSocket(), $command, $length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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\Rcon;
|
||||
|
||||
use xPaw\SourceQuery\Exception\AuthenticationException;
|
||||
@@ -15,15 +26,22 @@ interface RconInterface
|
||||
*/
|
||||
public function __construct(SocketInterface $socket);
|
||||
|
||||
/**
|
||||
* Open
|
||||
*/
|
||||
public function open(): void;
|
||||
|
||||
/**
|
||||
* Close
|
||||
*/
|
||||
public function close(): void;
|
||||
|
||||
/**
|
||||
* Open
|
||||
* @param string $password
|
||||
*
|
||||
* @throws AuthenticationException
|
||||
*/
|
||||
public function open(): void;
|
||||
public function authorize(string $password): void;
|
||||
|
||||
/**
|
||||
* @param string $command
|
||||
@@ -34,11 +52,4 @@ interface RconInterface
|
||||
* @throws InvalidPacketException
|
||||
*/
|
||||
public function command(string $command): string;
|
||||
|
||||
/**
|
||||
* @param string $password
|
||||
*
|
||||
* @throws AuthenticationException
|
||||
*/
|
||||
public function authorize(string $password): void;
|
||||
}
|
||||
|
||||
@@ -22,15 +22,6 @@ use xPaw\SourceQuery\Exception\SocketException;
|
||||
use xPaw\SourceQuery\Socket\SocketInterface;
|
||||
use xPaw\SourceQuery\SourceQuery;
|
||||
|
||||
/**
|
||||
* Class SourceRcon
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses AuthenticationException
|
||||
* @uses InvalidPacketException
|
||||
* @uses SocketException
|
||||
*/
|
||||
final class SourceRcon extends AbstractRcon
|
||||
{
|
||||
/**
|
||||
@@ -56,20 +47,6 @@ final class SourceRcon extends AbstractRcon
|
||||
$this->socket = $socket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close
|
||||
*/
|
||||
public function close(): void
|
||||
{
|
||||
if ($this->rconSocket) {
|
||||
fclose($this->rconSocket);
|
||||
|
||||
$this->rconSocket = null;
|
||||
}
|
||||
|
||||
$this->rconRequestId = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SocketException
|
||||
*/
|
||||
@@ -94,6 +71,49 @@ final class SourceRcon extends AbstractRcon
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close
|
||||
*/
|
||||
public function close(): void
|
||||
{
|
||||
if ($this->rconSocket) {
|
||||
fclose($this->rconSocket);
|
||||
|
||||
$this->rconSocket = null;
|
||||
}
|
||||
|
||||
$this->rconRequestId = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $password
|
||||
*
|
||||
* @throws AuthenticationException
|
||||
* @throws InvalidPacketException
|
||||
*/
|
||||
public function authorize(string $password): void
|
||||
{
|
||||
$this->write(SourceQuery::SERVERDATA_AUTH, $password);
|
||||
$buffer = $this->read();
|
||||
|
||||
$requestId = $buffer->getLong();
|
||||
$type = $buffer->getLong();
|
||||
|
||||
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again.
|
||||
// More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments
|
||||
|
||||
if ($type === SourceQuery::SERVERDATA_RESPONSE_VALUE) {
|
||||
$buffer = $this->read();
|
||||
|
||||
$requestId = $buffer->getLong();
|
||||
$type = $buffer->getLong();
|
||||
}
|
||||
|
||||
if ($requestId === -1 || $type !== SourceQuery::SERVERDATA_AUTH_RESPONSE) {
|
||||
throw new AuthenticationException('RCON authorization failed.', AuthenticationException::BAD_PASSWORD);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $command
|
||||
*
|
||||
@@ -119,7 +139,7 @@ final class SourceRcon extends AbstractRcon
|
||||
|
||||
$data = $buffer->get();
|
||||
|
||||
// We do this stupid hack to handle split packets
|
||||
// We do this stupid hack to handle split packets.
|
||||
// See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Multiple-packet_Responses
|
||||
if (strlen($data) >= 4000) {
|
||||
$this->write(SourceQuery::SERVERDATA_REQUESTVALUE);
|
||||
@@ -127,7 +147,7 @@ final class SourceRcon extends AbstractRcon
|
||||
do {
|
||||
$buffer = $this->read();
|
||||
|
||||
$buffer->getLong(); // RequestID
|
||||
$buffer->getLong(); // RequestID.
|
||||
|
||||
if ($buffer->getLong() !== SourceQuery::SERVERDATA_RESPONSE_VALUE) {
|
||||
break;
|
||||
@@ -146,53 +166,6 @@ final class SourceRcon extends AbstractRcon
|
||||
return rtrim($data, "\0");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $password
|
||||
*
|
||||
* @throws AuthenticationException
|
||||
* @throws InvalidPacketException
|
||||
*/
|
||||
public function authorize(string $password): void
|
||||
{
|
||||
$this->write(SourceQuery::SERVERDATA_AUTH, $password);
|
||||
$buffer = $this->read();
|
||||
|
||||
$requestId = $buffer->getLong();
|
||||
$type = $buffer->getLong();
|
||||
|
||||
// If we receive SERVERDATA_RESPONSE_VALUE, then we need to read again
|
||||
// More info: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Additional_Comments
|
||||
|
||||
if ($type === SourceQuery::SERVERDATA_RESPONSE_VALUE) {
|
||||
$buffer = $this->read();
|
||||
|
||||
$requestId = $buffer->getLong();
|
||||
$type = $buffer->getLong();
|
||||
}
|
||||
|
||||
if ($requestId === -1 || $type !== SourceQuery::SERVERDATA_AUTH_RESPONSE) {
|
||||
throw new AuthenticationException('RCON authorization failed.', AuthenticationException::BAD_PASSWORD);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $header
|
||||
* @param string $string
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function write(?int $header, string $string = ''): bool
|
||||
{
|
||||
// Pack the packet together
|
||||
$command = pack('VV', ++$this->rconRequestId, $header) . $string . "\x00\x00";
|
||||
|
||||
// Prepend packet length
|
||||
$command = pack('V', strlen($command)) . $command;
|
||||
$length = strlen($command);
|
||||
|
||||
return $length === fwrite($this->rconSocket, $command, $length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Buffer
|
||||
*
|
||||
@@ -232,4 +205,22 @@ final class SourceRcon extends AbstractRcon
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $header
|
||||
* @param string $string
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function write(?int $header, string $string = ''): bool
|
||||
{
|
||||
// Pack the packet together.
|
||||
$command = pack('VV', ++$this->rconRequestId, $header) . $string . "\x00\x00";
|
||||
|
||||
// Prepend packet length.
|
||||
$command = pack('V', strlen($command)) . $command;
|
||||
$length = strlen($command);
|
||||
|
||||
return $length === fwrite($this->rconSocket, $command, $length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+17
-31
@@ -26,16 +26,6 @@ use xPaw\SourceQuery\Rcon\SourceRcon;
|
||||
use xPaw\SourceQuery\Socket\SocketInterface;
|
||||
use xPaw\SourceQuery\Socket\SocketType;
|
||||
|
||||
/**
|
||||
* Class SourceQuery
|
||||
*
|
||||
* @package xPaw\SourceQuery
|
||||
*
|
||||
* @uses AuthenticationException
|
||||
* @uses InvalidArgumentException
|
||||
* @uses InvalidPacketException
|
||||
* @uses SocketException
|
||||
*/
|
||||
final class SourceQuery
|
||||
{
|
||||
/**
|
||||
@@ -53,7 +43,7 @@ final class SourceQuery
|
||||
private const A2A_ACK = 0x6A;
|
||||
private const S2C_CHALLENGE = 0x41;
|
||||
private const S2A_INFO_SRC = 0x49;
|
||||
private const S2A_INFO_OLD = 0x6D; // Old GoldSource, HLTV uses it (actually called S2A_INFO_DETAILED)
|
||||
private const S2A_INFO_OLD = 0x6D; // Old GoldSource, HLTV uses it (actually called S2A_INFO_DETAILED).
|
||||
private const S2A_PLAYER = 0x44;
|
||||
private const S2A_RULES = 0x45;
|
||||
public const S2A_RCON = 0x6C;
|
||||
@@ -76,7 +66,7 @@ final class SourceQuery
|
||||
*
|
||||
* @var RconInterface|null
|
||||
*/
|
||||
private $rcon;
|
||||
private ?RconInterface $rcon;
|
||||
|
||||
/**
|
||||
* Points to socket class
|
||||
@@ -104,6 +94,7 @@ final class SourceQuery
|
||||
public function __construct(SocketInterface $socket)
|
||||
{
|
||||
$this->socket = $socket;
|
||||
$this->rcon = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,12 +111,10 @@ final class SourceQuery
|
||||
* @param string $address Server ip
|
||||
* @param int $port Server port
|
||||
* @param int $timeout Timeout period
|
||||
* @param int $engine Engine the server runs on (goldsource, source)
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @throws SocketException
|
||||
*/
|
||||
public function connect(string $address, int $port, int $timeout = 3, int $engine = SocketType::SOURCE): void
|
||||
public function connect(string $address, int $port, int $timeout = 3): void
|
||||
{
|
||||
$this->disconnect();
|
||||
|
||||
@@ -133,7 +122,7 @@ final class SourceQuery
|
||||
throw new InvalidArgumentException('Timeout must be a positive integer.', InvalidArgumentException::TIMEOUT_NOT_INTEGER);
|
||||
}
|
||||
|
||||
$this->socket->open($address, $port, $timeout, $engine);
|
||||
$this->socket->open($address, $port, $timeout);
|
||||
|
||||
$this->connected = true;
|
||||
}
|
||||
@@ -175,7 +164,6 @@ final class SourceQuery
|
||||
* Sends ping packet to the server
|
||||
* NOTE: This may not work on some games (TF2 for example)
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
* @throws SocketException
|
||||
*
|
||||
* @return bool True on success, false on failure
|
||||
@@ -224,7 +212,7 @@ final class SourceQuery
|
||||
$type = $buffer->getByte();
|
||||
}
|
||||
|
||||
// Old GoldSource protocol, HLTV still uses it
|
||||
// Old GoldSource protocol, HLTV still uses it.
|
||||
if ($type === self::S2A_INFO_OLD && $this->socket->getType() === SocketType::GOLDSOURCE) {
|
||||
/**
|
||||
* If we try to read data again, and we get the result with type S2A_INFO (0x49)
|
||||
@@ -290,8 +278,8 @@ final class SourceQuery
|
||||
|
||||
$server[ 'Version' ] = $buffer->getString();
|
||||
|
||||
// Extra Data Flags
|
||||
if ($buffer->remaining() > 0) {
|
||||
// Extra Data Flags.
|
||||
if (!$buffer->isEmpty()) {
|
||||
$server[ 'ExtraDataFlags' ] = $Flags = $buffer->getByte();
|
||||
|
||||
// S2A_EXTRA_DATA_HAS_GAME_PORT - Next 2 bytes include the game port.
|
||||
@@ -299,12 +287,12 @@ final class SourceQuery
|
||||
$server[ 'GamePort' ] = $buffer->getShort();
|
||||
}
|
||||
|
||||
// S2A_EXTRA_DATA_HAS_STEAMID - Next 8 bytes are the steamID
|
||||
// S2A_EXTRA_DATA_HAS_STEAMID - Next 8 bytes are the steamID.
|
||||
// Want to play around with this?
|
||||
// You can use https://github.com/xPaw/SteamID.php
|
||||
if ($Flags & 0x10) {
|
||||
$steamIdLower = $buffer->getUnsignedLong();
|
||||
$steamIdInstance = $buffer->getUnsignedLong(); // This gets shifted by 32 bits, which should be steamid instance
|
||||
$steamIdInstance = $buffer->getUnsignedLong(); // This gets shifted by 32 bits, which should be steamid instance.
|
||||
|
||||
if (PHP_INT_SIZE === 4) {
|
||||
if (extension_loaded('gmp')) {
|
||||
@@ -329,12 +317,12 @@ final class SourceQuery
|
||||
$server[ 'SpecName' ] = $buffer->getString();
|
||||
}
|
||||
|
||||
// S2A_EXTRA_DATA_HAS_GAMETAG_DATA - Next bytes are the game tag string
|
||||
// S2A_EXTRA_DATA_HAS_GAMETAG_DATA - Next bytes are the game tag string.
|
||||
if ($Flags & 0x20) {
|
||||
$server[ 'GameTags' ] = $buffer->getString();
|
||||
}
|
||||
|
||||
// S2A_EXTRA_DATA_GAMEID - Next 8 bytes are the gameID of the server
|
||||
// S2A_EXTRA_DATA_GAMEID - Next 8 bytes are the gameID of the server.
|
||||
if ($Flags & 0x01) {
|
||||
$server[ 'GameID' ] = $buffer->getUnsignedLong() | ($buffer->getUnsignedLong() << 32);
|
||||
}
|
||||
@@ -367,7 +355,7 @@ final class SourceQuery
|
||||
$this->getChallenge(self::A2S_PLAYER, self::S2A_PLAYER);
|
||||
|
||||
$this->socket->write(self::A2S_PLAYER, $this->challenge);
|
||||
$buffer = $this->socket->read(14000); // Moronic Arma 3 developers do not split their packets, so we have to read more data
|
||||
$buffer = $this->socket->read(14000); // Moronic Arma 3 developers do not split their packets, so we have to read more data.
|
||||
// This violates the protocol spec, and they probably should fix it: https://developer.valvesoftware.com/wiki/Server_queries#Protocol
|
||||
|
||||
$type = $buffer->getByte();
|
||||
@@ -379,7 +367,7 @@ final class SourceQuery
|
||||
$players = [];
|
||||
$count = $buffer->getByte();
|
||||
|
||||
while ($count-- > 0 && $buffer->remaining() > 0) {
|
||||
while ($count-- > 0 && !$buffer->isEmpty()) {
|
||||
$player = [];
|
||||
$player[ 'Id' ] = $buffer->getByte(); // PlayerID, is it just always 0?
|
||||
$player[ 'Name' ] = $buffer->getString();
|
||||
@@ -421,7 +409,7 @@ final class SourceQuery
|
||||
$rules = [];
|
||||
$count = $buffer->getShort();
|
||||
|
||||
while ($count-- > 0 && $buffer->remaining() > 0) {
|
||||
while ($count-- > 0 && !$buffer->isEmpty()) {
|
||||
$rule = $buffer->getString();
|
||||
$value = $buffer->getString();
|
||||
|
||||
@@ -440,7 +428,6 @@ final class SourceQuery
|
||||
* @param int $expectedResult
|
||||
*
|
||||
* @throws InvalidPacketException
|
||||
* @throws SocketException
|
||||
*/
|
||||
private function getChallenge(int $header, int $expectedResult): void
|
||||
{
|
||||
@@ -466,8 +453,7 @@ final class SourceQuery
|
||||
}
|
||||
case $expectedResult:
|
||||
{
|
||||
// Goldsource (HLTV)
|
||||
|
||||
// Goldsource (HLTV).
|
||||
return;
|
||||
}
|
||||
case 0:
|
||||
@@ -525,7 +511,7 @@ final class SourceQuery
|
||||
* @param string $command Command to execute
|
||||
*
|
||||
* @return string Answer from server in string
|
||||
*@throws InvalidPacketException
|
||||
* @throws InvalidPacketException
|
||||
* @throws SocketException
|
||||
*
|
||||
* @throws AuthenticationException
|
||||
|
||||
+47
-72
@@ -24,7 +24,6 @@ final class Tests extends TestCase
|
||||
private SourceQuery $sourceQuery;
|
||||
|
||||
/**
|
||||
* @throws SocketException
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function setUp(): void
|
||||
@@ -46,7 +45,6 @@ final class Tests extends TestCase
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException
|
||||
* @throws SocketException
|
||||
*/
|
||||
public function testInvalidTimeout(): void
|
||||
{
|
||||
@@ -67,7 +65,6 @@ final class Tests extends TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidPacketException
|
||||
* @throws SocketException
|
||||
*/
|
||||
public function testNotConnectedPing(): void
|
||||
@@ -166,26 +163,7 @@ final class Tests extends TestCase
|
||||
*/
|
||||
public function infoProvider(): array
|
||||
{
|
||||
$dataProvider = [];
|
||||
|
||||
$files = glob(__DIR__ . '/Info/*.raw', GLOB_ERR);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$dataProvider[] =
|
||||
[
|
||||
hex2bin(trim(file_get_contents($file))),
|
||||
json_decode(
|
||||
file_get_contents(
|
||||
str_replace('.raw', '.json', $file)
|
||||
),
|
||||
true,
|
||||
512,
|
||||
JSON_THROW_ON_ERROR
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
return $dataProvider;
|
||||
return $this->getData('Info', true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -262,12 +240,12 @@ final class Tests extends TestCase
|
||||
return
|
||||
[
|
||||
[ "" ],
|
||||
[ "\xff\xff\xff\xff" ], // No type
|
||||
[ "\xff\xff\xff\xff\x49" ], // Correct type, but no data after
|
||||
[ "\xff\xff\xff\xff\x6D" ], // Old info packet, but tests are done for source
|
||||
[ "\xff\xff\xff\xff\x11" ], // Wrong type
|
||||
[ "\x11\x11\x11\x11" ], // Wrong header
|
||||
[ "\xff" ], // Should be 4 bytes, but it's 1
|
||||
[ "\xff\xff\xff\xff" ], // No type.
|
||||
[ "\xff\xff\xff\xff\x49" ], // Correct type, but no data after.
|
||||
[ "\xff\xff\xff\xff\x6D" ], // Old info packet, but tests are done for source.
|
||||
[ "\xff\xff\xff\xff\x11" ], // Wrong type.
|
||||
[ "\x11\x11\x11\x11" ], // Wrong header.
|
||||
[ "\xff" ], // Should be 4 bytes, but it's 1.
|
||||
];
|
||||
}
|
||||
|
||||
@@ -296,7 +274,7 @@ final class Tests extends TestCase
|
||||
*/
|
||||
public function testGetRules(array $rawInput, array $expectedOutput): void
|
||||
{
|
||||
$this->socket->queue(hex2bin("ffffffff4104fce20e")); // Challenge
|
||||
$this->socket->queue(hex2bin("ffffffff4104fce20e")); // Challenge.
|
||||
|
||||
foreach ($rawInput as $packet) {
|
||||
$this->socket->queue(hex2bin($packet));
|
||||
@@ -314,26 +292,7 @@ final class Tests extends TestCase
|
||||
*/
|
||||
public function rulesProvider(): array
|
||||
{
|
||||
$dataProvider = [];
|
||||
|
||||
$files = glob(__DIR__ . '/Rules/*.raw', GLOB_ERR);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$dataProvider[] =
|
||||
[
|
||||
file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES),
|
||||
json_decode(
|
||||
file_get_contents(
|
||||
str_replace('.raw', '.json', $file)
|
||||
),
|
||||
true,
|
||||
512,
|
||||
JSON_THROW_ON_ERROR
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
return $dataProvider;
|
||||
return $this->getData('Rules');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -347,7 +306,7 @@ final class Tests extends TestCase
|
||||
*/
|
||||
public function testGetPlayers(array $rawInput, array $expectedOutput): void
|
||||
{
|
||||
$this->socket->queue(hex2bin("ffffffff4104fce20e")); // Challenge
|
||||
$this->socket->queue(hex2bin("ffffffff4104fce20e")); // Challenge.
|
||||
|
||||
foreach ($rawInput as $packet) {
|
||||
$this->socket->queue(hex2bin($packet));
|
||||
@@ -365,30 +324,10 @@ final class Tests extends TestCase
|
||||
*/
|
||||
public function playersProvider(): array
|
||||
{
|
||||
$dataProvider = [];
|
||||
|
||||
$files = glob(__DIR__ . '/Players/*.raw', GLOB_ERR);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$dataProvider[] =
|
||||
[
|
||||
file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES),
|
||||
json_decode(
|
||||
file_get_contents(
|
||||
str_replace('.raw', '.json', $file)
|
||||
),
|
||||
true,
|
||||
512,
|
||||
JSON_THROW_ON_ERROR
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
return $dataProvider;
|
||||
return $this->getData('Players');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidPacketException
|
||||
* @throws SocketException
|
||||
*/
|
||||
public function testPing(): void
|
||||
@@ -399,4 +338,40 @@ final class Tests extends TestCase
|
||||
$this->socket->queue("\xFF\xFF\xFF\xFF\xEE");
|
||||
self::assertFalse($this->sourceQuery->ping());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param bool $hexToBin
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws JsonException
|
||||
*/
|
||||
private function getData(string $path, bool $hexToBin = false): array
|
||||
{
|
||||
$dataProvider = [];
|
||||
|
||||
$files = glob(__DIR__ . '/' . $path . '/*.raw', GLOB_ERR);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$content = $hexToBin
|
||||
? hex2bin(trim(file_get_contents($file)))
|
||||
: file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
|
||||
|
||||
$dataProvider[] =
|
||||
[
|
||||
$content,
|
||||
json_decode(
|
||||
file_get_contents(
|
||||
str_replace('.raw', '.json', $file)
|
||||
),
|
||||
true,
|
||||
512,
|
||||
JSON_THROW_ON_ERROR
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
return $dataProvider;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user