2022-12-30 21:54:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-12-14 16:47:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* APIFactory module.
|
|
|
|
*
|
|
|
|
* This file is part of MadelineProto.
|
|
|
|
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU Affero General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License along with MadelineProto.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author Daniil Gentili <daniil@daniil.it>
|
2023-01-04 12:43:01 +01:00
|
|
|
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
|
2019-12-14 16:47:04 +01:00
|
|
|
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
|
|
|
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace danog\MadelineProto;
|
|
|
|
|
2023-01-26 19:52:35 +01:00
|
|
|
use Amp\Future\UnhandledFutureError;
|
|
|
|
use Amp\SignalException;
|
|
|
|
use Revolt\EventLoop;
|
2019-12-14 16:47:04 +01:00
|
|
|
|
2023-01-27 14:20:47 +01:00
|
|
|
abstract class AbstractAPI extends InternalDoc
|
2019-12-14 16:47:04 +01:00
|
|
|
{
|
2023-01-26 19:52:35 +01:00
|
|
|
/**
|
|
|
|
* Start MadelineProto and the event handler (enables async).
|
|
|
|
*
|
|
|
|
* Also initializes error reporting, catching and reporting all errors surfacing from the event loop.
|
|
|
|
*
|
|
|
|
* @param string $eventHandler Event handler class name
|
|
|
|
*/
|
|
|
|
protected function startAndLoopInternal(string $eventHandler): void
|
|
|
|
{
|
|
|
|
$started = false;
|
|
|
|
$errors = [];
|
|
|
|
$prev = EventLoop::getErrorHandler();
|
|
|
|
EventLoop::setErrorHandler(
|
|
|
|
$cb = function (\Throwable $e) use (&$errors, &$started): void {
|
|
|
|
if ($e instanceof UnhandledFutureError) {
|
|
|
|
$e = $e->getPrevious();
|
|
|
|
}
|
|
|
|
if ($e instanceof SecurityException || $e instanceof SignalException) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
if (\str_starts_with($e->getMessage(), 'Could not connect to DC ')) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
$t = \time();
|
|
|
|
$errors = [$t => $errors[$t] ?? 0];
|
|
|
|
$errors[$t]++;
|
|
|
|
if ($errors[$t] > 10 && (!$this->wrapper->getAPI()->isInited() || !$started)) {
|
|
|
|
$this->wrapper->logger('More than 10 errors in a second and not inited, exiting!', Logger::FATAL_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
echo $e;
|
|
|
|
$this->wrapper->logger((string) $e, Logger::FATAL_ERROR);
|
|
|
|
$this->report("Surfaced: $e");
|
|
|
|
}
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
$this->startAndLoopLogic($eventHandler, $started);
|
|
|
|
} finally {
|
|
|
|
if (EventLoop::getErrorHandler() === $cb) {
|
|
|
|
EventLoop::setErrorHandler($prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-27 14:20:47 +01:00
|
|
|
abstract protected function reconnectFull(): bool;
|
|
|
|
|
2023-01-26 23:59:10 +01:00
|
|
|
protected function startAndLoopLogic(string $eventHandler, bool &$started): void
|
2023-01-26 19:52:35 +01:00
|
|
|
{
|
|
|
|
$this->start();
|
|
|
|
if (!$this->reconnectFull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->wrapper->getAPI()->setEventHandler($eventHandler);
|
|
|
|
$started = true;
|
2023-01-28 16:39:03 +01:00
|
|
|
/** @psalm-suppress TooFewArguments Always MTProto here */
|
2023-01-26 19:52:35 +01:00
|
|
|
$this->wrapper->getAPI()->loop();
|
|
|
|
}
|
2020-09-28 11:56:14 +02:00
|
|
|
/**
|
|
|
|
* Sleep function.
|
|
|
|
*/
|
2022-12-30 19:21:36 +01:00
|
|
|
public function __sleep(): array
|
2020-09-28 11:56:14 +02:00
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2019-12-14 16:47:04 +01:00
|
|
|
}
|