Skip to content

Commit

Permalink
add configurable timeout for querying servers + fixes&bumps
Browse files Browse the repository at this point in the history
  • Loading branch information
robske110 committed Aug 24, 2017
1 parent 2ba5062 commit 47bf812
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 48 deletions.
2 changes: 1 addition & 1 deletion SSS/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: SignServerStats
api: [3.0.0-ALPHA7]
version: 1.0.0-InDev-beta2
version: 1.0.0-InDev-beta3
load: POSTWORLD
main: robske_110\SSS\SignServerStats
author: robske_110
Expand Down
23 changes: 15 additions & 8 deletions SSS/src/robske_110/SSS/SSSAsyncTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,30 @@
use pocketmine\scheduler\AsyncTask;

class SSSAsyncTask extends AsyncTask{
/** @var array */
private $doCheckServer;

/* @var bool */
private $debug;
/** @var $startTick */
private $startTick;
/** @var float */
private $timeout;

public function __construct(array $doCheckServers, bool $debug, int $startTick){
public function __construct(array $doCheckServers, bool $debug, float $timeout, int $startTick){
$this->doCheckServer = $doCheckServers;
$this->debug = $debug;
$this->timeout = $timeout;
$this->startTick = $startTick;
}

private function doQuery(string $ip, int $port): array{
private function doQuery(string $ip, int $port, array $timeout): array{
if($this->debug){
echo("doQuery:\n");
}
$sock = @fsockopen("udp://".$ip,$port);
if(!$sock){return [-1, NULL];}
socket_set_timeout($sock, 0, 500000);
socket_set_timeout($sock, $timeout[0], $timeout[1]);
if(!@fwrite($sock, "\xFE\xFD\x09\x10\x20\x30\x40\xFF\xFF\xFF\x01")){return [0, NULL];}
$challenge = fread($sock, 1400);
if(!$challenge){return [0, NULL];}
Expand All @@ -40,18 +46,18 @@ private function doQuery(string $ip, int $port): array{
($challenge >> 16),
($challenge >> 8),
($challenge >> 0)
);
);
if(!@fwrite($sock, $query)){return [0, NULL];}
$response = array();
for($x = 0; $x < 2; $x++){
$response[] = @fread($sock,2048);
$response[] = @fread($sock, 2048);
}
if($this->debug){
var_dump($response);
}
$response = implode($response);
$response = substr($response,16);
$response = explode("\0",$response);
$response = substr($response, 16);
$response = explode("\0", $response);
if($this->debug){
var_dump($response);
}
Expand All @@ -77,13 +83,14 @@ public function onRun(){
echo("DoCheckServer:\n");
var_dump($this->doCheckServer);
}
$timeout = explode(".", (string) $this->timeout);
foreach($this->doCheckServer as $server){
$doCheck = $server[1];
if($doCheck){
$adressArray = $server[0];
$ip = $adressArray[0];
$port = $adressArray[1];
$return = $this->doQuery($ip, $port);
$return = $this->doQuery($ip, $port, $timeout);
$returnState = $return[0];
$queryResult = $return[1];
$serverData = [];
Expand Down
1 change: 1 addition & 0 deletions SSS/src/robske_110/SSS/SSSAsyncTaskCaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use pocketmine\scheduler\PluginTask;

class SSSAsyncTaskCaller extends PluginTask{
/** @var SignServerStats */
private $SSS;

public function __construct(SignServerStats $main){
Expand Down
86 changes: 51 additions & 35 deletions SSS/src/robske_110/SSS/SignServerStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ class SignServerStats extends PluginBase{
/** @var Server */
private $server;

/** @var float */
private $timeout;
/** @var array */
private $doCheckServers = [];
/** @var bool */
private $debug = false;
/** @var bool */
private $asyncTaskIsRunning = false;
/** @var array */
private $doRefreshSigns = [];
/** @var array */
private $asyncTaskMODTs = [];
/** @var array */
private $asyncTaskPlayers = [];
/** @var array */
private $asyncTaskIsOnline = [];

const API_VERSION = "1.0.0";
Expand All @@ -50,11 +59,12 @@ public function onEnable(){
$this->server = $this->getServer();
$this->db = new Config($this->getDataFolder() . "SignServerStatsDB.yml", Config::YAML, []); //TODO:betterDB
$this->signServerStatsCfg = new Config($this->getDataFolder() . "SSSconfig.yml", Config::YAML, []);
if($this->signServerStatsCfg->get("ConfigVersion") != 2){
$this->signServerStatsCfg->set('SSSAsyncTaskCall', 200);
if($this->signServerStatsCfg->get("ConfigVersion") != 3){
$this->signServerStatsCfg->set('async-task-call-ticks', 200);
$this->signServerStatsCfg->set('always-start-async-task', false);
$this->signServerStatsCfg->set('server-query-timeout-sec', 2.5);
$this->signServerStatsCfg->set('debug', false);
$this->signServerStatsCfg->set('ConfigVersion', 2);
$this->signServerStatsCfg->set('ConfigVersion', 3);
}
$this->signServerStatsCfg->save();
if($this->signServerStatsCfg->get('debug')){
Expand All @@ -64,7 +74,22 @@ public function onEnable(){
$this->server->getPluginManager()->registerEvents($this->listener, $this);
$this->doRefreshSigns = $this->db->getAll();
$this->recalcdRSvar();
$this->server->getScheduler()->scheduleRepeatingTask(new SSSAsyncTaskCaller($this), $this->signServerStatsCfg->get("SSSAsyncTaskCall"));
$this->timeout = $this->signServerStatsCfg->get('server-query-timeout-sec');
$this->server->getScheduler()->scheduleRepeatingTask(
new SSSAsyncTaskCaller($this), $this->signServerStatsCfg->get("async-task-call-ticks")
);
}

public function isCompatible(string $apiVersion): bool{
$extensionApiVersion = explode(".", $apiVersion);
$myApiVersion = explode(".", self::API_VERSION);
if($extensionApiVersion[0] !== $myApiVersion[0]){
return false;
}
if($extensionApiVersion[1] > $myApiVersion[1]){
return false;
}
return true;
}

public function getServerOnline(): array{
Expand All @@ -87,24 +112,6 @@ public function isAdmin(Player $player): bool{
return $player->hasPermission("SSS.signs");
}

public function doSignRefresh(){
foreach($this->doRefreshSigns as $signData){
$pos = $signData[0];
$adress = $signData[1];
if($this->server->loadLevel($pos[3])){
$signTile = $this->server->getLevelByName($pos[3])->getTile(new Vector3($pos[0], $pos[1], $pos[2]));
if($signTile instanceof Sign){
$lines = $this->calcSign($adress);
$signTile->setText($lines[0],$lines[1],$lines[2],$lines[3]);
}else{
$this->server->broadcast("r001_SIGN_NOT_FOUND_AT(".$pos[0]."/".$pos[1]."/".$pos[2]." in ".$pos[3].")", Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
}
}else{
$this->server->broadcast("r002_COULD_NOT_FIND_LEVEL_FOR_SIGN_AT(".$pos[0]."/".$pos[1]."/".$pos[2]." in ".$pos[3].")", Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
}
}
}

public function doesSignExist(Vector3 $pos, string $levelName, int &$index = 0): bool{
$deParsedPos = [$pos->x, $pos->y, $pos->z, $levelName];
foreach($this->doRefreshSigns as $key => $signData){
Expand Down Expand Up @@ -166,7 +173,7 @@ public function removeServer(string $ip, int $port): bool{
*/
public function startAsyncTask($currTick){
$this->asyncTaskIsRunning = true;
$this->server->getScheduler()->scheduleAsyncTask(new SSSAsyncTask($this->doCheckServers, $this->debug, $currTick));
$this->server->getScheduler()->scheduleAsyncTask(new SSSAsyncTask($this->doCheckServers, $this->debug, $this->timeout, $currTick));
}

/**
Expand Down Expand Up @@ -201,20 +208,29 @@ public function asyncTaskCallBack($data, $scheduleTime){
/**
* @internal
*/
public function isAllowedToStartAsyncTask(): bool{
return $this->signServerStatsCfg->get('always-start-async-task') ? true : !$this->asyncTaskIsRunning;
public function doSignRefresh(){
foreach($this->doRefreshSigns as $signData){
$pos = $signData[0];
$adress = $signData[1];
if($this->server->loadLevel($pos[3])){
$signTile = $this->server->getLevelByName($pos[3])->getTile(new Vector3($pos[0], $pos[1], $pos[2]));
if($signTile instanceof Sign){
$lines = $this->calcSign($adress);
$signTile->setText($lines[0],$lines[1],$lines[2],$lines[3]);
}else{
$this->server->broadcast("r001_SIGN_NOT_FOUND_AT(".$pos[0]."/".$pos[1]."/".$pos[2]." in ".$pos[3].")", Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
}
}else{
$this->server->broadcast("r002_COULD_NOT_FIND_LEVEL_FOR_SIGN_AT(".$pos[0]."/".$pos[1]."/".$pos[2]." in ".$pos[3].")", Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
}
}
}

public function isCompatible(string $apiVersion): bool{
$extensionApiVersion = explode(".", $apiVersion);
$myApiVersion = explode(".", self::API_VERSION);
if($extensionApiVersion[0] !== $myApiVersion[0]){
return false;
}
if($extensionApiVersion[1] > $myApiVersion[1]){
return false;
}
return true;
/**
* @internal
*/
public function isAllowedToStartAsyncTask(): bool{
return $this->signServerStatsCfg->get('always-start-async-task') ? true : !$this->asyncTaskIsRunning;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions examples/DumpInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* @name DumpServerInfo
* @main robske_110\DPS\DumpServerInfo
* @version 1.0.0
* @version 1.0.0-beta1
* @api 3.0.0-ALPHA7
* @description Dumps query info of a Server using SignServerStats
* @author robske_110
Expand Down Expand Up @@ -105,7 +105,7 @@ public function addServer(string $hostname, int $port, CommandSender $sender){
}

public function onRun(int $currentTick){
if(($sss = $this->getSSS()) === null){
if(($sss = $this->plugin->getSSS()) === null){
return;
}
foreach($this->checkServers as $index => $server){
Expand All @@ -114,7 +114,7 @@ public function onRun(int $currentTick){
$server[2]->sendMessage($msg);
}
unset($this->checkServers[$index]);
$sss->removeServer($server[0], $server[1]); //Warning: In future versions of SSS it could also immediately remove data, therefore breaking multiple requests at once.
$sss->removeServer($server[0], $server[1]); //Warning: In future versions of SSS this could also immediately remove data, therefore breaking multiple requests at once.
}
}
$this->checkServers = array_values($this->checkServers);
Expand Down
2 changes: 1 addition & 1 deletion examples/WarnOffline/src/robske_110/WO/WarnOffline.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getSL(){ //:?StatusList
return $sss;
}else{
$this->getLogger()->critical("Unexpected error: Trying to get SignServerStats plugin instance failed!");
return NULL;
return null;
}
}

Expand Down

0 comments on commit 47bf812

Please sign in to comment.