Implement fully passive update polling

This commit is contained in:
Daniil Gentili 2018-03-31 00:50:17 +00:00
parent 0368e2daf2
commit fbc774d995
5 changed files with 24 additions and 282 deletions

View File

@ -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']`

View File

@ -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);
}

View File

@ -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' => [

View File

@ -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['_']) {

View File

@ -1,266 +0,0 @@
<?php
/*
Copyright 2016-2018 Daniil Gentili
(https://daniil.it)
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/>.
*/
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;
}
}
}