Skip to content

Commit f968ce2

Browse files
committed
Cleanup
1 parent a312276 commit f968ce2

8 files changed

Lines changed: 37 additions & 38 deletions

File tree

src/DataCenterConnection.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,6 @@ public function signalDisconnect(int $id): void
361361
$backup = $this->connections[$id]->backupSession();
362362
$list = '';
363363
foreach ($backup as $k => $message) {
364-
if ($message->constructor === 'msgs_state_req'
365-
|| $message->constructor === 'ping_delay_disconnect') {
366-
unset($backup[$k]);
367-
continue;
368-
}
369364
$list .= $message->constructor;
370365
$list .= ', ';
371366
}

src/Loop/Connection/PingLoop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function __construct(Connection $connection)
5252
$this->constructCommon($connection);
5353
$this->timeout = $timeout = $this->shared->getSettings()->getPingInterval();
5454
$this->timeoutDisconnect = $timeout + 15;
55+
$this->timeoutSeconds = (float) $this->timeout;
5556
$connection->getShared()->auth->connectionState->subscribe($this);
5657
}
5758
#[\Override]
@@ -80,7 +81,6 @@ public function loop(): ?float
8081
}
8182

8283
EventLoop::queue(function (): void {
83-
$this->API->logger("Ping DC {$this->datacenter}");
8484
try {
8585
$this->connection->methodCallAsyncRead('ping_delay_disconnect', ['ping_id' => random_bytes(8), 'disconnect_delay' => $this->timeoutDisconnect]);
8686
} catch (Throwable $e) {

src/Loop/Connection/WriteLoop.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@ public function loop(): ?float
109109
};
110110
$this->pendingState = null;
111111
}
112-
if ($this->queue->isEmpty()) {
113-
$this->API->logger("No messages, pausing in $this...", Logger::ULTRA_VERBOSE);
114-
return self::LONG_POLL_TIMEOUT;
115-
}
116112
$this->connection->writing(true);
117113
try {
118114
if ($this->queue === $this->connection->unencryptedPendingOutgoing) {
119115
$this->unencryptedWriteLoop();
120116
} else {
121117
$this->encryptedWriteLoop();
122118
}
119+
if ($this->pendingState === null) {
120+
$this->API->logger("No messages, pausing in $this...", Logger::ULTRA_VERBOSE);
121+
return self::LONG_POLL_TIMEOUT;
122+
}
123123
} catch (StreamException $e) {
124124
if ($this->connection->shouldReconnect()) {
125125
$this->API->logger("Stopping $this because we have to reconnect");
@@ -171,7 +171,13 @@ public function unencryptedWriteLoop(): void
171171
}
172172
public function encryptedWriteLoop(): void
173173
{
174-
while ($this->pendingState === null && !$this->queue->isEmpty()) {
174+
while ($this->pendingState === null &&
175+
(
176+
!$this->queue->isEmpty()
177+
|| $this->connection->ack_queue
178+
|| $this->connection->check_queue->count()
179+
)
180+
) {
175181
if (0 !== ($check = $this->connection->check_queue)->count()) {
176182
$this->connection->check_queue = new WeakMap;
177183
$deferred = new DeferredFuture();
@@ -207,6 +213,7 @@ public function encryptedWriteLoop(): void
207213
$message = $arr[$key];
208214
if ($message->hasReply()) {
209215
$this->API->logger("Already got response for and forgot about message $message");
216+
$this->connection->ack_queue[] = $message->getMsgId();
210217
continue;
211218
}
212219
$chr = \ord($chr);
@@ -261,6 +268,7 @@ public function encryptedWriteLoop(): void
261268

262269
$has_state = false;
263270
$has_resend = false;
271+
$has_content_related = false;
264272

265273
$message = $this->queue;
266274
while (($message = $message->prev) instanceof MTProtoOutgoingMessage) {
@@ -310,6 +318,8 @@ public function encryptedWriteLoop(): void
310318
$MTmessages[] = $MTmessage;
311319
$messages[] = $message;
312320

321+
$has_content_related = $has_content_related || $message->hasPromise();
322+
313323
$message->setSeqNo($MTmessage['seqno'])
314324
->setMsgId($MTmessage['msg_id']);
315325
}
@@ -347,11 +357,14 @@ public function encryptedWriteLoop(): void
347357
if ($count > 1 || $has_seq) {
348358
$message_id = $this->connection->msgIdHandler->generateMessageId();
349359
$this->API->logger("Wrapping in msg_container ({$count} messages of total size {$total_length}, id $message_id) as encrypted message for DC {$this->datacenter}", Logger::ULTRA_VERBOSE);
350-
$messages []= $ct = new Container(
360+
$ct = new Container(
351361
$this->connection,
352362
$messages,
353363
$acks,
354364
);
365+
if ($has_content_related) {
366+
$messages []= $ct;
367+
}
355368
$this->connection->outgoingCtr?->inc();
356369
$message_data = $this->API->getTL()->serializeObject(['type' => ''], ['_' => 'msg_container', 'messages' => $MTmessages], 'container');
357370
$message_data_length = \strlen($message_data);

src/MTProto/MTProtoOutgoingMessage.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ public function __construct(
115115
public readonly ?SpecialMethodType $specialMethodType,
116116
public readonly ?Cancellation $cancellation,
117117
public readonly ?string $subtype = null,
118-
/**
119-
* Whether this message is related to a file upload, as in getting a redirect should redirect to a media server.
120-
*/
121-
public readonly bool $fileRelated = false,
122118
/**
123119
* Custom flood wait limit for this message.
124120
*/

src/MTProto/SpecialMethodType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ enum SpecialMethodType
2424
{
2525
case UNAUTHED_METHOD;
2626
case USER_RELATED;
27+
case FILE_RELATED;
2728
}

src/MTProtoSession/CallHandler.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use danog\MadelineProto\MTProto;
3030
use danog\MadelineProto\MTProto\Container;
3131
use danog\MadelineProto\MTProto\MTProtoOutgoingMessage;
32+
use danog\MadelineProto\MTProto\SpecialMethodType;
3233
use danog\MadelineProto\TL\Exception;
3334
use danog\MadelineProto\WrappedFuture;
3435
use Revolt\EventLoop;
@@ -144,8 +145,12 @@ public function methodCallAsyncWrite(string $method, array $args): WrappedFuture
144145
if (isset($args['id']) && \is_array($args['id']) && isset($args['id']['_']) && isset($args['id']['dc_id']) && ($args['id']['_'] === 'inputBotInlineMessageID' || $args['id']['_'] === 'inputBotInlineMessageID64') && $this->datacenter != $args['id']['dc_id']) {
145146
return $this->API->methodCallAsyncWrite($method, $args, $args['id']['dc_id']);
146147
}
147-
$file = \in_array($method, ['upload.saveFilePart', 'upload.saveBigFilePart', 'upload.getFile', 'upload.getCdnFile'], true);
148-
if ($file && !$this->shared->auth->isMedia && $this->API->datacenter->has(-$this->datacenter)) {
148+
$special = $args['specialMethodType'] ?? null;
149+
if ($special === SpecialMethodType::FILE_RELATED
150+
&& !$this->shared->auth->isMedia
151+
&& !$this->shared->auth->isCdn
152+
&& $this->API->datacenter->has(-$this->datacenter)
153+
) {
149154
$this->API->logger('Using media DC');
150155
return $this->API->methodCallAsyncWrite($method, $args, -$this->datacenter);
151156
}
@@ -177,10 +182,9 @@ public function methodCallAsyncWrite(string $method, array $args): WrappedFuture
177182
constructor: $method,
178183
type: $methodInfo['type'],
179184
subtype: $methodInfo['subtype'] ?? null,
180-
specialMethodType: $args['specialMethodType'] ?? null,
185+
specialMethodType: $special,
181186
isMethod: true,
182187
unencrypted: !$encrypted,
183-
fileRelated: $file,
184188
floodWaitLimit: $args['floodWaitLimit'] ?? null,
185189
resultDeferred: $response,
186190
cancellation: $cancellation,
@@ -200,7 +204,7 @@ public function methodCallAsyncWrite(string $method, array $args): WrappedFuture
200204
* @param string $object Object name
201205
* @param array $args Arguments
202206
*/
203-
public function objectCallAsync(string $object, array $args, DeferredFuture $promise): void
207+
public function objectCallAsync(string $object, array $args, ?DeferredFuture $promise = null): void
204208
{
205209
$cancellation = $args['cancellation'] ?? null;
206210
$cancellation?->throwIfRequested();

src/MTProtoSession/ResponseHandler.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Amp\SignalException;
2424
use danog\BetterPrometheus\BetterHistogram;
2525
use danog\Loop\Loop;
26-
use danog\MadelineProto\API;
2726
use danog\MadelineProto\DataCenterConnection;
2827
use danog\MadelineProto\FileRedirect;
2928
use danog\MadelineProto\Lang;
@@ -71,7 +70,7 @@ private function handleMessages(iterable $messages): ?float
7170
}
7271
try {
7372
match ($type) {
74-
'msgs_ack' => $this->handleAck($message),
73+
'msgs_ack' => $message->read(),
7574

7675
'rpc_result',
7776
'future_salts',
@@ -98,16 +97,6 @@ private function handleMessages(iterable $messages): ?float
9897
}
9998
return Loop::PAUSE;
10099
}
101-
private function handleAck(MTProtoIncomingMessage $message): void
102-
{
103-
$message->read();
104-
/*foreach ($message->read()['msg_ids'] as $msg_id) {
105-
// Acknowledge that the server received my message
106-
if (!isset($this->outgoing_messages[$msg_id])) {
107-
$this->API->logger("WARNING: Couldn't find message id ".$msg_id.' in the array of outgoing messages. Maybe try to increase its size?', Logger::WARNING);
108-
}
109-
}*/
110-
}
111100
private function handleFallback(MTProtoIncomingMessage $message): void
112101
{
113102
$message->ack();
@@ -317,7 +306,7 @@ private function handleRpcError(MTProtoOutgoingMessage $request, array $response
317306
if ($this->API->isTestMode()) {
318307
$datacenter += 10_000;
319308
}
320-
if ($request->fileRelated) {
309+
if ($request->specialMethodType === SpecialMethodType::FILE_RELATED) {
321310
return fn () => new FileRedirect(
322311
$this->API->datacenter->has(-$datacenter)
323312
? -$datacenter

src/MTProtoTools/Files.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use danog\MadelineProto\FileCallbackInterface;
4747
use danog\MadelineProto\FileRedirect;
4848
use danog\MadelineProto\Logger;
49+
use danog\MadelineProto\MTProto\SpecialMethodType;
4950
use danog\MadelineProto\MTProtoTools\Crypt\IGE;
5051
use danog\MadelineProto\RPCError\FileTokenInvalidError;
5152
use danog\MadelineProto\RPCError\FloodPremiumWaitError;
@@ -328,13 +329,13 @@ public function uploadFromCallable(callable $callable, int $size = 0, ?string $m
328329
$floodWaitError?->wait($cancellation);
329330
return $this->methodCallAsyncWrite(
330331
$method,
331-
$callable($part_num) + ['cancellation' => $cancellation, 'floodWaitLimit' => 0],
332+
$callable($part_num) + ['cancellation' => $cancellation, 'floodWaitLimit' => 0, 'specialMethodType' => SpecialMethodType::FILE_RELATED],
332333
$datacenter
333334
);
334335
};
335336
} else {
336337
try {
337-
$part = $callable($part_num) + ['cancellation' => $cancellation, 'floodWaitLimit' => 0];
338+
$part = $callable($part_num) + ['cancellation' => $cancellation, 'floodWaitLimit' => 0, 'specialMethodType' => SpecialMethodType::FILE_RELATED];
338339
} catch (StreamEof) {
339340
break;
340341
}
@@ -1207,9 +1208,9 @@ private function downloadPart(array &$messageMedia, bool &$cdn, int &$datacenter
12071208
{
12081209
do {
12091210
if (!$cdn) {
1210-
$basic_param = ['location' => $messageMedia['InputFileLocation'], 'cdn_supported' => true, 'floodWaitLimit' => 0, 'cancellation' => $cancellation];
1211+
$basic_param = ['location' => $messageMedia['InputFileLocation'], 'cdn_supported' => true, 'floodWaitLimit' => 0, 'cancellation' => $cancellation, 'specialMethodType' => SpecialMethodType::FILE_RELATED];
12111212
} else {
1212-
$basic_param = ['file_token' => $messageMedia['file_token'], 'floodWaitLimit' => 0, 'cancellation' => $cancellation];
1213+
$basic_param = ['file_token' => $messageMedia['file_token'], 'floodWaitLimit' => 0, 'cancellation' => $cancellation, 'specialMethodType' => SpecialMethodType::FILE_RELATED];
12131214
}
12141215
do {
12151216
$cancellation?->throwIfRequested();

0 commit comments

Comments
 (0)