PocketMine-MP 5.18.1 git-9381fc4172e5dce4cada1cb356050c8a2ab57b94
ConsoleReaderChildProcess.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\console;
25
26use pmmp\thread\Thread as NativeThread;
27use pmmp\thread\ThreadSafeArray;
29use function cli_set_process_title;
30use function count;
31use function dirname;
32use function feof;
33use function fwrite;
34use function stream_socket_client;
35
36require dirname(__DIR__, 2) . '/vendor/autoload.php';
37
38if(count($argv) !== 2){
39 die("Please provide a server to connect to");
40}
41
42@cli_set_process_title('PocketMine-MP Console Reader');
43$errCode = null;
44$errMessage = null;
45$socket = stream_socket_client($argv[1], $errCode, $errMessage, 15.0);
46if($socket === false){
47 throw new \RuntimeException("Failed to connect to server process ($errCode): $errMessage");
48}
49
51$channel = new ThreadSafeArray();
52$thread = new class($channel) extends NativeThread{
56 public function __construct(
57 private ThreadSafeArray $channel,
58 ){}
59
60 public function run() : void{
61 require dirname(__DIR__, 2) . '/vendor/autoload.php';
62
63 $channel = $this->channel;
64 $reader = new ConsoleReader();
65 while(true){ // @phpstan-ignore-line
66 $line = $reader->readLine();
67 if($line !== null){
68 $channel->synchronized(function() use ($channel, $line) : void{
69 $channel[] = $line;
70 $channel->notify();
71 });
72 }
73 }
74 }
75};
76
77$thread->start(NativeThread::INHERIT_NONE);
78while(!feof($socket)){
79 $line = $channel->synchronized(function() use ($channel) : ?string{
80 if(count($channel) === 0){
81 $channel->wait(1_000_000);
82 }
83 $line = $channel->shift();
84 return $line;
85 });
86 if(@fwrite($socket, ($line ?? "") . "\n") === false){
87 //Always send even if there's no line, to check if the parent is alive
88 //If the parent process was terminated forcibly, it won't close the connection properly, so feof() will return
89 //false even though the connection is actually broken. However, fwrite() will fail.
90 break;
91 }
92}
93
94//For simplicity's sake, we don't bother with a graceful shutdown here.
95//The parent process would normally forcibly terminate the child process anyway, so we only reach this point if the
96//parent process was terminated forcibly and didn't clean up after itself.
97Process::kill(Process::pid());
static kill(int $pid, bool $subprocesses=false)
Definition: Process.php:131