mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-05 11:27:07 +00:00
Use Console Thread for Async I/O
This commit is contained in:
parent
b3cc13d4f2
commit
302c865f4e
@ -26,21 +26,26 @@ the Free Software Foundation, either version 3 of the License, or
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class ConsoleAPI{
|
class ConsoleAPI{
|
||||||
private $input, $server, $event;
|
private $loop, $server, $event;
|
||||||
function __construct(PocketMinecraftServer $server){
|
function __construct(PocketMinecraftServer $server){
|
||||||
$this->help = array();
|
$this->help = array();
|
||||||
$this->server = $server;
|
$this->server = $server;
|
||||||
$this->input = fopen(FILE_PATH."logs/console.in", "w+b");
|
|
||||||
$this->last = microtime(true);
|
$this->last = microtime(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function init(){
|
public function init(){
|
||||||
$this->event = $this->server->event("server.tick", array($this, "handle"));
|
$this->event = $this->server->event("server.tick", array($this, "handle"));
|
||||||
|
$this->loop = new ConsoleLoop;
|
||||||
|
$this->loop->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
function __destroy(){
|
function __destruct(){
|
||||||
$this->server->deleteEvent($this->event);
|
$this->server->deleteEvent($this->event);
|
||||||
fclose($this->input);
|
$this->loop->stop = true;
|
||||||
|
$this->loop->notify();
|
||||||
|
$this->loop->join();
|
||||||
|
unset($this->loop);
|
||||||
|
gc_collect_cycles();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function defaultCommands($cmd, $params){
|
public function defaultCommands($cmd, $params){
|
||||||
@ -74,7 +79,8 @@ class ConsoleAPI{
|
|||||||
$this->server->api->setProperty("last-update", time());
|
$this->server->api->setProperty("last-update", time());
|
||||||
break;
|
break;
|
||||||
case "stop":
|
case "stop":
|
||||||
console("[INFO] Stopping the server...");
|
$this->loop->stop = true;
|
||||||
|
console("[INFO] Stopping the server...");
|
||||||
$this->server->close();
|
$this->server->close();
|
||||||
break;
|
break;
|
||||||
/*case "restart":
|
/*case "restart":
|
||||||
@ -229,22 +235,34 @@ class ConsoleAPI{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function handle($time){
|
public function handle($time){
|
||||||
while(($line = fgets($this->input)) !== false){
|
if($this->loop->line !== false){
|
||||||
$line = trim($line);
|
$line = trim($this->loop->line);
|
||||||
if($line === ""){
|
$this->loop->line = false;
|
||||||
continue;
|
if($line !== ""){
|
||||||
}
|
$params = explode(" ", $line);
|
||||||
$params = explode(" ", $line);
|
$cmd = strtolower(array_shift($params));
|
||||||
$cmd = strtolower(array_shift($params));
|
console("[INFO] Issued server command: /$cmd ".implode(" ", $params));
|
||||||
console("[INFO] Issued server command: /$cmd ".implode(" ", $params));
|
if(isset($this->help[$cmd]) and is_callable($this->help[$cmd][1])){
|
||||||
if(isset($this->help[$cmd]) and is_callable($this->help[$cmd][1])){
|
call_user_func($this->help[$cmd][1], $cmd, $params);
|
||||||
call_user_func($this->help[$cmd][1], $cmd, $params);
|
}elseif($this->server->trigger("api.console.command", array("cmd" => $cmd, "params" => $params)) !== false){
|
||||||
}elseif($this->server->trigger("api.console.command", array("cmd" => $cmd, "params" => $params)) !== false){
|
$this->defaultCommands($cmd, $params);
|
||||||
$this->defaultCommands($cmd, $params);
|
}
|
||||||
}
|
}
|
||||||
|
}else{
|
||||||
|
$this->loop->notify();
|
||||||
}
|
}
|
||||||
ftruncate($this->input, 0);
|
|
||||||
fseek($this->input, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class ConsoleLoop extends Thread{
|
||||||
|
var $line = false, $stop = false;
|
||||||
|
public function run(){
|
||||||
|
$fp = fopen("php://stdin", "r");
|
||||||
|
while($this->stop === false and ($line = fgets($fp)) !== false){
|
||||||
|
$this->line = $line;
|
||||||
|
$this->wait();
|
||||||
|
$this->line = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -36,7 +36,6 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run
|
|||||||
@mkdir(FILE_PATH."worlds/", 0777);
|
@mkdir(FILE_PATH."worlds/", 0777);
|
||||||
@mkdir(FILE_PATH."plugins/", 0777);
|
@mkdir(FILE_PATH."plugins/", 0777);
|
||||||
file_put_contents(FILE_PATH."logs/packets.log", "");
|
file_put_contents(FILE_PATH."logs/packets.log", "");
|
||||||
file_put_contents(FILE_PATH."logs/console.in", "");
|
|
||||||
if(!file_exists(FILE_PATH."logs/test.bin.log") or md5_file(FILE_PATH."logs/test.bin.log") !== TEST_MD5){
|
if(!file_exists(FILE_PATH."logs/test.bin.log") or md5_file(FILE_PATH."logs/test.bin.log") !== TEST_MD5){
|
||||||
console("[NOTICE] Executing integrity tests...");
|
console("[NOTICE] Executing integrity tests...");
|
||||||
console("[INFO] OS: ".PHP_OS.", ".Utils::getOS());
|
console("[INFO] OS: ".PHP_OS.", ".Utils::getOS());
|
||||||
@ -187,6 +186,16 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run
|
|||||||
$this->server->loadEntities();
|
$this->server->loadEntities();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __destruct(){
|
||||||
|
foreach($this->apiList as $ob){
|
||||||
|
if(is_callable($ob, "__destruct")){
|
||||||
|
$ob->__destruct();
|
||||||
|
unset($this->apiList[$ob]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private function loadProperties(){
|
private function loadProperties(){
|
||||||
if(isset($this->config["memory-limit"])){
|
if(isset($this->config["memory-limit"])){
|
||||||
@ini_set("memory_limit", $this->config["memory-limit"]);
|
@ini_set("memory_limit", $this->config["memory-limit"]);
|
||||||
@ -276,6 +285,7 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run
|
|||||||
public function start(){
|
public function start(){
|
||||||
$this->server->start();
|
$this->server->start();
|
||||||
unregister_tick_function(array($this->server, "tick"));
|
unregister_tick_function(array($this->server, "tick"));
|
||||||
|
$this->__destruct();
|
||||||
unset($this->server);
|
unset($this->server);
|
||||||
return $this->restart;
|
return $this->restart;
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ class Async extends Thread {
|
|||||||
/**
|
/**
|
||||||
* Provide a passthrough to call_user_func_array
|
* Provide a passthrough to call_user_func_array
|
||||||
**/
|
**/
|
||||||
public function __construct($method, $params){
|
public function __construct($method, $params = array()){
|
||||||
$this->method = $method;
|
$this->method = $method;
|
||||||
$this->params = $params;
|
$this->params = $params;
|
||||||
$this->result = null;
|
$this->result = null;
|
||||||
@ -40,15 +40,17 @@ class Async extends Thread {
|
|||||||
* The smallest thread in the world
|
* The smallest thread in the world
|
||||||
**/
|
**/
|
||||||
public function run(){
|
public function run(){
|
||||||
if (($this->result=call_user_func_array($this->method, $this->params))) {
|
if(($this->result=call_user_func_array($this->method, $this->params))){
|
||||||
return true;
|
return true;
|
||||||
} else return false;
|
}else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Static method to create your threads from functions ...
|
* Static method to create your threads from functions ...
|
||||||
**/
|
**/
|
||||||
public static function call($method, $params){
|
public static function call($method, $params = array()){
|
||||||
$thread = new Async($method, $params);
|
$thread = new Async($method, $params);
|
||||||
if($thread->start()){
|
if($thread->start()){
|
||||||
return $thread;
|
return $thread;
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
-
|
|
||||||
/ \
|
|
||||||
/ \
|
|
||||||
/ PocketMine \
|
|
||||||
/ MP \
|
|
||||||
|\ @shoghicp /|
|
|
||||||
|. \ / .|
|
|
||||||
| .. \ / .. |
|
|
||||||
| .. | .. |
|
|
||||||
| .. | .. |
|
|
||||||
\ | /
|
|
||||||
\ | /
|
|
||||||
\ | /
|
|
||||||
\ | /
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
$fp = fopen(dirname(__FILE__)."/../../logs/console.in","wb");
|
|
||||||
while(true){
|
|
||||||
$l = fgets(STDIN);
|
|
||||||
fwrite($fp, $l);
|
|
||||||
if(strtolower(trim($l)) === "stop" and isset($argv[1]) and trim($argv[1]) == "1"){
|
|
||||||
die();
|
|
||||||
}
|
|
||||||
}
|
|
@ -26,7 +26,5 @@ if not "%PHPOUTPUT%"=="1" (
|
|||||||
echo [ERROR] Couldn't find PHP binary in PATH.
|
echo [ERROR] Couldn't find PHP binary in PATH.
|
||||||
ping 127.0.0.1 -n 3 -w 1000>nul
|
ping 127.0.0.1 -n 3 -w 1000>nul
|
||||||
) else (
|
) else (
|
||||||
START /B CMD /C CALL php PocketMine-MP.php
|
START /B /WAIT php PocketMine-MP.php
|
||||||
START /B /WAIT php src/common/input.php 1
|
|
||||||
ping 127.0.0.1 -n 5 -w 1000>nul
|
|
||||||
)
|
)
|
Loading…
x
Reference in New Issue
Block a user