IPC improvements

This commit is contained in:
Daniil Gentili 2020-07-12 14:55:21 +02:00
parent 06e347ca85
commit 86f6887148
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
6 changed files with 39 additions and 30 deletions

View File

@ -70,6 +70,7 @@
"files": [
"src/BigIntegor.php",
"src/YieldReturnValue.php",
"src/danog/MadelineProto/Ipc/Runner/entry.php",
"src/polyfill.php"
]
},

View File

@ -188,7 +188,7 @@ class API extends InternalDoc
{
$this->init();
if (!$this->oldInstance) {
$this->logger->logger('Shutting down MadelineProto ('.\explode('\\', \get_class($this))[2].')');
$this->logger->logger('Shutting down MadelineProto ('.static::class.')');
if ($this->API) {
$this->API->destructing = true;
$this->API->unreference();

View File

@ -10,13 +10,11 @@ final class WebRunner extends RunnerAbstract
{
/** @var string|null Cached path to the runner script. */
private static $runPath;
/**
* Initialization payload.
*
* @var array
*/
private $params;
/**
* Resources
*/
private static array $resources = [];
/**
* Socket.
*
@ -46,7 +44,7 @@ final class WebRunner extends RunnerAbstract
if (!$rootDir) {
throw new ContextException('Could not get entry file!');
}
$rootDir = \dirname($rootDir);
$rootDir = \dirname($rootDir).DIRECTORY_SEPARATOR;
$uriDir = \dirname($uri);
if (\substr($rootDir, -\strlen($uriDir)) !== $uriDir) {
@ -78,12 +76,12 @@ final class WebRunner extends RunnerAbstract
self::$runPath = \str_replace('//', '/', self::$runPath);
}
$this->params = [
'argv' => ['pony', 'madeline-ipc', $session],
$params = [
'argv' => ['madeline-ipc', $session],
'cwd' => Magic::getcwd()
];
$params = \http_build_query($this->params);
$params = \http_build_query($params);
$address = ($_SERVER['HTTPS'] ?? false ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];
@ -95,6 +93,6 @@ final class WebRunner extends RunnerAbstract
// We don't care for results or timeouts here, PHP doesn't count IOwait time as execution time anyway
// Technically should use amphp/socket, but I guess it's OK to not introduce another dependency just for a socket that will be used once.
\fwrite($res = \fsockopen($address, $port), $payload);
\fclose($res);
self::$resources []= $res;
}
}

View File

@ -24,23 +24,31 @@ use danog\MadelineProto\Magic;
use danog\MadelineProto\SessionPaths;
use danog\MadelineProto\Tools;
(static function () use (&$argv): void {
$ipcPath = null;
if (\defined(\MADELINE_WORKER_START::class)) {
$ipcPath = \MADELINE_WORKER_START;
} elseif (\count(\debug_backtrace(0)) === 1) {
(static function (): void {
if (\defined(\MADELINE_ENTRY::class)) {
// Already called
return;
}
\define(\MADELINE_ENTRY::class, 1);
if (!\defined(\MADELINE_WORKER_TYPE::class)) {
if (\count(\debug_backtrace(0)) !== 1) {
// We're not being included directly
return;
}
$arguments = [];
if (isset($GLOBALS['argv']) && !empty($GLOBALS['argv'])) {
$arguments = $GLOBALS['argv'];
$arguments = \array_slice($GLOBALS['argv'], 1);
} elseif (isset($_GET['argv']) && !empty($_GET['argv'])) {
$arguments = $_GET['argv'];
} else {
return;
}
if (isset($arguments[1]) && $arguments[1] === 'madeline-ipc') {
$ipcPath = $arguments[2];
} else {
return;
if (\count($arguments) < 2) {
\trigger_error("Not enough arguments!", E_USER_ERROR);
exit(1);
}
\define(\MADELINE_WORKER_TYPE::class, \array_shift($arguments));
\define(\MADELINE_WORKER_ARGS::class, $arguments);
}
if (!\class_exists(API::class)) {
$paths = [
\dirname(__DIR__, 7)."/autoload.php",
\dirname(__DIR__, 5)."/vendor/autoload.php",
@ -60,8 +68,8 @@ use danog\MadelineProto\Tools;
include $autoloadPath;
}
if ($ipcPath) {
if (\MADELINE_WORKER_TYPE === 'madeline-ipc') {
$ipcPath = \MADELINE_WORKER_ARGS[0];
if (!\file_exists($ipcPath)) {
\trigger_error("IPC session $ipcPath does not exist!", E_USER_ERROR);
exit(1);

View File

@ -56,6 +56,7 @@ trait Loop
$this->logger->logger($needs_restart ? 'Will self-restart' : 'Will not self-restart');
if ($needs_restart) {
$this->logger->logger("Adding restart callback!");
$logger = $this->logger;
$id = Shutdown::addCallback(static function () use (&$logger) {
$address = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];

View File

@ -6,16 +6,17 @@ function ___install_madeline()
{
if (\count(\debug_backtrace(0)) === 1) {
if (isset($GLOBALS['argv']) && !empty($GLOBALS['argv'])) {
$arguments = $GLOBALS['argv'];
$arguments = array_slice($GLOBALS['argv'], 1);
} elseif (isset($_GET['argv']) && !empty($_GET['argv'])) {
$arguments = $_GET['argv'];
} else {
$arguments = [];
}
if (isset($arguments[1]) && $arguments[1] === 'madeline-ipc') {
\define(\MADELINE_WORKER_START::class, $arguments[2]);
if (count($arguments) >= 2) {
\define(\MADELINE_WORKER_TYPE::class, array_shift($arguments));
\define(\MADELINE_WORKER_ARGS::class, $arguments);
} else {
die('You must include this file in another PHP script'.PHP_EOL);
die('MadelineProto loader: you must include this file in another PHP script, see https://docs.madelineproto.xyz for more info.'.PHP_EOL);
}
}
$old = false;