Some fixes

This commit is contained in:
Daniil Gentili 2019-05-12 16:01:52 +02:00
parent 7dae233bf7
commit 714ade6fbc
12 changed files with 45 additions and 47 deletions

View File

@ -123,7 +123,7 @@ if (stripos(readline('Do you want to handle incoming calls? (y/n): '), 'y') !==
}
}
if (stripos(readline('Do you want to make the secret chat tests? (y/n): '), 'y') !== false) {
$secret = $MadelineProto->API->request_secret_chat(getenv('TEST_SECRET_CHAT'));
$secret = $MadelineProto->request_secret_chat(getenv('TEST_SECRET_CHAT'));
echo 'Waiting for '.getenv('TEST_SECRET_CHAT').' (secret chat id '.$secret.') to accept the secret chat...'.PHP_EOL;
while ($MadelineProto->secret_chat_status($secret) !== 2) {
$MadelineProto->get_updates();

View File

@ -92,21 +92,6 @@ class Connection
return $this->ctx;
}
/**
* Connect function.
*
* Connects to a telegram DC using the specified protocol, proxy and connection parameters
*
* @param string $proxy Proxy class name
*
* @internal
*
* @return \Amp\Promise
*/
public function connect(ConnectionContext $ctx): Promise
{
return $this->call($this->connectAsync($ctx));
}
/**
* Connect function.
@ -119,7 +104,7 @@ class Connection
*
* @return \Amp\Promise
*/
public function connectAsync(ConnectionContext $ctx): \Generator
public function connect(ConnectionContext $ctx): \Generator
{
$this->API->logger->logger("Trying connection via $ctx", \danog\MadelineProto\Logger::WARNING);
@ -170,12 +155,8 @@ class Connection
}
}
public function sendMessage($message, $flush = true): Promise
{
return $this->call($this->sendMessageGenerator($message, $flush));
}
public function sendMessageGenerator($message, $flush = true): \Generator
public function sendMessage($message, $flush = true): \Generator
{
$deferred = new Deferred();
@ -229,14 +210,9 @@ class Connection
}
}
public function reconnect(): Promise
public function reconnect(): \Generator
{
return $this->call($this->reconnectAsync());
}
public function reconnectAsync(): \Generator
{
$this->API->logger->logger('Reconnecting');
$this->API->logger->logger("Reconnecting DC {$this->datacenter}");
$this->disconnect();
yield $this->API->datacenter->dcConnectAsync($this->ctx->getDc());
}

View File

