Applied fixes from StyleCI

This commit is contained in:
Daniil Gentili 2016-09-13 22:15:14 +00:00 committed by StyleCI Bot
parent 016bb106f6
commit 369673b5e9
5 changed files with 25 additions and 18 deletions

View File

@ -61,12 +61,12 @@ Slv8kg9qv1m6XHVQY3PnEw+QQtqSIXklHwIDAQAB
'max_tries' => [ 'max_tries' => [
'query' => 5, 'query' => 5,
'authorization' => 5, 'authorization' => 5,
'response' => 5 'response' => 5,
], ],
'msg_array_limit' => [ 'msg_array_limit' => [
'incoming' => 30, 'incoming' => 30,
'outgoing' => 30, 'outgoing' => 30,
] ],
]; ];
foreach ($default_settings as $key => $param) { foreach ($default_settings as $key => $param) {
if (!isset($settings[$key])) { if (!isset($settings[$key])) {

View File

@ -17,7 +17,8 @@ namespace danog\MadelineProto\MTProtoTools;
*/ */
class CallHandler extends AuthKeyHandler class CallHandler extends AuthKeyHandler
{ {
public function wait_for_response($res_id) { public function wait_for_response($res_id)
{
$response = null; $response = null;
$count = 0; $count = 0;
while ($response == null && $count++ < $this->settings['max_tries']['response']) { while ($response == null && $count++ < $this->settings['max_tries']['response']) {
@ -25,14 +26,14 @@ class CallHandler extends AuthKeyHandler
$deserialized = $this->tl->deserialize(\danog\MadelineProto\Tools::fopen_and_write('php://memory', 'rw+b', $server_answer)); $deserialized = $this->tl->deserialize(\danog\MadelineProto\Tools::fopen_and_write('php://memory', 'rw+b', $server_answer));
$tempres = $this->handle_message($deserialized); $tempres = $this->handle_message($deserialized);
if ($tempres == null) { if ($tempres == null) {
if (isset($this->outgoing_messages[$res_id]["response"])) { if (isset($this->outgoing_messages[$res_id]['response'])) {
$response = $this->outgoing_messages[$res_id]["response"]; $response = $this->outgoing_messages[$res_id]['response'];
} }
} else { } else {
$response = $tempres; $response = $tempres;
} }
} }
switch ($response["_"]) { switch ($response['_']) {
case 'rpc_error': case 'rpc_error':
throw new Exception('Got rpc error '.$response['error_code'].': '.$response['error_message']); throw new Exception('Got rpc error '.$response['error_code'].': '.$response['error_message']);
break; break;
@ -41,13 +42,14 @@ class CallHandler extends AuthKeyHandler
break; break;
} }
} }
public function method_call($method, $args) public function method_call($method, $args)
{ {
foreach (range(1, $this->settings['max_tries']['query']) as $i) { foreach (range(1, $this->settings['max_tries']['query']) as $i) {
try { try {
$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]["method"] = $method; $this->outgoing_messages[$int_message_id]['method'] = $method;
$this->outgoing_messages[$int_message_id]["args"] = $args; $this->outgoing_messages[$int_message_id]['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().'. Recreating connection and retrying to call method...'); $this->log->log('An error occurred while calling method '.$method.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().'. Recreating connection and retrying to call method...');
@ -58,6 +60,7 @@ class CallHandler extends AuthKeyHandler
if ($server_answer == null) { if ($server_answer == null) {
throw new Exception('An error occurred while calling method '.$method.'.'); throw new Exception('An error occurred while calling method '.$method.'.');
} }
return $server_answer; return $server_answer;
} }
throw new Exception('An error occurred while calling method '.$method.'.'); throw new Exception('An error occurred while calling method '.$method.'.');
@ -69,8 +72,8 @@ class CallHandler extends AuthKeyHandler
try { try {
$int_message_id = $this->send_message($this->tl->serialize_obj($object, $args), $this->tl->content_related($object)); $int_message_id = $this->send_message($this->tl->serialize_obj($object, $args), $this->tl->content_related($object));
// $server_answer = $this->recv_message(); // $server_answer = $this->recv_message();
$this->outgoing_messages[$int_message_id]["method"] = $object; $this->outgoing_messages[$int_message_id]['method'] = $object;
$this->outgoing_messages[$int_message_id]["args"] = $args; $this->outgoing_messages[$int_message_id]['args'] = $args;
} catch (Exception $e) { } catch (Exception $e) {
$this->log->log('An error occurred while calling object '.$object.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().'. Recreating connection and retrying to call object...'); $this->log->log('An error occurred while calling object '.$object.': '.$e->getMessage().' in '.$e->getFile().':'.$e->getLine().'. Recreating connection and retrying to call object...');
unset($this->sock); unset($this->sock);

View File

@ -87,5 +87,4 @@ class Crypt extends CallHandler
return $ciphered; return $ciphered;
} }
} }

View File

@ -38,6 +38,7 @@ class MessageHandler extends Crypt
$this->outgoing_messages[$int_message_id]['seq_no'] = $seq_no; $this->outgoing_messages[$int_message_id]['seq_no'] = $seq_no;
} }
$this->sock->send_message($message); $this->sock->send_message($message);
return $int_message_id; return $int_message_id;
} }

View File

@ -25,7 +25,7 @@ class ResponseHandler extends MsgIdHandler
$this->ack_outgoing_message_id($msg_id); // Acknowledge that the server received my message $this->ack_outgoing_message_id($msg_id); // Acknowledge that the server received my message
} }
break; break;
case 'rpc_result': case 'rpc_result':
end($this->incoming_messages); end($this->incoming_messages);
end($this->outgoing_messages); end($this->outgoing_messages);
@ -34,9 +34,10 @@ class ResponseHandler extends MsgIdHandler
if ($response['req_msg_id'] != key($this->outgoing_messages)) { if ($response['req_msg_id'] != key($this->outgoing_messages)) {
throw new Exception('Message id mismatch; req_msg_id ('.$response['req_msg_id'].') != last sent msg id ('.key($this->outgoing_messages).').'); throw new Exception('Message id mismatch; req_msg_id ('.$response['req_msg_id'].') != last sent msg id ('.key($this->outgoing_messages).').');
} }
return $this->handle_response($response['result'], $response['req_msg_id']); return $this->handle_response($response['result'], $response['req_msg_id']);
break; break;
case 'future_salts': case 'future_salts':
end($this->outgoing_messages); end($this->outgoing_messages);
$this->ack_outgoing_message_id($response['req_msg_id']); // Acknowledge that the server received my request $this->ack_outgoing_message_id($response['req_msg_id']); // Acknowledge that the server received my request
@ -44,13 +45,14 @@ class ResponseHandler extends MsgIdHandler
throw new Exception('Message id mismatch; req_msg_id ('.$response['req_msg_id'].') != last sent msg id ('.key($this->outgoing_messages).').'); throw new Exception('Message id mismatch; req_msg_id ('.$response['req_msg_id'].') != last sent msg id ('.key($this->outgoing_messages).').');
} }
$this->log->log('Received future salts.'); $this->log->log('Received future salts.');
return $this->handle_response($response, $response['req_msg_id']); return $this->handle_response($response, $response['req_msg_id']);
break; break;
case 'pong': case 'pong':
foreach ($this->outgoing_messages as $omessage) { foreach ($this->outgoing_messages as $omessage) {
if ($omessage["args"]["ping_id"] == $response["ping_id"]) { if ($omessage['args']['ping_id'] == $response['ping_id']) {
$this->outgoing_messages[$response["msg_id"]]["response"] = $response; $this->outgoing_messages[$response['msg_id']]['response'] = $response;
} }
} }
break; break;
@ -65,7 +67,7 @@ class ResponseHandler extends MsgIdHandler
$this->log->log('Received container.'); $this->log->log('Received container.');
$this->log->log($response['messages']); $this->log->log($response['messages']);
foreach ($response['messages'] as $message) { foreach ($response['messages'] as $message) {
$this->incoming_messages[$message['msg_id']] = [ 'seq_no' => $message['seqno']]; $this->incoming_messages[$message['msg_id']] = ['seq_no' => $message['seqno']];
$responses[] = $this->handle_message($message['body']); $responses[] = $this->handle_message($message['body']);
} }
foreach ($responses as $response) { foreach ($responses as $response) {
@ -91,7 +93,9 @@ class ResponseHandler extends MsgIdHandler
break; break;
} }
} }
public function handle_response($response, $res_id = null) {
public function handle_response($response, $res_id = null)
{
if ($res_id == null) { if ($res_id == null) {
return $response; return $response;
} }
@ -100,7 +104,7 @@ class ResponseHandler extends MsgIdHandler
case 'rpc_answer_dropped': case 'rpc_answer_dropped':
$this->ack_outgoing_message_id($response['req_msg_id']); // Acknowledge that the server received the original query (the same one, the response to which we wish to forget) $this->ack_outgoing_message_id($response['req_msg_id']); // Acknowledge that the server received the original query (the same one, the response to which we wish to forget)
default: default:
$this->outgoing_messages[$res_id]["response"] = $response; $this->outgoing_messages[$res_id]['response'] = $response;
break; break;
} }
} }