mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-07 04:17:07 +00:00
This reverts commit cbe0f44c4f7bc3715acbf148f981bd93111c4c8f. This achieves the same result as the reverted commit wrt. process in the same manner (writing a keepalive into the socket and checking if it failed to send). However, it does _not_ allow the process to die on reaching pipe EOF, since this can cause many spams of subprocesses when stdin is actually not a tty (e.g. in a Docker container).
80 lines
1.9 KiB
PHP
80 lines
1.9 KiB
PHP
<?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/
|
|
*
|
|
*
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace pocketmine\console;
|
|
|
|
use function fclose;
|
|
use function fgets;
|
|
use function fopen;
|
|
use function is_resource;
|
|
use function stream_select;
|
|
use function trim;
|
|
use function usleep;
|
|
|
|
final class ConsoleReader{
|
|
/** @var resource */
|
|
private $stdin;
|
|
|
|
public function __construct(){
|
|
$this->initStdin();
|
|
}
|
|
|
|
private function initStdin() : void{
|
|
if(is_resource($this->stdin)){
|
|
fclose($this->stdin);
|
|
}
|
|
|
|
$this->stdin = fopen("php://stdin", "r");
|
|
}
|
|
|
|
/**
|
|
* Reads a line from the console and adds it to the buffer. This method may block the thread.
|
|
*/
|
|
public function readLine() : ?string{
|
|
if(!is_resource($this->stdin)){
|
|
$this->initStdin();
|
|
}
|
|
|
|
$r = [$this->stdin];
|
|
$w = $e = null;
|
|
if(($count = stream_select($r, $w, $e, 0, 200000)) === 0){ //nothing changed in 200000 microseconds
|
|
return null;
|
|
}elseif($count === false){ //stream error
|
|
return null;
|
|
}
|
|
|
|
if(($raw = fgets($this->stdin)) === false){ //broken pipe or EOF
|
|
usleep(200000); //prevent CPU waste if it's end of pipe
|
|
return null; //loop back round
|
|
}
|
|
|
|
$line = trim($raw);
|
|
|
|
return $line !== "" ? $line : null;
|
|
}
|
|
|
|
public function __destruct(){
|
|
fclose($this->stdin);
|
|
}
|
|
}
|