Small cleanups

This commit is contained in:
Daniil Gentili 2020-03-07 21:45:50 +01:00
parent a545e3463e
commit ec2719db4e
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
10 changed files with 88 additions and 81 deletions

View File

@ -48,7 +48,8 @@
"amphp/websocket": "dev-master as 1", "amphp/websocket": "dev-master as 1",
"ext-ctype": "*", "ext-ctype": "*",
"danog/7to70": "^1", "danog/7to70": "^1",
"danog/7to5": "^1" "danog/7to5": "^1",
"vimeo/psalm": "dev-master"
}, },
"suggest": { "suggest": {
"ext-libtgvoip": "Install the php-libtgvoip extension to make phone calls (https://github.com/danog/php-libtgvoip)" "ext-libtgvoip": "Install the php-libtgvoip extension to make phone calls (https://github.com/danog/php-libtgvoip)"
@ -64,7 +65,6 @@
"files": [ "files": [
"src/BigIntegor.php", "src/BigIntegor.php",
"src/YieldReturnValue.php", "src/YieldReturnValue.php",
"src/ReflectionGenerator.php",
"src/polyfill.php" "src/polyfill.php"
] ]
}, },

View File

@ -140,7 +140,6 @@ class API extends InternalDoc
if (isset($this->API)) { if (isset($this->API)) {
$this->storage = $this->API->storage ?? $this->storage; $this->storage = $this->API->storage ?? $this->storage;
$unserialized->oldInstance = true;
unset($unserialized); unset($unserialized);
yield from $this->API->initAsynchronously(); yield from $this->API->initAsynchronously();
@ -165,6 +164,15 @@ class API extends InternalDoc
$this->logger->logger(Lang::$current_lang['madelineproto_ready'], Logger::NOTICE); $this->logger->logger(Lang::$current_lang['madelineproto_ready'], Logger::NOTICE);
} }
/**
* Wakeup function.
*
* @return void
*/
public function __wakeup(): void
{
$this->oldInstance = true;
}
/** /**
* Destruct function. * Destruct function.
* *
@ -248,7 +256,7 @@ class API extends InternalDoc
$promises = []; $promises = [];
foreach ($instances as $k => $instance) { foreach ($instances as $k => $instance) {
$instance->start(['async' => false]); $instance->start(['async' => false]);
$promises []= Tools::call($instance->startAndLoopAsync($eventHandler[$k])); $promises []= $instance->startAndLoopAsync($eventHandler[$k]);
} }
Tools::wait(Tools::all($promises)); Tools::wait(Tools::all($promises));
return; return;

View File

@ -153,8 +153,10 @@ class DoHConnector implements Connector
} }
return ResourceSocket::fromClientSocket($socket, $socketContext->getTlsContext()); return ResourceSocket::fromClientSocket($socket, $socketContext->getTlsContext());
} }
// This is reached if either all URIs failed or the maximum number of attempts is reached. // This is reached if either all URIs failed or the maximum number of attempts is reached.
/** @noinspection PhpUndefinedVariableInspection */ /** @noinspection PhpUndefinedVariableInspection */
throw $e; throw $e;
})()); })());
} }

View File

