IPC improvements
This commit is contained in:
parent
06e347ca85
commit
86f6887148
@ -70,6 +70,7 @@
|
|||||||
"files": [
|
"files": [
|
||||||
"src/BigIntegor.php",
|
"src/BigIntegor.php",
|
||||||
"src/YieldReturnValue.php",
|
"src/YieldReturnValue.php",
|
||||||
|
"src/danog/MadelineProto/Ipc/Runner/entry.php",
|
||||||
"src/polyfill.php"
|
"src/polyfill.php"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -188,7 +188,7 @@ class API extends InternalDoc
|
|||||||
{
|
{
|
||||||
$this->init();
|
$this->init();
|
||||||
if (!$this->oldInstance) {
|
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) {
|
if ($this->API) {
|
||||||
$this->API->destructing = true;
|
$this->API->destructing = true;
|
||||||
$this->API->unreference();
|
$this->API->unreference();
|
||||||
|
@ -10,13 +10,11 @@ final class WebRunner extends RunnerAbstract
|
|||||||
{
|
{
|
||||||
/** @var string|null Cached path to the runner script. */
|
/** @var string|null Cached path to the runner script. */
|
||||||
private static $runPath;
|
private static $runPath;
|
||||||
/**
|
|
||||||
* Initialization payload.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $params;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resources
|
||||||
|
*/
|
||||||
|
private static array $resources = [];
|
||||||
/**
|
/**
|
||||||
* Socket.
|
* Socket.
|
||||||
*
|
*
|
||||||
@ -46,7 +44,7 @@ final class WebRunner extends RunnerAbstract
|
|||||||
if (!$rootDir) {
|
if (!$rootDir) {
|
||||||
throw new ContextException('Could not get entry file!');
|
throw new ContextException('Could not get entry file!');
|
||||||
}
|
}
|
||||||
$rootDir = \dirname($rootDir);
|
$rootDir = \dirname($rootDir).DIRECTORY_SEPARATOR;
|
||||||
$uriDir = \dirname($uri);
|
$uriDir = \dirname($uri);
|
||||||
|
|
||||||
if (\substr($rootDir, -\strlen($uriDir)) !== $uriDir) {
|
if (\substr($rootDir, -\strlen($uriDir)) !== $uriDir) {
|
||||||
@ -78,12 +76,12 @@ final class WebRunner extends RunnerAbstract
|
|||||||
self::$runPath = \str_replace('//', '/', self::$runPath);
|
self::$runPath = \str_replace('//', '/', self::$runPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->params = [
|
$params = [
|
||||||
'argv' => ['pony', 'madeline-ipc', $session],
|
'argv' => ['madeline-ipc', $session],
|
||||||
'cwd' => Magic::getcwd()
|
'cwd' => Magic::getcwd()
|
||||||
];
|
];
|
||||||
|
|
||||||
$params = \http_build_query($this->params);
|
$params = \http_build_query($params);
|
||||||
|
|
||||||
$address = ($_SERVER['HTTPS'] ?? false ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME'];
|
$address = ($_SERVER['HTTPS'] ?? false ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME'];
|
||||||
$port = $_SERVER['SERVER_PORT'];
|
$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
|
// 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.
|
// 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);
|
\fwrite($res = \fsockopen($address, $port), $payload);
|
||||||
\fclose($res);
|
self::$resources []= $res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,23 +24,31 @@ use danog\MadelineProto\Magic;
|
|||||||
use danog\MadelineProto\SessionPaths;
|
use danog\MadelineProto\SessionPaths;
|
||||||
use danog\MadelineProto\Tools;
|
use danog\MadelineProto\Tools;
|
||||||
|
|
||||||
(static function () use (&$argv): void {
|
(static function (): void {
|
||||||
$ipcPath = null;
|
if (\defined(\MADELINE_ENTRY::class)) {
|
||||||
if (\defined(\MADELINE_WORKER_START::class)) {
|
// Already called
|
||||||
$ipcPath = \MADELINE_WORKER_START;
|
return;
|
||||||
} elseif (\count(\debug_backtrace(0)) === 1) {
|
}
|
||||||
|
\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'])) {
|
if (isset($GLOBALS['argv']) && !empty($GLOBALS['argv'])) {
|
||||||
$arguments = $GLOBALS['argv'];
|
$arguments = \array_slice($GLOBALS['argv'], 1);
|
||||||
} elseif (isset($_GET['argv']) && !empty($_GET['argv'])) {
|
} elseif (isset($_GET['argv']) && !empty($_GET['argv'])) {
|
||||||
$arguments = $_GET['argv'];
|
$arguments = $_GET['argv'];
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (isset($arguments[1]) && $arguments[1] === 'madeline-ipc') {
|
if (\count($arguments) < 2) {
|
||||||
$ipcPath = $arguments[2];
|
\trigger_error("Not enough arguments!", E_USER_ERROR);
|
||||||
} else {
|
exit(1);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
\define(\MADELINE_WORKER_TYPE::class, \array_shift($arguments));
|
||||||
|
\define(\MADELINE_WORKER_ARGS::class, $arguments);
|
||||||
|
}
|
||||||
|
if (!\class_exists(API::class)) {
|
||||||
$paths = [
|
$paths = [
|
||||||
\dirname(__DIR__, 7)."/autoload.php",
|
\dirname(__DIR__, 7)."/autoload.php",
|
||||||
\dirname(__DIR__, 5)."/vendor/autoload.php",
|
\dirname(__DIR__, 5)."/vendor/autoload.php",
|
||||||
@ -60,8 +68,8 @@ use danog\MadelineProto\Tools;
|
|||||||
|
|
||||||
include $autoloadPath;
|
include $autoloadPath;
|
||||||
}
|
}
|
||||||
|
if (\MADELINE_WORKER_TYPE === 'madeline-ipc') {
|
||||||
if ($ipcPath) {
|
$ipcPath = \MADELINE_WORKER_ARGS[0];
|
||||||
if (!\file_exists($ipcPath)) {
|
if (!\file_exists($ipcPath)) {
|
||||||
\trigger_error("IPC session $ipcPath does not exist!", E_USER_ERROR);
|
\trigger_error("IPC session $ipcPath does not exist!", E_USER_ERROR);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -56,6 +56,7 @@ trait Loop
|
|||||||
$this->logger->logger($needs_restart ? 'Will self-restart' : 'Will not self-restart');
|
$this->logger->logger($needs_restart ? 'Will self-restart' : 'Will not self-restart');
|
||||||
if ($needs_restart) {
|
if ($needs_restart) {
|
||||||
$this->logger->logger("Adding restart callback!");
|
$this->logger->logger("Adding restart callback!");
|
||||||
|
$logger = $this->logger;
|
||||||
$id = Shutdown::addCallback(static function () use (&$logger) {
|
$id = Shutdown::addCallback(static function () use (&$logger) {
|
||||||
$address = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME'];
|
$address = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME'];
|
||||||
$port = $_SERVER['SERVER_PORT'];
|
$port = $_SERVER['SERVER_PORT'];
|
||||||
|
@ -6,16 +6,17 @@ function ___install_madeline()
|
|||||||
{
|
{
|
||||||
if (\count(\debug_backtrace(0)) === 1) {
|
if (\count(\debug_backtrace(0)) === 1) {
|
||||||
if (isset($GLOBALS['argv']) && !empty($GLOBALS['argv'])) {
|
if (isset($GLOBALS['argv']) && !empty($GLOBALS['argv'])) {
|
||||||
$arguments = $GLOBALS['argv'];
|
$arguments = array_slice($GLOBALS['argv'], 1);
|
||||||
} elseif (isset($_GET['argv']) && !empty($_GET['argv'])) {
|
} elseif (isset($_GET['argv']) && !empty($_GET['argv'])) {
|
||||||
$arguments = $_GET['argv'];
|
$arguments = $_GET['argv'];
|
||||||
} else {
|
} else {
|
||||||
$arguments = [];
|
$arguments = [];
|
||||||
}
|
}
|
||||||
if (isset($arguments[1]) && $arguments[1] === 'madeline-ipc') {
|
if (count($arguments) >= 2) {
|
||||||
\define(\MADELINE_WORKER_START::class, $arguments[2]);
|
\define(\MADELINE_WORKER_TYPE::class, array_shift($arguments));
|
||||||
|
\define(\MADELINE_WORKER_ARGS::class, $arguments);
|
||||||
} else {
|
} 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;
|
$old = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user