2022-12-30 21:54:44 +01:00
< ? php
declare ( strict_types = 1 );
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 >
2023-01-04 12:43:01 +01:00
* @ copyright 2016 - 2023 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
2022-12-30 19:21:36 +01:00
use danog\MadelineProto\Logger ;
2020-10-18 14:46:34 +02:00
use danog\MadelineProto\MTProto\IncomingMessage ;
use danog\MadelineProto\MTProto\OutgoingMessage ;
2022-12-30 19:21:36 +01:00
use danog\MadelineProto\Tools ;
use function time ;
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-09-22 11:48:12 +02:00
*
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
{
2022-08-13 16:36:51 +02:00
use AuthKeyHandler ;
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 ;
2020-07-09 18:23:16 +02:00
use Reliable ;
2020-04-05 14:57:33 +02:00
/**
* Incoming message array .
*
2023-01-04 15:13:55 +01:00
* @ var array < IncomingMessage >
2020-04-05 14:57:33 +02:00
*/
2023-01-04 15:13:55 +01:00
public array $incoming_messages = [];
2020-04-05 14:57:33 +02:00
/**
* Outgoing message array .
*
2023-01-04 15:13:55 +01:00
* @ var array < OutgoingMessage >
2020-04-05 14:57:33 +02:00
*/
2023-01-04 15:13:55 +01:00
public array $outgoing_messages = [];
2020-04-05 14:57:33 +02:00
/**
* New incoming message ID array .
*
2023-01-04 15:13:55 +01:00
* @ var array < IncomingMessage >
2020-04-05 14:57:33 +02:00
*/
2023-01-04 15:13:55 +01:00
public array $new_incoming = [];
2020-04-05 14:57:33 +02:00
/**
2020-10-18 14:46:34 +02:00
* New outgoing message array .
2020-04-05 14:57:33 +02:00
*
2023-01-04 15:13:55 +01:00
* @ var array < OutgoingMessage >
2020-04-05 14:57:33 +02:00
*/
2023-01-04 15:13:55 +01:00
public array $new_outgoing = [];
2020-04-05 14:57:33 +02:00
/**
* Pending outgoing messages .
*
2023-01-04 15:13:55 +01:00
* @ var array < OutgoingMessage >
2020-04-05 14:57:33 +02:00
*/
2023-01-04 15:13:55 +01:00
public array $pendingOutgoing = [];
2020-04-05 14:57:33 +02:00
/**
* Pending outgoing key .
*
*/
2023-01-20 14:37:09 +01:00
public int $pendingOutgoingKey = 0 ;
2020-04-05 14:57:33 +02:00
/**
* Time delta with server .
*
*/
2023-01-04 15:13:55 +01:00
public int $time_delta = 0 ;
2020-04-05 14:57:33 +02:00
/**
* Call queue .
*
*/
2023-01-04 15:13:55 +01:00
public array $call_queue = [];
2020-04-05 14:57:33 +02:00
/**
* Ack queue .
*
*/
2023-01-04 15:13:55 +01:00
public array $ack_queue = [];
2020-06-23 20:30:40 +02:00
/**
2020-02-07 21:13:49 +01:00
* Message ID handler .
*
*/
2023-01-04 15:13:55 +01:00
public MsgIdHandler $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
*/
2020-02-07 21:13:49 +01:00
public function resetSession () : void
2019-09-01 14:07:04 +02:00
{
2022-12-30 19:21:36 +01:00
$this -> API -> logger -> logger ( " Resetting session in DC { $this -> datacenterId } ... " , Logger :: WARNING );
$this -> session_id = Tools :: random ( 8 );
2019-09-01 14:07:04 +02:00
$this -> session_in_seq_no = 0 ;
$this -> session_out_seq_no = 0 ;
2023-01-14 19:51:23 +01:00
$this -> msgIdHandler ? ? = new MsgIdHandler ( $this );
2020-07-12 01:27:26 +02:00
foreach ( $this -> outgoing_messages as & $msg ) {
2020-10-18 14:46:34 +02:00
if ( $msg -> hasMsgId ()) {
$msg -> setMsgId ( null );
2020-07-12 01:27:26 +02:00
}
2020-10-18 14:46:34 +02:00
if ( $msg -> hasSeqNo ()) {
$msg -> setSeqNo ( null );
2020-07-12 01:27:26 +02:00
}
}
2019-09-01 14:07:04 +02:00
}
2021-12-05 17:37:23 +01:00
/**
* Cleanup incoming and outgoing messages .
*/
public function cleanupSession () : void
{
$count = 0 ;
$incoming = [];
foreach ( $this -> incoming_messages as $key => $message ) {
if ( $message -> canGarbageCollect ()) {
$count ++ ;
} else {
2022-12-30 19:21:36 +01:00
$this -> API -> logger -> logger ( " Can't garbage collect $message in DC { $this -> datacenter } , not handled yet! " , Logger :: VERBOSE );
2021-12-05 17:37:23 +01:00
$incoming [ $key ] = $message ;
}
}
$this -> incoming_messages = $incoming ;
$total = \count ( $this -> incoming_messages );
if ( $count + $total ) {
2022-12-30 19:21:36 +01:00
$this -> API -> logger -> logger ( " Garbage collected $count incoming messages in DC { $this -> datacenter } , $total left " , Logger :: VERBOSE );
2021-12-05 17:37:23 +01:00
}
$count = 0 ;
$outgoing = [];
foreach ( $this -> outgoing_messages as $key => $message ) {
if ( $message -> canGarbageCollect ()) {
$count ++ ;
} else {
$ago = \time () - $message -> getSent ();
if ( $ago > 2 ) {
2023-01-22 17:42:04 +01:00
$this -> API -> logger -> logger ( " Can't garbage collect $message in DC { $this -> datacenter } , no response has been received or it wasn't yet handled! " , Logger :: VERBOSE );
2021-12-05 17:37:23 +01:00
}
$outgoing [ $key ] = $message ;
}
}
$this -> outgoing_messages = $outgoing ;
$total = \count ( $this -> outgoing_messages );
if ( $count + $total ) {
2022-12-30 19:21:36 +01:00
$this -> API -> logger -> logger ( " Garbage collected $count outgoing messages in DC { $this -> datacenter } , $total left " , Logger :: VERBOSE );
2021-12-05 17:37:23 +01:00
}
}
2019-09-02 16:54:36 +02:00
/**
2019-09-02 17:08:36 +02:00
* Create MTProto session if needed .
2019-09-02 16:54:36 +02:00
*/
2020-02-07 21:13:49 +01:00
public function createSession () : void
2019-09-02 16:54:36 +02:00
{
if ( $this -> session_id === null ) {
2020-02-07 21:13:49 +01:00
$this -> resetSession ();
2019-09-02 16:54:36 +02:00
}
}
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
*
2023-01-04 15:13:55 +01:00
* @ return array < OutgoingMessage >
2019-09-04 17:48:07 +02:00
*/
public function backupSession () : array
{
2020-10-18 14:46:34 +02:00
$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
}