From 6102efc80932ab272640941d4518a0d19f7afbaf Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Fri, 11 Jan 2013 00:20:13 +0100 Subject: [PATCH] Fixed Memory Leak --- src/API/EntityAPI.php | 34 +++------------------ src/API/PlayerAPI.php | 28 ++++++++++++++--- src/classes/Player.class.php | 32 ++++++++++--------- src/classes/PocketMinecraftServer.class.php | 1 + 4 files changed, 46 insertions(+), 49 deletions(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index c6246d536..56a8adc97 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -31,42 +31,16 @@ class EntityAPI{ $this->server = $server; } - public function init(){ - $this->server->addHandler("player.death", array($this, "handle"), 1); - } - - public function handle($data, $event){ - switch($event){ - case "player.death": - $message = $data["name"]; - if(is_numeric($data["cause"]) and isset($this->entities[$data["cause"]])){ - $e = $this->api->entity->get($data["cause"]); - switch($e->class){ - case ENTITY_PLAYER: - $message .= " was killed by ".$e->name; - break; - default: - $message .= " was killed"; - break; - } - }else{ - switch($data["cause"]){ - default: - $message .= " was killed"; - break; - } - } - $this->server->chat(false, $message); - break; - } - } - public function get($eid){ if(isset($this->server->entities[$eid])){ return $this->server->entities[$eid]; } return false; } + + public function init(){ + + } public function getAll(){ return $this->server->entities; diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 4494e8bf5..2ea4f5c33 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -32,7 +32,8 @@ class PlayerAPI{ } public function init(){ - $this->server->event("server.regeneration", array($this, "handle")); + $this->server->addHandler("server.regeneration", array($this, "handle")); + $this->server->addHandler("player.death", array($this, "handle"), 1); $this->server->api->console->register("list", "Shows connected player list", array($this, "commandHandler")); $this->server->api->console->register("kill", "Kills a player", array($this, "commandHandler")); $this->server->api->console->register("tppos", "Teleports a player to a position", array($this, "commandHandler")); @@ -51,6 +52,27 @@ class PlayerAPI{ } } break; + case "player.death": + $message = $data["name"]; + if(is_numeric($data["cause"]) and isset($this->entities[$data["cause"]])){ + $e = $this->api->entity->get($data["cause"]); + switch($e->class){ + case ENTITY_PLAYER: + $message .= " was killed by ".$e->name; + break; + default: + $message .= " was killed"; + break; + } + }else{ + switch($data["cause"]){ + default: + $message .= " was killed"; + break; + } + } + $this->server->chat(false, $message); + break; } } @@ -180,9 +202,7 @@ class PlayerAPI{ public function remove($CID){ if(isset($this->server->clients[$CID])){ $player = $this->server->clients[$CID]; - if(is_object($player->entity)){ - $player->entity->close(); - } + $this->server->api->entity->remove($player->entity->eid); $this->saveOffline($player->username, $player->data); $this->server->query("DELETE FROM players WHERE name = '".$player->username."';"); unset($this->server->entities[$player->eid]); diff --git a/src/classes/Player.class.php b/src/classes/Player.class.php index 7c47925da..e63d8c571 100644 --- a/src/classes/Player.class.php +++ b/src/classes/Player.class.php @@ -92,23 +92,25 @@ class Player{ } public function close($reason = "", $msg = true){ - $reason = $reason == "" ? "server stop":$reason; - $this->save(); - foreach($this->evid as $ev){ - $this->server->deleteEvent($ev); - } - $this->eventHandler("You have been kicked. Reason: ".$reason, "server.chat"); - $this->dataPacket(MC_LOGIN_STATUS, array( - "status" => 1, - )); - $this->dataPacket(MC_DISCONNECT); + if($this->connected === true){ + $reason = $reason == "" ? "server stop":$reason; + $this->save(); + foreach($this->evid as $ev){ + $this->server->deleteEvent($ev); + } + $this->eventHandler("You have been kicked. Reason: ".$reason, "server.chat"); + $this->dataPacket(MC_LOGIN_STATUS, array( + "status" => 1, + )); + $this->dataPacket(MC_DISCONNECT); - $this->connected = false; - if($msg === true){ - $this->server->api->dhandle("server.chat", $this->username." left the game"); + $this->connected = false; + if($msg === true){ + $this->server->api->dhandle("server.chat", $this->username." left the game"); + } + console("[INFO] Session with ".$this->ip.":".$this->port." Client ID ".$this->clientID." closed due to ".$reason); + $this->server->api->player->remove($this->CID); } - console("[INFO] Session with ".$this->ip.":".$this->port." Client ID ".$this->clientID." closed due to ".$reason); - $this->server->api->player->remove($this->CID); } public function eventHandler($data, $event){ diff --git a/src/classes/PocketMinecraftServer.class.php b/src/classes/PocketMinecraftServer.class.php index 7b018fd4a..6797a3e5e 100644 --- a/src/classes/PocketMinecraftServer.class.php +++ b/src/classes/PocketMinecraftServer.class.php @@ -210,6 +210,7 @@ class PocketMinecraftServer{ $this->preparedSQL->selectHandlers->bindValue(":name", $event, SQLITE3_TEXT); $handlers = $this->preparedSQL->selectHandlers->execute(); $result = true; + console("[INTERNAL] Handling ".$event, true, true, 3); if($handlers !== false and $handlers !== true){ while(false !== ($hn = $handlers->fetchArray(SQLITE3_ASSOC)) and $result !== false){ $handler = $this->handlers[(int) $hn["ID"]];