Skip to content

Commit

Permalink
Always pass default cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jun 26, 2024
1 parent 19f3092 commit 658d7ad
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 41 deletions.
2 changes: 1 addition & 1 deletion docs
11 changes: 11 additions & 0 deletions src/APIWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace danog\MadelineProto;

use Amp\Cancellation;
use Amp\TimeoutCancellation;
use danog\MadelineProto\Ipc\Client;

final class APIWrapper
Expand Down Expand Up @@ -74,6 +76,15 @@ public function getAPI(): Client|MTProto|null
return $this->API;
}

private ?int $drop = null;
/**
* @internal
*/
public function getRpcDropCancellation(): Cancellation
{
return new TimeoutCancellation($this->drop ??= $this->getAPI()->getSettings()->getRpc()->getRpcDropTimeout());
}

/**
* Get IPC path.
*
Expand Down
60 changes: 30 additions & 30 deletions src/InternalDoc.php

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/MTProto.php
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,9 @@ public function getDNSClient(): DnsResolver
*
* @param string $url URL
*/
public function fileGetContents(string $url): string
public function fileGetContents(string $url, ?Cancellation $cancellation = null): string
{
return $this->getHTTPClient()->request(new Request($url))->getBody()->buffer();
return $this->getHTTPClient()->request(new Request($url), $cancellation)->getBody()->buffer($cancellation);
}
/**
* Get main DC ID.
Expand Down
5 changes: 3 additions & 2 deletions src/MTProtoTools/AuthKeyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace danog\MadelineProto\MTProtoTools;

use Amp\Cancellation;
use danog\MadelineProto\DataCenter;
use danog\MadelineProto\Logger;
use phpseclib3\Math\BigInteger;
Expand All @@ -30,9 +31,9 @@ trait AuthKeyHandler
/**
* Get diffie-hellman configuration.
*/
public function getDhConfig(): array
public function getDhConfig(?Cancellation $cancellation = null): array
{
$dh_config = $this->methodCallAsyncRead('messages.getDhConfig', ['version' => $this->dh_config['version'], 'random_length' => 0]);
$dh_config = $this->methodCallAsyncRead('messages.getDhConfig', ['version' => $this->dh_config['version'], 'random_length' => 0, 'cancellation' => $cancellation]);
if ($dh_config['_'] === 'messages.dhConfigNotModified') {
$this->logger->logger('DH configuration not modified', Logger::VERBOSE);
return $this->dh_config;
Expand Down
5 changes: 3 additions & 2 deletions src/VoIP/AuthKeyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use Amp\ByteStream\ReadableStream;
use Amp\ByteStream\WritableStream;
use Amp\Cancellation;
use Amp\DeferredFuture;
use AssertionError;
use danog\MadelineProto\LocalFile;
Expand Down Expand Up @@ -121,9 +122,9 @@ public function getCallVisualization(int $id): ?array
/**
* Accept call.
*/
public function acceptCall(int $id): void
public function acceptCall(int $id, ?Cancellation $cancellation = null): void
{
($this->calls[$id] ?? null)?->accept();
($this->calls[$id] ?? null)?->accept($cancellation);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/VoIPController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Amp\ByteStream\ReadableBuffer;
use Amp\ByteStream\ReadableStream;
use Amp\ByteStream\WritableStream;
use Amp\Cancellation;
use Amp\Sync\LocalMutex;
use danog\Loop\Loop;
use danog\MadelineProto\Loop\VoIP\DjLoop;
Expand Down Expand Up @@ -276,7 +277,7 @@ public function confirm(array $params): bool
/**
* Accept incoming call.
*/
public function accept(): self
public function accept(?Cancellation $cancellation = null): self
{
$lock = $this->authMutex->acquire();
try {
Expand All @@ -286,7 +287,7 @@ public function accept(): self
Assert::eq($this->callState->name, CallState::INCOMING->name);

$this->log(sprintf(Lang::$current_lang['accepting_call'], $this->public->otherID), Logger::VERBOSE);
$dh_config = $this->API->getDhConfig();
$dh_config = $this->API->getDhConfig($cancellation);
$this->log('Generating b...', Logger::VERBOSE);
$b = BigInteger::randomRange(Magic::$two, $dh_config['p']->subtract(Magic::$two));
$g_b = $dh_config['g']->powMod($b, $dh_config['p']);
Expand All @@ -302,13 +303,14 @@ public function accept(): self
],
'g_b' => $g_b->toBytes(),
'protocol' => self::CALL_PROTOCOL,
'cancellation' => $cancellation,
]);
} catch (CallAlreadyAcceptedError) {
$this->log(sprintf(Lang::$current_lang['call_already_accepted'], $this->public->callID));
return $this;
} catch (CallAlreadyDeclinedError) {
$this->log(Lang::$current_lang['call_already_declined']);
$this->discard(DiscardReason::HANGUP);
$this->discard(DiscardReason::HANGUP, cancellation: $cancellation);
return $this;
}
$this->call['b'] = $b;
Expand Down
11 changes: 10 additions & 1 deletion tools/AnnotationsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ private function createInternalClasses(): void
$doc .= $name;
$doc .= '(';
$paramList = '';
$hasCancellation = false;
foreach ($method->getParameters() as $param) {
if ($type = $param->getType()) {
$doc .= $this->typeToStr($type).' ';
Expand All @@ -519,7 +520,15 @@ private function createInternalClasses(): void
if ($param->isVariadic()) {
$paramList .= '...';
}
$paramList .= '$'.$param->getName().', ';
$paramList .= '$'.$param->getName();
if ($param->getName() === 'cancellation') {
$paramList .= ' ?? $this->wrapper->getRpcDropCancellation()';
$hasCancellation = true;
}
$paramList .= ', ';
}
if (!$hasCancellation && !$static) {
Logger::log($name.'.'.$param->getName().' has no cancellation!', Logger::WARNING);
}
$type = $method->getReturnType();
$hasReturnValue = $type !== null;
Expand Down

0 comments on commit 658d7ad

Please sign in to comment.