Added asynchronous file writing

This commit is contained in:
Shoghi Cervantes 2015-05-22 16:32:08 +02:00
parent cfe5ca91b2
commit 149234f125
No known key found for this signature in database
GPG Key ID: 78464DB0A7837F89
4 changed files with 61 additions and 8 deletions

View File

@ -351,7 +351,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
* @return bool
*/
public function canSee(Player $player){
return !isset($this->hiddenPlayers[$player->getName()]);
return !isset($this->hiddenPlayers[$player->getUniqueId()]);
}
/**
@ -361,7 +361,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
if($player === $this){
return;
}
$this->hiddenPlayers[$player->getName()] = $player;
$this->hiddenPlayers[$player->getUniqueId()] = $player;
$player->despawnFrom($this);
}
@ -372,7 +372,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
if($player === $this){
return;
}
unset($this->hiddenPlayers[$player->getName()]);
unset($this->hiddenPlayers[$player->getUniqueId()]);
if($player->isOnline()){
$player->spawnTo($this);
}
@ -2735,14 +2735,14 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
public function close($message = "", $reason = "generic reason", $notify = true){
if($this->connected and !$this->closed){
if($notify and $reason != ""){
if($notify and strlen((string) $reason) > 0){
$pk = new DisconnectPacket;
$pk->message = $reason;
$this->directDataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
}
$this->connected = false;
if($this->username != ""){
if(strlen($this->getName()) > 0){
$this->server->getPluginManager()->callEvent($ev = new PlayerQuitEvent($this, $message));
if($this->server->getAutoSave() and $this->loggedIn === true){
$this->save();

View File

@ -91,6 +91,7 @@ use pocketmine\plugin\PharPluginLoader;
use pocketmine\plugin\Plugin;
use pocketmine\plugin\PluginLoadOrder;
use pocketmine\plugin\PluginManager;
use pocketmine\scheduler\FileWriteTask;
use pocketmine\scheduler\SendUsageTask;
use pocketmine\scheduler\ServerScheduler;
use pocketmine\tile\Chest;
@ -843,7 +844,8 @@ class Server{
$nbt = new NBT(NBT::BIG_ENDIAN);
try{
$nbt->setData($nbtTag);
file_put_contents($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed());
$this->getScheduler()->scheduleAsyncTask(new FileWriteTask($this->getDataPath() . "players/" . strtolower($name) . ".dat", $nbt->writeCompressed()));
}catch(\Exception $e){
$this->logger->critical($this->getLanguage()->translateString("pocketmine.data.saveError", [$name, $e->getMessage()]));
if(\pocketmine\DEBUG > 1 and $this->logger instanceof MainLogger){

View File

@ -0,0 +1,43 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\scheduler;
class FileWriteTask extends AsyncTask{
private $path;
private $contents;
private $flags;
public function __construct($path, $contents, $flags = 0){
$this->path = $path;
$this->contents = $contents;
$this->flags = (int) $flags;
}
public function onRun(){
try{
file_put_contents($this->path, $this->contents, (int) $this->flags);
}catch (\Exception $e){
}
}
}

View File

@ -20,6 +20,7 @@
*/
namespace pocketmine\utils;
use pocketmine\scheduler\FileWriteTask;
use pocketmine\Server;
@ -170,9 +171,11 @@ class Config{
}
/**
* @param bool $async
*
* @return boolean
*/
public function save(){
public function save($async = false){
if($this->correct === true){
try{
$content = null;
@ -194,7 +197,12 @@ class Config{
$content = implode("\r\n", array_keys($this->config));
break;
}
file_put_contents($this->file, $content);
if($async){
Server::getInstance()->getScheduler()->scheduleAsyncTask(new FileWriteTask($this->file, $content));
}else{
file_put_contents($this->file, $content);
}
}catch(\Exception $e){
$logger = Server::getInstance()->getLogger();
$logger->critical("Could not save Config " . $this->file . ": " . $e->getMessage());