From c8e157156e857c0e804829c9ef3e43e7ea9a0b15 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Tue, 11 Jun 2013 11:00:02 +0200 Subject: [PATCH] Added bandwidth usage measurement to Window Status --- src/PocketMinecraftServer.php | 4 +++- src/network/MinecraftInterface.php | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 1ece9fe14..9fbd75ae7 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -94,9 +94,11 @@ class PocketMinecraftServer{ } public function titleTick(){ + $time = microtime(true); if(ENABLE_ANSI === true){ - echo "\x1b]0;PocketMine-MP ".MAJOR_VERSION." | Online ". count($this->clients)."/".$this->maxClients." | RAM ".round((memory_get_usage() / 1024) / 1024, 2)."MB | TPS ".$this->getTPS()."\x07"; + echo "\x1b]0;PocketMine-MP ".MAJOR_VERSION." | Online ". count($this->clients)."/".$this->maxClients." | RAM ".round((memory_get_usage() / 1024) / 1024, 2)."MB | U ".round(($this->interface->bandwidth[0] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2)." D ".round(($this->interface->bandwidth[1] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2)." kB/s | TPS ".$this->getTPS()."\x07"; } + $this->interface->bandwidth = array(0, 0, $time); } public function loadEvents(){ diff --git a/src/network/MinecraftInterface.php b/src/network/MinecraftInterface.php index 052264133..5e504c1dc 100644 --- a/src/network/MinecraftInterface.php +++ b/src/network/MinecraftInterface.php @@ -27,6 +27,7 @@ the Free Software Foundation, either version 3 of the License, or class MinecraftInterface{ public $client; + public $bandwidth; private $socket; private $data; private $chunked; @@ -38,6 +39,7 @@ class MinecraftInterface{ console("[ERROR] Couldn't bind to $serverip:".$port, true, true, 0); exit(1); } + $this->bandwidth = array(0, 0, microtime(true)); $this->client = (bool) $client; $this->start = microtime(true); $this->chunked = array(); @@ -69,6 +71,7 @@ class MinecraftInterface{ if($len === false){ return $pk; } + $this->bandwidth[0] += $len; $this->parsePacket($buf, $source, $port); return ($pk !== false ? $pk : $this->popPacket()); } @@ -244,6 +247,7 @@ class MinecraftInterface{ $write = strlen($packet->raw); }else{ $write = $this->socket->write($packet->raw, $dest, $port); + $this->bandwidth[1] += $write; } }else{ if($force === false and $this->isChunked($CID)){ @@ -254,6 +258,7 @@ class MinecraftInterface{ $write = strlen($data); }else{ $write = $this->socket->write($data, $dest, $port); + $this->bandwidth[1] += $write; } } return $write;