@ -57,11 +57,19 @@ final class Coroutine implements Promise
/** @var mixed Promise success value when executing next coroutine step, null at all other times. */
private $value;
private $frames = [];
/**
* @param \Generator $generator
*/
public function __construct(\Generator $generator)
{
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
$frame = '';
$frame .= basename($backtrace[1]['file']).': '.$backtrace[2]['function'];
$this->frames []= $frame;
var_dump($frame);
$this->generator = $generator;
try {
@ -73,7 +81,7 @@ final class Coroutine implements Promise
return;
}
if (!$this->generator->valid()) {
if (method_exists($this->generator, 'getReturn')) {
if (PHP_MAJOR_VERSION >= 7) {
$this->resolve($this->generator->getReturn());
} else {
$this->resolve(null);
@ -83,6 +91,8 @@ final class Coroutine implements Promise
return;
}
$yielded = $this->transform($yielded);
} else {
var_dump(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
}
} catch (\Throwable $exception) {
$this->fail($exception);
@ -121,7 +131,7 @@ final class Coroutine implements Promise
}
if (!$this->generator->valid()) {
if (method_exists($this->generator, 'getReturn')) {
if (PHP_MAJOR_VERSION >= 7) {
$this->resolve($this->generator->getReturn());
} else {
$this->resolve(null);

View File

@ -44,7 +44,7 @@ class CheckLoop extends ResumableSignalLoop
while (true) {
while (empty($connection->new_outgoing)) {
if (yield $this->waitSignal($this->pause())) {
$API->logger->logger('Exiting check loop');
$API->logger->logger("Exiting check loop in DC {$datacenter}");
$this->exitedLoop();
return;
@ -129,7 +129,7 @@ class CheckLoop extends ResumableSignalLoop
}
//$t = time();
if (yield $this->waitSignal($this->pause($timeout))) {
$API->logger->logger('Exiting check loop');
$API->logger->logger("Exiting check loop in DC $datacenter");
$this->exitedLoop();
return;
@ -146,7 +146,7 @@ class CheckLoop extends ResumableSignalLoop
}
} else {
if (yield $this->waitSignal($this->pause($timeout))) {
$API->logger->logger('Exiting check loop');
$API->logger->logger("Exiting check loop in DC $datacenter");
$this->exitedLoop();
return;

View File

@ -50,7 +50,7 @@ class HttpWaitLoop extends ResumableSignalLoop
while (true) {
//var_dump("http loop DC $datacenter");
if ($a = yield $this->waitSignal($this->pause())) {
$API->logger->logger('Exiting HTTP wait loop');
$API->logger->logger("Exiting HTTP wait loop in DC $datacenter");
$this->exitedLoop();
return;
@ -63,7 +63,7 @@ class HttpWaitLoop extends ResumableSignalLoop
}
while ($connection->temp_auth_key === null) {
if (yield $this->waitSignal($this->pause())) {
$API->logger->logger('Exiting HTTP wait loop');
$API->logger->logger("Exiting HTTP wait loop in DC $datacenter");
$this->exitedLoop();
return;

View File

@ -68,10 +68,10 @@ class ReadLoop extends SignalLoop
if (is_int($error)) {
$this->exitedLoop();
yield $connection->reconnect();
if ($error === -404) {
if ($connection->temp_auth_key !== null) {
yield $connection->reconnect();
$API->logger->logger("WARNING: Resetting auth key in DC {$datacenter}...", \danog\MadelineProto\Logger::WARNING);
$connection->temp_auth_key = null;
$connection->session_id = null;
@ -80,13 +80,16 @@ class ReadLoop extends SignalLoop
}
yield $API->init_authorization_async();
} else {
//throw new \danog\MadelineProto\RPCErrorException($error, $error);
yield $connection->reconnect();
}
} elseif ($error === -1) {
yield $connection->reconnect();
$API->logger->logger("WARNING: Got quick ack from DC {$datacenter}", \danog\MadelineProto\Logger::WARNING);
} elseif ($error === 0) {
yield $connection->reconnect();
$API->logger->logger("Got NOOP from DC {$datacenter}", \danog\MadelineProto\Logger::WARNING);
} else {
yield $connection->reconnect();
throw new \danog\MadelineProto\RPCErrorException($error, $error);
}

View File

@ -50,7 +50,7 @@ class UpdateLoop extends ResumableSignalLoop
while (true) {
while (!$this->API->settings['updates']['handle_updates'] || !$this->has_all_auth()) {
if (yield $this->waitSignal($this->pause())) {
$API->logger->logger('Exiting update loop');
$API->logger->logger("Exiting update loop in DC $datacenter");
$this->exitedLoop();
return;
@ -62,7 +62,7 @@ class UpdateLoop extends ResumableSignalLoop
}
}
if (yield $this->waitSignal($this->pause(($API->last_getdifference + $timeout) - time()))) {
$API->logger->logger('Exiting update loop');
$API->logger->logger("Exiting update loop in DC $datacenter");
$this->exitedLoop();
return;

View File

@ -48,7 +48,7 @@ class WriteLoop extends ResumableSignalLoop
while (true) {
if (empty($connection->pending_outgoing)) {
if (yield $this->waitSignal($this->pause())) {
$API->logger->logger('Exiting write loop');
$API->logger->logger("Exiting write loop in DC $datacenter");
$this->exitedLoop();
yield new Success(0);

View File

@ -405,6 +405,7 @@ class MTProto implements TLCallback
if (yield $this->get_self_async()) {
$this->authorized = self::LOGGED_IN;
}
if ($this->authorized === self::LOGGED_IN) {
yield $this->get_cdn_config_async($this->datacenter->curdc);
$this->setup_logger();
@ -857,7 +858,7 @@ class MTProto implements TLCallback
public function get_phone_config_async($watcherId = null)
{
if ($this->authorized === self::LOGGED_IN && class_exists('\\danog\\MadelineProto\\VoIPServerConfig') && !$this->authorization['user']['bot']) {
if ($this->authorized === self::LOGGED_IN && class_exists('\\danog\\MadelineProto\\VoIPServerConfig') && !$this->authorization['user']['bot'] && $this->datacenter->sockets[$this->settings['connection_settings']['default_dc']]->temp_auth_key !== null) {
$this->logger->logger("Fetching phone config...");
VoIPServerConfig::updateDefault(yield $this->method_call_async_read('phone.getCallConfig', [], ['datacenter' => $this->settings['connection_settings']['default_dc']]));
} else {

View File

@ -19,6 +19,8 @@
namespace danog\MadelineProto\MTProtoTools;
use danog\MadelineProto\Exception;
/**
* Manages the creation of the authorization key.
*
@ -562,6 +564,7 @@ trait AuthKeyHandler
// Creates authorization keys
public function init_authorization_async()
{
var_dump((string) new Exception());
if ($this->pending_auth) {
return;
}
@ -591,13 +594,18 @@ trait AuthKeyHandler
};
}
if ($dcs) {
yield array_shift($dcs)();
$first = array_shift($dcs)();
var_dumP("Yielding first ");
var_dump(yield $first);
var_dumP("Yielded first");
}
foreach ($dcs as $id => &$dc) {
$dc = $dc();
}
var_dump("yielding all", array_keys($dcs));
yield $dcs;
var_dump("yielded all");
foreach ($postpone as $id => $socket) {
yield $this->init_authorization_socket_async($id, $socket);
@ -620,7 +628,6 @@ trait AuthKeyHandler
public function init_authorization_socket_async($id, $socket)
{
$this->init_auth_dcs[$id] = true;
try {
if ($socket->session_id === null) {
$socket->session_id = $this->random(8);
@ -629,7 +636,6 @@ trait AuthKeyHandler
}
$cdn = strpos($id, 'cdn');
$media = strpos($id, 'media');
if ($socket->temp_auth_key === null || $socket->auth_key === null) {
$dc_config_number = isset($this->settings['connection_settings'][$id]) ? $id : 'all';
if ($socket->auth_key === null && !$cdn && !$media) {
@ -651,7 +657,9 @@ trait AuthKeyHandler
//$socket->authorized = false;
$socket->temp_auth_key = null;
var_dump("creating auth k $id");
$socket->temp_auth_key = yield $this->create_auth_key_async($this->settings['authorization']['default_temp_auth_key_expires_in'], $id);
var_dump("done creating auth k $id");
yield $this->bind_temp_auth_key_async($this->settings['authorization']['default_temp_auth_key_expires_in'], $id);
//$socket->authorized = $authorized;

View File

@ -161,7 +161,7 @@ if (stripos(readline('Do you want to handle incoming calls? (y/n): '), 'y') !==
}
}
if (stripos(readline('Do you want to make the secret chat tests? (y/n): '), 'y') !== false) {
$secret = $MadelineProto->API->request_secret_chat(getenv('TEST_SECRET_CHAT'));
$secret = $MadelineProto->request_secret_chat(getenv('TEST_SECRET_CHAT'));
echo 'Waiting for '.getenv('TEST_SECRET_CHAT').' (secret chat id '.$secret.') to accept the secret chat...'.PHP_EOL;
while ($MadelineProto->secret_chat_status($secret) !== 2) {
$MadelineProto->get_updates();

View File

@ -126,7 +126,7 @@ if (stripos(readline('Do you want to make the secret chat tests? (y/n): '), 'y')
/**
* Request a secret chat.
*/
$secret_chat_id = $MadelineProto->API->request_secret_chat(getenv('TEST_SECRET_CHAT'));
$secret_chat_id = $MadelineProto->request_secret_chat(getenv('TEST_SECRET_CHAT'));
echo 'Waiting for '.getenv('TEST_SECRET_CHAT').' (secret chat id '.$secret_chat_id.') to accept the secret chat...'.PHP_EOL;
/*