From bb18b863104630dc8167db6b9906932cd2634ab4 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 1 Oct 2023 21:46:16 +0200 Subject: [PATCH] Improve cancellation support --- docs | 2 +- examples/magnaluna | 2 +- src/MTProtoSession/CallHandler.php | 4 +++- src/MTProtoTools/Files.php | 6 +++++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docs b/docs index b3251b13d..e5574eae3 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit b3251b13da49ce7c88cd263d1b4859b50b428546 +Subproject commit e5574eae36016400e3e56aad4a71b03e59458a2f diff --git a/examples/magnaluna b/examples/magnaluna index 32214a5ad..453cf521d 160000 --- a/examples/magnaluna +++ b/examples/magnaluna @@ -1 +1 @@ -Subproject commit 32214a5ad3aa7ffdf4cd7a1592b98990ca5a559b +Subproject commit 453cf521d71f99f21f84fff96651c744b0f99a34 diff --git a/src/MTProtoSession/CallHandler.php b/src/MTProtoSession/CallHandler.php index 3c4e9ac9e..6397fb8bf 100644 --- a/src/MTProtoSession/CallHandler.php +++ b/src/MTProtoSession/CallHandler.php @@ -115,6 +115,8 @@ trait CallHandler */ public function methodCallAsyncWrite(string $method, array|callable $args = [], array $aargs = ['msg_id' => null]): WrappedFuture { + $cancellation = $aargs['cancellation'] ?? null; + $cancellation?->throwIfRequested(); if (\is_array($args) && isset($args['id']['_']) && isset($args['id']['dc_id']) && ($args['id']['_'] === 'inputBotInlineMessageID' || $args['id']['_'] === 'inputBotInlineMessageID64') && $this->datacenter != $args['id']['dc_id']) { $aargs['datacenter'] = $args['id']['dc_id']; return $this->API->methodCallAsyncWrite($method, $args, $aargs); @@ -185,7 +187,7 @@ trait CallHandler queueId: $aargs['queue'] ?? null, floodWaitLimit: $aargs['FloodWaitLimit'] ?? null, resultDeferred: $response, - cancellation: $aargs['cancellation'] ?? null, + cancellation: $cancellation, ); if (isset($aargs['msg_id'])) { $message->setMsgId($aargs['msg_id']); diff --git a/src/MTProtoTools/Files.php b/src/MTProtoTools/Files.php index caf1cfe07..aaa16cd73 100644 --- a/src/MTProtoTools/Files.php +++ b/src/MTProtoTools/Files.php @@ -1086,6 +1086,7 @@ trait Files $previous_promise = true; $promises = []; foreach ($params as $key => $param) { + $cancellation?->throwIfRequested(); $param['previous_promise'] = $previous_promise; $previous_promise = async($this->downloadPart(...), $messageMedia, $cdn, $datacenter, $old_dc, $ige, $cb, $param, $callable, $seekable, $cancellation); $previous_promise->map(static function (int $res) use (&$size): void { @@ -1147,6 +1148,7 @@ trait Files } //$x = 0; while (true) { + $cancellation?->throwIfRequested(); try { $res = $this->methodCallAsyncRead( $cdn ? 'upload.getCdnFile' : 'upload.getFile', @@ -1167,6 +1169,7 @@ trait Files } } } + $cancellation?->throwIfRequested(); if ($res['_'] === 'upload.fileCdnRedirect') { $cdn = true; @@ -1204,8 +1207,9 @@ trait Files $datacenter = 0; } while ($cdn === false && $res['type']['_'] === 'storage.fileUnknown' && $res['bytes'] === '' && $this->datacenter->has(++$datacenter)) { - $res = $this->methodCallAsyncRead('upload.getFile', $basic_param + $offset, ['heavy' => true, 'FloodWaitLimit' => 0, 'datacenter' => $datacenter]); + $res = $this->methodCallAsyncRead('upload.getFile', $basic_param + $offset, ['heavy' => true, 'FloodWaitLimit' => 0, 'datacenter' => $datacenter, 'cancellation' => $cancellation]); } + $cancellation?->throwIfRequested(); $res['bytes'] = (string) $res['bytes']; if ($res['bytes'] === '') { return 0;