Added bandwidth measure

This commit is contained in:
Shoghi Cervantes 2013-06-04 10:55:20 +02:00
parent 0c7c36cc03
commit 033a1673f0
4 changed files with 30 additions and 13 deletions

View File

@ -40,19 +40,21 @@ class PlayerAPI{
$this->server->api->console->register("tp", "[target player] <destination player|w:world> OR /tp [target player] <x> <y> <z>", 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;

View File

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

View File

@ -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(){

View File

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