From 033a1673f05d702ae472686feef7759999801bed Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Tue, 4 Jun 2013 10:55:20 +0200 Subject: [PATCH] Added bandwidth measure --- src/API/PlayerAPI.php | 14 ++++++++------ src/Player.php | 21 ++++++++++++++++++--- src/PocketMinecraftServer.php | 6 +++--- src/network/MinecraftInterface.php | 2 +- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 62b8f6027..d883613bd 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -40,19 +40,21 @@ class PlayerAPI{ $this->server->api->console->register("tp", "[target player] OR /tp [target player] ", array($this, "commandHandler")); $this->server->api->console->register("spawnpoint", "[player] [x] [y] [z]", array($this, "commandHandler")); $this->server->api->console->register("spawn", "", array($this, "commandHandler")); - $this->server->api->console->register("lag", "", array($this, "commandHandler")); + $this->server->api->console->register("ping", "", array($this, "commandHandler")); + $this->server->api->console->alias("lag", "ping"); $this->server->api->console->alias("suicide", "kill"); $this->server->api->console->alias("tppos", "tp"); $this->server->api->ban->cmdWhitelist("list"); - $this->server->api->ban->cmdWhitelist("lag"); + $this->server->api->ban->cmdWhitelist("ping"); $this->server->api->ban->cmdWhitelist("spawn"); + $this->server->preparedSQL->selectPlayersToHeal = $this->server->database->prepare("SELECT EID FROM entities WHERE class = ".ENTITY_PLAYER." AND health < 20;"); } public function handle($data, $event){ switch($event){ case "server.regeneration": - $result = $this->server->query("SELECT EID FROM entities WHERE class = ".ENTITY_PLAYER." AND health < 20;"); - if($result !== true and $result !== false){ + $result = $this->server->preparedSQL->selectPlayersToHeal->execute(); + if($result !== false){ while(($player = $result->fetchArray()) !== false){ if(($player = $this->server->api->entity->get($player["EID"])) !== false){ if($player->getHealth() <= 0){ @@ -152,12 +154,12 @@ class PlayerAPI{ $spawn = $issuer->getSpawn(); $issuer->teleport($spawn); break; - case "lag": + case "ping": if(!($issuer instanceof Player)){ $output .= "Please run this command in-game.\n"; break; } - $output .= "ping ".round($issuer->getLag(), 2)."ms, packet loss ".round($issuer->getPacketLoss() * 100, 2)."%\n"; + $output .= "ping ".round($issuer->getLag(), 2)."ms, packet loss ".round($issuer->getPacketLoss() * 100, 2)."%, ".round($issuer->getBandwidth() / 1024, 2)." KB/s\n"; break; case "gamemode": $player = false; diff --git a/src/Player.php b/src/Player.php index 1ce68fd1b..8295f18cb 100644 --- a/src/Player.php +++ b/src/Player.php @@ -60,6 +60,9 @@ class Player{ public $blocked = true; public $chunksLoaded = array(); private $chunksOrder = array(); + private $lastMeasure = 0; + private $bandwidthRaw = 0; + private $bandwidthStats = array(0, 0, 0); private $lag = array(); private $lagStat = 0; private $spawnPosition; @@ -726,8 +729,14 @@ class Player{ if($this->connected === false){ return false; } - $this->packetLoss = $this->packetStats[1] / max(1, $this->packetStats[0]); + if($this->packetStats[1] > 2){ + $this->packetLoss = $this->packetStats[1] / max(1, $this->packetStats[0]); + }else{ + $this->packetLoss = 0; + } $this->packetStats = array(0, 0); + array_shift($this->bandwidthStats); + $this->bandwidthStats[] = $this->bandwidthRaw / max(0.00001, microtime(true) - $this->lastMeasure); $this->lagStat = array_sum($this->lag) / max(1, count($this->lag)); $this->lag = array(); $this->sendBuffer(); @@ -735,6 +744,7 @@ class Player{ $this->sendChat("Your connection suffers high packet loss"); $this->close("packet.loss"); } + $this->lastMeasure = microtime(true); } public function getLag(){ @@ -744,6 +754,10 @@ class Player{ public function getPacketLoss(){ return $this->packetLoss; } + + public function getBandwidth(){ + return array_sum($this->bandwidthStats) / max(1, count($this->bandwidthStats)); + } public function handle($pid, $data){ if($this->connected === true){ @@ -769,7 +783,7 @@ class Player{ unset($this->recovery[$count]); } } - $limit = microtime(true) - 3; //max lag + $limit = microtime(true) - 6; //max lag foreach($this->recovery as $count => $d){ $diff = $this->counter[2] - $count; if($diff > 16 and $d["sendtime"] < $limit){ @@ -979,6 +993,7 @@ class Player{ $this->evid[] = $this->server->event("player.pickup", array($this, "eventHandler")); $this->evid[] = $this->server->event("tile.container.slot", array($this, "eventHandler")); $this->evid[] = $this->server->event("tile.update", array($this, "eventHandler")); + $this->lastMeasure = microtime(true); $this->server->schedule(50, array($this, "measureLag"), array(), true); console("[INFO] \x1b[33m".$this->username."\x1b[0m[/".$this->ip.":".$this->port."] logged in with entity id ".$this->eid." at (".$this->entity->level->getName().", ".round($this->entity->x, 2).", ".round($this->entity->y, 2).", ".round($this->entity->z, 2).")"); break; @@ -1444,7 +1459,7 @@ class Player{ public function send($pid, $data = array(), $raw = false){ if($this->connected === true){ - $this->server->send($pid, $data, $raw, $this->ip, $this->port); + $this->bandwidthRaw += $this->server->send($pid, $data, $raw, $this->ip, $this->port); } } diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 78dc937e2..d1e79215c 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -27,8 +27,8 @@ the Free Software Foundation, either version 3 of the License, or class PocketMinecraftServer{ public $tCnt; - public $serverID, $version, $invisible, $api, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $port, $saveEnabled; - private $serverip, $database, $interface, $evCnt, $handCnt, $events, $eventsID, $handlers, $serverType, $lastTick, $ticks; + public $serverID, $database, $version, $invisible, $api, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $port, $saveEnabled; + private $serverip, $interface, $evCnt, $handCnt, $events, $eventsID, $handlers, $serverType, $lastTick, $ticks; private function load(){ $this->version = new VersionString(); @@ -457,7 +457,7 @@ class PocketMinecraftServer{ } public function send($pid, $data = array(), $raw = false, $dest = false, $port = false){ - $this->interface->writePacket($pid, $data, $raw, $dest, $port); + return $this->interface->writePacket($pid, $data, $raw, $dest, $port); } public function process(){ diff --git a/src/network/MinecraftInterface.php b/src/network/MinecraftInterface.php index efd285468..c33668ac7 100644 --- a/src/network/MinecraftInterface.php +++ b/src/network/MinecraftInterface.php @@ -143,7 +143,7 @@ class MinecraftInterface{ $write = $this->socket->write($data, $dest, $port); $this->writeDump($pid, $data, false, "client", $dest, $port); } - return true; + return $write; } }