From fbc774d995866a7aa3a01c21688d70949ece1954 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 31 Mar 2018 00:50:17 +0000 Subject: [PATCH] Implement fully passive update polling --- docs/docs/SETTINGS.md | 2 +- src/danog/MadelineProto/DataCenter.php | 2 +- src/danog/MadelineProto/MTProto.php | 7 +- .../MTProtoTools/UpdateHandler.php | 29 +- src/danog/MadelineProto/VoIP.php | 266 ------------------ 5 files changed, 24 insertions(+), 282 deletions(-) delete mode 100644 src/danog/MadelineProto/VoIP.php diff --git a/docs/docs/SETTINGS.md b/docs/docs/SETTINGS.md index d2420664..bad6a949 100644 --- a/docs/docs/SETTINGS.md +++ b/docs/docs/SETTINGS.md @@ -378,7 +378,7 @@ Default: true Description: Should I handle old updates on startup? ### `$settings['updates']['getdifference_interval']` -Default: -1 +Default: 10 Description: If positive and bigger than zero, no requests will be sent to the socket to request updates in N seconds, passive update listening will be used instead ### `$settings['updates']['callback']` diff --git a/src/danog/MadelineProto/DataCenter.php b/src/danog/MadelineProto/DataCenter.php index 82bbf1d9..c9e79165 100644 --- a/src/danog/MadelineProto/DataCenter.php +++ b/src/danog/MadelineProto/DataCenter.php @@ -153,7 +153,7 @@ class DataCenter foreach ($this->sockets as $dc_id => $socket) { $read[$dc_id] = $socket->getSocket(); } - \Socket::select($read, $write, $except, 0); + \Socket::select($read, $write, $except, $this->settings['all']['timeout']); return array_keys($read); } diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index a713eb8a..e589c7af 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -54,7 +54,7 @@ class MTProto /* const V = 71; */ - const V = 98; + const V = 99; const NOT_LOGGED_IN = 0; const WAITING_CODE = 1; const WAITING_SIGNUP = -1; @@ -242,6 +242,9 @@ class MTProto if (isset($settings['updates']['callback'][0]) && $settings['updates']['callback'][0] === $this) { $settings['updates']['callback'] = 'get_updates_update_handler'; } + if (isset($settings['updates']['getdifference_interval']) && $settings['updates']['getdifference_interval'] === -1) { + unset($settings['updates']['getdifference_interval']); + } unset($settings['tl_schema']); if (isset($settings['authorization']['rsa_key'])) { unset($settings['authorization']['rsa_key']); @@ -525,7 +528,7 @@ class MTProto // Should I handle updates? 'handle_old_updates' => true, // Should I handle old updates on startup? - 'getdifference_interval' => -1, + 'getdifference_interval' => 10, // Getdifference manual polling interval 'callback' => 'get_updates_update_handler', ], 'secret_chats' => ['accept_chats' => true], 'serialization' => ['serialization_interval' => 30], 'threading' => [ diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index ce238bf8..2b2315b8 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -24,6 +24,7 @@ trait UpdateHandler private $channels_state = []; public $updates = []; public $updates_key = 0; + public $last_getdifference = 0; public function pwr_update_handler($update) { @@ -58,26 +59,29 @@ trait UpdateHandler } }); $time = microtime(true); - try { try { - if (($error = $this->recv_message($this->datacenter->curdc)) !== true) { - if ($error === -404) { - if ($this->datacenter->sockets[$this->datacenter->curdc]->temp_auth_key !== null) { - \danog\MadelineProto\Logger::log('WARNING: Resetting auth key...', \danog\MadelineProto\Logger::WARNING); - $this->datacenter->sockets[$this->datacenter->curdc]->temp_auth_key = null; - $this->init_authorization(); + $waiting = $this->datacenter->select(); + $dc = count($waiting) ? $waiting[0] : $this->datacenter->curdc; + $last_recv = $this->datacenter->sockets[$dc]->last_recv; + if (count($waiting) && !$this->is_http($dc)) { + if (($error = $this->recv_message($dc)) !== true) { + if ($error === -404) { + if ($this->datacenter->sockets[$dc]->temp_auth_key !== null) { + \danog\MadelineProto\Logger::log('WARNING: Resetting auth key...', \danog\MadelineProto\Logger::WARNING); + $this->datacenter->sockets[$dc]->temp_auth_key = null; + $this->init_authorization(); - throw new \danog\MadelineProto\Exception('I had to recreate the temporary authorization key'); + throw new \danog\MadelineProto\Exception('I had to recreate the temporary authorization key'); + } } + throw new \danog\MadelineProto\RPCErrorException($error, $error); } - - throw new \danog\MadelineProto\RPCErrorException($error, $error); + $only_updates = $this->handle_messages($dc); } - $only_updates = $this->handle_messages($this->datacenter->curdc); } catch (\danog\MadelineProto\NothingInTheSocketException $e) { } - if (time() - $this->datacenter->sockets[$this->datacenter->curdc]->last_recv > $this->settings['updates']['getdifference_interval']) { + if ($this->is_http($dc) || time() - $this->last_getdifference > $this->settings['updates']['getdifference_interval']) { $this->get_updates_difference(); } } catch (\danog\MadelineProto\RPCErrorException $e) { @@ -298,6 +302,7 @@ trait UpdateHandler \danog\MadelineProto\Logger::log('Got '.$difference['_'], \danog\MadelineProto\Logger::ULTRA_VERBOSE); $this->postpone_updates = true; $this->updates_state['sync_loading'] = true; + $this->last_getdifference = time(); try { switch ($difference['_']) { diff --git a/src/danog/MadelineProto/VoIP.php b/src/danog/MadelineProto/VoIP.php deleted file mode 100644 index 3770e039..00000000 --- a/src/danog/MadelineProto/VoIP.php +++ /dev/null @@ -1,266 +0,0 @@ -. -*/ - -namespace danog\MadelineProto; - -if (!extension_loaded('php-libtgvoip') && false) { - class VoIP - { - use \danog\MadelineProto\MTProtoTools\MessageHandler; - - const PHP_LIBTGVOIP_VERSION = '1.1.2'; - const STATE_CREATED = 0; - const STATE_WAIT_INIT = 1; - const STATE_WAIT_INIT_ACK = 2; - const STATE_ESTABLISHED = 3; - const STATE_FAILED = 4; - const STATE_RECONNECTING = 5; - - const TGVOIP_ERROR_UNKNOWN = 0; - const TGVOIP_ERROR_INCOMPATIBLE = 1; - const TGVOIP_ERROR_TIMEOUT = 2; - const TGVOIP_ERROR_AUDIO_IO = 3; - - const NET_TYPE_UNKNOWN = 0; - const NET_TYPE_GPRS = 1; - const NET_TYPE_EDGE = 2; - const NET_TYPE_3G = 3; - const NET_TYPE_HSPA = 4; - const NET_TYPE_LTE = 5; - const NET_TYPE_WIFI = 6; - const NET_TYPE_ETHERNET = 7; - const NET_TYPE_OTHER_HIGH_SPEED = 8; - const NET_TYPE_OTHER_LOW_SPEED = 9; - const NET_TYPE_DIALUP = 10; - const NET_TYPE_OTHER_MOBILE = 11; - - const DATA_SAVING_NEVER = 0; - const DATA_SAVING_MOBILE = 1; - const DATA_SAVING_ALWAYS = 2; - - const PROXY_NONE = 0; - const PROXY_SOCKS5 = 1; - - const AUDIO_STATE_NONE = -1; - const AUDIO_STATE_CREATED = 0; - const AUDIO_STATE_CONFIGURED = 1; - const AUDIO_STATE_RUNNING = 2; - - const CALL_STATE_NONE = -1; - const CALL_STATE_REQUESTED = 0; - const CALL_STATE_INCOMING = 1; - const CALL_STATE_ACCEPTED = 2; - const CALL_STATE_CONFIRMED = 3; - const CALL_STATE_READY = 4; - const CALL_STATE_ENDED = 5; - - private $MadelineProto; - public $configuration = ['endpoints' => [], 'shared_config' => []]; - public $storage = []; - public $internalStorage = []; - private $signal = 0; - private $callState; - private $callID; - private $creatorID; - private $otherID; - private $protocol; - private $visualization; - private $holdFiles = []; - private $inputFiles; - private $outputFile; - private $isPlaying = false; - - private $connection_settings = []; - private $dclist = []; - - private $datacenter; - - public function __construct($creator, $otherID, $callID, $MadelineProto, $callState, $protocol) - { - $this->creator = $creator; - $this->otherID = $otherID; - $this->callID = $callID; - $this->MadelineProto = $MadelineProto; - $this->callState = $callState; - $this->protocol = $protocol; - } - - public function deInitVoIPController() - { - } - - public function setVisualization($visualization) - { - $this->visualization = $visualization; - } - - public function getVisualization() - { - return $this->visualization; - } - - public function discard($reason = ['_' => 'phoneCallDiscardReasonDisconnect'], $rating = [], $debug = false) - { - if ($this->callState === self::CALL_STATE_ENDED || empty($this->configuration)) { - return false; - } - $this->MadelineProto->discard_call($this->callID, $reason, $rating, $debug); - $this->deinitVoIPController(); - - return $this; - } - - public function accept() - { - if ($this->callState !== self::CALL_STATE_INCOMING) { - return false; - } - $this->callState = self::CALL_STATE_ACCEPTED; - if (!$this->MadelineProto->accept_call($this->callID)) { - $this->discard_call(['_' => 'phoneCallDiscardReasonDisconnect']); - - return false; - } - - return $this; - } - - public function close() - { - $this->deinitVoIPController(); - } - - public function startTheMagic() - { - return $this; - } - - public function play($file) - { - $this->inputFiles[] = $file; - - return $this; - } - - public function playOnHold($files) - { - $this->holdFiles = $files; - } - - public function setOutputFile($file) - { - $this->outputFile = $file; - } - - public function unsetOutputFile() - { - $this->outputFile = null; - } - - public function setMadeline($MadelineProto) - { - $this->MadelineProto = $MadelineProto; - } - - public function getProtocol() - { - return $this->protocol; - } - - public function getOtherID() - { - return $this->otherID; - } - - public function getCallID() - { - return $this->callID; - } - - public function isCreator() - { - return $this->creator; - } - - public function whenCreated() - { - return isset($this->internalStorage['created']) ? $this->internalStorage['created'] : false; - } - - public function parseConfig() - { - if (count($this->configuration['endpoints'])) { - $this->connection_settings['all'] = $this->MadelineProto->settings['connection_settings']['all']; - $this->connection_settings['all']['protocol'] = 'obfuscated2'; - $this->connection_settings['all']['do_not_retry'] = true; - - $test = $this->connection_settings['all']['test_mode'] ? 'test' : 'main'; - foreach ($this->configuration['endpoints'] as $endpoint) { - $this->dclist[$test]['ipv6'][$endpoint['id']] = ['ip_address' => $endpoint['ipv6'], 'port' => $endpoint['port'], 'peer_tag' => $endpoint['peer_tag']]; - $this->dclist[$test]['ipv4'][$endpoint['id']] = ['ip_address' => $endpoint['ip'], 'port' => $endpoint['port'], 'peer_tag' => $endpoint['peer_tag']]; - } - if (!isset($this->datacenter)) { - $this->datacenter = new DataCenter($this->dclist, $this->connection_settings); - } else { - //$this->datacenter->__construct($this->dclist, $this->connection_settings); - } - foreach ($this->datacenter->get_dcs() as $new_dc) { - $this->datacenter->dc_connect($new_dc); - } - $this->init_all(); - foreach ($this->datacenter->get_dcs(false) as $new_dc) { - $this->datacenter->dc_connect($new_dc); - } - $this->init_all(); - } - } - - private function init_all() - { - foreach ($this->datacenter->sockets as $dc_id => $socket) { - if ($socket->auth_key === null) { - $socket->auth_key = ['id' => $this->configuration['auth_key_id'], 'auth_key' => $this->configuration['auth_key']]; - } - } - } - - public function getCallState() - { - return $this->callState; - } - - public function getVersion() - { - return 'libponyvoip-1.0'; - } - - public function getPreferredRelayID() - { - return 0; - } - - public function getLastError() - { - return ''; - } - - public function getDebugLog() - { - return ''; - } - - public function getSignalBarsCount() - { - return $this->signal; - } - } -}