22declare(strict_types=1);
24namespace pocketmine\console;
26use pmmp\thread\Thread as NativeThread;
27use pmmp\thread\ThreadSafeArray;
29use
function cli_set_process_title;
34use
function stream_socket_client;
36require dirname(__DIR__, 2) .
'/vendor/autoload.php';
38if(count($argv) !== 2){
39 die(
"Please provide a server to connect to");
42@cli_set_process_title(
'PocketMine-MP Console Reader');
45$socket = stream_socket_client($argv[1], $errCode, $errMessage, 15.0);
47 throw new \RuntimeException(
"Failed to connect to server process ($errCode): $errMessage");
51$channel =
new ThreadSafeArray();
52$thread =
new class($channel) extends NativeThread{
56 public function __construct(
57 private ThreadSafeArray $channel,
60 public function run() : void{
61 require dirname(__DIR__, 2) .
'/vendor/autoload.php';
63 $channel = $this->channel;
64 $reader =
new ConsoleReader();
66 $line = $reader->readLine();
68 $channel->synchronized(
function() use ($channel, $line) :
void{
77$thread->start(NativeThread::INHERIT_NONE);
79 $line = $channel->synchronized(
function() use ($channel) : ?
string{
80 if(count($channel) === 0){
81 $channel->wait(1_000_000);
83 $line = $channel->shift();
86 if(@fwrite($socket, ($line ??
"") .
"\n") ===
false){
static kill(int $pid, bool $subprocesses=false)