1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 13:34:42 +01:00

Refactoring and cleaning up
Some checks failed
ci/woodpecker/push/build Pipeline failed

This commit is contained in:
Daniil Gentili 2023-01-08 19:02:49 +01:00
parent e86a6e3138
commit a6e473a8ae
12 changed files with 59 additions and 38 deletions

View File

@ -533,11 +533,11 @@ class DataCenterConnection implements JsonSerializable
*/
public function disconnect(): void
{
$this->connectionsDeferred = new DeferredFuture();
$this->connectionsPromise = $this->connectionsDeferred->getFuture();
if (!isset($this->ctx)) {
return;
}
$this->connectionsDeferred = new DeferredFuture();
$this->connectionsPromise = $this->connectionsDeferred->getFuture();
$this->API->logger->logger("Disconnecting from shared DC {$this->datacenter}");
if ($this->robinLoop) {
$this->robinLoop->signal(true);

View File

@ -199,9 +199,9 @@ class Client extends ClientAbstract
*
* If the $aargs['noResponse'] is true, will not wait for a response.
*
* @param string $method Method name
* @param array $args Arguments
* @param array $aargs Additional arguments
* @param string $method Method name
* @param array|(callable(): array) $args Arguments
* @param array $aargs Additional arguments
*/
public function methodCallAsyncRead(string $method, array $args, array $aargs)
{

View File

@ -93,13 +93,13 @@ class CheckLoop extends ResumableSignalLoop
break;
}
$API->logger->logger("Message $message not received by server, resending...", Logger::ERROR);
$connection->methodRecall('watcherId', ['message_id' => $message_id, 'postpone' => true]);
$connection->methodRecall(['message_id' => $message_id, 'postpone' => true]);
break;
case 4:
if ($chr & 32) {
if ($message->getSent() + $timeoutResend < \time()) {
$API->logger->logger("Message $message received by server and is being processed for way too long, resending request...", Logger::ERROR);
$connection->methodRecall('', ['message_id' => $message_id, 'postpone' => true]);
$connection->methodRecall(['message_id' => $message_id, 'postpone' => true]);
} else {
$API->logger->logger("Message $message received by server and is being processed, waiting...", Logger::ERROR);
}
@ -141,7 +141,7 @@ class CheckLoop extends ResumableSignalLoop
&& $message->isUnencrypted()
) {
$API->logger->logger("Still missing $message on DC $datacenter, resending", Logger::ERROR);
$connection->methodRecall('', ['message_id' => $message->getMsgId(), 'postpone' => true]);
$connection->methodRecall(['message_id' => $message->getMsgId(), 'postpone' => true]);
}
}
$connection->flush();

View File

@ -1071,7 +1071,7 @@ class MTProto implements TLCallback, LoggerGetter
}
$this->logger->logger(Lang::$current_lang['serialization_ofd'], Logger::WARNING);
foreach ($this->datacenter->getDataCenterConnections() as $dc_id => $socket) {
if ($this->authorized === self::LOGGED_IN && \strpos($dc_id, '_') === false && $socket->hasPermAuthKey() && $socket->hasTempAuthKey()) {
if ($this->authorized === self::LOGGED_IN && is_int($dc_id) && $socket->hasPermAuthKey() && $socket->hasTempAuthKey()) {
$socket->bind();
$socket->authorized(true);
}
@ -1170,6 +1170,15 @@ class MTProto implements TLCallback, LoggerGetter
if (!isset($this->snitch)) {
$this->snitch = new Snitch;
}
// Re-set TL closures
$callbacks = [$this];
if ($this->settings->getDb()->getEnableFileReferenceDb()) {
$callbacks []= $this->referenceDatabase;
}
if (!($this->authorization['user']['bot'] ?? false) && $this->settings->getDb()->getEnableMinDb()) {
$callbacks[] = $this->minDatabase;
}
$this->TL->updateCallbacks($callbacks);
// Convert old array settings to new settings object
if (\is_array($this->settings)) {
if (($this->settings['updates']['callback'] ?? '') === 'getUpdatesUpdateHandler') {

View File

@ -89,8 +89,9 @@ class OutgoingMessage extends Message
/**
* Message body.
*
* @var array|(callable(): array)|null
*/
private ?array $body = null;
private $body = null;
/**
* Serialized body.
@ -139,13 +140,13 @@ class OutgoingMessage extends Message
/**
* Create outgoing message.
*
* @param array $body Body
* @param string $constructor Constructor name
* @param string $type Constructor type
* @param boolean $method Is this a method?
* @param boolean $unencrypted Is this an unencrypted message?
* @param array|callable(): array $body Body
* @param string $constructor Constructor name
* @param string $type Constructor type
* @param boolean $method Is this a method?
* @param boolean $unencrypted Is this an unencrypted message?
*/
public function __construct(array $body, string $constructor, string $type, bool $method, bool $unencrypted)
public function __construct(array|callable $body, string $constructor, string $type, bool $method, bool $unencrypted)
{
$this->body = $body;
$this->constructor = $constructor;
@ -230,7 +231,7 @@ class OutgoingMessage extends Message
*/
public function getBody()
{
return $this->body;
return is_callable($this->body) ? ($this->body)() : $this->body;
}
/**

View File

@ -41,7 +41,7 @@ trait CallHandler
* @param string $watcherId Watcher ID for defer
* @param array $args Args
*/
public function methodRecall(string $watcherId, array $args): void
public function methodRecall(array $args): void
{
$message_id = $args['message_id'];
$postpone = $args['postpone'] ?? false;
@ -91,7 +91,7 @@ trait CallHandler
* If the $aargs['noResponse'] is true, will not wait for a response.
*
* @param string $method Method name
* @param array $args Arguments
* @param array|(callable(): array) $args Arguments
* @param array $aargs Additional arguments
*/
public function methodCallAsyncRead(string $method, array $args = [], array $aargs = ['msg_id' => null])
@ -111,7 +111,7 @@ trait CallHandler
* Call method and make sure it is asynchronously sent (generator).
*
* @param string $method Method name
* @param array $args Arguments
* @param array|(callable(): array) $args Arguments
* @param array $aargs Additional arguments
* @return list<Future>|Future
*/

View File

@ -64,7 +64,7 @@ trait Reliable
}
if ($ok) {
foreach ($content['msg_ids'] as $msg_id) {
$this->methodRecall('', ['message_id' => $msg_id, 'postpone' => true]);
$this->methodRecall(['message_id' => $msg_id, 'postpone' => true]);
}
} else {
$this->sendMsgsStateInfo($content['msg_ids'], $current_msg_id);

View File

@ -207,12 +207,12 @@ trait ResponseHandler
switch ($response['error_code']) {
case 48:
$this->shared->getTempAuthKey()->setServerSalt($response['new_server_salt']);
$this->methodRecall('', ['message_id' => $requestId, 'postpone' => true]);
$this->methodRecall(['message_id' => $requestId, 'postpone' => true]);
return;
case 20:
$request->setMsgId(null);
$request->setSeqNo(null);
$this->methodRecall('', ['message_id' => $requestId, 'postpone' => true]);
$this->methodRecall(['message_id' => $requestId, 'postpone' => true]);
return;
case 16:
case 17:
@ -222,7 +222,7 @@ trait ResponseHandler
$this->shared->setTempAuthKey(null);
async(function () use ($requestId): void {
$this->API->initAuthorization();
$this->methodRecall('', ['message_id' => $requestId]);
$this->methodRecall(['message_id' => $requestId]);
});
return;
}
@ -284,7 +284,7 @@ trait ResponseHandler
$request->setRefreshReferences(true);
$request->setMsgId(null);
$request->setSeqNo(null);
$this->methodRecall('', ['message_id' => $msgId, 'postpone' => true]);
$this->methodRecall(['message_id' => $msgId, 'postpone' => true]);
return null;
}
@ -293,7 +293,7 @@ trait ResponseHandler
case -500:
if ($response['error_message'] === 'MSG_WAIT_FAILED') {
$this->call_queue[$request->getQueueId()] = [];
$this->methodRecall('', ['message_id' => $request->getMsgId(), 'postpone' => true]);
$this->methodRecall(['message_id' => $request->getMsgId(), 'postpone' => true]);
return null;
}
if (\in_array($response['error_message'], ['MSGID_DECREASE_RETRY', 'HISTORY_GET_FAILED', 'RPC_CONNECT_FAILED', 'RPC_CALL_FAIL', 'PERSISTENT_TIMESTAMP_OUTDATED', 'RPC_MCGET_FAIL', 'no workers running', 'No workers running'])) {
@ -310,7 +310,7 @@ trait ResponseHandler
$this->API->settings->setDefaultDc($this->API->authorized_dc = $this->API->datacenter->currentDatacenter);
}
EventLoop::defer(fn () => $this->methodRecall(['message_id' => $request->getMsgId(), 'datacenter' => $datacenter]));
//$this->API->methodRecall('', ['message_id' => $requestId, 'datacenter' => $datacenter, 'postpone' => true]);
//$this->API->methodRecall(['message_id' => $requestId, 'datacenter' => $datacenter, 'postpone' => true]);
return null;
case 401:
switch ($response['error_message']) {
@ -356,7 +356,7 @@ trait ResponseHandler
}
async(function () use ($request): void {
$this->API->initAuthorization();
$this->methodRecall('', ['message_id' => $request->getMsgId()]);
$this->methodRecall(['message_id' => $request->getMsgId()]);
});
return null;
case 'AUTH_KEY_PERM_EMPTY':
@ -364,7 +364,7 @@ trait ResponseHandler
$this->shared->setTempAuthKey(null);
async(function () use ($request): void {
$this->API->initAuthorization();
$this->methodRecall('', ['message_id' => $request->getMsgId()]);
$this->methodRecall(['message_id' => $request->getMsgId()]);
});
return null;
}

View File

@ -35,7 +35,7 @@ trait CallHandler
* If the $aargs['noResponse'] is true, will not wait for a response.
*
* @param string $method Method name
* @param array $args Arguments
* @param array|(callable(): array) $args Arguments
* @param array $aargs Additional arguments
*/
public function methodCallAsyncRead(string $method, array $args = [], array $aargs = ['msg_id' => null])
@ -46,7 +46,7 @@ trait CallHandler
* Call method and make sure it is asynchronously sent.
*
* @param string $method Method name
* @param array $args Arguments
* @param array|(callable(): array) $args Arguments
* @param array $aargs Additional arguments
*/
public function methodCallAsyncWrite(string $method, array $args = [], array $aargs = ['msg_id' => null])

View File

@ -167,7 +167,7 @@ trait Files
$exception = null;
$start = \microtime(true);
while ($part_num < $part_total_num) {
$resa = $callable($part_num);
$resa = fn () => $callable($part_num);
$writePromise = $this->methodCallAsyncWrite($method, $resa, ['heavy' => true, 'file' => true, 'datacenter' => &$datacenter]);
if (!$seekable) {
$writePromise->await();

View File

@ -89,9 +89,20 @@ final class TL
/**
* API instance.
*
*/
private MTProto $API;
public function __sleep()
{
return [
'secretLayer',
'constructors',
'methods',
'tdConstructors',
'tdMethods',
'tdDescriptions',
'API',
];
}
/**
* Constructor function.
*
@ -362,10 +373,10 @@ final class TL
$callbacks
));
$this->beforeConstructorSerialization = [...\array_map(
$this->beforeConstructorSerialization = array_merge(...\array_map(
fn (TLCallback $t) => $t->getConstructorBeforeSerializationCallbacks(),
$callbacks
)];
));
$this->beforeConstructorDeserialization = \array_merge_recursive(...\array_map(
fn (TLCallback $t) => $t->getConstructorBeforeDeserializationCallbacks(),
$callbacks
@ -375,10 +386,10 @@ final class TL
$callbacks
));
$this->typeMismatch = [...\array_map(
$this->typeMismatch = array_merge(...\array_map(
fn (TLCallback $t) => $t->getTypeMismatchCallbacks(),
$callbacks
)];
));
}
/**
* Deserialize bool.

View File

@ -95,7 +95,7 @@ trait Events
}
}
$this->setReportPeers(Tools::call($this->event_handler_instance->getReportPeers())->await());
async(fn () => $this->event_handler_instance->startInternal());
async($this->event_handler_instance->startInternal(...));
$this->updateHandler = [$this, 'eventUpdateHandler'];
$this->startUpdateSystem();
}