Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklul committed Jul 19, 2020
1 parent 3e526f0 commit 623a021
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 54 deletions.
45 changes: 27 additions & 18 deletions src/MasterServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ public function __construct($address, $port, $protocol)
$this->protocol = $protocol;
}

/**
* @param string $data
*
* @return array|null
*/
private function parseData($data)
{
$servers = [];

for ($i = 0; $i < (strlen($data) - 10); $i++) {
if ($data[$i] === "\\" && $data[$i + 7] === "\\") {
$ip = ord($data[$i + 1]) . '.' . ord($data[$i + 2]) . '.' . ord($data[$i + 3]) . '.' . ord($data[$i + 4]);
$port = (ord($data[$i + 5]) << 8) + ord($data[$i + 6]);

$servers[] = new Server($ip, $port);
}
}

return $servers;
}

/**
* @param string $keywords
* @param int $timeout
Expand All @@ -88,32 +109,20 @@ public function getServers($keywords = 'empty full', $timeout = 1)
}

if ($socket = fsockopen('udp://' . $this->address, $this->port)) {
stream_set_blocking($socket, false);
stream_set_timeout($socket, $timeout);

fwrite($socket, str_repeat(chr(255), 4) . 'getservers ' . $this->protocol . ' ' . $keywords . "\n");

$time = time() + $timeout;
$returned = '';
while ($time > time()) {
$returned .= fgets($socket);
$data = '';
while (!feof($socket)) {
$data .= fgets($socket);

if (strpos(substr($returned, -10), 'EOT') !== false) {
$meta = stream_get_meta_data($socket);
if (isset($meta['unread_bytes']) && $meta['unread_bytes'] === 0) {
break;
}
}

$servers = array();
for ($i = 0; $i < (strlen($returned) - 10); $i++) {
if ($returned[$i] === "\\" && $returned[$i + 7] === "\\") {
$ip = ord($returned[$i + 1]) . '.' . ord($returned[$i + 2]) . '.' . ord($returned[$i + 3]) . '.' . ord($returned[$i + 4]);
$port = (ord($returned[$i + 5]) << 8) + ord($returned[$i + 6]);

$servers[] = new Server($ip, $port);
}
}

return $this->servers = $servers;
return $this->servers = $this->parseData($data);
}

return false;
Expand Down
101 changes: 65 additions & 36 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct($address, $port)
*
* @return array|bool
*/
public function getInfo($timeout = 1, $length = 10000)
public function getInfo($timeout = 1, $length = 1000000)
{
if (!empty($this->info)) {
return $this->info;
Expand All @@ -78,6 +78,10 @@ public function getInfo($timeout = 1, $length = 10000)
throw new InvalidArgumentException('Timeout must be a NUMBER!');
}

if (!is_int($length)) {
throw new InvalidArgumentException('Length must be a NUMBER!');
}

if ($socket = fsockopen('udp://' . $this->address, $this->port)) {
stream_set_timeout($socket, $timeout);
fwrite($socket, str_repeat(chr(255), 4) . 'getinfo' . "\n");
Expand All @@ -88,16 +92,18 @@ public function getInfo($timeout = 1, $length = 10000)
$vars = explode("\n", $data);

if (isset($vars[1])) {
$list['address'] = $this->address;
$list['port'] = $this->port;

$ret = explode("\\", substr($vars[1], 1, strlen($vars[1])));

for ($i = 0, $iMax = count($ret); $i <= $iMax; $i += 2) {
$list[strtolower(@$ret[$i])] = @$ret[$i + 1];
if (isset($ret[$i], $ret[$i + 1])) {
$list[strtolower($ret[$i])] = $ret[$i + 1];
}
}
array_pop($list);

$list['address'] = $this->address;
$list['port'] = $this->port;

return $this->info = $list;
}
}
Expand All @@ -106,22 +112,67 @@ public function getInfo($timeout = 1, $length = 10000)
return false;
}

/**
* @param array $data
*
* @return array
*/
private function parsePlayersData($data)
{
$players = [];

for ($i = 2, $iMax = sizeof($data); $i < $iMax; $i++) {
$infos = explode(' ', $data[$i], 3);

$name = '';
if (isset($infos[2])) {
$name = explode('"', $infos[2]);

if (isset($name[1])) {
$name = $name[1];
}
}

$score = 0;
if (isset($infos[0])) {
$score = $infos[0];
}

$ping = 999;
if (isset($infos[1])) {
$ping = $infos[1];
}

$players[] = [
'score' => $score,
'ping' => $ping,
'name' => $name,
];
}

return $players;
}

/**
* @param int $timeout
* @param int $length
*
* @return array|bool
*/
public function getStatus($timeout = 1, $length = 10000)
public function getStatus($timeout = 1, $length = 1000000)
{
if (!empty($this->status)) {
return $this->status;
}

if (!is_int($timeout)) {
if (!is_numeric($timeout)) {
throw new InvalidArgumentException('Timeout must be a NUMBER!');
}

if (!is_numeric($length)) {
throw new InvalidArgumentException('Length must be a NUMBER!');
}

if ($socket = fsockopen('udp://' . $this->address, $this->port)) {
stream_set_timeout($socket, $timeout);
fwrite($socket, str_repeat(chr(255), 4) . 'getstatus' . "\n");
Expand All @@ -132,43 +183,21 @@ public function getStatus($timeout = 1, $length = 10000)
$vars = explode("\n", $data);

if (isset($vars[1])) {
$ret = explode("\\", substr($vars[1], 1, strlen($vars[1])));

for ($i = 0, $iMax = count($ret); $i <= $iMax; $i += 2) {
$list[strtolower(@$ret[$i])] = @$ret[$i + 1];
}
array_pop($list);

$list['address'] = $this->address;
$list['port'] = $this->port;

$players = array();
for ($i = 2, $iMax = sizeof($vars); $i < $iMax; $i++) {
$infos = explode(' ', $vars[$i], 3);

$name = '';
if (isset($infos[2])) {
$name = explode('"', $infos[2]);

if (isset($name[1])) {
$name = $name[1];
}
}

$score = 0;
if (isset($infos[0])) {
$score = $infos[0];
}
$ret = explode("\\", substr($vars[1], 1, strlen($vars[1])));

$ping = 999;
if (isset($infos[1])) {
$ping = $infos[1];
for ($i = 0, $iMax = count($ret); $i <= $iMax; $i += 2) {
if (isset($ret[$i], $ret[$i + 1])) {
$list[strtolower($ret[$i])] = $ret[$i + 1];
}

$players[] = ['score' => $score, 'ping' => $ping, 'name' => $name];
}
array_pop($list);

$players = $this->parsePlayersData($vars);
array_pop($players);

$list['players'] = $players;

$list['numplayers'] = 0;
Expand Down

0 comments on commit 623a021

Please sign in to comment.