From c09488aa1251ecda9121e43547c7b48e88aeb67d Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 13 Jun 2019 19:46:58 +0200 Subject: [PATCH] Minor fixes --- src/danog/MadelineProto/API.php | 5 +++- src/danog/MadelineProto/Connection.php | 3 +++ .../MadelineProto/Loop/Update/FeedLoop.php | 24 +++++-------------- .../MadelineProto/Loop/Update/SeqLoop.php | 18 ++------------ .../MadelineProto/Loop/Update/UpdateLoop.php | 19 ++------------- src/danog/MadelineProto/MTProto.php | 19 +++++++++++---- src/danog/MadelineProto/Magic.php | 4 ++-- src/danog/MadelineProto/Wrappers/Loop.php | 6 ++--- 8 files changed, 36 insertions(+), 62 deletions(-) diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index 0e2c0372..251a6954 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -120,7 +120,10 @@ class API extends APIFactory } Logger::log((string) $e, Logger::ERROR); - if (!$changed) throw $e; + if (!$changed) { + throw $e; + } + $unserialized = \danog\Serialization::unserialize($tounserialize); } catch (\Throwable $e) { Logger::log((string) $e, Logger::ERROR); diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index e1510614..a8743d9d 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -214,6 +214,9 @@ class Connection $this->API->logger->logger("Reconnecting DC {$this->datacenter}"); $this->disconnect(); yield $this->API->datacenter->dcConnectAsync($this->ctx->getDc()); + if ($this->API->hasAllAuth() && !$this->hasPendingCalls()) { + yield $this->API->method_call_async_read('ping', ['ping_id' => $this->random_int()], ['datacenter' => $this->datacenter]); + } } public function hasPendingCalls() diff --git a/src/danog/MadelineProto/Loop/Update/FeedLoop.php b/src/danog/MadelineProto/Loop/Update/FeedLoop.php index bccbbc03..3d2bf69f 100644 --- a/src/danog/MadelineProto/Loop/Update/FeedLoop.php +++ b/src/danog/MadelineProto/Loop/Update/FeedLoop.php @@ -49,7 +49,7 @@ class FeedLoop extends ResumableSignalLoop return false; } - while (!$this->API->settings['updates']['handle_updates'] || !$this->has_all_auth()) { + while (!$this->API->settings['updates']['handle_updates'] || !$API->hasAllAuth()) { if (yield $this->waitSignal($this->pause())) { return; } @@ -57,7 +57,7 @@ class FeedLoop extends ResumableSignalLoop $this->state = $this->channelId === false ? (yield $API->load_update_state_async()) : $API->loadChannelState($this->channelId); while (true) { - while (!$this->API->settings['updates']['handle_updates'] || !$this->has_all_auth()) { + while (!$this->API->settings['updates']['handle_updates'] || !$API->hasAllAuth()) { if (yield $this->waitSignal($this->pause())) { return; } @@ -220,7 +220,10 @@ class FeedLoop extends ResumableSignalLoop $this->API->logger->logger("Not enough data: for message update $log, getting difference...", \danog\MadelineProto\Logger::VERBOSE); $update = ['_' => 'updateChannelTooLong']; - if ($channelId && $to) $channelId = false; + if ($channelId && $to) { + $channelId = false; + } + } break; default: @@ -264,21 +267,6 @@ class FeedLoop extends ResumableSignalLoop } } - public function has_all_auth() - { - if ($this->API->isInitingAuthorization()) { - return false; - } - - foreach ($this->API->datacenter->sockets as $dc) { - if (!$dc->authorized || $dc->temp_auth_key === null) { - return false; - } - } - - return true; - } - public function __toString(): string { return !$this->channelId ? 'update feed loop generic' : "update feed loop channel {$this->channelId}"; diff --git a/src/danog/MadelineProto/Loop/Update/SeqLoop.php b/src/danog/MadelineProto/Loop/Update/SeqLoop.php index d5c972ec..990f2ebf 100644 --- a/src/danog/MadelineProto/Loop/Update/SeqLoop.php +++ b/src/danog/MadelineProto/Loop/Update/SeqLoop.php @@ -46,7 +46,7 @@ class SeqLoop extends ResumableSignalLoop return false; } - while (!$this->API->settings['updates']['handle_updates'] || !$this->has_all_auth()) { + while (!$this->API->settings['updates']['handle_updates'] || !$API->hasAllAuth()) { if (yield $this->waitSignal($this->pause())) { return; } @@ -54,7 +54,7 @@ class SeqLoop extends ResumableSignalLoop $this->state = yield $API->load_update_state_async(); while (true) { - while (!$this->API->settings['updates']['handle_updates'] || !$this->has_all_auth()) { + while (!$this->API->settings['updates']['handle_updates'] || !$API->hasAllAuth()) { if (yield $this->waitSignal($this->pause())) { return; } @@ -134,20 +134,6 @@ class SeqLoop extends ResumableSignalLoop $this->pendingWakeups += $wakeups; } - public function has_all_auth() - { - if ($this->API->isInitingAuthorization()) { - return false; - } - - foreach ($this->API->datacenter->sockets as $dc) { - if (!$dc->authorized || $dc->temp_auth_key === null) { - return false; - } - } - - return true; - } public function __toString(): string { diff --git a/src/danog/MadelineProto/Loop/Update/UpdateLoop.php b/src/danog/MadelineProto/Loop/Update/UpdateLoop.php index dadedfe4..6d6f04b3 100644 --- a/src/danog/MadelineProto/Loop/Update/UpdateLoop.php +++ b/src/danog/MadelineProto/Loop/Update/UpdateLoop.php @@ -47,7 +47,7 @@ class UpdateLoop extends ResumableSignalLoop $API = $this->API; $feeder = $this->feeder = $API->feeders[$this->channelId]; - while (!$API->settings['updates']['handle_updates'] || !$this->has_all_auth()) { + while (!$API->settings['updates']['handle_updates'] || !$API->hasAllAuth()) { if (yield $this->waitSignal($this->pause())) { $API->logger->logger("Exiting $this due to signal"); @@ -59,7 +59,7 @@ class UpdateLoop extends ResumableSignalLoop $timeout = $API->settings['updates']['getdifference_interval']; $first = true; while (true) { - while (!$API->settings['updates']['handle_updates'] || !$this->has_all_auth()) { + while (!$API->settings['updates']['handle_updates'] || !$API->hasAllAuth()) { if (yield $this->waitSignal($this->pause())) { $API->logger->logger("Exiting $this due to signal"); @@ -206,21 +206,6 @@ class UpdateLoop extends ResumableSignalLoop $this->toPts = $toPts; } - public function has_all_auth() - { - if ($this->API->isInitingAuthorization()) { - return false; - } - - foreach ($this->API->datacenter->sockets as $dc) { - if (!$dc->authorized || $dc->temp_auth_key === null) { - return false; - } - } - - return true; - } - public function __toString(): string { return !$this->channelId ? 'getUpdate loop generic' : "getUpdate loop channel {$this->channelId}"; diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 8a4de981..cb992073 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -264,6 +264,20 @@ class MTProto extends AsyncConstruct implements TLCallback return $this->datacenter->fileGetContents($url); } + public function hasAllAuth() + { + if ($this->isInitingAuthorization()) { + return false; + } + + foreach ($this->datacenter->sockets as $dc) { + if (!$dc->authorized || $dc->temp_auth_key === null) { + return false; + } + } + + return true; + } public function __wakeup() { $backtrace = debug_backtrace(0, 3); @@ -885,11 +899,6 @@ class MTProto extends AsyncConstruct implements TLCallback return in_array($this->datacenter->sockets[$datacenter]->getCtx()->getStreamName(), [HttpStream::getName(), HttpsStream::getName()]); } - public function close_and_reopen($datacenter) - { - $this->wait($this->datacenter->sockets[$datacenter]->reconnect()); - } - // Connects to all datacenters and if necessary creates authorization keys, binds them and writes client info public function connect_to_all_dcs_async(): \Generator { diff --git a/src/danog/MadelineProto/Magic.php b/src/danog/MadelineProto/Magic.php index 48cee5db..d898737b 100644 --- a/src/danog/MadelineProto/Magic.php +++ b/src/danog/MadelineProto/Magic.php @@ -144,11 +144,11 @@ class Magic Logger::log('Got sigint', Logger::FATAL_ERROR); die(); }); - Loop::onSignal(SIGTERM, static function () { + /*Loop::onSignal(SIGTERM, static function () { Logger::log('Got sigterm', Logger::FATAL_ERROR); Loop::stop(); die(); - }); + });*/ } $DohConfig = new DoHConfig( [ diff --git a/src/danog/MadelineProto/Wrappers/Loop.php b/src/danog/MadelineProto/Wrappers/Loop.php index 7d17975b..bf417830 100644 --- a/src/danog/MadelineProto/Wrappers/Loop.php +++ b/src/danog/MadelineProto/Wrappers/Loop.php @@ -66,17 +66,17 @@ trait Loop $this->logger->logger($needs_restart ? 'Will self-restart' : 'Will not self-restart'); $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - $lockfile = dirname(end($backtrace)['file']).'/bot.lock'; + $lockfile = dirname(end($backtrace)['file']).'/bot'.$this->authorization['user']['id'].'.lock'; unset($backtrace); $try_locking = true; if (!file_exists($lockfile)) { touch($lockfile); - $lock = fopen('bot.lock', 'r+'); + $lock = fopen($lockfile, 'r+'); } elseif (isset($GLOBALS['lock'])) { $try_locking = false; $lock = $GLOBALS['lock']; } else { - $lock = fopen('bot.lock', 'r+'); + $lock = fopen($lockfile, 'r+'); } if ($try_locking) { $try = 1;