Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watcher api #1588

Open
wants to merge 24 commits into
base: v8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1119,11 +1119,13 @@ Some of MadelineProto's core components are also available as separate, standalo
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.checkDownloadFileParams.html" name="bots.checkDownloadFileParams">bots.checkDownloadFileParams</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.deletePreviewMedia.html" name="bots.deletePreviewMedia">bots.deletePreviewMedia</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.editPreviewMedia.html" name="bots.editPreviewMedia">bots.editPreviewMedia</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.getAdminedBots.html" name="bots.getAdminedBots">bots.getAdminedBots</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.getPopularAppBots.html" name="bots.getPopularAppBots">bots.getPopularAppBots</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.getPreviewInfo.html" name="bots.getPreviewInfo">bots.getPreviewInfo</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.getPreviewMedias.html" name="bots.getPreviewMedias">bots.getPreviewMedias</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.reorderPreviewMedias.html" name="bots.reorderPreviewMedias">bots.reorderPreviewMedias</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.toggleUserEmojiStatusPermission.html" name="bots.toggleUserEmojiStatusPermission">bots.toggleUserEmojiStatusPermission</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.updateStarRefProgram.html" name="bots.updateStarRefProgram">bots.updateStarRefProgram</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.updateUserEmojiStatus.html" name="bots.updateUserEmojiStatus">bots.updateUserEmojiStatus</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/channels.restrictSponsoredMessages.html" name="channels.restrictSponsoredMessages">channels.restrictSponsoredMessages</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/channels.searchPosts.html" name="channels.searchPosts">channels.searchPosts</a>
Expand Down Expand Up @@ -1158,6 +1160,7 @@ Some of MadelineProto's core components are also available as separate, standalo
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.reportSponsoredMessage.html" name="messages.reportSponsoredMessage">messages.reportSponsoredMessage</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.requestMainWebView.html" name="messages.requestMainWebView">messages.requestMainWebView</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.savePreparedInlineMessage.html" name="messages.savePreparedInlineMessage">messages.savePreparedInlineMessage</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.searchStickers.html" name="messages.searchStickers">messages.searchStickers</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.sendPaidReaction.html" name="messages.sendPaidReaction">messages.sendPaidReaction</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.sendQuickReplyMessages.html" name="messages.sendQuickReplyMessages">messages.sendQuickReplyMessages</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.toggleDialogFilterTags.html" name="messages.toggleDialogFilterTags">messages.toggleDialogFilterTags</a>
Expand All @@ -1168,8 +1171,12 @@ Some of MadelineProto's core components are also available as separate, standalo
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#rleEncode" name="rleEncode">null-byte RLE encode: rleEncode</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.botCancelStarsSubscription.html" name="payments.botCancelStarsSubscription">payments.botCancelStarsSubscription</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.changeStarsSubscription.html" name="payments.changeStarsSubscription">payments.changeStarsSubscription</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.connectStarRefBot.html" name="payments.connectStarRefBot">payments.connectStarRefBot</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.convertStarGift.html" name="payments.convertStarGift">payments.convertStarGift</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.editConnectedStarRefBot.html" name="payments.editConnectedStarRefBot">payments.editConnectedStarRefBot</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.fulfillStarsSubscription.html" name="payments.fulfillStarsSubscription">payments.fulfillStarsSubscription</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.getConnectedStarRefBot.html" name="payments.getConnectedStarRefBot">payments.getConnectedStarRefBot</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.getConnectedStarRefBots.html" name="payments.getConnectedStarRefBots">payments.getConnectedStarRefBots</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.getStarGifts.html" name="payments.getStarGifts">payments.getStarGifts</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.getStarsGiftOptions.html" name="payments.getStarsGiftOptions">payments.getStarsGiftOptions</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.getStarsGiveawayOptions.html" name="payments.getStarsGiveawayOptions">payments.getStarsGiveawayOptions</a>
Expand All @@ -1181,6 +1188,7 @@ Some of MadelineProto's core components are also available as separate, standalo
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.getStarsTopupOptions.html" name="payments.getStarsTopupOptions">payments.getStarsTopupOptions</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.getStarsTransactions.html" name="payments.getStarsTransactions">payments.getStarsTransactions</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.getStarsTransactionsByID.html" name="payments.getStarsTransactionsByID">payments.getStarsTransactionsByID</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.getSuggestedStarRefBots.html" name="payments.getSuggestedStarRefBots">payments.getSuggestedStarRefBots</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.getUserStarGifts.html" name="payments.getUserStarGifts">payments.getUserStarGifts</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.refundStarsCharge.html" name="payments.refundStarsCharge">payments.refundStarsCharge</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/payments.saveStarGift.html" name="payments.saveStarGift">payments.saveStarGift</a>
Expand Down
15 changes: 12 additions & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3509,9 +3509,6 @@
</UnsupportedReferenceUsage>
</file>
<file src="src/MTProtoSession/AckHandler.php">
<PossiblyNullArrayOffset>
<code><![CDATA[$this->new_outgoing]]></code>
</PossiblyNullArrayOffset>
<PossiblyNullOperand>
<code><![CDATA[$message->getSent()]]></code>
<code><![CDATA[$message->getSent()]]></code>
Expand Down Expand Up @@ -3625,6 +3622,8 @@
<code><![CDATA[$sub['queueId']]]></code>
</MixedArrayAssignment>
<MixedArrayOffset>
<code><![CDATA[$this->new_outgoing[$message_id]]]></code>
<code><![CDATA[$this->new_outgoing[$message_id]]]></code>
<code><![CDATA[$this->outgoing_messages[$message_id]]]></code>
<code><![CDATA[$this->outgoing_messages[$message_id]]]></code>
</MixedArrayOffset>
Expand Down Expand Up @@ -3752,6 +3751,12 @@
<code><![CDATA[$msgId]]></code>
<code><![CDATA[$request->getMsgId()]]></code>
</PossiblyNullArgument>
<PossiblyNullArrayOffset>
<code><![CDATA[$this->new_outgoing]]></code>
<code><![CDATA[$this->new_outgoing]]></code>
<code><![CDATA[$this->new_outgoing]]></code>
<code><![CDATA[$this->new_outgoing]]></code>
</PossiblyNullArrayOffset>
<RedundantConditionGivenDocblockType>
<code><![CDATA[$this->API->authorized_dc == $this->datacenter && $this->API->authorized === \danog\MadelineProto\API::LOGGED_IN]]></code>
</RedundantConditionGivenDocblockType>
Expand Down Expand Up @@ -4281,6 +4286,8 @@
<code><![CDATA[static function (string $payload, int $offset) use ($stream, $seekable, $lock) {]]></code>
</MissingClosureReturnType>
<MixedArgument>
<code><![CDATA[$end]]></code>
<code><![CDATA[$end]]></code>
<code><![CDATA[$end]]></code>
<code><![CDATA[$end]]></code>
<code><![CDATA[$file]]></code>
Expand All @@ -4297,6 +4304,8 @@
<code><![CDATA[$size]]></code>
<code><![CDATA[$start]]></code>
<code><![CDATA[$start]]></code>
<code><![CDATA[$start]]></code>
<code><![CDATA[$start]]></code>
<code><![CDATA[$stream]]></code>
<code><![CDATA[$stream]]></code>
<code><![CDATA[$url]]></code>
Expand Down
4 changes: 2 additions & 2 deletions src/APIWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ public function getAPI(): Client|MTProto|null
return $this->API;
}

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