@ -101,7 +101,7 @@ trait CallHandler
* @param array $args Arguments * @param array $args Arguments
* @param array $aargs Additional arguments * @param array $aargs Additional arguments
* *
* @return Generator * @return \Generator
*/ */
public function methodCallAsyncWrite(string $method, $args = [], array $aargs = ['msg_id' => null]): \Generator public function methodCallAsyncWrite(string $method, $args = [], array $aargs = ['msg_id' => null]): \Generator
{ {

View File

@ -17,11 +17,68 @@
* @link https://docs.madelineproto.xyz MadelineProto documentation * @link https://docs.madelineproto.xyz MadelineProto documentation
*/ */
namespace danog\MadelineProto\MTProtoSession;
use danog\MadelineProto\MTProtoSession\MsgIdHandler\MsgIdHandler32; use danog\MadelineProto\MTProtoSession\MsgIdHandler\MsgIdHandler32;
use danog\MadelineProto\MTProtoSession\MsgIdHandler\MsgIdHandler64; use danog\MadelineProto\MTProtoSession\MsgIdHandler\MsgIdHandler64;
if (PHP_INT_SIZE === 8) { /**
\class_alias(MsgIdHandler64::class, \danog\MadelineProto\MTProtoSession\MsgIdHandler::class); * Manages message ids.
} else { */
\class_alias(MsgIdHandler32::class, \danog\MadelineProto\MTProtoSession\MsgIdHandler::class); abstract class MsgIdHandler
{
/**
* Session instance.
*
* @var Session
*/
protected $session;
/**
* Constructor.
*
* @param Session $session Session
*/
private function __construct(Session $session)
{
$this->session = $session;
}
/**
* Create MsgIdHandler instance.
*
* @param Session $session Session
*
* @return self
*/
public static function createInstance(Session $session): self
{
if (PHP_INT_SIZE === 8) {
return new MsgIdHandler64($session);
}
return new MsgIdHandler32($session);
}
/**
* Check validity of given message ID.
*
* @param string $newMessageId New message ID
* @param array $aargs Params
*
* @return void
*/
abstract public function checkMessageId($newMessageId, array $aargs): void;
/**
* Generate outgoing message ID.
*
* @return string
*/
abstract public function generateMessageId(): string;
/**
* Get maximum message ID.
*
* @param boolean $incoming Incoming or outgoing message ID
*
* @return mixed
*/
abstract public function getMaxId(bool $incoming);
} }

View File

@ -19,12 +19,12 @@
namespace danog\MadelineProto\MTProtoSession\MsgIdHandler; namespace danog\MadelineProto\MTProtoSession\MsgIdHandler;
use danog\MadelineProto\MTProtoSession\MsgIdHandlerAbstract; use danog\MadelineProto\MTProtoSession\MsgIdHandler;
/** /**
* Manages message ids. * Manages message ids.
*/ */
class MsgIdHandler32 extends MsgIdHandlerAbstract class MsgIdHandler32 extends MsgIdHandler
{ {
/** /**
* Maximum incoming ID. * Maximum incoming ID.

View File

@ -19,13 +19,13 @@
namespace danog\MadelineProto\MTProtoSession\MsgIdHandler; namespace danog\MadelineProto\MTProtoSession\MsgIdHandler;
use danog\MadelineProto\MTProtoSession\MsgIdHandlerAbstract; use danog\MadelineProto\MTProtoSession\MsgIdHandler;
use danog\MadelineProto\Tools; use danog\MadelineProto\Tools;
/** /**
* Manages message ids. * Manages message ids.
*/ */
class MsgIdHandler64 extends MsgIdHandlerAbstract class MsgIdHandler64 extends MsgIdHandler
{ {
/** /**
* Maximum incoming ID. * Maximum incoming ID.

View File

@ -1,66 +0,0 @@
<?php
/**
* MsgIdHandler module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\MTProtoSession;
/**
* Manages message ids.
*/
abstract class MsgIdHandlerAbstract
{
/**
* Session instance.
*
* @var Session
*/
protected $session;
/**
* Constructor.
*
* @param Session $session Session
*/
public function __construct(Session $session)
{
$this->session = $session;
}
/**
* Check validity of given message ID.
*
* @param string $newMessageId New message ID
* @param array $aargs Params
*
* @return void
*/
abstract public function checkMessageId($newMessageId, array $aargs): void;
/**
* Generate outgoing message ID.
*
* @return string
*/
abstract public function generateMessageId(): string;
/**
* Get maximum message ID.
*
* @param boolean $incoming Incoming or outgoing message ID
*
* @return mixed
*/
abstract public function getMaxId(bool $incoming);
}

View File

@ -53,7 +53,7 @@ abstract class Session
$this->session_id = \danog\MadelineProto\Tools::random(8); $this->session_id = \danog\MadelineProto\Tools::random(8);
$this->session_in_seq_no = 0; $this->session_in_seq_no = 0;
$this->session_out_seq_no = 0; $this->session_out_seq_no = 0;
$this->msgIdHandler = new MsgIdHandler($this); $this->msgIdHandler = MsgIdHandler::createInstance($this);
} }
/** /**
* Create MTProto session if needed. * Create MTProto session if needed.

View File

@ -27,6 +27,12 @@ use function Amp\File\get;
*/ */
class Serialization class Serialization
{ {
/**
* List of session paths.
*
* @var array
*/
private $paths = [];
/** /**
* Extract path components for serialization. * Extract path components for serialization.
* *
@ -60,7 +66,7 @@ class Serialization
} finally { } finally {
$unlock(); $unlock();
} }
\danog\MadelineProto\Magic::classExists(); Magic::classExists();
try { try {
$unserialized = \unserialize($tounserialize); $unserialized = \unserialize($tounserialize);
} catch (\danog\MadelineProto\Bug74586Exception $e) { } catch (\danog\MadelineProto\Bug74586Exception $e) {