MadelineProto/src/danog/MadelineProto/MTProtoSession/Session.php

156 lines
3.8 KiB
PHP
Raw Normal View History

2019-06-29 14:54:12 +02:00
<?php
2020-01-31 19:29:43 +01:00
2019-06-29 14:54:12 +02:00
/**
* Session module.
*
* 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/>.
*
* @author Daniil Gentili <daniil@daniil.it>
2020-02-17 14:13:46 +01:00
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
2019-06-29 14:54:12 +02:00
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
2019-10-31 15:07:35 +01:00
* @link https://docs.madelineproto.xyz MadelineProto documentation
2019-06-29 14:54:12 +02:00
*/
2019-08-31 23:07:20 +02:00
namespace danog\MadelineProto\MTProtoSession;
2019-08-31 22:43:58 +02:00
2020-10-03 17:41:21 +02:00
use danog\MadelineProto\Connection;
2020-10-03 12:36:08 +02:00
use danog\MadelineProto\MTProto;
use danog\MadelineProto\MTProto\IncomingMessage;
use danog\MadelineProto\MTProto\OutgoingMessage;
2020-10-03 12:36:08 +02:00
2019-06-29 14:54:12 +02:00
/**
2019-08-31 23:07:20 +02:00
* Manages MTProto session-specific data.
*
2020-10-03 17:41:21 +02:00
* @extends Connection
2019-06-29 14:54:12 +02:00
*/
2020-10-04 14:55:05 +02:00
trait Session
2019-06-29 14:54:12 +02:00
{
2019-08-31 22:43:58 +02:00
use AckHandler;
use ResponseHandler;
2019-06-29 14:54:12 +02:00
use SeqNoHandler;
2019-09-01 01:52:28 +02:00
use CallHandler;
use Reliable;
2020-04-05 14:57:33 +02:00
/**
* Incoming message array.
*
* @var IncomingMessage[]
2020-04-05 14:57:33 +02:00
*/
2019-06-29 14:54:12 +02:00
public $incoming_messages = [];
2020-04-05 14:57:33 +02:00
/**
* Outgoing message array.
*
* @var OutgoingMessage[]
2020-04-05 14:57:33 +02:00
*/
2019-06-29 14:54:12 +02:00
public $outgoing_messages = [];
2020-04-05 14:57:33 +02:00
/**
* New incoming message ID array.
*
* @var IncomingMessage[]
2020-04-05 14:57:33 +02:00
*/
2019-06-29 14:54:12 +02:00
public $new_incoming = [];
2020-04-05 14:57:33 +02:00
/**
* New outgoing message array.
2020-04-05 14:57:33 +02:00
*
* @var OutgoingMessage[]
2020-04-05 14:57:33 +02:00
*/
2019-06-29 14:54:12 +02:00
public $new_outgoing = [];
2020-04-05 14:57:33 +02:00
/**
* Pending outgoing messages.
*
* @var OutgoingMessage[]
2020-04-05 14:57:33 +02:00
*/
public $pendingOutgoing = [];
2020-04-05 14:57:33 +02:00
/**
* Pending outgoing key.
*
* @var string
*/
public $pendingOutgoingKey = 'a';
2020-04-05 14:57:33 +02:00
/**
* Time delta with server.
*
* @var integer
*/
2019-06-29 14:54:12 +02:00
public $time_delta = 0;
2020-04-05 14:57:33 +02:00
/**
* Call queue.
*
* @var array
*/
2019-06-29 14:54:12 +02:00
public $call_queue = [];
2020-04-05 14:57:33 +02:00
/**
* Ack queue.
*
* @var array
*/
2019-06-29 14:54:12 +02:00
public $ack_queue = [];
2020-02-07 21:13:49 +01:00
/**
* State request queue.
*
* @var array
*/
public $state_queue = [];
/**
* Resend request queue.
*
* @var array
*/
public $resend_queue = [];
/**
2020-02-07 21:13:49 +01:00
* Message ID handler.
*
2020-03-08 17:24:31 +01:00
* @var MsgIdHandler
2020-02-07 21:13:49 +01:00
*/
public $msgIdHandler;
2019-09-01 14:07:04 +02:00
/**
2019-09-01 23:39:29 +02:00
* Reset MTProto session.
2019-09-01 14:07:04 +02:00
*
* @return void
*/
2020-02-07 21:13:49 +01:00
public function resetSession(): void
2019-09-01 14:07:04 +02:00
{
$this->API->logger->logger("Resetting session in DC {$this->datacenterId}...", \danog\MadelineProto\Logger::WARNING);
2019-10-29 23:02:47 +01:00
$this->session_id = \danog\MadelineProto\Tools::random(8);
2019-09-01 14:07:04 +02:00
$this->session_in_seq_no = 0;
$this->session_out_seq_no = 0;
if (!isset($this->msgIdHandler)) {
$this->msgIdHandler = MsgIdHandler::createInstance($this);
}
2020-07-12 01:27:26 +02:00
foreach ($this->outgoing_messages as &$msg) {
if ($msg->hasMsgId()) {
$msg->setMsgId(null);
2020-07-12 01:27:26 +02:00
}
if ($msg->hasSeqNo()) {
$msg->setSeqNo(null);
2020-07-12 01:27:26 +02:00
}
}
2019-09-01 14:07:04 +02:00
}
/**
2019-09-02 17:08:36 +02:00
* Create MTProto session if needed.
*
* @return void
*/
2020-02-07 21:13:49 +01:00
public function createSession(): void
{
if ($this->session_id === null) {
2020-02-07 21:13:49 +01:00
$this->resetSession();
}
}
2019-09-04 17:48:07 +02:00
/**
2019-09-12 18:56:26 +02:00
* Backup eventual unsent messages before session deletion.
2019-09-04 17:48:07 +02:00
*
* @return array
*/
public function backupSession(): array
{
$pending = \array_values($this->pendingOutgoing);
return \array_merge($pending, $this->new_outgoing);
2019-09-04 17:48:07 +02:00
}
2019-08-31 23:07:20 +02:00
}