diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 794221916..b4f594595 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -676,6 +676,7 @@ class MTProto { $this->initing_authorization = true; $this->updates_state['sync_loading'] = true; + try { foreach ($this->datacenter->sockets as $id => $socket) { if (strpos($id, 'media')) { continue; @@ -702,13 +703,16 @@ class MTProto } } } - $this->initing_authorization = false; - $this->updates_state['sync_loading'] = false; + } finally { + $this->initing_authorization = false; + $this->updates_state['sync_loading'] = false; + } } public function sync_authorization($authorized_dc) { $this->updates_state['sync_loading'] = true; + try { foreach ($this->datacenter->sockets as $new_dc => $socket) { if (($int_dc = preg_replace('|/D+|', '', $new_dc)) == $authorized_dc) { continue; @@ -725,8 +729,9 @@ class MTProto $this->method_call('auth.logOut', [], ['datacenter' => $new_dc]); $authorization = $this->method_call('auth.importAuthorization', $exported_authorization, ['datacenter' => $new_dc]); } + } finally { $this->updates_state['sync_loading'] = false; - + } return $authorization; } diff --git a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php index 4c5686434..54ad3b28c 100644 --- a/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/AuthKeyHandler.php @@ -501,8 +501,11 @@ trait AuthKeyHandler public function get_dh_config() { $this->updates_state['sync_loading'] = true; - $dh_config = $this->method_call('messages.getDhConfig', ['version' => $this->dh_config['version'], 'random_length' => 0], ['datacenter' => $this->datacenter->curdc]); - $this->updates_state['sync_loading'] = false; + try { + $dh_config = $this->method_call('messages.getDhConfig', ['version' => $this->dh_config['version'], 'random_length' => 0], ['datacenter' => $this->datacenter->curdc]); + } finally { + $this->updates_state['sync_loading'] = false; + } if ($dh_config['_'] === 'messages.dhConfigNotModified') { \danog\MadelineProto\Logger::log(\danog\MadelineProto\Logger::VERBOSE, ['DH configuration not modified']); diff --git a/src/danog/MadelineProto/MTProtoTools/CallHandler.php b/src/danog/MadelineProto/MTProtoTools/CallHandler.php index f5243baab..cc9f5e875 100644 --- a/src/danog/MadelineProto/MTProtoTools/CallHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/CallHandler.php @@ -79,10 +79,10 @@ trait CallHandler \danog\MadelineProto\Logger::log(['Using GZIP compression for '.$method.', saved '.($l - $g).' bytes of data, reduced call size by '.($g * 100 / $l).'%'], \danog\MadelineProto\Logger::VERBOSE); } $last_recv = $this->last_recv; - if ($canunset = !$this->updates_state['sync_loading'] && !$this->threads && !$this->run_workers) { - $this->updates_state['sync_loading'] = true; - } for ($count = 1; $count <= $this->settings['max_tries']['query']; $count++) { + if ($canunset = !$this->updates_state['sync_loading'] && !$this->threads && !$this->run_workers) { + $this->updates_state['sync_loading'] = true; + } try { \danog\MadelineProto\Logger::log(['Calling method (try number '.$count.' for '.$method.')...'], \danog\MadelineProto\Logger::ULTRA_VERBOSE); @@ -209,22 +209,20 @@ trait CallHandler } catch (\danog\MadelineProto\Exception $e) { $last_error = $e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine(); \danog\MadelineProto\Logger::log(['An error occurred while calling method '.$method.': '.$last_error.'. Recreating connection and retrying to call method...'], \danog\MadelineProto\Logger::WARNING); - if (in_array($this->datacenter->sockets[$aargs['datacenter']]->protocol, ['http', 'https']) && $method !== 'http_wait') { + //if (in_array($this->datacenter->sockets[$aargs['datacenter']]->protocol, ['http', 'https']) && $method !== 'http_wait') { //$this->method_call('http_wait', ['max_wait' => $this->datacenter->sockets[$aargs['datacenter']]->timeout, 'wait_after' => 0, 'max_delay' => 0], ['datacenter' => $aargs['datacenter']]); - } else { + //} else { $this->datacenter->sockets[$aargs['datacenter']]->close_and_reopen(); - } - //sleep(1); // To avoid flooding + //} continue; } catch (\RuntimeException $e) { $last_error = $e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine(); \danog\MadelineProto\Logger::log(['An error occurred while calling method '.$method.': '.$last_error.'. Recreating connection and retrying to call method...'], \danog\MadelineProto\Logger::WARNING); - if (in_array($this->datacenter->sockets[$aargs['datacenter']]->protocol, ['http', 'https']) && $method !== 'http_wait') { + //if (in_array($this->datacenter->sockets[$aargs['datacenter']]->protocol, ['http', 'https']) && $method !== 'http_wait') { //$this->method_call('http_wait', ['max_wait' => $this->datacenter->sockets[$aargs['datacenter']]->timeout, 'wait_after' => 0, 'max_delay' => 0], ['datacenter' => $aargs['datacenter']]); - } else { + //} else { $this->datacenter->sockets[$aargs['datacenter']]->close_and_reopen(); - } - //sleep(1); // To avoid flooding + //} continue; } finally { if (isset($aargs['heavy']) && $aargs['heavy'] && isset($message_id)) { diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index a47e18635..782da27e7 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -23,7 +23,6 @@ trait UpdateHandler private $channels_state = []; public $updates = []; public $updates_key = 0; - private $getting_state = false; public function pwr_update_handler($update) { @@ -168,37 +167,37 @@ trait UpdateHandler } $input = $input['InputChannel']; } catch (\danog\MadelineProto\Exception $e) { - $this->load_channel_state($channel)['sync_loading'] = false; - return false; } catch (\danog\MadelineProto\RPCErrorException $e) { - $this->load_channel_state($channel)['sync_loading'] = false; - return false; + } finally { + $this->load_channel_state($channel)['sync_loading'] = false; } \danog\MadelineProto\Logger::log(['Fetching '.$channel.' difference...'], \danog\MadelineProto\Logger::ULTRA_VERBOSE); try { $difference = $this->method_call('updates.getChannelDifference', ['channel' => $input, 'filter' => ['_' => 'channelMessagesFilterEmpty'], 'pts' => $this->load_channel_state($channel)['pts'], 'limit' => 30], ['datacenter' => $this->datacenter->curdc]); } catch (\danog\MadelineProto\RPCErrorException $e) { - $this->load_channel_state($channel)['sync_loading'] = false; if ($e->getMessage() === "You haven't joined this channel/supergroup") { return false; } throw $e; + } finally { + $this->load_channel_state($channel)['sync_loading'] = false; } unset($input); switch ($difference['_']) { case 'updates.channelDifferenceEmpty': - $this->load_channel_state($channel)['sync_loading'] = false; $this->set_channel_state($channel, $difference); break; case 'updates.channelDifference': + $this->load_channel_state($channel)['sync_loading'] = true; + try { $this->set_channel_state($channel, $difference); $this->handle_update_messages($difference['new_messages'], $channel); $this->handle_multiple_update($difference['other_updates'], [], $channel); - $this->load_channel_state($channel)['sync_loading'] = false; + } finally { $this->load_channel_state($channel)['sync_loading'] = false; } if (!$difference['final']) { unset($difference); $this->get_channel_difference($channel); @@ -206,10 +205,12 @@ trait UpdateHandler break; case 'updates.channelDifferenceTooLong': \danog\MadelineProto\Logger::log(['Got '.$difference['_']], \danog\MadelineProto\Logger::VERBOSE); + $this->load_channel_state($channel)['sync_loading'] = true; + try { $this->set_channel_state($channel, $difference); $this->handle_update_messages($difference['messages'], $channel); unset($difference); - $this->load_channel_state($channel)['sync_loading'] = false; + } finally { $this->load_channel_state($channel)['sync_loading'] = false; } $this->get_channel_difference($channel); break; default: @@ -267,15 +268,19 @@ trait UpdateHandler } catch (\danog\MadelineProto\PTSException $e) { $this->updates_state['sync_loading'] = false; $this->got_state = false; + } finally { + $this->updates_state['sync_loading'] = false; } } \danog\MadelineProto\Logger::log(['Got '.$difference['_']], \danog\MadelineProto\Logger::ULTRA_VERBOSE); + try { switch ($difference['_']) { case 'updates.differenceEmpty': $this->set_update_state($difference); break; case 'updates.difference': + $this->updates_state['sync_loading'] = true; $this->handle_multiple_update($difference['other_updates']); foreach ($difference['new_encrypted_messages'] as $encrypted) { $this->handle_encrypted_update(['_' => 'updateNewEncryptedMessage', 'message' => $encrypted], true); @@ -285,6 +290,7 @@ trait UpdateHandler break; case 'updates.differenceSlice': + $this->updates_state['sync_loading'] = true; $this->handle_multiple_update($difference['other_updates']); $this->handle_update_messages($difference['new_messages']); $this->set_update_state($difference['intermediate_state']); @@ -296,16 +302,17 @@ trait UpdateHandler throw new \danog\MadelineProto\Exception('Unrecognized update difference received: '.var_export($difference, true)); break; } - $this->updates_state['sync_loading'] = false; + } finally { $this->updates_state['sync_loading'] = false; } } public function get_updates_state() { $last = $this->updates_state['sync_loading']; $this->updates_state['sync_loading'] = true; + try { $data = $this->method_call('updates.getState', [], ['datacenter' => $this->datacenter->curdc]); $this->get_cdn_config($this->datacenter->curdc); - $this->updates_state['sync_loading'] = $last; + } finally { $this->updates_state['sync_loading'] = $last; } return $data; } diff --git a/src/danog/MadelineProto/Wrappers/DialogHandler.php b/src/danog/MadelineProto/Wrappers/DialogHandler.php index b04f41cf4..9e96337d8 100644 --- a/src/danog/MadelineProto/Wrappers/DialogHandler.php +++ b/src/danog/MadelineProto/Wrappers/DialogHandler.php @@ -23,6 +23,7 @@ trait DialogHandler $res = ['dialogs' => [0], 'count' => 1]; $datacenter = $this->datacenter->curdc; $peers = []; + try { while ($this->dialog_params['count'] < $res['count']) { \danog\MadelineProto\Logger::log([\danog\MadelineProto\Lang::$current_lang['getting_dialogs']]); $res = $this->method_call('messages.getDialogs', $this->dialog_params, ['datacenter' => $datacenter, 'FloodWaitLimit' => 100]); @@ -39,9 +40,9 @@ trait DialogHandler break; } } - + } finally { $this->updates_state['sync_loading'] = false; - + } return $peers; } }