From 93bd9073e16a22efa2dca613511ad025b7297e35 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 14 Mar 2020 17:29:49 +0100 Subject: [PATCH] Bugfix --- composer.json | 5 +- docs | 2 +- examples/index.php | 2 +- .../Loop/SignalLoopInterface.php | 2 +- src/danog/MadelineProto/Magic.php | 19 ++-- src/danog/MadelineProto/Server.php | 102 ------------------ 6 files changed, 17 insertions(+), 115 deletions(-) delete mode 100644 src/danog/MadelineProto/Server.php diff --git a/composer.json b/composer.json index ccff9c0d..601e19ec 100644 --- a/composer.json +++ b/composer.json @@ -25,8 +25,8 @@ "amphp/http-client": "^4", "amphp/socket": "^1", "amphp/dns": "^1", - "amphp/file": "^1", "amphp/byte-stream": "^1", + "amphp/file": "^1", "danog/dns-over-https": "^0.2", "amphp/http-client-cookies": "^1", "danog/tg-file-decoder": "^0.1", @@ -49,7 +49,8 @@ "ext-ctype": "*", "danog/7to70": "^1", "danog/7to5": "^1", - "vimeo/psalm": "dev-master" + "vimeo/psalm": "dev-master", + "phpstan/phpstan": "^0.12.14" }, "suggest": { "ext-libtgvoip": "Install the php-libtgvoip extension to make phone calls (https://github.com/danog/php-libtgvoip)" diff --git a/docs b/docs index 1e196739..eda40443 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 1e1967391fe643b6ecc0e6f22a800386cbd22e5d +Subproject commit eda40443bf6215bc8eff7842a5eaaae70b4c4ee3 diff --git a/examples/index.php b/examples/index.php index 5be305ad..88947ebf 100644 --- a/examples/index.php +++ b/examples/index.php @@ -20,7 +20,7 @@ require '../vendor/autoload.php'; -$MadelineProto = new \danog\MadelineProto\API('session.madeline'); +$MadelineProto = new \danog\MadelineProto\API('index.madeline'); $me = $MadelineProto->start(); $me = $MadelineProto->getSelf(); diff --git a/src/danog/MadelineProto/Loop/SignalLoopInterface.php b/src/danog/MadelineProto/Loop/SignalLoopInterface.php index ed21c33a..28dc7daf 100644 --- a/src/danog/MadelineProto/Loop/SignalLoopInterface.php +++ b/src/danog/MadelineProto/Loop/SignalLoopInterface.php @@ -39,7 +39,7 @@ interface SignalLoopInterface extends LoopInterface /** * Send a signal to the the loop. * - * @param \Throwable|any $data Signal to send + * @param \Throwable|mixed $data Signal to send * * @return void */ diff --git a/src/danog/MadelineProto/Magic.php b/src/danog/MadelineProto/Magic.php index 48759d21..22a720f7 100644 --- a/src/danog/MadelineProto/Magic.php +++ b/src/danog/MadelineProto/Magic.php @@ -310,14 +310,17 @@ class Magic // Even an empty handler is enough to catch ctrl+c if (\defined('SIGINT')) { //if (function_exists('pcntl_async_signals')) pcntl_async_signals(true); - Loop::unreference(Loop::onSignal(SIGINT, static function () { - Logger::log('Got sigint', Logger::FATAL_ERROR); - Magic::shutdown(1); - })); - Loop::unreference(Loop::onSignal(SIGTERM, static function () { - Logger::log('Got sigterm', Logger::FATAL_ERROR); - Magic::shutdown(1); - })); + try { + Loop::unreference(Loop::onSignal(SIGINT, static function () { + Logger::log('Got sigint', Logger::FATAL_ERROR); + Magic::shutdown(1); + })); + Loop::unreference(Loop::onSignal(SIGTERM, static function () { + Logger::log('Got sigterm', Logger::FATAL_ERROR); + Magic::shutdown(1); + })); + } catch (\Throwable $e) { + } } /*if (!self::$altervista && !self::$zerowebhost) { $DohConfig = new DoHConfig( diff --git a/src/danog/MadelineProto/Server.php b/src/danog/MadelineProto/Server.php deleted file mode 100644 index 3cd305e9..00000000 --- a/src/danog/MadelineProto/Server.php +++ /dev/null @@ -1,102 +0,0 @@ -. - * - * @author Daniil Gentili - * @copyright 2016-2020 Daniil Gentili - * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 - * - * @link https://docs.madelineproto.xyz MadelineProto documentation - */ - -namespace danog\MadelineProto; - -/* - * Socket server for multi-language API - */ -class Server -{ - private $settings; - private $pids = []; - private $mypid; - public function __construct($settings) - { - \set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']); - \danog\MadelineProto\Logger::constructor(3); - if (!\extension_loaded('sockets')) { - throw new Exception(['extension', 'sockets']); - } - if (!\extension_loaded('pcntl')) { - throw new Exception(['extension', 'pcntl']); - } - $this->settings = $settings; - $this->mypid = \getmypid(); - } - public function start() - { - \pcntl_signal(SIGTERM, [$this, 'sigHandler']); - \pcntl_signal(SIGINT, [$this, 'sigHandler']); - \pcntl_signal(SIGCHLD, [$this, 'sigHandler']); - $this->sock = new \Socket($this->settings['type'], SOCK_STREAM, $this->settings['protocol']); - $this->sock->bind($this->settings['address'], $this->settings['port']); - $this->sock->listen(); - $this->sock->setBlocking(true); - $timeout = 2; - $this->sock->setOption(\SOL_SOCKET, \SO_RCVTIMEO, $timeout); - $this->sock->setOption(\SOL_SOCKET, \SO_SNDTIMEO, $timeout); - \danog\MadelineProto\Logger::log('Server started! Listening on ' . $this->settings['address'] . ':' . $this->settings['port']); - while (true) { - \pcntl_signal_dispatch(); - try { - if ($sock = $this->sock->accept()) { - $this->handle($sock); - } - } catch (\danog\MadelineProto\Exception $e) { - } - } - } - private function handle($socket) - { - $pid = \pcntl_fork(); - if ($pid == -1) { - die('could not fork'); - } elseif ($pid) { - return $this->pids[] = $pid; - } - $handler = new $this->settings['handler']($socket, $this->settings['extra'], null, null, null, null, null); - $handler->loop(); - die; - } - public function __destruct() - { - if ($this->mypid === \getmypid()) { - \danog\MadelineProto\Logger::log('Shutting main process ' . $this->mypid . ' down'); - unset($this->sock); - foreach ($this->pids as $pid) { - \danog\MadelineProto\Logger::log("Waiting for {$pid}"); - \pcntl_wait($pid); - } - \danog\MadelineProto\Logger::log('Done, closing main process'); - return; - } - } - public function sigHandler($sig) - { - switch ($sig) { - case SIGTERM: - case SIGINT: - exit; - case SIGCHLD: - \pcntl_waitpid(-1, $status); - break; - } - } -}