2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2019-01-18 11:26:43 +00:00
|
|
|
#include "base/timer.h"
|
2021-03-12 12:48:00 +00:00
|
|
|
#include "mtproto/mtproto_response.h"
|
2019-11-13 14:12:04 +00:00
|
|
|
#include "mtproto/mtproto_proxy_data.h"
|
2019-11-22 09:15:38 +00:00
|
|
|
#include "mtproto/details/mtproto_serialized_request.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-09-13 06:06:02 +00:00
|
|
|
#include <QtCore/QTimer>
|
|
|
|
|
2016-03-24 08:57:11 +00:00
|
|
|
namespace MTP {
|
2017-02-24 17:15:41 +00:00
|
|
|
|
|
|
|
class Instance;
|
2017-06-26 17:38:16 +00:00
|
|
|
class AuthKey;
|
|
|
|
using AuthKeyPtr = std::shared_ptr<AuthKey>;
|
2019-12-28 10:52:29 +00:00
|
|
|
enum class DcType;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
namespace details {
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-06-26 17:38:16 +00:00
|
|
|
class Dcenter;
|
2019-12-02 13:10:19 +00:00
|
|
|
class SessionPrivate;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2019-11-21 10:37:39 +00:00
|
|
|
enum class TemporaryKeyType;
|
|
|
|
enum class CreatingKeyType;
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
struct SessionOptions {
|
|
|
|
SessionOptions() = default;
|
|
|
|
SessionOptions(
|
2018-04-24 08:46:27 +00:00
|
|
|
const QString &systemLangCode,
|
|
|
|
const QString &cloudLangCode,
|
2018-08-20 11:31:40 +00:00
|
|
|
const QString &langPackName,
|
2018-04-24 08:46:27 +00:00
|
|
|
const ProxyData &proxy,
|
|
|
|
bool useIPv4,
|
|
|
|
bool useIPv6,
|
|
|
|
bool useHttp,
|
|
|
|
bool useTcp);
|
|
|
|
|
|
|
|
QString systemLangCode;
|
|
|
|
QString cloudLangCode;
|
2018-08-20 11:31:40 +00:00
|
|
|
QString langPackName;
|
2018-04-24 08:46:27 +00:00
|
|
|
ProxyData proxy;
|
|
|
|
bool useIPv4 = true;
|
|
|
|
bool useIPv6 = true;
|
|
|
|
bool useHttp = true;
|
|
|
|
bool useTcp = true;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-12-07 13:32:25 +00:00
|
|
|
class Session;
|
2019-12-02 13:10:19 +00:00
|
|
|
class SessionData final {
|
2014-05-30 08:53:19 +00:00
|
|
|
public:
|
2019-11-27 14:22:22 +00:00
|
|
|
explicit SessionData(not_null<Session*> creator) : _owner(creator) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
void notifyConnectionInited(const SessionOptions &options);
|
|
|
|
void setOptions(SessionOptions options) {
|
2019-11-20 10:41:14 +00:00
|
|
|
QWriteLocker locker(&_optionsLock);
|
2018-04-24 08:46:27 +00:00
|
|
|
_options = options;
|
2017-05-30 09:31:32 +00:00
|
|
|
}
|
2019-12-02 13:10:19 +00:00
|
|
|
[[nodiscard]] SessionOptions options() const {
|
2019-11-20 10:41:14 +00:00
|
|
|
QReadLocker locker(&_optionsLock);
|
2018-04-24 08:46:27 +00:00
|
|
|
return _options;
|
2017-04-13 17:59:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
not_null<QReadWriteLock*> toSendMutex() {
|
2017-04-30 14:23:57 +00:00
|
|
|
return &_toSendLock;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2019-11-22 09:15:38 +00:00
|
|
|
not_null<QReadWriteLock*> haveSentMutex() {
|
2017-04-30 14:23:57 +00:00
|
|
|
return &_haveSentLock;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2019-11-22 09:15:38 +00:00
|
|
|
not_null<QReadWriteLock*> haveReceivedMutex() {
|
2017-04-30 14:23:57 +00:00
|
|
|
return &_haveReceivedLock;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
base::flat_map<mtpRequestId, SerializedRequest> &toSendMap() {
|
2017-04-30 14:23:57 +00:00
|
|
|
return _toSend;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2019-12-02 13:10:19 +00:00
|
|
|
base::flat_map<mtpMsgId, SerializedRequest> &haveSentMap() {
|
2017-04-30 14:23:57 +00:00
|
|
|
return _haveSent;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2021-03-12 10:45:13 +00:00
|
|
|
std::vector<Response> &haveReceivedMessages() {
|
|
|
|
return _receivedMessages;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
// SessionPrivate -> Session interface.
|
2019-11-18 12:53:37 +00:00
|
|
|
void queueTryToReceive();
|
|
|
|
void queueNeedToResumeAndSend();
|
|
|
|
void queueConnectionStateChange(int newState);
|
|
|
|
void queueResetDone();
|
|
|
|
void queueSendAnything(crl::time msCanWait = 0);
|
|
|
|
|
|
|
|
[[nodiscard]] bool connectionInited() const;
|
2019-11-19 10:10:51 +00:00
|
|
|
[[nodiscard]] AuthKeyPtr getPersistentKey() const;
|
2019-11-21 10:37:39 +00:00
|
|
|
[[nodiscard]] AuthKeyPtr getTemporaryKey(TemporaryKeyType type) const;
|
2019-12-28 10:52:29 +00:00
|
|
|
[[nodiscard]] CreatingKeyType acquireKeyCreation(DcType type);
|
2019-11-21 10:37:39 +00:00
|
|
|
[[nodiscard]] bool releaseKeyCreationOnDone(
|
2019-11-19 10:10:51 +00:00
|
|
|
const AuthKeyPtr &temporaryKey,
|
2019-11-21 10:37:39 +00:00
|
|
|
const AuthKeyPtr &persistentKeyUsedForBind);
|
2019-12-28 10:52:29 +00:00
|
|
|
[[nodiscard]] bool releaseCdnKeyCreationOnDone(
|
|
|
|
const AuthKeyPtr &temporaryKey);
|
2019-11-18 12:53:37 +00:00
|
|
|
void releaseKeyCreationOnFail();
|
2019-11-19 10:10:51 +00:00
|
|
|
void destroyTemporaryKey(uint64 keyId);
|
2019-11-18 12:53:37 +00:00
|
|
|
|
|
|
|
void detach();
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
private:
|
2019-11-18 12:53:37 +00:00
|
|
|
template <typename Callback>
|
|
|
|
void withSession(Callback &&callback);
|
|
|
|
|
|
|
|
Session *_owner = nullptr;
|
|
|
|
mutable QMutex _ownerMutex;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
SessionOptions _options;
|
2019-11-22 09:15:38 +00:00
|
|
|
mutable QReadWriteLock _optionsLock;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
base::flat_map<mtpRequestId, SerializedRequest> _toSend; // map of request_id -> request, that is waiting to be sent
|
2019-11-22 09:15:38 +00:00
|
|
|
QReadWriteLock _toSendLock;
|
2017-04-30 14:23:57 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
base::flat_map<mtpMsgId, SerializedRequest> _haveSent; // map of msg_id -> request, that was sent
|
2019-11-22 09:15:38 +00:00
|
|
|
QReadWriteLock _haveSentLock;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
std::vector<Response> _receivedMessages; // list of responses / updates that should be processed in the main thread
|
2019-11-22 09:15:38 +00:00
|
|
|
QReadWriteLock _haveReceivedLock;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
class Session final : public QObject {
|
2014-05-30 08:53:19 +00:00
|
|
|
public:
|
2019-11-15 13:04:32 +00:00
|
|
|
// Main thread.
|
2019-11-15 07:28:33 +00:00
|
|
|
Session(
|
|
|
|
not_null<Instance*> instance,
|
2019-11-27 15:09:56 +00:00
|
|
|
not_null<QThread*> thread,
|
2019-11-15 07:28:33 +00:00
|
|
|
ShiftedDcId shiftedDcId,
|
2019-11-20 13:33:45 +00:00
|
|
|
not_null<Dcenter*> dc);
|
2019-11-15 13:04:32 +00:00
|
|
|
~Session();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
void start();
|
2019-11-15 13:04:32 +00:00
|
|
|
void reInitConnection();
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void restart();
|
2018-05-17 19:58:00 +00:00
|
|
|
void refreshOptions();
|
2014-05-30 08:53:19 +00:00
|
|
|
void stop();
|
2015-05-14 16:50:04 +00:00
|
|
|
void kill();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-10-15 10:18:24 +00:00
|
|
|
void unpaused();
|
|
|
|
|
2019-11-15 13:04:32 +00:00
|
|
|
// Thread-safe.
|
|
|
|
[[nodiscard]] ShiftedDcId getDcWithShift() const;
|
2019-11-19 10:10:51 +00:00
|
|
|
[[nodiscard]] AuthKeyPtr getPersistentKey() const;
|
2019-11-21 10:37:39 +00:00
|
|
|
[[nodiscard]] AuthKeyPtr getTemporaryKey(TemporaryKeyType type) const;
|
2019-11-15 13:04:32 +00:00
|
|
|
[[nodiscard]] bool connectionInited() const;
|
2019-11-22 09:15:38 +00:00
|
|
|
void sendPrepared(
|
2019-12-02 13:10:19 +00:00
|
|
|
const SerializedRequest &request,
|
2019-11-22 09:15:38 +00:00
|
|
|
crl::time msCanWait = 0);
|
2019-11-15 13:04:32 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
// SessionPrivate thread.
|
2019-12-28 10:52:29 +00:00
|
|
|
[[nodiscard]] CreatingKeyType acquireKeyCreation(DcType type);
|
2019-11-21 10:37:39 +00:00
|
|
|
[[nodiscard]] bool releaseKeyCreationOnDone(
|
2019-11-19 10:10:51 +00:00
|
|
|
const AuthKeyPtr &temporaryKey,
|
2019-11-21 10:37:39 +00:00
|
|
|
const AuthKeyPtr &persistentKeyUsedForBind);
|
2019-12-28 10:52:29 +00:00
|
|
|
[[nodiscard]] bool releaseCdnKeyCreationOnDone(const AuthKeyPtr &temporaryKey);
|
2019-11-15 13:04:32 +00:00
|
|
|
void releaseKeyCreationOnFail();
|
2019-11-19 10:10:51 +00:00
|
|
|
void destroyTemporaryKey(uint64 keyId);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
void notifyDcConnectionInited();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-04-16 14:59:42 +00:00
|
|
|
void ping();
|
2014-11-05 17:43:32 +00:00
|
|
|
void cancel(mtpRequestId requestId, mtpMsgId msgId);
|
2019-11-27 14:22:22 +00:00
|
|
|
int requestState(mtpRequestId requestId) const;
|
|
|
|
int getState() const;
|
2014-05-30 08:53:19 +00:00
|
|
|
QString transport() const;
|
|
|
|
|
2019-11-18 12:53:37 +00:00
|
|
|
void tryToReceive();
|
|
|
|
void needToResumeAndSend();
|
|
|
|
void connectionStateChange(int newState);
|
|
|
|
void resetDone();
|
|
|
|
void sendAnything(crl::time msCanWait = 0);
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
private:
|
2019-11-18 12:53:37 +00:00
|
|
|
void watchDcKeyChanges();
|
2019-11-27 14:22:22 +00:00
|
|
|
void watchDcOptionsChanges();
|
|
|
|
|
|
|
|
void killConnection();
|
2016-10-24 15:36:17 +00:00
|
|
|
|
2019-12-28 10:52:29 +00:00
|
|
|
[[nodiscard]] bool releaseGenericKeyCreationOnDone(
|
|
|
|
const AuthKeyPtr &temporaryKey,
|
|
|
|
const AuthKeyPtr &persistentKeyUsedForBind);
|
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
const not_null<Instance*> _instance;
|
|
|
|
const ShiftedDcId _shiftedDcId = 0;
|
2019-11-15 13:04:32 +00:00
|
|
|
const not_null<Dcenter*> _dc;
|
2019-11-18 12:53:37 +00:00
|
|
|
const std::shared_ptr<SessionData> _data;
|
2019-11-27 15:09:56 +00:00
|
|
|
const not_null<QThread*> _thread;
|
2019-11-27 14:22:22 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
SessionPrivate *_private = nullptr;
|
2015-05-14 16:50:04 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
bool _killed = false;
|
|
|
|
bool _needToReceive = false;
|
2016-02-29 16:53:26 +00:00
|
|
|
|
2019-11-14 13:34:58 +00:00
|
|
|
AuthKeyPtr _dcKeyForCheck;
|
2019-11-21 10:37:39 +00:00
|
|
|
CreatingKeyType _myKeyCreation = CreatingKeyType();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-11-14 07:13:17 +00:00
|
|
|
crl::time _msSendCall = 0;
|
|
|
|
crl::time _msWait = 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
bool _ping = false;
|
2015-04-16 14:59:42 +00:00
|
|
|
|
2019-11-14 07:13:17 +00:00
|
|
|
base::Timer _sender;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2019-11-18 12:53:37 +00:00
|
|
|
rpl::lifetime _lifetime;
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
} // namespace details
|
2016-03-24 08:57:11 +00:00
|
|
|
} // namespace MTP
|