Fixed handling of certain responses when named params aren't used and beautified logging

This commit is contained in:
danogentili 2016-10-13 23:22:11 +02:00
parent aed44236d1
commit 0459498eb8
6 changed files with 31 additions and 20 deletions

View File

@ -21,7 +21,7 @@ class API extends Tools
set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']); set_error_handler(['\danog\MadelineProto\Exception', 'ExceptionErrorHandler']);
$this->session = new MTProto($params); $this->session = new MTProto($params);
var_dump($future_salts = $this->ping(3)); var_dump($future_salts = $this->ping(3));
$future_salts = $this->get_future_salts(3); var_dump($this->get_future_salts(3));
} }
public function __destruct() public function __destruct()

View File

@ -64,7 +64,7 @@ class Logging
if (!is_string($param)) { if (!is_string($param)) {
$param = var_export($param, true); $param = var_export($param, true);
} }
$param = basename(debug_backtrace()[0]['file'], '.php').': '.$param; $param = str_pad(basename(debug_backtrace()[0]['file'], '.php').': ', 16).(($mode == 3) ? "\t" : "").$param;
switch ($mode) { switch ($mode) {
case '1': case '1':
error_log($param); error_log($param);

View File

@ -46,11 +46,12 @@ class CallHandler extends AuthKeyHandler
{ {
foreach (range(1, $this->settings['max_tries']['query']) as $i) { foreach (range(1, $this->settings['max_tries']['query']) as $i) {
try { try {
$args = $this->tl->get_named_method_args($method, $args);
$int_message_id = $this->send_message($this->tl->serialize_method($method, $args), $this->tl->content_related($method)); $int_message_id = $this->send_message($this->tl->serialize_method($method, $args), $this->tl->content_related($method));
$this->outgoing_messages[$int_message_id]['content'] = ['method' => $method, 'args' => $args]; $this->outgoing_messages[$int_message_id]['content'] = ['method' => $method, 'args' => $args];
$server_answer = $this->wait_for_response($int_message_id); $server_answer = $this->wait_for_response($int_message_id);
} catch (Exception $e) { } catch (Exception $e) {
$this->log->log('An error occurred while calling method '.$method.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().$e->getTraceAsString().'. Recreating connection and retrying to call method...'); $this->log->log('An error occurred while calling method '.$method.': '.$e->getMessage().' in '.basename($e->getFile(), '.php').' on line '.$e->getLine().'. Recreating connection and retrying to call method...');
unset($this->connection); unset($this->connection);
$this->connection = new \danog\MadelineProto\DataCenter($this->settings['connection'], $this->settings['connection_settings']); $this->connection = new \danog\MadelineProto\DataCenter($this->settings['connection'], $this->settings['connection_settings']);
continue; continue;

View File

@ -74,7 +74,8 @@ class MsgIdHandler extends MessageHandler
); );
$keys = array_keys($this->outgoing_messages); $keys = array_keys($this->outgoing_messages);
asort($keys); asort($keys);
if ($int_message_id <= end($keys)) { $keys = end($keys);
while ($int_message_id <= $keys) {
$int_message_id += 4; $int_message_id += 4;
} }
$this->check_message_id($int_message_id, true); $this->check_message_id($int_message_id, true);

View File

@ -41,6 +41,7 @@ class ResponseHandler extends MsgIdHandler
break; break;
case 'bad_msg_notification': case 'bad_msg_notification':
break;
case 'bad_server_salt': case 'bad_server_salt':
$this->ack_outgoing_message_id($response['bad_msg_id']); // Acknowledge that the server received my request $this->ack_outgoing_message_id($response['bad_msg_id']); // Acknowledge that the server received my request
$this->outgoing_messages[$response['bad_msg_id']]['response'] = $last_received; $this->outgoing_messages[$response['bad_msg_id']]['response'] = $last_received;
@ -48,14 +49,13 @@ class ResponseHandler extends MsgIdHandler
break; break;
case 'pong': case 'pong':
var_dump($this->outgoing_messages); foreach ($this->outgoing_messages as $msg_id => &$omessage) {
foreach ($this->outgoing_messages as $msg_id => &$omessage) { if (isset($omessage['content']['args']['ping_id']) && $omessage['content']['args']['ping_id'] == $response['ping_id']) {
if (isset($omessage['content']['args']['ping_id']) && $omessage['content']['args']['ping_id'] == $response['ping_id']) { $omessage['response'] = $response['msg_id'];
$omessage['response'] = $response['msg_id']; $this->incoming_messages[$response['msg_id']]['content'] = $response;
$this->incoming_messages[$response['msg_id']]['content'] = $response; $this->ack_outgoing_message_id($msg_id);
$this->ack_outgoing_message_id($msg_id);
}
} }
}
break; break;
case 'new_session_created': case 'new_session_created':
// $this->settings['authorization']['temp_auth_key']['server_salt'] = $response['server_salt']; // $this->settings['authorization']['temp_auth_key']['server_salt'] = $response['server_salt'];

View File

@ -58,7 +58,23 @@ class TL
return $bytes_io; return $bytes_io;
} }
public function get_named_method_args($type_, $kwargs) {
if (isset($this->method_name[$type_])) {
$tl_method = $this->method_name[$type_];
} else {
throw new Exception('Could not extract type: '.$type_);
}
if (count(array_filter(array_keys($kwargs), 'is_string')) == 0) {
$argcount = 0;
$newargs = [];
foreach ($tl_method->params as $arg) {
$newargs[$arg['name']] = $kwargs[$argcount++];
}
$kwargs = $newargs;
}
return $kwargs;
}
public function serialize_method($type_, $kwargs) public function serialize_method($type_, $kwargs)
{ {
$bytes_io = ''; $bytes_io = '';
@ -68,15 +84,8 @@ class TL
throw new Exception('Could not extract type: '.$type_); throw new Exception('Could not extract type: '.$type_);
} }
$bytes_io .= \danog\PHP\Struct::pack('<i', $tl_method->id); $bytes_io .= \danog\PHP\Struct::pack('<i', $tl_method->id);
if (count(array_filter(array_keys($kwargs), 'is_string')) > 0) { foreach ($tl_method->params as $arg) {
foreach ($tl_method->params as $arg) { $bytes_io .= $this->serialize_param($arg['type'], $kwargs[$arg['name']]);
$bytes_io .= $this->serialize_param($arg['type'], $kwargs[$arg['name']]);
}
} else {
$argcount = 0;
foreach ($tl_method->params as $arg) {
$bytes_io .= $this->serialize_param($arg['type'], $kwargs[$argcount++]);
}
} }
return $bytes_io; return $bytes_io;