/**
Expand Down
56 changes: 13 additions & 43 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
use AssertionError;
use danog\DialogId\DialogId;
use danog\Loop\GenericLoop;
use danog\MadelineProto\Loop\Connection\CheckLoop;
use danog\MadelineProto\Loop\Connection\CleanupLoop;
use danog\MadelineProto\Loop\Connection\HttpWaitLoop;
use danog\MadelineProto\Loop\Connection\PingLoop;
use danog\MadelineProto\Loop\Connection\ReadLoop;
use danog\MadelineProto\Loop\Connection\WriteLoop;
use danog\MadelineProto\MTProto\MTProtoIncomingMessage;
use danog\MadelineProto\MTProto\MTProtoOutgoingMessage;
use danog\MadelineProto\MTProtoSession\Session;
use danog\MadelineProto\Stream\BufferedStreamInterface;
Expand Down Expand Up @@ -72,21 +71,11 @@ final class Connection
*
*/
protected ?ReadLoop $reader = null;
/**
* Checker loop.
*
*/
protected ?CheckLoop $checker = null;
/**
* Waiter loop.
*
*/
protected ?HttpWaitLoop $waiter = null;
/**
* Ping loop.
*
*/
protected ?PingLoop $pinger = null;
public ?PingLoop $pinger = null;
/**
* Cleanup loop.
*
Expand Down Expand Up @@ -301,26 +290,22 @@ private function connect(): self
$this->httpResCount = 0;
$this->writer ??= new WriteLoop($this);
$this->reader ??= new ReadLoop($this);
$this->checker ??= new CheckLoop($this);
$this->cleanup ??= new CleanupLoop($this);
$this->waiter ??= new HttpWaitLoop($this);
$this->handler ??= new GenericLoop(fn () => $this->handleMessages($this->new_incoming), "Handler loop");
$this->handler ??= new GenericLoop(function (): void {
$this->handleMessages($this->new_incoming);
if ($this->ack_queue) {
$this->flush(); // Flush acks
}
}, "Handler loop");
if (!isset($this->pinger) && !$ctx->isMedia() && !$ctx->isCDN() && !$this->isHttp()) {
$this->pinger = new PingLoop($this);
}
foreach ($this->new_outgoing as $message_id => $message) {
if ($message->unencrypted) {
if (!($message->getState() & MTProtoOutgoingMessage::STATE_REPLIED)) {
$message->reply(static fn () => new Exception('Restart because we were reconnected'));
}
unset($this->new_outgoing[$message_id], $this->outgoing_messages[$message_id]);
}
foreach ($this->unencrypted_new_outgoing as $message) {
$message->reply(static fn () => new Exception('Restart because we were reconnected'));
}
Assert::true($this->writer->start(), "Could not start writer stream");
Assert::true($this->reader->start(), "Could not start reader stream");
Assert::true($this->checker->start(), "Could not start checker stream");
Assert::true($this->cleanup->start(), "Could not start cleanup stream");
$this->waiter->start();
if ($this->pinger) {
Assert::true($this->pinger->start(), "Could not start pinger stream");
}
Expand All @@ -334,8 +319,9 @@ private function connect(): self
EventLoop::queue($lock->release(...));
}
}
public function wakeupHandler(): void
public function wakeupHandler(MTProtoIncomingMessage $message): void
{
$this->new_incoming->enqueue($message);
\assert($this->handler !== null);
Assert::true($this->handler->resume() || $this->handler->isRunning(), "Could not resume handler!");
}
Expand Down Expand Up @@ -586,9 +572,7 @@ public function sendMessage(MTProtoOutgoingMessage $message): void
$this->pendingOutgoing[$this->pendingOutgoingKey++] = $message;
$this->outgoingCtr?->inc();
$this->pendingOutgoingGauge?->set(\count($this->pendingOutgoing));
if (isset($this->writer)) {
$this->writer->resume();
}
$this->flush();
$this->connect();
$promise->await();
}
Expand All @@ -601,18 +585,6 @@ public function flush(): void
$this->writer->resume();
}
}
/**
* Resume HttpWaiter.
*/
public function pingHttpWaiter(): void
{
if (isset($this->waiter)) {
$this->waiter->resume();
}
if (isset($this->pinger)) {
$this->pinger->resume();
}
}
/**
* Connect main instance.
*
Expand Down Expand Up @@ -663,9 +635,7 @@ public function disconnect(bool $temporary = false): void

$this->reader?->stop();
$this->writer?->stop();
$this->checker?->stop();
$this->cleanup?->stop();
$this->pinger?->stop();

if (!$temporary) {
$this->shared->signalDisconnect($this->id);
Expand Down
2 changes: 1 addition & 1 deletion src/DataCenterConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function initAuthorization(): void
EventLoop::queue($lock->release(...));
}
if ($this->hasTempAuthKey()) {
$connection->pingHttpWaiter();
$connection->pinger?->resume();
}
}
/**
Expand Down
Loading