tdesktop/Telegram/SourceFiles/mtproto/session_private.h

238 lines
6.5 KiB
C
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
2019-11-20 10:41:14 +00:00
#include "mtproto/details/mtproto_received_ids_manager.h"
#include "mtproto/details/mtproto_serialized_request.h"
2019-11-18 09:28:14 +00:00
#include "mtproto/mtproto_auth_key.h"
#include "mtproto/mtproto_dc_options.h"
#include "mtproto/connection_abstract.h"
2019-11-13 08:31:12 +00:00
#include "mtproto/facade.h"
2018-08-10 19:19:46 +00:00
#include "base/openssl_help.h"
#include "base/timer.h"
namespace MTP {
2019-11-13 14:12:04 +00:00
namespace details {
class BoundKeyCreator;
2019-11-13 14:12:04 +00:00
} // namespace details
class Instance;
2019-12-02 13:10:19 +00:00
namespace details {
class AbstractConnection;
class SessionData;
class RSAPublicKey;
2019-12-02 13:10:19 +00:00
struct SessionOptions;
2019-12-02 13:10:19 +00:00
class SessionPrivate final : public QObject {
public:
2019-12-02 13:10:19 +00:00
SessionPrivate(
not_null<Instance*> instance,
not_null<QThread*> thread,
std::shared_ptr<SessionData> data,
ShiftedDcId shiftedDcId);
2019-12-02 13:10:19 +00:00
~SessionPrivate();
[[nodiscard]] int32 getShiftedDcId() const;
void dcOptionsChanged();
void cdnConfigChanged();
[[nodiscard]] int32 getState() const;
[[nodiscard]] QString transport() const;
void updateAuthKey();
void restartNow();
void sendPingForce();
void tryToSend();
private:
static constexpr auto kUpdateStateAlways = 666;
struct TestConnection {
ConnectionPointer data;
int priority = 0;
};
2019-12-02 10:32:09 +00:00
struct SentContainer {
crl::time sent = 0;
std::vector<mtpMsgId> messages;
};
enum class HandleResult {
Success,
Ignored,
RestartConnection,
ResetSession,
DestroyTemporaryKey,
ParseError,
};
void connectToServer(bool afterConfig = false);
void connectingTimedOut();
2015-08-08 09:14:47 +00:00
void doDisconnect();
void restart();
void requestCDNConfig();
void handleError(int errorCode);
void onError(
not_null<AbstractConnection*> connection,
qint32 errorCode);
void onConnected(not_null<AbstractConnection*> connection);
void onDisconnected(not_null<AbstractConnection*> connection);
void onSentSome(uint64 size);
void onReceivedSome();
void handleReceived();
void retryByTimer();
void waitConnectedFailed();
void waitReceivedFailed();
void waitBetterFailed();
void markConnectionOld();
void sendPingByTimer();
2018-04-23 10:24:03 +00:00
void destroyAllConnections();
void confirmBestConnection();
void removeTestConnection(not_null<AbstractConnection*> connection);
2019-11-20 10:41:14 +00:00
[[nodiscard]] int16 getProtocolDcId() const;
void checkSentRequests();
2019-12-02 10:32:09 +00:00
void clearOldContainers();
mtpMsgId placeToContainer(
2019-12-02 13:10:19 +00:00
SerializedRequest &toSendRequest,
mtpMsgId &bigMsgId,
bool forceNewMsgId,
2019-12-02 13:10:19 +00:00
SerializedRequest &req);
mtpMsgId prepareToSend(
2019-12-02 13:10:19 +00:00
SerializedRequest &request,
mtpMsgId currentLastId,
bool forceNewMsgId);
mtpMsgId replaceMsgId(
2019-12-02 13:10:19 +00:00
SerializedRequest &request,
mtpMsgId newId);
bool sendSecureRequest(
2019-12-02 13:10:19 +00:00
SerializedRequest &&request,
bool needAnyResponse);
mtpRequestId wasSent(mtpMsgId msgId) const;
2019-07-18 14:06:38 +00:00
[[nodiscard]] HandleResult handleOneReceived(const mtpPrime *from, const mtpPrime *end, uint64 msgId, int32 serverTime, uint64 serverSalt, bool badTime);
2019-11-22 09:06:48 +00:00
[[nodiscard]] HandleResult handleBindResponse(
mtpMsgId requestMsgId,
const mtpBuffer &response);
mtpBuffer ungzip(const mtpPrime *from, const mtpPrime *end) const;
void handleMsgsStates(const QVector<MTPlong> &ids, const QByteArray &states);
// _sessionDataMutex must be locked for read.
bool setState(int state, int ifState = kUpdateStateAlways);
void appendTestConnection(
DcOptions::Variants::Protocol protocol,
const QString &ip,
int port,
const bytes::vector &protocolSecret);
// if badTime received - search for ids in sessionData->haveSent and sessionData->wereAcked and sync time/salt, return true if found
bool requestsFixTimeSalt(const QVector<MTPlong> &ids, int32 serverTime, uint64 serverSalt);
// if we had a confirmed fast request use its unixtime as a correct one.
void correctUnixtimeByFastRequest(
const QVector<MTPlong> &ids,
TimeId serverTime);
void correctUnixtimeWithBadLocal(TimeId serverTime);
// remove msgs with such ids from sessionData->haveSent, add to sessionData->wereAcked
void requestsAcked(const QVector<MTPlong> &ids, bool byResponse = false);
void resend(
mtpMsgId msgId,
crl::time msCanWait = 0,
2019-11-19 11:55:39 +00:00
bool forceContainer = false);
2019-11-22 09:06:48 +00:00
void resendAll();
void clearSpecialMsgId(mtpMsgId msgId);
2019-11-21 10:37:39 +00:00
[[nodiscard]] DcType tryAcquireKeyCreation();
2019-11-13 14:12:04 +00:00
void resetSession();
void checkAuthKey();
void authKeyChecked();
void destroyTemporaryKey();
void clearUnboundKeyCreator();
void releaseKeyCreationOnFail();
2019-11-20 11:50:19 +00:00
void applyAuthKey(AuthKeyPtr &&encryptionKey);
2019-12-09 14:32:51 +00:00
[[nodiscard]] bool noMediaKeyWithExistingRegularKey() const;
2019-11-20 13:33:45 +00:00
bool destroyOldEnoughPersistentKey();
2019-11-20 10:41:14 +00:00
void setCurrentKeyId(uint64 newKeyId);
void changeSessionId();
[[nodiscard]] bool markSessionAsStarted();
[[nodiscard]] uint32 nextRequestSeqNumber(bool needAck);
2019-11-21 10:37:39 +00:00
[[nodiscard]] bool realDcTypeChanged();
2020-04-07 11:19:52 +00:00
[[nodiscard]] MTPVector<MTPJSONObjectValue> prepareInitParams();
2019-11-21 10:37:39 +00:00
const not_null<Instance*> _instance;
2019-11-21 10:37:39 +00:00
const ShiftedDcId _shiftedDcId = 0;
DcType _realDcType = DcType();
DcType _currentDcType = DcType();
2019-11-21 10:37:39 +00:00
mutable QReadWriteLock _stateMutex;
int _state = DisconnectedState;
bool _needSessionReset = false;
ConnectionPointer _connection;
std::vector<TestConnection> _testConnections;
crl::time _startedConnectingAt = 0;
base::Timer _retryTimer; // exp retry timer
int _retryTimeout = 1;
qint64 _retryWillFinish = 0;
base::Timer _oldConnectionTimer;
bool _oldConnection = true;
base::Timer _waitForConnectedTimer;
base::Timer _waitForReceivedTimer;
base::Timer _waitForBetterTimer;
crl::time _waitForReceived = 0;
crl::time _waitForConnected = 0;
2019-11-14 07:13:17 +00:00
crl::time _firstSentAt = -1;
mtpPingId _pingId = 0;
mtpPingId _pingIdToSend = 0;
crl::time _pingSendAt = 0;
mtpMsgId _pingMsgId = 0;
base::Timer _pingSender;
2019-11-20 10:41:14 +00:00
base::Timer _checkSentRequestsTimer;
std::shared_ptr<SessionData> _sessionData;
2019-12-02 13:10:19 +00:00
std::unique_ptr<SessionOptions> _options;
2019-11-20 11:50:19 +00:00
AuthKeyPtr _encryptionKey;
2019-11-20 10:41:14 +00:00
uint64 _keyId = 0;
uint64 _sessionId = 0;
uint64 _sessionSalt = 0;
uint32 _messagesCounter = 0;
bool _sessionMarkedAsStarted = false;
QVector<MTPlong> _ackRequestData;
QVector<MTPlong> _resendRequestData;
base::flat_set<mtpMsgId> _stateRequestData;
2019-12-02 13:10:19 +00:00
ReceivedIdsManager _receivedMessageIds;
2019-11-22 09:06:48 +00:00
base::flat_map<mtpMsgId, mtpRequestId> _resendingIds;
base::flat_map<mtpMsgId, mtpRequestId> _ackedIds;
2019-12-02 13:10:19 +00:00
base::flat_map<mtpMsgId, SerializedRequest> _stateAndResendRequests;
2019-12-02 10:32:09 +00:00
base::flat_map<mtpMsgId, SentContainer> _sentContainers;
2019-12-02 13:10:19 +00:00
std::unique_ptr<BoundKeyCreator> _keyCreator;
2019-11-22 09:06:48 +00:00
mtpMsgId _bindMsgId = 0;
crl::time _bindMessageSent = 0;
};
2019-12-02 13:10:19 +00:00
} // namespace details
} // namespace MTP