Fixed client crashing (temp.), fixed clients not spawning. (Fixes #1640 #1636 #1590 #1573)

This commit is contained in:
Shoghi Cervantes
2014-07-14 02:19:14 +02:00
parent eccd82ca4b
commit 6c442551f7
8 changed files with 89 additions and 73 deletions

View File

@ -29,22 +29,18 @@ use pocketmine\Server;
*/
abstract class AsyncTask extends \Threaded{
private $complete = null;
private $finished = null;
private $result = null;
public $complete = null;
public $finished = null;
public $result = null;
public function run(){
$this->lock();
$this->finished = false;
$this->complete = false;
$this->result = null;
$this->unlock();
$this->onRun();
$this->lock();
$this->finished = true;
$this->unlock();
}
@ -52,7 +48,6 @@ abstract class AsyncTask extends \Threaded{
* @return bool
*/
public function isFinished(){
return $this->finished === true;
}
@ -88,6 +83,14 @@ abstract class AsyncTask extends \Threaded{
$this->result = @serialize($result);
}
public function setTaskId($taskId){
$this->taskId = $taskId;
}
public function getTaskId(){
return $this->taskId;
}
/**
* Actions to execute when run
*

View File

@ -77,8 +77,10 @@ class ServerScheduler{
* @return void
*/
public function scheduleAsyncTask(AsyncTask $task){
$id = $this->nextId();
$task->setTaskId($id);
$this->asyncPool->submit($task);
$this->asyncTaskStorage[spl_object_hash($task)] = $task;
$this->asyncTaskStorage[$id] = $task;
++$this->asyncTasks;
}
@ -222,6 +224,12 @@ class ServerScheduler{
if($this->asyncTasks > 0){ //Garbage collector
$this->asyncPool->collect([$this, "collectAsyncTask"]);
foreach($this->asyncTaskStorage as $asyncTask){
if($asyncTask->isFinished() and !$asyncTask->isCompleted()){
$this->collectAsyncTask($asyncTask);
}
}
}
}
@ -230,7 +238,7 @@ class ServerScheduler{
--$this->asyncTasks;
$task->onCompletion(Server::getInstance());
$task->setCompleted();
unset($this->asyncTaskStorage[spl_object_hash($task)]);
unset($this->asyncTaskStorage[$task->getTaskId()]);
return true;
}
return false;