Rewriting connection module..

This commit is contained in:
Daniil Gentili 2016-09-14 21:37:08 +02:00
parent 6179589fb1
commit 97fe13daad
4 changed files with 20 additions and 22 deletions

View File

@ -22,16 +22,13 @@ class CallHandler extends AuthKeyHandler
$response = null;
$count = 0;
while ($response == null && $count++ < $this->settings['max_tries']['response']) {
$server_answer = $this->recv_message();
$deserialized = $this->tl->deserialize(\danog\MadelineProto\Tools::fopen_and_write('php://memory', 'rw+b', $server_answer));
$this->log->log("Getting response....");
$deserialized = $this->recv_message();
end($this->incoming_messages);
$tempres = $this->handle_message($deserialized, $last_sent, key($this->incoming_messages));
if ($tempres == null) {
if (isset($this->outgoing_messages[$last_sent]['response']) && isset($this->incoming_messages[$this->outgoing_messages[$last_sent]['response']]['content'])) {
$response = $this->incoming_messages[$this->outgoing_messages[$last_sent]['response']]['content'];
}
} else {
$response = $tempres;
var_dump($this->incoming_messages);
if (isset($this->outgoing_messages[$last_sent]['response']) && isset($this->incoming_messages[$this->outgoing_messages[$last_sent]['response']]['content'])) {
$response = $this->incoming_messages[$this->outgoing_messages[$last_sent]['response']]['content'];
}
}
if ($response == null) {
@ -74,8 +71,8 @@ class CallHandler extends AuthKeyHandler
foreach (range(1, $this->settings['max_tries']['query']) as $i) {
try {
$int_message_id = $this->send_message($this->tl->serialize_obj($object, $args), $this->tl->content_related($object));
// $server_answer = $this->recv_message();
$this->outgoing_messages[$int_message_id]['content'] = ['object' => $object, 'args' => $args ];
// $server_answer = $this->wait_for_response($int_message_id);
} 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...');
unset($this->sock);

View File

@ -104,7 +104,8 @@ class MessageHandler extends Crypt
} else {
throw new Exception('Got unknown auth_key id');
}
return $message_data;
$deserialized = $this->tl->deserialize(\danog\MadelineProto\Tools::fopen_and_write('php://memory', 'rw+b', $message_data));
$this->incoming_messages[$message_id]['content'] = $deserialized;
return $deserialized;
}
}

View File

@ -19,6 +19,9 @@ class ResponseHandler extends MsgIdHandler
{
public function handle_message($response, $last_sent, $last_received)
{
$this->incoming_messages[$last_received]['content'] = $response;
ksort($this->incoming_messages);
end($this->incoming_messages);
switch ($response['_']) {
case 'msgs_ack':
foreach ($response['msg_ids'] as $msg_id) {
@ -30,20 +33,20 @@ class ResponseHandler extends MsgIdHandler
$this->ack_incoming_message_id($last_received); // Acknowledge that I received the server's response
$this->ack_outgoing_message_id($response['req_msg_id']); // Acknowledge that the server received my request
return $this->handle_response($response['result'], $response['req_msg_id']);
return $this->handle_response($response['result'], $response['req_msg_id'], $last_received);
break;
case 'future_salts':
$this->ack_outgoing_message_id($response['req_msg_id']); // Acknowledge that the server received my request
return $this->handle_response($response, $response['req_msg_id']);
return $this->handle_response($response, $response['req_msg_id'], $last_received);
break;
case 'bad_msg_notification':
case 'bad_server_salt':
$this->ack_outgoing_message_id($response['bad_msg_id']); // Acknowledge that the server received my request
return $this->handle_response($response, $response['bad_msg_id']);
return $this->handle_response($response, $response['bad_msg_id'], $last_received);
break;
case 'pong':
@ -59,7 +62,6 @@ class ResponseHandler extends MsgIdHandler
$this->ack_incoming_message_id($last_received); // Acknowledge that I received the server's response
$this->log->log('new session created');
$this->log->log($response);
$this->settings['authorization']['temp_auth_key']['server_salt'] = $response['server_salt'];
break;
case 'msg_container':
$responses = [];
@ -93,7 +95,8 @@ class ResponseHandler extends MsgIdHandler
if (isset($this->incoming_messages[$response['orig_message']['msg_id']])) {
$this->ack_incoming_message_id($response['orig_message']['msg_id']); // Acknowledge that I received the server's response
} else {
return $this->handle_response($response['orig_message']);
$this->check_message_id($message['orig_message']['msg_id'], false);
return $this->handle_message($response['orig_message']);
}
break;
case 'http_wait':
@ -102,16 +105,13 @@ class ResponseHandler extends MsgIdHandler
break;
default:
$this->ack_incoming_message_id($last_received); // Acknowledge that I received the server's response
return $this->handle_response($response);
return $this->handle_response($response, $last_sent, $last_received);
break;
}
}
public function handle_response($response, $res_id = null, $last_received = null)
public function handle_response($response, $res_id, $last_received)
{
if ($res_id == null) {
return $response;
}
switch ($response['_']) {
case 'gzip_packed':
return $this->handle_response(gzdecode($response), $res_id, $last_received);

View File

@ -15,7 +15,7 @@ namespace danog\MadelineProto\MTProtoTools;
/**
* Manages sequence number.
*/
class SeqNoHandler extends ResponseHandler
class SeqNoHandler extends SaltHandler
{
public function generate_seq_no($content_related = true)
{