. * * @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\Loop\Connection; use danog\Loop\ResumableSignalLoop; /** * Ping loop. * * @author Daniil Gentili */ class PingLoop extends ResumableSignalLoop { use Common; /** * Main loop. * * @return \Generator */ public function loop(): \Generator { $API = $this->API; $datacenter = $this->datacenter; $connection = $this->connection; $shared = $this->datacenterConnection; $timeout = $shared->getSettings()['timeout']; $timeoutMs = $timeout * 1000; while (true) { while (!$shared->hasTempAuthKey()) { if (yield $this->waitSignal($this->pause())) { return; } } if (yield $this->waitSignal($this->pause($timeoutMs))) { return; } if (\time() - $connection->getLastChunk() >= $timeout) { $API->logger->logger("Ping DC {$datacenter}"); try { yield from $connection->methodCallAsyncRead('ping', ['ping_id' => \random_bytes(8)]); } catch (\Throwable $e) { $API->logger->logger("Error while pinging DC {$datacenter}"); $API->logger->logger((string) $e); } } } } /** * Get loop name. * * @return string */ public function __toString(): string { return "Ping loop in DC {$this->datacenter}"; } }