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

Misc fixes

This commit is contained in:
Daniil Gentili 2021-04-08 15:38:55 +02:00
parent a779c75826
commit 0980bd8718
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
5 changed files with 38 additions and 16 deletions

View File

@ -269,7 +269,7 @@ class API extends InternalDoc
if ($unserialized === 0) {
// Timeout
throw new \RuntimeException("Could not connect to MadelineProto, please check the logs for more details.");
throw new Exception("Could not connect to MadelineProto, please check the logs for more details.");
} elseif ($unserialized instanceof \Throwable) {
// IPC server error, try fetching full session
return yield from $this->connectToMadelineProto($settings, true);
@ -370,6 +370,7 @@ class API extends InternalDoc
*/
public function startAndLoop(string $eventHandler): void
{
$this->forceFull = true;
while (true) {
try {
Tools::wait($this->startAndLoopAsync($eventHandler));
@ -399,6 +400,7 @@ class API extends InternalDoc
try {
$promises = [];
foreach ($instances as $k => $instance) {
$instance->forceFull = true;
$instance->start(['async' => false]);
$promises []= $instance->startAndLoopAsync($eventHandler[$k]);
}
@ -422,8 +424,8 @@ class API extends InternalDoc
public function startAndLoopAsync(string $eventHandler): \Generator
{
$errors = [];
$this->async(true);
$this->forceFull = true;
$this->async(true);
if (!yield from $this->reconnectFull()) {
return;

View File

@ -205,6 +205,8 @@ trait ResponseHandler
$this->methodRecall('', ['message_id' => $requestId, 'postpone' => true]);
return;
case 20:
$request->setMsgId(null);
$request->setSeqNo(null);
$this->methodRecall('', ['message_id' => $requestId, 'postpone' => true]);
return;
case 16:

View File

@ -27,6 +27,7 @@ use danog\MadelineProto\Ipc\IpcState;
use function Amp\File\exists;
use function Amp\File\open;
use function Amp\File\rename;
use function Amp\File\stat;
/**
* Session path information.
@ -91,13 +92,22 @@ class SessionPaths
*/
public function serialize(object $object, string $path): \Generator
{
Logger::log("Waiting for exclusive lock of $path.lock...");
$unlock = yield from Tools::flockGenerator("$path.lock", LOCK_EX, 0.1);
try {
Logger::log("Got exclusive lock of $path.temp.php.lock...");
$file = yield open("$path.temp.php", 'bw+');
yield $file->write(Serialization::PHP_HEADER);
yield $file->write(\chr(Serialization::VERSION));
yield $file->write(\serialize($object));
yield $file->close();
yield rename("$path.temp.php", $path);
yield \rename("$path.temp.php", $path);
} finally {
$unlock();
}
}
/**
@ -119,14 +129,22 @@ class SessionPaths
}
$headerLen = \strlen(Serialization::PHP_HEADER) + 1;
Logger::log("Waiting for shared lock of $path.lock...");
$unlock = yield from Tools::flockGenerator("$path.lock", LOCK_SH, 0.1);
try {
Logger::log("Got shared lock of $path.lock...");
$file = yield open($path, 'rb');
$size = yield \stat($path);
$size = yield stat($path);
$size = $size['size'] ?? $headerLen;
yield $file->seek($headerLen); // Skip version for now
$unserialized = \unserialize((yield $file->read($size - $headerLen)) ?? '');
yield $file->close();
} finally {
$unlock();
}
return $unserialized;
}

View File

@ -77,7 +77,7 @@ class Connection extends SettingsAbstract
/**
* Whether to use ipv6.
*/
protected bool $ipv6;
protected bool $ipv6 = false;
/**
* Connection timeout.

View File

@ -603,7 +603,7 @@ abstract class Tools extends StrTools
* @param ?Promise $token Cancellation token
* @param ?callable $failureCb Failure callback, called only once if the first locking attempt fails.
*
* @return Promise<?callable>
* @return Promise<$token is null ? callable : ?callable>
*/
public static function flock(string $file, int $operation, float $polling = 0.1, ?Promise $token = null, $failureCb = null): Promise
{