Improve management of connections and server errors

This commit is contained in:
Daniil Gentili 2018-02-25 12:29:20 +00:00
parent d547164e7c
commit afb15deb9e
3 changed files with 13 additions and 5 deletions

View File

@ -47,7 +47,7 @@ class Connection
public $object_queue = [];
public $ack_queue = [];
public $i = [];
private $must_open = false;
public $must_open = false;
public function __magic_construct($proxy, $extra, $ip, $port, $protocol, $timeout, $ipv6)
{
@ -330,8 +330,8 @@ class Connection
throw new Exception($response['description'], $response['code']);
}
$close = $response['protocol'] === 'HTTP/1.0';
if (isset($headers['connection'])) {
$close = $headers['connection'] === 'close';
if (isset($response['headers']['connection'])) {
$close = strtolower($response['headers']['connection']) === 'close';
}
if ($close) {
$this->close_and_reopen();

View File

@ -70,6 +70,13 @@ trait MessageHandler
*/
public function recv_message($datacenter)
{
if ($this->datacenter->sockets[$datacenter]->must_open) {
if ($this->is_http($datacenter)) {
$this->method_call('http_wait', ['max_wait' => 500, 'wait_after' => 150, 'max_delay' => 500], ['datacenter' => $datacenter]);
} else {
$this->method_call('ping', ['ping_id' => 0], ['datacenter' => $datacenter]);
}
}
$payload = $this->datacenter->sockets[$datacenter]->read_message();
if (strlen($payload) === 4) {
return $this->unpack_signed_int($payload);

View File

@ -311,6 +311,8 @@ trait ResponseHandler
throw new \danog\MadelineProto\PTSException($server_answer['error_message']);
}
switch ($server_answer['error_code']) {
case 500:
throw new \danog\MadelineProto\Exception('Re-executing query after server error...');
case 303:
$this->datacenter->curdc = $aargs['datacenter'] = (int) preg_replace('/[^0-9]+/', '', $server_answer['error_message']);
@ -359,8 +361,7 @@ trait ResponseHandler
throw new \danog\MadelineProto\Exception('Re-executing query...');
}
case 500:
throw new \danog\MadelineProto\Exception('Re-executing query after server error...');
default:
throw new \danog\MadelineProto\RPCErrorException($server_answer['error_message'], $server_answer['error_code']);
}