2017-02-24 17:15:41 +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.
|
2017-02-24 17:15:41 +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
|
2017-02-24 17:15:41 +00:00
|
|
|
*/
|
|
|
|
#include "mtproto/mtp_instance.h"
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
#include "mtproto/details/mtproto_dcenter.h"
|
|
|
|
#include "mtproto/details/mtproto_rsa_public_key.h"
|
|
|
|
#include "mtproto/special_config_request.h"
|
2017-12-18 12:40:15 +00:00
|
|
|
#include "mtproto/session.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "mtproto/mtproto_config.h"
|
|
|
|
#include "mtproto/mtproto_dc_options.h"
|
2017-06-26 17:38:16 +00:00
|
|
|
#include "mtproto/config_loader.h"
|
|
|
|
#include "mtproto/sender.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2018-09-12 17:02:30 +00:00
|
|
|
#include "calls/calls_instance.h"
|
2019-11-13 08:31:12 +00:00
|
|
|
#include "main/main_account.h" // Account::configUpdated.
|
2017-04-13 17:59:05 +00:00
|
|
|
#include "apiwrap.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2021-06-10 22:49:08 +00:00
|
|
|
#include "core/core_settings.h"
|
2017-04-13 17:59:05 +00:00
|
|
|
#include "lang/lang_instance.h"
|
2017-05-30 09:31:32 +00:00
|
|
|
#include "lang/lang_cloud_manager.h"
|
2019-09-26 10:55:35 +00:00
|
|
|
#include "base/unixtime.h"
|
|
|
|
#include "base/call_delayed.h"
|
2017-04-13 17:59:05 +00:00
|
|
|
#include "base/timer.h"
|
2021-04-05 20:07:23 +00:00
|
|
|
#include "base/network_reachability.h"
|
2017-02-24 17:15:41 +00:00
|
|
|
|
|
|
|
namespace MTP {
|
2018-04-24 07:21:19 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kConfigBecomesOldIn = 2 * 60 * crl::time(1000);
|
|
|
|
constexpr auto kConfigBecomesOldForBlockedIn = 8 * crl::time(1000);
|
2018-04-24 07:21:19 +00:00
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
using namespace details;
|
2019-11-14 17:39:15 +00:00
|
|
|
|
2019-11-18 09:28:14 +00:00
|
|
|
std::atomic<int> GlobalAtomicRequestId = 0;
|
|
|
|
|
2018-04-24 07:21:19 +00:00
|
|
|
} // namespace
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
namespace details {
|
2019-11-18 09:28:14 +00:00
|
|
|
|
|
|
|
int GetNextRequestId() {
|
|
|
|
const auto result = ++GlobalAtomicRequestId;
|
|
|
|
if (result == std::numeric_limits<int>::max() / 2) {
|
|
|
|
GlobalAtomicRequestId = 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-12-02 13:10:19 +00:00
|
|
|
} // namespace details
|
2019-11-18 09:28:14 +00:00
|
|
|
|
2017-04-18 15:21:03 +00:00
|
|
|
class Instance::Private : private Sender {
|
2017-02-24 17:15:41 +00:00
|
|
|
public:
|
2020-06-17 09:36:25 +00:00
|
|
|
Private(
|
|
|
|
not_null<Instance*> instance,
|
|
|
|
Instance::Mode mode,
|
|
|
|
Fields &&fields);
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
void start();
|
|
|
|
|
|
|
|
[[nodiscard]] Config &config() const;
|
|
|
|
[[nodiscard]] const ConfigFields &configValues() const;
|
|
|
|
[[nodiscard]] DcOptions &dcOptions() const;
|
|
|
|
[[nodiscard]] Environment environment() const;
|
|
|
|
[[nodiscard]] bool isTestMode() const;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
void resolveProxyDomain(const QString &host);
|
|
|
|
void setGoodProxyDomain(const QString &host, const QString &ip);
|
2017-02-24 17:15:41 +00:00
|
|
|
void suggestMainDcId(DcId mainDcId);
|
|
|
|
void setMainDcId(DcId mainDcId);
|
2021-08-31 11:06:03 +00:00
|
|
|
[[nodiscard]] bool hasMainDcId() const;
|
2019-07-10 15:03:48 +00:00
|
|
|
[[nodiscard]] DcId mainDcId() const;
|
2021-08-30 17:32:30 +00:00
|
|
|
[[nodiscard]] rpl::producer<DcId> mainDcIdValue() const;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
[[nodiscard]] rpl::producer<> writeKeysRequests() const;
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
void dcPersistentKeyChanged(DcId dcId, const AuthKeyPtr &persistentKey);
|
|
|
|
void dcTemporaryKeyChanged(DcId dcId);
|
|
|
|
[[nodiscard]] rpl::producer<DcId> dcTemporaryKeyChanged() const;
|
2019-07-10 15:03:48 +00:00
|
|
|
[[nodiscard]] AuthKeysList getKeysForWrite() const;
|
2017-02-25 16:44:02 +00:00
|
|
|
void addKeysForDestroy(AuthKeysList &&keys);
|
2019-11-27 14:22:22 +00:00
|
|
|
[[nodiscard]] rpl::producer<> allKeysDestroyed() const;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2019-02-01 07:48:31 +00:00
|
|
|
// Thread safe.
|
2019-07-10 15:03:48 +00:00
|
|
|
[[nodiscard]] QString deviceModel() const;
|
|
|
|
[[nodiscard]] QString systemVersion() const;
|
2019-02-01 07:48:31 +00:00
|
|
|
|
2019-07-10 15:03:48 +00:00
|
|
|
// Main thread.
|
2017-04-13 17:59:05 +00:00
|
|
|
void requestConfig();
|
2018-04-24 07:21:19 +00:00
|
|
|
void requestConfigIfOld();
|
2017-04-13 17:59:05 +00:00
|
|
|
void requestCDNConfig();
|
2018-05-02 19:27:03 +00:00
|
|
|
void setUserPhone(const QString &phone);
|
2018-05-24 13:40:19 +00:00
|
|
|
void badConfigurationError();
|
2019-07-10 15:03:48 +00:00
|
|
|
void syncHttpUnixtime();
|
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
void restartedByTimeout(ShiftedDcId shiftedDcId);
|
|
|
|
[[nodiscard]] rpl::producer<ShiftedDcId> restartsByTimeout() const;
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
void restart();
|
|
|
|
void restart(ShiftedDcId shiftedDcId);
|
2019-07-10 15:03:48 +00:00
|
|
|
[[nodiscard]] int32 dcstate(ShiftedDcId shiftedDcId = 0);
|
|
|
|
[[nodiscard]] QString dctransport(ShiftedDcId shiftedDcId = 0);
|
2017-02-24 17:15:41 +00:00
|
|
|
void ping();
|
|
|
|
void cancel(mtpRequestId requestId);
|
2019-07-10 15:03:48 +00:00
|
|
|
[[nodiscard]] int32 state(mtpRequestId requestId); // < 0 means waiting for such count of ms
|
2017-02-24 17:15:41 +00:00
|
|
|
void killSession(ShiftedDcId shiftedDcId);
|
|
|
|
void stopSession(ShiftedDcId shiftedDcId);
|
2017-04-18 15:21:03 +00:00
|
|
|
void reInitConnection(DcId dcId);
|
2019-11-20 13:33:45 +00:00
|
|
|
void logout(Fn<void()> done);
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2019-11-15 07:28:33 +00:00
|
|
|
not_null<Dcenter*> getDcById(ShiftedDcId shiftedDcId);
|
|
|
|
Dcenter *findDc(ShiftedDcId shiftedDcId);
|
|
|
|
not_null<Dcenter*> addDc(
|
2019-11-14 17:39:15 +00:00
|
|
|
ShiftedDcId shiftedDcId,
|
|
|
|
AuthKeyPtr &&key = nullptr);
|
|
|
|
void removeDc(ShiftedDcId shiftedDcId);
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
void sendRequest(
|
|
|
|
mtpRequestId requestId,
|
2019-11-22 09:15:38 +00:00
|
|
|
SerializedRequest &&request,
|
2021-03-12 10:45:13 +00:00
|
|
|
ResponseHandler &&callbacks,
|
2018-06-02 14:29:21 +00:00
|
|
|
ShiftedDcId shiftedDcId,
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time msCanWait,
|
2018-06-02 14:29:21 +00:00
|
|
|
bool needsLayer,
|
|
|
|
mtpRequestId afterRequestId);
|
|
|
|
void registerRequest(mtpRequestId requestId, ShiftedDcId shiftedDcId);
|
2017-02-24 17:15:41 +00:00
|
|
|
void unregisterRequest(mtpRequestId requestId);
|
2018-06-02 14:29:21 +00:00
|
|
|
void storeRequest(
|
|
|
|
mtpRequestId requestId,
|
2019-11-22 09:15:38 +00:00
|
|
|
const SerializedRequest &request,
|
2021-03-12 10:45:13 +00:00
|
|
|
ResponseHandler &&callbacks);
|
2019-11-22 09:15:38 +00:00
|
|
|
SerializedRequest getRequest(mtpRequestId requestId);
|
2021-03-12 10:45:13 +00:00
|
|
|
[[nodiscard]] bool hasCallback(mtpRequestId requestId) const;
|
|
|
|
void processCallback(const Response &response);
|
|
|
|
void processUpdate(const Response &message);
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
void onStateChange(ShiftedDcId shiftedDcId, int32 state);
|
|
|
|
void onSessionReset(ShiftedDcId shiftedDcId);
|
2017-02-24 17:15:41 +00:00
|
|
|
|
|
|
|
// return true if need to clean request data
|
2021-03-12 10:45:13 +00:00
|
|
|
bool rpcErrorOccured(
|
|
|
|
const Response &response,
|
|
|
|
const FailHandler &onFail,
|
2021-03-12 12:48:00 +00:00
|
|
|
const Error &error);
|
2021-03-12 10:45:13 +00:00
|
|
|
inline bool rpcErrorOccured(
|
|
|
|
const Response &response,
|
|
|
|
const ResponseHandler &handler,
|
2021-03-12 12:48:00 +00:00
|
|
|
const Error &error) {
|
2021-03-12 10:45:13 +00:00
|
|
|
return rpcErrorOccured(response, handler.fail, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setUpdatesHandler(Fn<void(const Response&)> handler);
|
|
|
|
void setGlobalFailHandler(
|
2021-03-12 12:48:00 +00:00
|
|
|
Fn<void(const Error&, const Response&)> handler);
|
2018-06-04 15:35:11 +00:00
|
|
|
void setStateChangedHandler(Fn<void(ShiftedDcId shiftedDcId, int32 state)> handler);
|
|
|
|
void setSessionResetHandler(Fn<void(ShiftedDcId shiftedDcId)> handler);
|
2017-02-24 17:15:41 +00:00
|
|
|
void clearGlobalHandlers();
|
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
[[nodiscard]] not_null<Session*> getSession(ShiftedDcId shiftedDcId);
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2017-06-26 17:38:16 +00:00
|
|
|
bool isNormal() const {
|
|
|
|
return (_mode == Instance::Mode::Normal);
|
|
|
|
}
|
2017-02-25 16:44:02 +00:00
|
|
|
bool isKeysDestroyer() const {
|
|
|
|
return (_mode == Instance::Mode::KeysDestroyer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scheduleKeyDestroy(ShiftedDcId shiftedDcId);
|
2019-11-20 13:33:45 +00:00
|
|
|
void keyWasPossiblyDestroyed(ShiftedDcId shiftedDcId);
|
2017-02-25 16:44:02 +00:00
|
|
|
void performKeyDestroy(ShiftedDcId shiftedDcId);
|
|
|
|
void completedKeyDestroy(ShiftedDcId shiftedDcId);
|
2019-11-20 13:33:45 +00:00
|
|
|
void keyDestroyedOnServer(ShiftedDcId shiftedDcId, uint64 keyId);
|
2017-02-25 16:44:02 +00:00
|
|
|
|
2017-02-25 17:55:11 +00:00
|
|
|
void prepareToDestroy();
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
[[nodiscard]] rpl::lifetime &lifetime();
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
private:
|
2021-03-12 10:45:13 +00:00
|
|
|
void importDone(
|
|
|
|
const MTPauth_Authorization &result,
|
|
|
|
const Response &response);
|
2021-03-12 12:48:00 +00:00
|
|
|
bool importFail(const Error &error, const Response &response);
|
2021-03-12 10:45:13 +00:00
|
|
|
void exportDone(
|
|
|
|
const MTPauth_ExportedAuthorization &result,
|
|
|
|
const Response &response);
|
2021-03-12 12:48:00 +00:00
|
|
|
bool exportFail(const Error &error, const Response &response);
|
|
|
|
bool onErrorDefault(const Error &error, const Response &response);
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2020-06-11 16:09:46 +00:00
|
|
|
void unpaused();
|
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
Session *findSession(ShiftedDcId shiftedDcId);
|
|
|
|
not_null<Session*> startSession(ShiftedDcId shiftedDcId);
|
2021-08-30 17:32:30 +00:00
|
|
|
void scheduleSessionDestroy(ShiftedDcId shiftedDcId);
|
2019-11-27 15:09:56 +00:00
|
|
|
[[nodiscard]] not_null<QThread*> getThreadForDc(ShiftedDcId shiftedDcId);
|
2019-11-14 17:39:15 +00:00
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
void applyDomainIps(
|
|
|
|
const QString &host,
|
|
|
|
const QStringList &ips,
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time expireAt);
|
2018-05-17 19:58:00 +00:00
|
|
|
|
2017-12-18 12:40:15 +00:00
|
|
|
void logoutGuestDcs();
|
2017-02-24 17:15:41 +00:00
|
|
|
bool logoutGuestDone(mtpRequestId requestId);
|
|
|
|
|
2018-05-02 19:52:57 +00:00
|
|
|
void requestConfigIfExpired();
|
2017-02-24 17:15:41 +00:00
|
|
|
void configLoadDone(const MTPConfig &result);
|
2021-03-12 12:48:00 +00:00
|
|
|
bool configLoadFail(const Error &error);
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<ShiftedDcId> queryRequestByDc(
|
2018-04-27 13:08:12 +00:00
|
|
|
mtpRequestId requestId) const;
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<ShiftedDcId> changeRequestByDc(
|
2018-04-27 13:08:12 +00:00
|
|
|
mtpRequestId requestId, DcId newdc);
|
2017-03-23 16:11:35 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
void checkDelayedRequests();
|
|
|
|
|
2019-07-24 08:46:23 +00:00
|
|
|
const not_null<Instance*> _instance;
|
|
|
|
const Instance::Mode _mode = Instance::Mode::Normal;
|
2020-06-17 09:36:25 +00:00
|
|
|
const std::unique_ptr<Config> _config;
|
2021-04-05 20:07:23 +00:00
|
|
|
const std::shared_ptr<base::NetworkReachability> _networkReachability;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2019-11-27 15:09:56 +00:00
|
|
|
std::unique_ptr<QThread> _mainSessionThread;
|
|
|
|
std::unique_ptr<QThread> _otherSessionsThread;
|
|
|
|
std::vector<std::unique_ptr<QThread>> _fileSessionThreads;
|
|
|
|
|
2021-11-16 16:48:40 +00:00
|
|
|
QString _deviceModelDefault;
|
2019-02-01 07:48:31 +00:00
|
|
|
QString _systemVersion;
|
|
|
|
|
2021-11-16 16:48:40 +00:00
|
|
|
mutable QMutex _deviceModelMutex;
|
|
|
|
QString _customDeviceModel;
|
|
|
|
|
2021-08-30 17:32:30 +00:00
|
|
|
rpl::variable<DcId> _mainDcId = Fields::kDefaultMainDc;
|
2019-11-15 07:28:33 +00:00
|
|
|
bool _mainDcIdForced = false;
|
|
|
|
base::flat_map<DcId, std::unique_ptr<Dcenter>> _dcenters;
|
|
|
|
std::vector<std::unique_ptr<Dcenter>> _dcentersToDestroy;
|
2019-11-19 10:10:51 +00:00
|
|
|
rpl::event_stream<DcId> _dcTemporaryKeyChanged;
|
2019-11-15 07:28:33 +00:00
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
Session *_mainSession = nullptr;
|
|
|
|
base::flat_map<ShiftedDcId, std::unique_ptr<Session>> _sessions;
|
2019-11-15 07:28:33 +00:00
|
|
|
std::vector<std::unique_ptr<Session>> _sessionsToDestroy;
|
2019-12-05 11:46:28 +00:00
|
|
|
rpl::event_stream<ShiftedDcId> _restartsByTimeout;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
std::unique_ptr<ConfigLoader> _configLoader;
|
2018-05-17 19:58:00 +00:00
|
|
|
std::unique_ptr<DomainResolver> _domainResolver;
|
2019-07-10 15:03:48 +00:00
|
|
|
std::unique_ptr<SpecialConfigRequest> _httpUnixtimeLoader;
|
2018-05-02 19:27:03 +00:00
|
|
|
QString _userPhone;
|
2017-03-23 16:11:35 +00:00
|
|
|
mtpRequestId _cdnConfigLoadRequestId = 0;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time _lastConfigLoadedTime = 0;
|
|
|
|
crl::time _configExpiresAt = 0;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
base::flat_map<DcId, AuthKeyPtr> _keysForWrite;
|
|
|
|
base::flat_map<ShiftedDcId, mtpRequestId> _logoutGuestRequestIds;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
rpl::event_stream<> _writeKeysRequests;
|
2019-11-27 14:22:22 +00:00
|
|
|
rpl::event_stream<> _allKeysDestroyed;
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
// holds dcWithShift for request to this dc or -dc for request to main dc
|
|
|
|
std::map<mtpRequestId, ShiftedDcId> _requestsByDc;
|
2018-04-27 13:08:12 +00:00
|
|
|
mutable QMutex _requestByDcLock;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
|
|
|
// holds target dcWithShift for auth export request
|
|
|
|
std::map<mtpRequestId, ShiftedDcId> _authExportRequests;
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
std::map<mtpRequestId, ResponseHandler> _parserMap;
|
|
|
|
mutable QMutex _parserMapLock;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
std::map<mtpRequestId, SerializedRequest> _requestMap;
|
2017-02-24 17:15:41 +00:00
|
|
|
QReadWriteLock _requestMapLock;
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
std::deque<std::pair<mtpRequestId, crl::time>> _delayedRequests;
|
2021-07-06 08:20:18 +00:00
|
|
|
base::flat_map<mtpRequestId, mtpRequestId> _dependentRequests;
|
|
|
|
mutable QMutex _dependentRequestsLock;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
|
|
|
std::map<mtpRequestId, int> _requestsDelays;
|
|
|
|
|
|
|
|
std::set<mtpRequestId> _badGuestDcRequests;
|
|
|
|
|
|
|
|
std::map<DcId, std::vector<mtpRequestId>> _authWaiters;
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
Fn<void(const Response&)> _updatesHandler;
|
2021-03-12 12:48:00 +00:00
|
|
|
Fn<void(const Error&, const Response&)> _globalFailHandler;
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(ShiftedDcId shiftedDcId, int32 state)> _stateChangedHandler;
|
|
|
|
Fn<void(ShiftedDcId shiftedDcId)> _sessionResetHandler;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
base::Timer _checkDelayedTimer;
|
|
|
|
|
2021-06-10 22:49:08 +00:00
|
|
|
Core::SettingsProxy &_proxySettings;
|
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
rpl::lifetime _lifetime;
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
};
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
Instance::Fields::Fields() = default;
|
|
|
|
|
|
|
|
Instance::Fields::Fields(Fields &&other) = default;
|
|
|
|
|
|
|
|
auto Instance::Fields::operator=(Fields &&other) -> Fields & = default;
|
|
|
|
|
|
|
|
Instance::Fields::~Fields() = default;
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
Instance::Private::Private(
|
|
|
|
not_null<Instance*> instance,
|
2020-06-17 09:36:25 +00:00
|
|
|
Instance::Mode mode,
|
|
|
|
Fields &&fields)
|
2019-11-27 08:02:56 +00:00
|
|
|
: Sender(instance)
|
2017-06-26 17:38:16 +00:00
|
|
|
, _instance(instance)
|
2020-06-17 09:36:25 +00:00
|
|
|
, _mode(mode)
|
2021-04-05 20:07:23 +00:00
|
|
|
, _config(std::move(fields.config))
|
2021-06-10 22:49:08 +00:00
|
|
|
, _networkReachability(base::NetworkReachability::Instance())
|
|
|
|
, _proxySettings(Core::App().settings().proxy()) {
|
2020-06-17 09:36:25 +00:00
|
|
|
Expects(_config != nullptr);
|
|
|
|
|
2019-11-27 15:09:56 +00:00
|
|
|
const auto idealThreadPoolSize = QThread::idealThreadCount();
|
|
|
|
_fileSessionThreads.resize(2 * std::max(idealThreadPoolSize / 2, 1));
|
2020-06-17 09:36:25 +00:00
|
|
|
|
2020-06-11 16:09:46 +00:00
|
|
|
details::unpaused(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
unpaused();
|
|
|
|
}, _lifetime);
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2021-04-05 20:07:23 +00:00
|
|
|
_networkReachability->availableChanges(
|
|
|
|
) | rpl::start_with_next([=](bool available) {
|
|
|
|
restart();
|
|
|
|
}, _lifetime);
|
|
|
|
|
2021-11-16 16:48:40 +00:00
|
|
|
_deviceModelDefault = std::move(fields.deviceModel);
|
2020-06-17 09:36:25 +00:00
|
|
|
_systemVersion = std::move(fields.systemVersion);
|
2019-02-01 07:48:31 +00:00
|
|
|
|
2021-11-16 16:48:40 +00:00
|
|
|
_customDeviceModel = Core::App().settings().customDeviceModel();
|
|
|
|
Core::App().settings().customDeviceModelChanges(
|
|
|
|
) | rpl::start_with_next([=](const QString &value) {
|
|
|
|
QMutexLocker lock(&_deviceModelMutex);
|
|
|
|
_customDeviceModel = value;
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
reInitConnection(mainDcId());
|
|
|
|
}, _lifetime);
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
for (auto &key : fields.keys) {
|
2017-02-25 16:44:02 +00:00
|
|
|
auto dcId = key->dcId();
|
|
|
|
auto shiftedDcId = dcId;
|
|
|
|
if (isKeysDestroyer()) {
|
|
|
|
shiftedDcId = MTP::destroyKeyNextDcId(shiftedDcId);
|
|
|
|
|
|
|
|
// There could be several keys for one dc if we're destroying them.
|
|
|
|
// Place them all in separate shiftedDcId so that they won't conflict.
|
|
|
|
while (_keysForWrite.find(shiftedDcId) != _keysForWrite.cend()) {
|
|
|
|
shiftedDcId = MTP::destroyKeyNextDcId(shiftedDcId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_keysForWrite[shiftedDcId] = key;
|
2019-11-14 17:39:15 +00:00
|
|
|
addDc(shiftedDcId, std::move(key));
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
if (fields.mainDcId != Fields::kNotSetMainDc) {
|
|
|
|
_mainDcId = fields.mainDcId;
|
2017-02-24 17:15:41 +00:00
|
|
|
_mainDcIdForced = true;
|
|
|
|
}
|
2021-06-10 22:49:08 +00:00
|
|
|
|
|
|
|
_proxySettings.connectionTypeChanges(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
if (_configLoader) {
|
|
|
|
_configLoader->setProxyEnabled(_proxySettings.isEnabled());
|
|
|
|
}
|
|
|
|
}, _lifetime);
|
2020-06-17 09:36:25 +00:00
|
|
|
}
|
2017-02-25 16:44:02 +00:00
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
void Instance::Private::start() {
|
2017-02-25 16:44:02 +00:00
|
|
|
if (isKeysDestroyer()) {
|
2019-11-14 17:39:15 +00:00
|
|
|
for (const auto &[shiftedDcId, dc] : _dcenters) {
|
|
|
|
startSession(shiftedDcId);
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
2021-08-31 11:06:03 +00:00
|
|
|
} else if (hasMainDcId()) {
|
2021-08-30 17:32:30 +00:00
|
|
|
_mainSession = startSession(mainDcId());
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
_checkDelayedTimer.setCallback([this] { checkDelayedRequests(); });
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2021-08-31 11:06:03 +00:00
|
|
|
Assert(!hasMainDcId() == isKeysDestroyer());
|
2018-04-27 13:08:12 +00:00
|
|
|
requestConfig();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
void Instance::Private::resolveProxyDomain(const QString &host) {
|
|
|
|
if (!_domainResolver) {
|
|
|
|
_domainResolver = std::make_unique<DomainResolver>([=](
|
|
|
|
const QString &host,
|
|
|
|
const QStringList &ips,
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time expireAt) {
|
2018-05-17 19:58:00 +00:00
|
|
|
applyDomainIps(host, ips, expireAt);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_domainResolver->resolve(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::applyDomainIps(
|
|
|
|
const QString &host,
|
|
|
|
const QStringList &ips,
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time expireAt) {
|
2018-05-17 19:58:00 +00:00
|
|
|
const auto applyToProxy = [&](ProxyData &proxy) {
|
|
|
|
if (!proxy.tryCustomResolve() || proxy.host != host) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
proxy.resolvedExpireAt = expireAt;
|
|
|
|
auto copy = ips;
|
|
|
|
auto ¤t = proxy.resolvedIPs;
|
|
|
|
const auto i = ranges::remove_if(current, [&](const QString &ip) {
|
|
|
|
const auto index = copy.indexOf(ip);
|
|
|
|
if (index < 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
copy.removeAt(index);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
if (i == end(current) && copy.isEmpty()) {
|
|
|
|
// Even if the proxy was changed already, we still want
|
|
|
|
// to refreshOptions in all sessions across all instances.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
current.erase(i, end(current));
|
2021-03-13 08:26:58 +00:00
|
|
|
for (const auto &ip : std::as_const(copy)) {
|
2018-05-17 19:58:00 +00:00
|
|
|
proxy.resolvedIPs.push_back(ip);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2021-06-10 22:49:08 +00:00
|
|
|
for (auto &proxy : _proxySettings.list()) {
|
2018-05-17 19:58:00 +00:00
|
|
|
applyToProxy(proxy);
|
|
|
|
}
|
2021-06-10 22:49:08 +00:00
|
|
|
auto selected = _proxySettings.selected();
|
|
|
|
if (applyToProxy(selected) && _proxySettings.isEnabled()) {
|
|
|
|
_proxySettings.setSelected(selected);
|
2019-11-14 17:39:15 +00:00
|
|
|
for (const auto &[shiftedDcId, session] : _sessions) {
|
|
|
|
session->refreshOptions();
|
2018-05-17 19:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-03 18:22:42 +00:00
|
|
|
_instance->proxyDomainResolved(host, ips, expireAt);
|
2018-05-17 19:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::setGoodProxyDomain(
|
|
|
|
const QString &host,
|
|
|
|
const QString &ip) {
|
|
|
|
const auto applyToProxy = [&](ProxyData &proxy) {
|
|
|
|
if (!proxy.tryCustomResolve() || proxy.host != host) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto ¤t = proxy.resolvedIPs;
|
|
|
|
auto i = ranges::find(current, ip);
|
|
|
|
if (i == end(current) || i == begin(current)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while (i != begin(current)) {
|
|
|
|
const auto j = i--;
|
|
|
|
std::swap(*i, *j);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2021-06-10 22:49:08 +00:00
|
|
|
for (auto &proxy : _proxySettings.list()) {
|
2018-05-17 19:58:00 +00:00
|
|
|
applyToProxy(proxy);
|
|
|
|
}
|
2021-06-10 22:49:08 +00:00
|
|
|
|
|
|
|
auto selected = _proxySettings.selected();
|
|
|
|
if (applyToProxy(selected) && _proxySettings.isEnabled()) {
|
|
|
|
_proxySettings.setSelected(selected);
|
2019-01-18 11:26:43 +00:00
|
|
|
Core::App().refreshGlobalProxy();
|
2018-05-17 19:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
void Instance::Private::suggestMainDcId(DcId mainDcId) {
|
2020-06-26 11:28:06 +00:00
|
|
|
if (!_mainDcIdForced) {
|
|
|
|
setMainDcId(mainDcId);
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::setMainDcId(DcId mainDcId) {
|
|
|
|
if (!_mainSession) {
|
|
|
|
LOG(("MTP Error: attempting to change mainDcId in an MTP instance without main session."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_mainDcIdForced = true;
|
2021-08-30 17:32:30 +00:00
|
|
|
const auto oldMainDcId = _mainSession->getDcWithShift();
|
|
|
|
if (oldMainDcId != mainDcId) {
|
|
|
|
scheduleSessionDestroy(oldMainDcId);
|
|
|
|
scheduleSessionDestroy(mainDcId);
|
|
|
|
_mainSession = startSession(mainDcId);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2021-08-30 17:32:30 +00:00
|
|
|
_mainDcId = mainDcId;
|
2020-06-09 16:57:05 +00:00
|
|
|
_writeKeysRequests.fire({});
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 11:06:03 +00:00
|
|
|
bool Instance::Private::hasMainDcId() const {
|
|
|
|
return (_mainDcId.current() != Fields::kNoneMainDc);
|
|
|
|
}
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
DcId Instance::Private::mainDcId() const {
|
2021-08-31 11:06:03 +00:00
|
|
|
Expects(hasMainDcId());
|
2021-08-30 17:32:30 +00:00
|
|
|
|
|
|
|
return _mainDcId.current();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<DcId> Instance::Private::mainDcIdValue() const {
|
|
|
|
Expects(_mainDcId.current() != Fields::kNoneMainDc);
|
2020-06-17 09:36:25 +00:00
|
|
|
|
2021-08-30 17:32:30 +00:00
|
|
|
return _mainDcId.value();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
void Instance::Private::requestConfig() {
|
2018-04-27 13:08:12 +00:00
|
|
|
if (_configLoader || isKeysDestroyer()) {
|
2017-02-24 17:15:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-14 17:39:15 +00:00
|
|
|
_configLoader = std::make_unique<ConfigLoader>(
|
2018-05-02 19:27:03 +00:00
|
|
|
_instance,
|
|
|
|
_userPhone,
|
2021-03-12 10:45:13 +00:00
|
|
|
[=](const MTPConfig &result) { configLoadDone(result); },
|
2021-03-12 12:48:00 +00:00
|
|
|
[=](const Error &error, const Response &) {
|
2021-03-12 10:45:13 +00:00
|
|
|
return configLoadFail(error);
|
2021-06-10 22:49:08 +00:00
|
|
|
},
|
|
|
|
_proxySettings.isEnabled());
|
2017-02-24 17:15:41 +00:00
|
|
|
_configLoader->load();
|
|
|
|
}
|
|
|
|
|
2018-05-02 19:27:03 +00:00
|
|
|
void Instance::Private::setUserPhone(const QString &phone) {
|
|
|
|
if (_userPhone != phone) {
|
|
|
|
_userPhone = phone;
|
|
|
|
if (_configLoader) {
|
|
|
|
_configLoader->setPhone(_userPhone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 13:40:19 +00:00
|
|
|
void Instance::Private::badConfigurationError() {
|
|
|
|
if (_mode == Mode::Normal) {
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().badMtprotoConfigurationError();
|
2018-05-24 13:40:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 15:03:48 +00:00
|
|
|
void Instance::Private::syncHttpUnixtime() {
|
2019-07-10 17:28:33 +00:00
|
|
|
if (base::unixtime::http_valid() || _httpUnixtimeLoader) {
|
2019-07-10 15:03:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-10 17:28:33 +00:00
|
|
|
_httpUnixtimeLoader = std::make_unique<SpecialConfigRequest>([=] {
|
2019-07-10 15:03:48 +00:00
|
|
|
InvokeQueued(_instance, [=] {
|
2019-07-10 17:28:33 +00:00
|
|
|
_httpUnixtimeLoader = nullptr;
|
2019-07-10 15:03:48 +00:00
|
|
|
});
|
2020-06-17 09:36:25 +00:00
|
|
|
}, configValues().txtDomainString);
|
2019-07-10 15:03:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
void Instance::Private::restartedByTimeout(ShiftedDcId shiftedDcId) {
|
|
|
|
_restartsByTimeout.fire_copy(shiftedDcId);
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<ShiftedDcId> Instance::Private::restartsByTimeout() const {
|
|
|
|
return _restartsByTimeout.events();
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:21:19 +00:00
|
|
|
void Instance::Private::requestConfigIfOld() {
|
2020-06-17 09:36:25 +00:00
|
|
|
const auto timeout = _config->values().blockedMode
|
2018-05-02 19:52:57 +00:00
|
|
|
? kConfigBecomesOldForBlockedIn
|
|
|
|
: kConfigBecomesOldIn;
|
2019-02-19 06:57:53 +00:00
|
|
|
if (crl::now() - _lastConfigLoadedTime >= timeout) {
|
2018-05-02 19:52:57 +00:00
|
|
|
requestConfig();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::requestConfigIfExpired() {
|
2019-02-19 06:57:53 +00:00
|
|
|
const auto requestIn = (_configExpiresAt - crl::now());
|
2018-05-02 19:52:57 +00:00
|
|
|
if (requestIn > 0) {
|
2019-09-26 10:55:35 +00:00
|
|
|
base::call_delayed(
|
2019-02-19 06:57:53 +00:00
|
|
|
std::min(requestIn, 3600 * crl::time(1000)),
|
2018-05-02 19:52:57 +00:00
|
|
|
_instance,
|
|
|
|
[=] { requestConfigIfExpired(); });
|
|
|
|
} else {
|
2018-04-24 07:21:19 +00:00
|
|
|
requestConfig();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
void Instance::Private::requestCDNConfig() {
|
2021-08-31 11:06:03 +00:00
|
|
|
if (_cdnConfigLoadRequestId || !hasMainDcId()) {
|
2017-03-23 16:11:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-27 13:08:12 +00:00
|
|
|
_cdnConfigLoadRequestId = request(
|
|
|
|
MTPhelp_GetCdnConfig()
|
|
|
|
).done([this](const MTPCdnConfig &result) {
|
2017-03-23 16:11:35 +00:00
|
|
|
_cdnConfigLoadRequestId = 0;
|
2019-11-27 14:22:22 +00:00
|
|
|
result.match([&](const MTPDcdnConfig &data) {
|
2020-06-17 09:36:25 +00:00
|
|
|
dcOptions().setCDNConfig(data);
|
2019-11-27 14:22:22 +00:00
|
|
|
});
|
2017-03-23 16:11:35 +00:00
|
|
|
Local::writeSettings();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
void Instance::Private::restart() {
|
2019-11-14 17:39:15 +00:00
|
|
|
for (const auto &[shiftedDcId, session] : _sessions) {
|
|
|
|
session->restart();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::restart(ShiftedDcId shiftedDcId) {
|
2019-11-14 17:39:15 +00:00
|
|
|
const auto dcId = BareDcId(shiftedDcId);
|
|
|
|
for (const auto &[shiftedDcId, session] : _sessions) {
|
|
|
|
if (BareDcId(shiftedDcId) == dcId) {
|
|
|
|
session->restart();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 Instance::Private::dcstate(ShiftedDcId shiftedDcId) {
|
|
|
|
if (!shiftedDcId) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_mainSession != nullptr);
|
2017-02-24 17:15:41 +00:00
|
|
|
return _mainSession->getState();
|
|
|
|
}
|
|
|
|
|
2018-06-11 18:35:27 +00:00
|
|
|
if (!BareDcId(shiftedDcId)) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_mainSession != nullptr);
|
2018-06-11 18:35:27 +00:00
|
|
|
shiftedDcId += BareDcId(_mainSession->getDcWithShift());
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
if (const auto session = findSession(shiftedDcId)) {
|
|
|
|
return session->getState();
|
2017-02-27 18:47:29 +00:00
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
return DisconnectedState;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Instance::Private::dctransport(ShiftedDcId shiftedDcId) {
|
|
|
|
if (!shiftedDcId) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_mainSession != nullptr);
|
2017-02-24 17:15:41 +00:00
|
|
|
return _mainSession->transport();
|
|
|
|
}
|
2018-06-11 18:35:27 +00:00
|
|
|
if (!BareDcId(shiftedDcId)) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_mainSession != nullptr);
|
2018-06-11 18:35:27 +00:00
|
|
|
shiftedDcId += BareDcId(_mainSession->getDcWithShift());
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
if (const auto session = findSession(shiftedDcId)) {
|
|
|
|
return session->transport();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::ping() {
|
2018-06-02 14:29:21 +00:00
|
|
|
getSession(0)->ping();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::cancel(mtpRequestId requestId) {
|
|
|
|
if (!requestId) return;
|
|
|
|
|
2018-06-24 13:19:49 +00:00
|
|
|
DEBUG_LOG(("MTP Info: Cancel request %1.").arg(requestId));
|
2018-04-27 13:08:12 +00:00
|
|
|
const auto shiftedDcId = queryRequestByDc(requestId);
|
|
|
|
auto msgId = mtpMsgId(0);
|
2017-02-24 17:15:41 +00:00
|
|
|
{
|
|
|
|
QWriteLocker locker(&_requestMapLock);
|
|
|
|
auto it = _requestMap.find(requestId);
|
|
|
|
if (it != _requestMap.end()) {
|
|
|
|
msgId = *(mtpMsgId*)(it->second->constData() + 4);
|
|
|
|
_requestMap.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2018-04-27 13:08:12 +00:00
|
|
|
unregisterRequest(requestId);
|
|
|
|
if (shiftedDcId) {
|
2018-06-02 14:29:21 +00:00
|
|
|
const auto session = getSession(qAbs(*shiftedDcId));
|
|
|
|
session->cancel(requestId, msgId);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2019-12-02 09:40:53 +00:00
|
|
|
|
|
|
|
QMutexLocker locker(&_parserMapLock);
|
|
|
|
_parserMap.erase(requestId);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
// result < 0 means waiting for such count of ms.
|
|
|
|
int32 Instance::Private::state(mtpRequestId requestId) {
|
2017-02-24 17:15:41 +00:00
|
|
|
if (requestId > 0) {
|
2018-04-27 13:08:12 +00:00
|
|
|
if (const auto shiftedDcId = queryRequestByDc(requestId)) {
|
2018-06-02 14:29:21 +00:00
|
|
|
const auto session = getSession(qAbs(*shiftedDcId));
|
|
|
|
return session->requestState(requestId);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
return MTP::RequestSent;
|
|
|
|
}
|
2018-06-02 14:29:21 +00:00
|
|
|
const auto session = getSession(-requestId);
|
|
|
|
return session->requestState(0);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::killSession(ShiftedDcId shiftedDcId) {
|
2021-08-30 17:32:30 +00:00
|
|
|
const auto i = _sessions.find(shiftedDcId);
|
|
|
|
if (i != _sessions.cend()) {
|
|
|
|
Assert(i->second.get() != _mainSession);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2021-08-30 17:32:30 +00:00
|
|
|
scheduleSessionDestroy(shiftedDcId);
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
void Instance::Private::stopSession(ShiftedDcId shiftedDcId) {
|
2019-11-14 17:39:15 +00:00
|
|
|
if (const auto session = findSession(shiftedDcId)) {
|
|
|
|
if (session != _mainSession) { // don't stop main session
|
|
|
|
session->stop();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 15:21:03 +00:00
|
|
|
void Instance::Private::reInitConnection(DcId dcId) {
|
2019-11-14 17:39:15 +00:00
|
|
|
for (const auto &[shiftedDcId, session] : _sessions) {
|
|
|
|
if (BareDcId(shiftedDcId) == dcId) {
|
|
|
|
session->reInitConnection();
|
2018-05-17 19:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:33:45 +00:00
|
|
|
void Instance::Private::logout(Fn<void()> done) {
|
2021-03-12 10:45:13 +00:00
|
|
|
_instance->send(MTPauth_LogOut(), [=](Response) {
|
2019-11-20 13:33:45 +00:00
|
|
|
done();
|
2021-03-12 10:45:13 +00:00
|
|
|
return true;
|
2021-03-12 12:48:00 +00:00
|
|
|
}, [=](const Error&, Response) {
|
2019-11-20 13:33:45 +00:00
|
|
|
done();
|
|
|
|
return true;
|
2021-03-12 10:45:13 +00:00
|
|
|
});
|
2017-12-18 12:40:15 +00:00
|
|
|
logoutGuestDcs();
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2017-12-18 12:40:15 +00:00
|
|
|
void Instance::Private::logoutGuestDcs() {
|
2021-08-31 11:06:03 +00:00
|
|
|
Expects(!isKeysDestroyer());
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
auto dcIds = std::vector<DcId>();
|
2019-11-19 10:10:51 +00:00
|
|
|
dcIds.reserve(_keysForWrite.size());
|
|
|
|
for (const auto &key : _keysForWrite) {
|
|
|
|
dcIds.push_back(key.first);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2019-11-19 10:10:51 +00:00
|
|
|
for (const auto dcId : dcIds) {
|
2020-06-17 09:36:25 +00:00
|
|
|
if (dcId == mainDcId() || dcOptions().dcType(dcId) == DcType::Cdn) {
|
2019-11-19 10:10:51 +00:00
|
|
|
continue;
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2019-11-19 10:10:51 +00:00
|
|
|
const auto shiftedDcId = MTP::logoutDcId(dcId);
|
2021-03-12 10:45:13 +00:00
|
|
|
const auto requestId = _instance->send(MTPauth_LogOut(), [=](
|
|
|
|
const Response &response) {
|
|
|
|
logoutGuestDone(response.requestId);
|
|
|
|
return true;
|
2021-03-12 12:48:00 +00:00
|
|
|
}, [=](const Error &, const Response &response) {
|
2021-03-12 10:45:13 +00:00
|
|
|
logoutGuestDone(response.requestId);
|
|
|
|
return true;
|
|
|
|
}, shiftedDcId);
|
2019-11-19 10:10:51 +00:00
|
|
|
_logoutGuestRequestIds.emplace(shiftedDcId, requestId);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Instance::Private::logoutGuestDone(mtpRequestId requestId) {
|
|
|
|
for (auto i = _logoutGuestRequestIds.begin(), e = _logoutGuestRequestIds.end(); i != e; ++i) {
|
|
|
|
if (i->second == requestId) {
|
|
|
|
killSession(i->first);
|
|
|
|
_logoutGuestRequestIds.erase(i);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-15 07:28:33 +00:00
|
|
|
Dcenter *Instance::Private::findDc(ShiftedDcId shiftedDcId) {
|
2019-11-14 17:39:15 +00:00
|
|
|
const auto i = _dcenters.find(shiftedDcId);
|
2019-11-15 07:28:33 +00:00
|
|
|
return (i != _dcenters.end()) ? i->second.get() : nullptr;
|
2019-11-14 17:39:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 07:28:33 +00:00
|
|
|
not_null<Dcenter*> Instance::Private::addDc(
|
2019-11-14 17:39:15 +00:00
|
|
|
ShiftedDcId shiftedDcId,
|
|
|
|
AuthKeyPtr &&key) {
|
|
|
|
const auto dcId = BareDcId(shiftedDcId);
|
|
|
|
return _dcenters.emplace(
|
|
|
|
shiftedDcId,
|
2019-11-15 13:04:32 +00:00
|
|
|
std::make_unique<Dcenter>(dcId, std::move(key))
|
2019-11-15 07:28:33 +00:00
|
|
|
).first->second.get();
|
2019-11-14 17:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::removeDc(ShiftedDcId shiftedDcId) {
|
2019-11-15 07:28:33 +00:00
|
|
|
const auto i = _dcenters.find(shiftedDcId);
|
|
|
|
if (i != _dcenters.end()) {
|
|
|
|
_dcentersToDestroy.push_back(std::move(i->second));
|
|
|
|
_dcenters.erase(i);
|
|
|
|
}
|
2019-11-14 17:39:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 07:28:33 +00:00
|
|
|
not_null<Dcenter*> Instance::Private::getDcById(
|
2019-11-14 17:39:15 +00:00
|
|
|
ShiftedDcId shiftedDcId) {
|
2019-11-15 07:28:33 +00:00
|
|
|
if (const auto result = findDc(shiftedDcId)) {
|
2019-11-14 17:39:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
2019-11-15 07:28:33 +00:00
|
|
|
const auto dcId = [&] {
|
|
|
|
const auto result = BareDcId(shiftedDcId);
|
|
|
|
if (isTemporaryDcId(result)) {
|
|
|
|
if (const auto realDcId = getRealIdFromTemporaryDcId(result)) {
|
|
|
|
return realDcId;
|
|
|
|
}
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
2019-11-14 17:39:15 +00:00
|
|
|
return result;
|
2019-11-15 07:28:33 +00:00
|
|
|
}();
|
|
|
|
if (dcId != shiftedDcId) {
|
|
|
|
if (const auto result = findDc(dcId)) {
|
|
|
|
return result;
|
|
|
|
}
|
2019-11-14 17:39:15 +00:00
|
|
|
}
|
|
|
|
return addDc(dcId);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
void Instance::Private::dcPersistentKeyChanged(
|
|
|
|
DcId dcId,
|
|
|
|
const AuthKeyPtr &persistentKey) {
|
|
|
|
dcTemporaryKeyChanged(dcId);
|
2019-11-18 12:53:37 +00:00
|
|
|
|
2017-06-26 17:38:16 +00:00
|
|
|
if (isTemporaryDcId(dcId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
const auto i = _keysForWrite.find(dcId);
|
|
|
|
if (i != _keysForWrite.end() && i->second == persistentKey) {
|
|
|
|
return;
|
|
|
|
} else if (i == _keysForWrite.end() && !persistentKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!persistentKey) {
|
|
|
|
_keysForWrite.erase(i);
|
|
|
|
} else if (i != _keysForWrite.end()) {
|
|
|
|
i->second = persistentKey;
|
2017-02-24 17:15:41 +00:00
|
|
|
} else {
|
2019-11-19 10:10:51 +00:00
|
|
|
_keysForWrite.emplace(dcId, persistentKey);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2020-06-09 16:57:05 +00:00
|
|
|
DEBUG_LOG(("AuthKey Info: writing auth keys, called by dc %1"
|
|
|
|
).arg(dcId));
|
|
|
|
_writeKeysRequests.fire({});
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
void Instance::Private::dcTemporaryKeyChanged(DcId dcId) {
|
|
|
|
_dcTemporaryKeyChanged.fire_copy(dcId);
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<DcId> Instance::Private::dcTemporaryKeyChanged() const {
|
|
|
|
return _dcTemporaryKeyChanged.events();
|
2019-11-18 12:53:37 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
AuthKeysList Instance::Private::getKeysForWrite() const {
|
|
|
|
auto result = AuthKeysList();
|
|
|
|
|
|
|
|
result.reserve(_keysForWrite.size());
|
2019-11-19 10:10:51 +00:00
|
|
|
for (const auto &key : _keysForWrite) {
|
2017-02-24 17:15:41 +00:00
|
|
|
result.push_back(key.second);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
void Instance::Private::addKeysForDestroy(AuthKeysList &&keys) {
|
2017-06-26 17:38:16 +00:00
|
|
|
Expects(isKeysDestroyer());
|
2017-02-25 16:44:02 +00:00
|
|
|
|
|
|
|
for (auto &key : keys) {
|
2019-11-14 17:39:15 +00:00
|
|
|
const auto dcId = key->dcId();
|
2017-02-25 16:44:02 +00:00
|
|
|
auto shiftedDcId = MTP::destroyKeyNextDcId(dcId);
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
// There could be several keys for one dc if we're destroying them.
|
|
|
|
// Place them all in separate shiftedDcId so that they won't conflict.
|
|
|
|
while (_keysForWrite.find(shiftedDcId) != _keysForWrite.cend()) {
|
|
|
|
shiftedDcId = MTP::destroyKeyNextDcId(shiftedDcId);
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
2019-11-19 10:10:51 +00:00
|
|
|
_keysForWrite[shiftedDcId] = key;
|
2017-02-25 16:44:02 +00:00
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
addDc(shiftedDcId, std::move(key));
|
|
|
|
startSession(shiftedDcId);
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:22:22 +00:00
|
|
|
rpl::producer<> Instance::Private::allKeysDestroyed() const {
|
|
|
|
return _allKeysDestroyed.events();
|
|
|
|
}
|
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
rpl::producer<> Instance::Private::writeKeysRequests() const {
|
|
|
|
return _writeKeysRequests.events();
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
Config &Instance::Private::config() const {
|
|
|
|
return *_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ConfigFields &Instance::Private::configValues() const {
|
|
|
|
return _config->values();
|
|
|
|
}
|
|
|
|
|
|
|
|
DcOptions &Instance::Private::dcOptions() const {
|
|
|
|
return _config->dcOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
Environment Instance::Private::environment() const {
|
|
|
|
return _config->environment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Instance::Private::isTestMode() const {
|
|
|
|
return _config->isTestMode();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 07:48:31 +00:00
|
|
|
QString Instance::Private::deviceModel() const {
|
2021-11-16 16:48:40 +00:00
|
|
|
QMutexLocker lock(&_deviceModelMutex);
|
|
|
|
return _customDeviceModel.isEmpty()
|
|
|
|
? _deviceModelDefault
|
|
|
|
: _customDeviceModel;
|
2019-02-01 07:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Instance::Private::systemVersion() const {
|
|
|
|
return _systemVersion;
|
|
|
|
}
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
void Instance::Private::unpaused() {
|
2019-11-14 17:39:15 +00:00
|
|
|
for (const auto &[shiftedDcId, session] : _sessions) {
|
|
|
|
session->unpaused();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::configLoadDone(const MTPConfig &result) {
|
2017-06-26 17:38:16 +00:00
|
|
|
Expects(result.type() == mtpc_config);
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
_configLoader.reset();
|
2019-02-19 06:57:53 +00:00
|
|
|
_lastConfigLoadedTime = crl::now();
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2018-05-03 06:39:10 +00:00
|
|
|
const auto &data = result.c_config();
|
2020-06-17 09:36:25 +00:00
|
|
|
_config->apply(data);
|
2018-05-02 19:52:57 +00:00
|
|
|
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto lang = qs(data.vsuggested_lang_code().value_or_empty());
|
2018-05-03 06:39:10 +00:00
|
|
|
Lang::CurrentCloudManager().setSuggestedLanguage(lang);
|
2018-11-06 11:29:01 +00:00
|
|
|
Lang::CurrentCloudManager().setCurrentVersions(
|
2019-07-05 13:38:38 +00:00
|
|
|
data.vlang_pack_version().value_or_empty(),
|
|
|
|
data.vbase_lang_pack_version().value_or_empty());
|
|
|
|
if (const auto prefix = data.vautoupdate_url_prefix()) {
|
|
|
|
Local::writeAutoupdatePrefix(qs(*prefix));
|
2018-05-03 06:39:10 +00:00
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
_configExpiresAt = crl::now()
|
2019-07-10 17:28:33 +00:00
|
|
|
+ (data.vexpires().v - base::unixtime::now()) * crl::time(1000);
|
2018-05-02 19:52:57 +00:00
|
|
|
requestConfigIfExpired();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 12:48:00 +00:00
|
|
|
bool Instance::Private::configLoadFail(const Error &error) {
|
|
|
|
if (IsDefaultHandledError(error)) return false;
|
2017-02-24 17:15:41 +00:00
|
|
|
|
|
|
|
// loadingConfig = false;
|
|
|
|
LOG(("MTP Error: failed to get config!"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<ShiftedDcId> Instance::Private::queryRequestByDc(
|
2018-04-27 13:08:12 +00:00
|
|
|
mtpRequestId requestId) const {
|
|
|
|
QMutexLocker locker(&_requestByDcLock);
|
|
|
|
auto it = _requestsByDc.find(requestId);
|
|
|
|
if (it != _requestsByDc.cend()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2018-04-27 13:08:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<ShiftedDcId> Instance::Private::changeRequestByDc(
|
2018-04-27 13:08:12 +00:00
|
|
|
mtpRequestId requestId,
|
|
|
|
DcId newdc) {
|
|
|
|
QMutexLocker locker(&_requestByDcLock);
|
|
|
|
auto it = _requestsByDc.find(requestId);
|
|
|
|
if (it != _requestsByDc.cend()) {
|
|
|
|
if (it->second < 0) {
|
|
|
|
it->second = -newdc;
|
|
|
|
} else {
|
2018-06-11 18:35:27 +00:00
|
|
|
it->second = ShiftDcId(newdc, GetDcIdShift(it->second));
|
2018-04-27 13:08:12 +00:00
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2018-04-27 13:08:12 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
void Instance::Private::checkDelayedRequests() {
|
2019-02-19 06:57:53 +00:00
|
|
|
auto now = crl::now();
|
2017-02-24 17:15:41 +00:00
|
|
|
while (!_delayedRequests.empty() && now >= _delayedRequests.front().second) {
|
|
|
|
auto requestId = _delayedRequests.front().first;
|
|
|
|
_delayedRequests.pop_front();
|
|
|
|
|
|
|
|
auto dcWithShift = ShiftedDcId(0);
|
2018-04-27 13:08:12 +00:00
|
|
|
if (const auto shiftedDcId = queryRequestByDc(requestId)) {
|
|
|
|
dcWithShift = *shiftedDcId;
|
|
|
|
} else {
|
|
|
|
LOG(("MTP Error: could not find request dc for delayed resend, requestId %1").arg(requestId));
|
|
|
|
continue;
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
auto request = SerializedRequest();
|
2017-02-24 17:15:41 +00:00
|
|
|
{
|
|
|
|
QReadLocker locker(&_requestMapLock);
|
|
|
|
auto it = _requestMap.find(requestId);
|
|
|
|
if (it == _requestMap.cend()) {
|
|
|
|
DEBUG_LOG(("MTP Error: could not find request %1").arg(requestId));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
request = it->second;
|
|
|
|
}
|
2018-06-02 14:29:21 +00:00
|
|
|
const auto session = getSession(qAbs(dcWithShift));
|
|
|
|
session->sendPrepared(request);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_delayedRequests.empty()) {
|
2017-04-13 17:59:05 +00:00
|
|
|
_checkDelayedTimer.callOnce(_delayedRequests.front().second - now);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
void Instance::Private::sendRequest(
|
|
|
|
mtpRequestId requestId,
|
2019-11-22 09:15:38 +00:00
|
|
|
SerializedRequest &&request,
|
2021-03-12 10:45:13 +00:00
|
|
|
ResponseHandler &&callbacks,
|
2018-06-02 14:29:21 +00:00
|
|
|
ShiftedDcId shiftedDcId,
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time msCanWait,
|
2018-06-02 14:29:21 +00:00
|
|
|
bool needsLayer,
|
|
|
|
mtpRequestId afterRequestId) {
|
|
|
|
const auto session = getSession(shiftedDcId);
|
|
|
|
|
|
|
|
request->requestId = requestId;
|
|
|
|
storeRequest(requestId, request, std::move(callbacks));
|
|
|
|
|
|
|
|
const auto toMainDc = (shiftedDcId == 0);
|
|
|
|
const auto realShiftedDcId = session->getDcWithShift();
|
|
|
|
const auto signedDcId = toMainDc ? -realShiftedDcId : realShiftedDcId;
|
|
|
|
registerRequest(requestId, signedDcId);
|
|
|
|
|
|
|
|
if (afterRequestId) {
|
|
|
|
request->after = getRequest(afterRequestId);
|
|
|
|
}
|
2019-11-22 09:15:38 +00:00
|
|
|
request->lastSentTime = crl::now();
|
2018-06-02 14:29:21 +00:00
|
|
|
request->needsLayer = needsLayer;
|
|
|
|
|
|
|
|
session->sendPrepared(request, msCanWait);
|
|
|
|
}
|
|
|
|
|
2018-01-02 13:44:12 +00:00
|
|
|
void Instance::Private::registerRequest(
|
|
|
|
mtpRequestId requestId,
|
2018-06-02 14:29:21 +00:00
|
|
|
ShiftedDcId shiftedDcId) {
|
2018-01-02 13:44:12 +00:00
|
|
|
QMutexLocker locker(&_requestByDcLock);
|
2018-06-12 13:30:00 +00:00
|
|
|
_requestsByDc[requestId] = shiftedDcId;
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::unregisterRequest(mtpRequestId requestId) {
|
2018-06-24 13:19:49 +00:00
|
|
|
DEBUG_LOG(("MTP Info: unregistering request %1.").arg(requestId));
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
_requestsDelays.erase(requestId);
|
|
|
|
|
|
|
|
{
|
|
|
|
QWriteLocker locker(&_requestMapLock);
|
|
|
|
_requestMap.erase(requestId);
|
|
|
|
}
|
2021-07-06 08:20:18 +00:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&_requestByDcLock);
|
|
|
|
_requestsByDc.erase(requestId);
|
|
|
|
}
|
|
|
|
{
|
2021-07-06 08:31:01 +00:00
|
|
|
auto toRemove = base::flat_set<mtpRequestId>();
|
|
|
|
auto toResend = base::flat_set<mtpRequestId>();
|
|
|
|
|
|
|
|
toRemove.emplace(requestId);
|
|
|
|
|
2021-07-06 08:20:18 +00:00
|
|
|
QMutexLocker locker(&_dependentRequestsLock);
|
2021-07-06 08:31:01 +00:00
|
|
|
|
|
|
|
auto handling = 0;
|
|
|
|
do {
|
|
|
|
handling = toResend.size();
|
2021-07-20 06:57:49 +00:00
|
|
|
for (const auto &[resendingId, afterId] : _dependentRequests) {
|
2021-07-06 08:31:01 +00:00
|
|
|
if (toRemove.contains(afterId)) {
|
|
|
|
toRemove.emplace(resendingId);
|
|
|
|
toResend.emplace(resendingId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (handling != toResend.size());
|
|
|
|
|
|
|
|
for (const auto removingId : toRemove) {
|
|
|
|
_dependentRequests.remove(removingId);
|
|
|
|
}
|
|
|
|
locker.unlock();
|
|
|
|
|
|
|
|
for (const auto resendingId : toResend) {
|
|
|
|
if (const auto shiftedDcId = queryRequestByDc(resendingId)) {
|
|
|
|
SerializedRequest request;
|
|
|
|
{
|
|
|
|
QReadLocker locker(&_requestMapLock);
|
|
|
|
auto it = _requestMap.find(resendingId);
|
|
|
|
if (it == _requestMap.cend()) {
|
|
|
|
LOG(("MTP Error: could not find dependent request %1").arg(resendingId));
|
|
|
|
return;
|
2021-07-06 08:20:18 +00:00
|
|
|
}
|
2021-07-06 08:31:01 +00:00
|
|
|
request = it->second;
|
2021-07-06 08:20:18 +00:00
|
|
|
}
|
2021-07-06 08:31:01 +00:00
|
|
|
getSession(qAbs(*shiftedDcId))->sendPrepared(request);
|
2021-07-06 08:20:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
void Instance::Private::storeRequest(
|
|
|
|
mtpRequestId requestId,
|
2019-11-22 09:15:38 +00:00
|
|
|
const SerializedRequest &request,
|
2021-03-12 10:45:13 +00:00
|
|
|
ResponseHandler &&callbacks) {
|
|
|
|
if (callbacks.done || callbacks.fail) {
|
2017-02-24 17:15:41 +00:00
|
|
|
QMutexLocker locker(&_parserMapLock);
|
2017-12-18 12:40:15 +00:00
|
|
|
_parserMap.emplace(requestId, std::move(callbacks));
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
QWriteLocker locker(&_requestMapLock);
|
2017-12-18 12:40:15 +00:00
|
|
|
_requestMap.emplace(requestId, request);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
SerializedRequest Instance::Private::getRequest(mtpRequestId requestId) {
|
|
|
|
auto result = SerializedRequest();
|
2017-02-24 17:15:41 +00:00
|
|
|
{
|
|
|
|
QReadLocker locker(&_requestMapLock);
|
|
|
|
auto it = _requestMap.find(requestId);
|
|
|
|
if (it != _requestMap.cend()) {
|
|
|
|
result = it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
bool Instance::Private::hasCallback(mtpRequestId requestId) const {
|
|
|
|
QMutexLocker locker(&_parserMapLock);
|
|
|
|
auto it = _parserMap.find(requestId);
|
|
|
|
return (it != _parserMap.cend());
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::Private::processCallback(const Response &response) {
|
|
|
|
const auto requestId = response.requestId;
|
|
|
|
ResponseHandler handler;
|
2017-02-24 17:15:41 +00:00
|
|
|
{
|
|
|
|
QMutexLocker locker(&_parserMapLock);
|
|
|
|
auto it = _parserMap.find(requestId);
|
|
|
|
if (it != _parserMap.cend()) {
|
2021-03-12 10:45:13 +00:00
|
|
|
handler = std::move(it->second);
|
2017-02-24 17:15:41 +00:00
|
|
|
_parserMap.erase(it);
|
|
|
|
|
|
|
|
DEBUG_LOG(("RPC Info: found parser for request %1, trying to parse response...").arg(requestId));
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 10:45:13 +00:00
|
|
|
if (handler.done || handler.fail) {
|
2021-03-12 12:48:00 +00:00
|
|
|
const auto handleError = [&](const Error &error) {
|
2018-06-24 13:19:49 +00:00
|
|
|
DEBUG_LOG(("RPC Info: "
|
2021-03-13 11:50:34 +00:00
|
|
|
"error received, code %1, type %2, description: %3").arg(
|
|
|
|
QString::number(error.code()),
|
|
|
|
error.type(),
|
|
|
|
error.description()));
|
2021-03-12 10:45:13 +00:00
|
|
|
if (rpcErrorOccured(response, handler, error)) {
|
2018-06-24 13:19:49 +00:00
|
|
|
unregisterRequest(requestId);
|
|
|
|
} else {
|
|
|
|
QMutexLocker locker(&_parserMapLock);
|
2021-03-12 10:45:13 +00:00
|
|
|
_parserMap.emplace(requestId, std::move(handler));
|
2018-06-24 13:19:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
auto from = response.reply.constData();
|
|
|
|
if (response.reply.isEmpty()) {
|
2021-03-12 12:48:00 +00:00
|
|
|
handleError(Error::Local(
|
2018-06-24 13:19:49 +00:00
|
|
|
"RESPONSE_PARSE_FAILED",
|
2019-07-18 14:06:38 +00:00
|
|
|
"Empty response."));
|
|
|
|
} else if (*from == mtpc_rpc_error) {
|
|
|
|
auto error = MTPRpcError();
|
2021-03-12 10:45:13 +00:00
|
|
|
handleError(
|
2021-03-12 12:48:00 +00:00
|
|
|
Error(error.read(from, from + response.reply.size())
|
2021-03-12 10:45:13 +00:00
|
|
|
? error
|
2021-03-12 12:48:00 +00:00
|
|
|
: Error::MTPLocal(
|
2019-07-18 14:06:38 +00:00
|
|
|
"RESPONSE_PARSE_FAILED",
|
2021-03-12 10:45:13 +00:00
|
|
|
"Error parse failed.")));
|
|
|
|
} else {
|
|
|
|
if (handler.done && !handler.done(response)) {
|
2021-03-12 12:48:00 +00:00
|
|
|
handleError(Error::Local(
|
2021-03-12 10:45:13 +00:00
|
|
|
"RESPONSE_PARSE_FAILED",
|
|
|
|
"Response parse failed."));
|
2019-07-18 14:06:38 +00:00
|
|
|
}
|
|
|
|
unregisterRequest(requestId);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DEBUG_LOG(("RPC Info: parser not found for %1").arg(requestId));
|
2018-06-24 13:19:49 +00:00
|
|
|
unregisterRequest(requestId);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::Private::processUpdate(const Response &message) {
|
|
|
|
if (_updatesHandler) {
|
|
|
|
_updatesHandler(message);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 14:12:04 +00:00
|
|
|
void Instance::Private::onStateChange(ShiftedDcId dcWithShift, int32 state) {
|
2017-02-24 17:15:41 +00:00
|
|
|
if (_stateChangedHandler) {
|
|
|
|
_stateChangedHandler(dcWithShift, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 14:12:04 +00:00
|
|
|
void Instance::Private::onSessionReset(ShiftedDcId dcWithShift) {
|
2017-02-24 17:15:41 +00:00
|
|
|
if (_sessionResetHandler) {
|
|
|
|
_sessionResetHandler(dcWithShift);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
bool Instance::Private::rpcErrorOccured(
|
|
|
|
const Response &response,
|
|
|
|
const FailHandler &onFail,
|
2021-03-12 12:48:00 +00:00
|
|
|
const Error &error) { // return true if need to clean request data
|
|
|
|
if (IsDefaultHandledError(error)) {
|
2021-03-12 10:45:13 +00:00
|
|
|
if (onFail && onFail(error, response)) {
|
2019-11-19 16:14:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
if (onErrorDefault(error, response)) {
|
2017-02-24 17:15:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-13 11:50:34 +00:00
|
|
|
LOG(("RPC Error: request %1 got fail with code %2, error %3%4").arg(
|
|
|
|
QString::number(response.requestId),
|
|
|
|
QString::number(error.code()),
|
|
|
|
error.type(),
|
|
|
|
error.description().isEmpty()
|
2021-03-12 10:45:13 +00:00
|
|
|
? QString()
|
|
|
|
: QString(": %1").arg(error.description())));
|
|
|
|
if (onFail) {
|
|
|
|
onFail(error, response);
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::Private::importDone(
|
|
|
|
const MTPauth_Authorization &result,
|
|
|
|
const Response &response) {
|
|
|
|
const auto shiftedDcId = queryRequestByDc(response.requestId);
|
2018-04-27 13:08:12 +00:00
|
|
|
if (!shiftedDcId) {
|
2021-03-12 10:45:13 +00:00
|
|
|
LOG(("MTP Error: "
|
|
|
|
"auth import request not found in requestsByDC, requestId: %1"
|
|
|
|
).arg(response.requestId));
|
2018-05-13 08:42:25 +00:00
|
|
|
//
|
|
|
|
// Don't log out on export/import problems, perhaps this is a server side error.
|
|
|
|
//
|
2021-03-12 12:48:00 +00:00
|
|
|
//const auto error = Error::Local(
|
2019-01-13 13:28:05 +00:00
|
|
|
// "AUTH_IMPORT_FAIL",
|
|
|
|
// QString("did not find import request in requestsByDC, "
|
|
|
|
// "request %1").arg(requestId));
|
2021-03-12 10:45:13 +00:00
|
|
|
//if (_globalFailHandler && hasAuthorization()) {
|
|
|
|
// _globalFailHandler(error, response); // auth failed in main dc
|
2018-05-13 08:42:25 +00:00
|
|
|
//}
|
2017-02-24 17:15:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-11 18:35:27 +00:00
|
|
|
auto newdc = BareDcId(*shiftedDcId);
|
2017-02-24 17:15:41 +00:00
|
|
|
|
|
|
|
DEBUG_LOG(("MTP Info: auth import to dc %1 succeeded").arg(newdc));
|
|
|
|
|
|
|
|
auto &waiters = _authWaiters[newdc];
|
|
|
|
if (waiters.size()) {
|
|
|
|
QReadLocker locker(&_requestMapLock);
|
|
|
|
for (auto waitedRequestId : waiters) {
|
|
|
|
auto it = _requestMap.find(waitedRequestId);
|
|
|
|
if (it == _requestMap.cend()) {
|
|
|
|
LOG(("MTP Error: could not find request %1 for resending").arg(waitedRequestId));
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-27 13:08:12 +00:00
|
|
|
const auto shiftedDcId = changeRequestByDc(waitedRequestId, newdc);
|
|
|
|
if (!shiftedDcId) {
|
|
|
|
LOG(("MTP Error: could not find request %1 by dc for resending").arg(waitedRequestId));
|
|
|
|
continue;
|
|
|
|
} else if (*shiftedDcId < 0) {
|
|
|
|
_instance->setMainDcId(newdc);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2018-04-27 13:08:12 +00:00
|
|
|
DEBUG_LOG(("MTP Info: resending request %1 to dc %2 after import auth").arg(waitedRequestId).arg(*shiftedDcId));
|
2018-06-02 14:29:21 +00:00
|
|
|
const auto session = getSession(*shiftedDcId);
|
|
|
|
session->sendPrepared(it->second);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
waiters.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
bool Instance::Private::importFail(
|
2021-03-12 12:48:00 +00:00
|
|
|
const Error &error,
|
2021-03-12 10:45:13 +00:00
|
|
|
const Response &response) {
|
2021-03-12 12:48:00 +00:00
|
|
|
if (IsDefaultHandledError(error)) {
|
2021-03-12 10:45:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2018-05-13 08:42:25 +00:00
|
|
|
//
|
|
|
|
// Don't log out on export/import problems, perhaps this is a server side error.
|
|
|
|
//
|
2021-03-12 10:45:13 +00:00
|
|
|
//if (_globalFailHandler && hasAuthorization()) {
|
|
|
|
// _globalFailHandler(error, response); // auth import failed
|
2018-05-13 08:42:25 +00:00
|
|
|
//}
|
2017-02-24 17:15:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::Private::exportDone(
|
|
|
|
const MTPauth_ExportedAuthorization &result,
|
|
|
|
const Response &response) {
|
|
|
|
auto it = _authExportRequests.find(response.requestId);
|
2017-02-24 17:15:41 +00:00
|
|
|
if (it == _authExportRequests.cend()) {
|
2021-03-12 10:45:13 +00:00
|
|
|
LOG(("MTP Error: "
|
|
|
|
"auth export request target dcWithShift not found, requestId: %1"
|
|
|
|
).arg(response.requestId));
|
2018-05-13 08:42:25 +00:00
|
|
|
//
|
|
|
|
// Don't log out on export/import problems, perhaps this is a server side error.
|
|
|
|
//
|
2021-03-12 12:48:00 +00:00
|
|
|
//const auto error = Error::Local(
|
2019-01-13 13:28:05 +00:00
|
|
|
// "AUTH_IMPORT_FAIL",
|
|
|
|
// QString("did not find target dcWithShift, request %1"
|
|
|
|
// ).arg(requestId));
|
2021-03-12 10:45:13 +00:00
|
|
|
//if (_globalFailHandler && hasAuthorization()) {
|
|
|
|
// _globalFailHandler(error, response); // auth failed in main dc
|
2018-05-13 08:42:25 +00:00
|
|
|
//}
|
2017-02-24 17:15:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &data = result.c_auth_exportedAuthorization();
|
2021-03-12 10:45:13 +00:00
|
|
|
_instance->send(MTPauth_ImportAuthorization(
|
|
|
|
data.vid(),
|
|
|
|
data.vbytes()
|
|
|
|
), [this](const Response &response) {
|
|
|
|
auto result = MTPauth_Authorization();
|
|
|
|
auto from = response.reply.constData();
|
|
|
|
if (!result.read(from, from + response.reply.size())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
importDone(result, response);
|
|
|
|
return true;
|
2021-03-12 12:48:00 +00:00
|
|
|
}, [this](const Error &error, const Response &response) {
|
2021-03-12 10:45:13 +00:00
|
|
|
return importFail(error, response);
|
|
|
|
}, it->second);
|
|
|
|
_authExportRequests.erase(response.requestId);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
bool Instance::Private::exportFail(
|
2021-03-12 12:48:00 +00:00
|
|
|
const Error &error,
|
2021-03-12 10:45:13 +00:00
|
|
|
const Response &response) {
|
2021-03-12 12:48:00 +00:00
|
|
|
if (IsDefaultHandledError(error)) {
|
2021-03-12 10:45:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
auto it = _authExportRequests.find(response.requestId);
|
2017-02-24 17:15:41 +00:00
|
|
|
if (it != _authExportRequests.cend()) {
|
2018-06-11 18:35:27 +00:00
|
|
|
_authWaiters[BareDcId(it->second)].clear();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2018-05-13 08:42:25 +00:00
|
|
|
//
|
|
|
|
// Don't log out on export/import problems, perhaps this is a server side error.
|
|
|
|
//
|
2021-03-12 10:45:13 +00:00
|
|
|
//if (_globalFailHandler && hasAuthorization()) {
|
|
|
|
// _globalFailHandler(error, response); // auth failed in main dc
|
2018-05-13 08:42:25 +00:00
|
|
|
//}
|
2017-02-24 17:15:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
bool Instance::Private::onErrorDefault(
|
2021-03-12 12:48:00 +00:00
|
|
|
const Error &error,
|
2021-03-12 10:45:13 +00:00
|
|
|
const Response &response) {
|
|
|
|
const auto requestId = response.requestId;
|
|
|
|
const auto &type = error.type();
|
|
|
|
const auto code = error.code();
|
|
|
|
auto badGuestDc = (code == 400) && (type == qsl("FILE_ID_INVALID"));
|
2021-05-10 12:28:28 +00:00
|
|
|
QRegularExpressionMatch m1, m2;
|
|
|
|
if ((m1 = QRegularExpression("^(FILE|PHONE|NETWORK|USER)_MIGRATE_(\\d+)$").match(type)).hasMatch()) {
|
2017-02-24 17:15:41 +00:00
|
|
|
if (!requestId) return false;
|
|
|
|
|
2018-04-27 13:08:12 +00:00
|
|
|
auto dcWithShift = ShiftedDcId(0);
|
2021-05-10 12:28:28 +00:00
|
|
|
auto newdcWithShift = ShiftedDcId(m1.captured(2).toInt());
|
2018-04-27 13:08:12 +00:00
|
|
|
if (const auto shiftedDcId = queryRequestByDc(requestId)) {
|
|
|
|
dcWithShift = *shiftedDcId;
|
|
|
|
} else {
|
|
|
|
LOG(("MTP Error: could not find request %1 for migrating to %2").arg(requestId).arg(newdcWithShift));
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
if (!dcWithShift || !newdcWithShift) return false;
|
|
|
|
|
|
|
|
DEBUG_LOG(("MTP Info: changing request %1 from dcWithShift%2 to dc%3").arg(requestId).arg(dcWithShift).arg(newdcWithShift));
|
|
|
|
if (dcWithShift < 0) { // newdc shift = 0
|
2020-06-10 13:24:41 +00:00
|
|
|
if (false/* && hasAuthorization() && _authExportRequests.find(requestId) == _authExportRequests.cend()*/) {
|
2017-02-24 17:15:41 +00:00
|
|
|
//
|
|
|
|
// migrate not supported at this moment
|
|
|
|
// this was not tested even once
|
|
|
|
//
|
|
|
|
//DEBUG_LOG(("MTP Info: importing auth to dc %1").arg(newdcWithShift));
|
|
|
|
//auto &waiters(_authWaiters[newdcWithShift]);
|
|
|
|
//if (waiters.empty()) {
|
2021-03-12 10:45:13 +00:00
|
|
|
// auto exportRequestId = _instance->send(MTPauth_ExportAuthorization(
|
|
|
|
// MTP_int(newdcWithShift)
|
|
|
|
// ), [this](const Response &response) {
|
|
|
|
// auto result = MTPauth_ExportedAuthorization();
|
|
|
|
// auto from = response.reply.constData();
|
|
|
|
// if (!result.read(from, from + response.reply.size())) {
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// exportDone(result, response);
|
|
|
|
// return true;
|
2021-03-12 12:48:00 +00:00
|
|
|
// }, [this](const Error &error, const Response &response) {
|
2021-03-12 10:45:13 +00:00
|
|
|
// return exportFail(error, response);
|
|
|
|
// });
|
2017-02-24 17:15:41 +00:00
|
|
|
// _authExportRequests.emplace(exportRequestId, newdcWithShift);
|
|
|
|
//}
|
|
|
|
//waiters.push_back(requestId);
|
|
|
|
//return true;
|
|
|
|
} else {
|
|
|
|
_instance->setMainDcId(newdcWithShift);
|
|
|
|
}
|
|
|
|
} else {
|
2018-06-11 18:35:27 +00:00
|
|
|
newdcWithShift = ShiftDcId(newdcWithShift, GetDcIdShift(dcWithShift));
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
auto request = SerializedRequest();
|
2017-02-24 17:15:41 +00:00
|
|
|
{
|
|
|
|
QReadLocker locker(&_requestMapLock);
|
|
|
|
auto it = _requestMap.find(requestId);
|
|
|
|
if (it == _requestMap.cend()) {
|
|
|
|
LOG(("MTP Error: could not find request %1").arg(requestId));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
request = it->second;
|
|
|
|
}
|
2018-06-02 14:29:21 +00:00
|
|
|
const auto session = getSession(newdcWithShift);
|
|
|
|
registerRequest(
|
|
|
|
requestId,
|
|
|
|
(dcWithShift < 0) ? -newdcWithShift : newdcWithShift);
|
|
|
|
session->sendPrepared(request);
|
2017-02-24 17:15:41 +00:00
|
|
|
return true;
|
2021-07-06 08:20:18 +00:00
|
|
|
} else if (type == qstr("MSG_WAIT_TIMEOUT") || type == qstr("MSG_WAIT_FAILED")) {
|
|
|
|
SerializedRequest request;
|
|
|
|
{
|
|
|
|
QReadLocker locker(&_requestMapLock);
|
|
|
|
auto it = _requestMap.find(requestId);
|
|
|
|
if (it == _requestMap.cend()) {
|
|
|
|
LOG(("MTP Error: could not find MSG_WAIT_* request %1").arg(requestId));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
request = it->second;
|
|
|
|
}
|
|
|
|
if (!request->after) {
|
|
|
|
LOG(("MTP Error: MSG_WAIT_* for not dependent request %1").arg(requestId));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto dcWithShift = ShiftedDcId(0);
|
|
|
|
if (const auto shiftedDcId = queryRequestByDc(requestId)) {
|
|
|
|
dcWithShift = *shiftedDcId;
|
|
|
|
if (const auto afterDcId = queryRequestByDc(request->after->requestId)) {
|
|
|
|
if (*shiftedDcId != *afterDcId) {
|
|
|
|
request->after = SerializedRequest();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
request->after = SerializedRequest();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(("MTP Error: could not find MSG_WAIT_* request %1 by dc").arg(requestId));
|
|
|
|
}
|
|
|
|
if (!dcWithShift) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request->after) {
|
2021-07-06 08:31:01 +00:00
|
|
|
getSession(qAbs(dcWithShift))->sendPrepared(request);
|
2021-07-06 08:20:18 +00:00
|
|
|
} else {
|
|
|
|
QMutexLocker locker(&_dependentRequestsLock);
|
|
|
|
_dependentRequests.emplace(requestId, request->after->requestId);
|
|
|
|
}
|
|
|
|
return true;
|
2021-05-10 12:28:28 +00:00
|
|
|
} else if (code < 0
|
|
|
|
|| code >= 500
|
|
|
|
|| (m1 = QRegularExpression("^FLOOD_WAIT_(\\d+)$").match(type)).hasMatch()
|
|
|
|
|| ((m2 = QRegularExpression("^SLOWMODE_WAIT_(\\d+)$").match(type)).hasMatch()
|
|
|
|
&& m2.captured(1).toInt() < 3)) {
|
2017-02-24 17:15:41 +00:00
|
|
|
if (!requestId) return false;
|
|
|
|
|
|
|
|
int32 secs = 1;
|
|
|
|
if (code < 0 || code >= 500) {
|
|
|
|
auto it = _requestsDelays.find(requestId);
|
|
|
|
if (it != _requestsDelays.cend()) {
|
|
|
|
secs = (it->second > 60) ? it->second : (it->second *= 2);
|
|
|
|
} else {
|
|
|
|
_requestsDelays.emplace(requestId, secs);
|
|
|
|
}
|
2021-05-10 12:28:28 +00:00
|
|
|
} else if (m1.hasMatch()) {
|
|
|
|
secs = m1.captured(1).toInt();
|
2017-02-24 17:15:41 +00:00
|
|
|
// if (secs >= 60) return false;
|
2021-05-10 12:28:28 +00:00
|
|
|
} else if (m2.hasMatch()) {
|
|
|
|
secs = m2.captured(1).toInt();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2019-02-19 06:57:53 +00:00
|
|
|
auto sendAt = crl::now() + secs * 1000 + 10;
|
2017-02-24 17:15:41 +00:00
|
|
|
auto it = _delayedRequests.begin(), e = _delayedRequests.end();
|
|
|
|
for (; it != e; ++it) {
|
|
|
|
if (it->first == requestId) return true;
|
|
|
|
if (it->second > sendAt) break;
|
|
|
|
}
|
|
|
|
_delayedRequests.insert(it, std::make_pair(requestId, sendAt));
|
|
|
|
|
|
|
|
checkDelayedRequests();
|
|
|
|
|
|
|
|
return true;
|
2021-03-12 10:45:13 +00:00
|
|
|
} else if ((code == 401 && type != qstr("AUTH_KEY_PERM_EMPTY"))
|
2019-11-19 10:10:51 +00:00
|
|
|
|| (badGuestDc && _badGuestDcRequests.find(requestId) == _badGuestDcRequests.cend())) {
|
2017-02-24 17:15:41 +00:00
|
|
|
auto dcWithShift = ShiftedDcId(0);
|
2018-04-27 13:08:12 +00:00
|
|
|
if (const auto shiftedDcId = queryRequestByDc(requestId)) {
|
|
|
|
dcWithShift = *shiftedDcId;
|
|
|
|
} else {
|
|
|
|
LOG(("MTP Error: unauthorized request without dc info, requestId %1").arg(requestId));
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2018-06-11 18:35:27 +00:00
|
|
|
auto newdc = BareDcId(qAbs(dcWithShift));
|
2021-08-31 11:06:03 +00:00
|
|
|
if (!newdc || !hasMainDcId() || newdc == mainDcId()) {
|
2021-03-12 10:45:13 +00:00
|
|
|
if (!badGuestDc && _globalFailHandler) {
|
|
|
|
_globalFailHandler(error, response); // auth failed in main dc
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
DEBUG_LOG(("MTP Info: importing auth to dcWithShift %1"
|
|
|
|
).arg(dcWithShift));
|
2017-02-24 17:15:41 +00:00
|
|
|
auto &waiters(_authWaiters[newdc]);
|
|
|
|
if (!waiters.size()) {
|
2021-03-12 10:45:13 +00:00
|
|
|
auto exportRequestId = _instance->send(MTPauth_ExportAuthorization(
|
|
|
|
MTP_int(newdc)
|
|
|
|
), [this](const Response &response) {
|
|
|
|
auto result = MTPauth_ExportedAuthorization();
|
|
|
|
auto from = response.reply.constData();
|
|
|
|
if (!result.read(from, from + response.reply.size())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
exportDone(result, response);
|
|
|
|
return true;
|
2021-03-12 12:48:00 +00:00
|
|
|
}, [this](const Error &error, const Response &response) {
|
2021-03-12 10:45:13 +00:00
|
|
|
return exportFail(error, response);
|
|
|
|
});
|
2017-02-24 17:15:41 +00:00
|
|
|
_authExportRequests.emplace(exportRequestId, abs(dcWithShift));
|
|
|
|
}
|
|
|
|
waiters.push_back(requestId);
|
|
|
|
if (badGuestDc) _badGuestDcRequests.insert(requestId);
|
|
|
|
return true;
|
2021-03-12 10:45:13 +00:00
|
|
|
} else if (type == qstr("CONNECTION_NOT_INITED")
|
|
|
|
|| type == qstr("CONNECTION_LAYER_INVALID")) {
|
2019-11-22 09:15:38 +00:00
|
|
|
SerializedRequest request;
|
2017-02-24 17:15:41 +00:00
|
|
|
{
|
|
|
|
QReadLocker locker(&_requestMapLock);
|
|
|
|
auto it = _requestMap.find(requestId);
|
|
|
|
if (it == _requestMap.cend()) {
|
|
|
|
LOG(("MTP Error: could not find request %1").arg(requestId));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
request = it->second;
|
|
|
|
}
|
|
|
|
auto dcWithShift = ShiftedDcId(0);
|
2018-04-27 13:08:12 +00:00
|
|
|
if (const auto shiftedDcId = queryRequestByDc(requestId)) {
|
|
|
|
dcWithShift = *shiftedDcId;
|
|
|
|
} else {
|
|
|
|
LOG(("MTP Error: could not find request %1 for resending with init connection").arg(requestId));
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
if (!dcWithShift) return false;
|
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
const auto session = getSession(qAbs(dcWithShift));
|
|
|
|
request->needsLayer = true;
|
|
|
|
session->sendPrepared(request);
|
2017-02-24 17:15:41 +00:00
|
|
|
return true;
|
2021-03-12 10:45:13 +00:00
|
|
|
} else if (type == qstr("CONNECTION_LANG_CODE_INVALID")) {
|
2017-05-30 17:58:25 +00:00
|
|
|
Lang::CurrentCloudManager().resetToDefault();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
if (badGuestDc) _badGuestDcRequests.erase(requestId);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
not_null<Session*> Instance::Private::getSession(
|
2018-06-02 14:29:21 +00:00
|
|
|
ShiftedDcId shiftedDcId) {
|
2017-02-24 17:15:41 +00:00
|
|
|
if (!shiftedDcId) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_mainSession != nullptr);
|
2017-02-24 17:15:41 +00:00
|
|
|
return _mainSession;
|
2019-11-14 17:39:15 +00:00
|
|
|
} else if (!BareDcId(shiftedDcId)) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_mainSession != nullptr);
|
2018-06-11 18:35:27 +00:00
|
|
|
shiftedDcId += BareDcId(_mainSession->getDcWithShift());
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
if (const auto session = findSession(shiftedDcId)) {
|
|
|
|
return session;
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2019-11-14 17:39:15 +00:00
|
|
|
return startSession(shiftedDcId);
|
|
|
|
}
|
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
rpl::lifetime &Instance::Private::lifetime() {
|
|
|
|
return _lifetime;
|
|
|
|
}
|
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
Session *Instance::Private::findSession(ShiftedDcId shiftedDcId) {
|
|
|
|
const auto i = _sessions.find(shiftedDcId);
|
|
|
|
return (i != _sessions.end()) ? i->second.get() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<Session*> Instance::Private::startSession(ShiftedDcId shiftedDcId) {
|
|
|
|
Expects(BareDcId(shiftedDcId) != 0);
|
|
|
|
|
2019-11-20 13:33:45 +00:00
|
|
|
const auto dc = getDcById(shiftedDcId);
|
2019-11-27 15:09:56 +00:00
|
|
|
const auto thread = getThreadForDc(shiftedDcId);
|
2019-11-14 17:39:15 +00:00
|
|
|
const auto result = _sessions.emplace(
|
|
|
|
shiftedDcId,
|
2019-11-27 15:09:56 +00:00
|
|
|
std::make_unique<Session>(_instance, thread, shiftedDcId, dc)
|
2019-11-14 17:39:15 +00:00
|
|
|
).first->second.get();
|
2019-11-20 13:33:45 +00:00
|
|
|
if (isKeysDestroyer()) {
|
|
|
|
scheduleKeyDestroy(shiftedDcId);
|
|
|
|
}
|
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-30 17:32:30 +00:00
|
|
|
void Instance::Private::scheduleSessionDestroy(ShiftedDcId shiftedDcId) {
|
2019-11-14 17:39:15 +00:00
|
|
|
const auto i = _sessions.find(shiftedDcId);
|
|
|
|
if (i == _sessions.cend()) {
|
2021-08-30 17:32:30 +00:00
|
|
|
return;
|
2019-11-14 17:39:15 +00:00
|
|
|
}
|
|
|
|
i->second->kill();
|
2019-11-15 07:28:33 +00:00
|
|
|
_sessionsToDestroy.push_back(std::move(i->second));
|
2019-11-14 17:39:15 +00:00
|
|
|
_sessions.erase(i);
|
2021-08-30 17:32:30 +00:00
|
|
|
InvokeQueued(_instance, [=] {
|
|
|
|
_sessionsToDestroy.clear();
|
|
|
|
});
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 15:09:56 +00:00
|
|
|
|
|
|
|
not_null<QThread*> Instance::Private::getThreadForDc(
|
|
|
|
ShiftedDcId shiftedDcId) {
|
2019-12-12 13:26:26 +00:00
|
|
|
static const auto EnsureStarted = [](
|
|
|
|
std::unique_ptr<QThread> &thread,
|
|
|
|
auto name) {
|
2019-11-27 15:09:56 +00:00
|
|
|
if (!thread) {
|
|
|
|
thread = std::make_unique<QThread>();
|
2019-12-12 13:26:26 +00:00
|
|
|
thread->setObjectName(name());
|
2019-11-27 15:09:56 +00:00
|
|
|
thread->start();
|
|
|
|
}
|
|
|
|
return thread.get();
|
|
|
|
};
|
|
|
|
static const auto FindOne = [](
|
|
|
|
std::vector<std::unique_ptr<QThread>> &threads,
|
2019-12-12 13:26:26 +00:00
|
|
|
const char *prefix,
|
2019-11-27 15:09:56 +00:00
|
|
|
int index,
|
|
|
|
bool shift) {
|
|
|
|
Expects(!threads.empty());
|
|
|
|
Expects(!(threads.size() % 2));
|
|
|
|
|
|
|
|
const auto count = int(threads.size());
|
|
|
|
index %= count;
|
|
|
|
if (index >= count / 2) {
|
|
|
|
index = (count - 1) - (index - count / 2);
|
|
|
|
}
|
|
|
|
if (shift) {
|
|
|
|
index = (index + count / 2) % count;
|
|
|
|
}
|
2019-12-12 13:26:26 +00:00
|
|
|
return EnsureStarted(threads[index], [=] {
|
|
|
|
return QString("MTP %1 Session (%2)").arg(prefix).arg(index);
|
|
|
|
});
|
2019-11-27 15:09:56 +00:00
|
|
|
};
|
|
|
|
if (shiftedDcId == BareDcId(shiftedDcId)) {
|
2019-12-12 13:26:26 +00:00
|
|
|
return EnsureStarted(_mainSessionThread, [] {
|
|
|
|
return QString("MTP Main Session");
|
|
|
|
});
|
2019-11-27 15:09:56 +00:00
|
|
|
} else if (isDownloadDcId(shiftedDcId)) {
|
|
|
|
const auto index = GetDcIdShift(shiftedDcId) - kBaseDownloadDcShift;
|
|
|
|
const auto composed = index + BareDcId(shiftedDcId);
|
2019-12-12 13:26:26 +00:00
|
|
|
return FindOne(_fileSessionThreads, "Download", composed, false);
|
2019-11-27 15:09:56 +00:00
|
|
|
} else if (isUploadDcId(shiftedDcId)) {
|
|
|
|
const auto index = GetDcIdShift(shiftedDcId) - kBaseUploadDcShift;
|
|
|
|
const auto composed = index + BareDcId(shiftedDcId);
|
2019-12-12 13:26:26 +00:00
|
|
|
return FindOne(_fileSessionThreads, "Upload", composed, true);
|
2019-11-27 15:09:56 +00:00
|
|
|
}
|
2019-12-12 13:26:26 +00:00
|
|
|
return EnsureStarted(_otherSessionsThread, [] {
|
|
|
|
return QString("MTP Other Session");
|
|
|
|
});
|
2019-11-27 15:09:56 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
void Instance::Private::scheduleKeyDestroy(ShiftedDcId shiftedDcId) {
|
2017-06-26 17:38:16 +00:00
|
|
|
Expects(isKeysDestroyer());
|
2017-02-25 16:44:02 +00:00
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
if (dcOptions().dcType(shiftedDcId) == DcType::Cdn) {
|
2017-02-25 16:44:02 +00:00
|
|
|
performKeyDestroy(shiftedDcId);
|
2018-05-18 16:41:59 +00:00
|
|
|
} else {
|
2021-03-12 10:45:13 +00:00
|
|
|
_instance->send(MTPauth_LogOut(), [=](const Response &) {
|
2018-05-18 16:41:59 +00:00
|
|
|
performKeyDestroy(shiftedDcId);
|
2021-03-12 10:45:13 +00:00
|
|
|
return true;
|
2021-03-12 12:48:00 +00:00
|
|
|
}, [=](const Error &error, const Response &) {
|
|
|
|
if (IsDefaultHandledError(error)) {
|
2019-11-15 07:28:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-18 16:41:59 +00:00
|
|
|
performKeyDestroy(shiftedDcId);
|
|
|
|
return true;
|
2021-03-12 10:45:13 +00:00
|
|
|
}, shiftedDcId);
|
2018-05-18 16:41:59 +00:00
|
|
|
}
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:33:45 +00:00
|
|
|
void Instance::Private::keyWasPossiblyDestroyed(ShiftedDcId shiftedDcId) {
|
|
|
|
Expects(isKeysDestroyer());
|
|
|
|
|
2019-11-15 07:28:33 +00:00
|
|
|
InvokeQueued(_instance, [=] {
|
2019-11-20 13:33:45 +00:00
|
|
|
LOG(("MTP Info: checkIfKeyWasDestroyed on destroying key %1, "
|
|
|
|
"assuming it is destroyed.").arg(shiftedDcId));
|
|
|
|
completedKeyDestroy(shiftedDcId);
|
2019-11-15 07:28:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
void Instance::Private::performKeyDestroy(ShiftedDcId shiftedDcId) {
|
2017-06-26 17:38:16 +00:00
|
|
|
Expects(isKeysDestroyer());
|
2017-02-25 16:44:02 +00:00
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
_instance->send(MTPDestroy_auth_key(), [=](const Response &response) {
|
|
|
|
auto result = MTPDestroyAuthKeyRes();
|
|
|
|
auto from = response.reply.constData();
|
|
|
|
if (!result.read(from, from + response.reply.size())) {
|
|
|
|
return false;
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
2021-03-12 10:45:13 +00:00
|
|
|
result.match([&](const MTPDdestroy_auth_key_ok &) {
|
|
|
|
LOG(("MTP Info: key %1 destroyed.").arg(shiftedDcId));
|
|
|
|
}, [&](const MTPDdestroy_auth_key_fail &) {
|
|
|
|
LOG(("MTP Error: key %1 destruction fail, leave it for now."
|
|
|
|
).arg(shiftedDcId));
|
|
|
|
killSession(shiftedDcId);
|
|
|
|
}, [&](const MTPDdestroy_auth_key_none &) {
|
|
|
|
LOG(("MTP Info: key %1 already destroyed.").arg(shiftedDcId));
|
|
|
|
});
|
2019-11-20 13:33:45 +00:00
|
|
|
_instance->keyWasPossiblyDestroyed(shiftedDcId);
|
2021-03-12 10:45:13 +00:00
|
|
|
return true;
|
2021-03-12 12:48:00 +00:00
|
|
|
}, [=](const Error &error, const Response &response) {
|
2021-03-12 10:45:13 +00:00
|
|
|
LOG(("MTP Error: key %1 destruction resulted in error: %2"
|
|
|
|
).arg(shiftedDcId).arg(error.type()));
|
2019-11-20 13:33:45 +00:00
|
|
|
_instance->keyWasPossiblyDestroyed(shiftedDcId);
|
2017-02-25 16:44:02 +00:00
|
|
|
return true;
|
2021-03-12 10:45:13 +00:00
|
|
|
}, shiftedDcId);
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::completedKeyDestroy(ShiftedDcId shiftedDcId) {
|
2017-06-26 17:38:16 +00:00
|
|
|
Expects(isKeysDestroyer());
|
2017-02-25 16:44:02 +00:00
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
removeDc(shiftedDcId);
|
2019-11-19 10:10:51 +00:00
|
|
|
_keysForWrite.erase(shiftedDcId);
|
2017-02-25 16:44:02 +00:00
|
|
|
killSession(shiftedDcId);
|
|
|
|
if (_dcenters.empty()) {
|
2019-11-27 14:22:22 +00:00
|
|
|
_allKeysDestroyed.fire({});
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:33:45 +00:00
|
|
|
void Instance::Private::keyDestroyedOnServer(
|
|
|
|
ShiftedDcId shiftedDcId,
|
|
|
|
uint64 keyId) {
|
|
|
|
LOG(("Destroying key for dc: %1").arg(shiftedDcId));
|
|
|
|
if (const auto dc = findDc(BareDcId(shiftedDcId))) {
|
2019-11-15 13:04:32 +00:00
|
|
|
if (dc->destroyConfirmedForgottenKey(keyId)) {
|
|
|
|
LOG(("Key destroyed!"));
|
2019-11-20 13:33:45 +00:00
|
|
|
dcPersistentKeyChanged(BareDcId(shiftedDcId), nullptr);
|
2019-11-15 13:04:32 +00:00
|
|
|
} else {
|
|
|
|
LOG(("Key already is different."));
|
2019-11-14 13:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 13:33:45 +00:00
|
|
|
restart(shiftedDcId);
|
2019-11-13 14:12:04 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::Private::setUpdatesHandler(
|
|
|
|
Fn<void(const Response&)> handler) {
|
|
|
|
_updatesHandler = std::move(handler);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::Private::setGlobalFailHandler(
|
2021-03-12 12:48:00 +00:00
|
|
|
Fn<void(const Error&, const Response&)> handler) {
|
2021-03-12 10:45:13 +00:00
|
|
|
_globalFailHandler = std::move(handler);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::Private::setStateChangedHandler(
|
|
|
|
Fn<void(ShiftedDcId shiftedDcId, int32 state)> handler) {
|
2017-02-24 17:15:41 +00:00
|
|
|
_stateChangedHandler = std::move(handler);
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::Private::setSessionResetHandler(
|
|
|
|
Fn<void(ShiftedDcId shiftedDcId)> handler) {
|
2017-02-24 17:15:41 +00:00
|
|
|
_sessionResetHandler = std::move(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::Private::clearGlobalHandlers() {
|
2021-03-12 10:45:13 +00:00
|
|
|
setUpdatesHandler(nullptr);
|
|
|
|
setGlobalFailHandler(nullptr);
|
|
|
|
setStateChangedHandler(nullptr);
|
|
|
|
setSessionResetHandler(nullptr);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 17:55:11 +00:00
|
|
|
void Instance::Private::prepareToDestroy() {
|
2017-02-27 18:47:29 +00:00
|
|
|
// It accesses Instance in destructor, so it should be destroyed first.
|
|
|
|
_configLoader.reset();
|
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
requestCancellingDiscard();
|
|
|
|
|
2019-11-14 17:39:15 +00:00
|
|
|
for (const auto &[shiftedDcId, session] : base::take(_sessions)) {
|
|
|
|
session->kill();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
2017-02-27 18:47:29 +00:00
|
|
|
_mainSession = nullptr;
|
2019-11-27 15:09:56 +00:00
|
|
|
|
|
|
|
auto threads = std::vector<std::unique_ptr<QThread>>();
|
|
|
|
threads.push_back(base::take(_mainSessionThread));
|
|
|
|
threads.push_back(base::take(_otherSessionsThread));
|
|
|
|
for (auto &thread : base::take(_fileSessionThreads)) {
|
|
|
|
threads.push_back(std::move(thread));
|
|
|
|
}
|
|
|
|
for (const auto &thread : threads) {
|
|
|
|
if (thread) {
|
|
|
|
thread->quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto &thread : threads) {
|
|
|
|
if (thread) {
|
|
|
|
thread->wait();
|
|
|
|
}
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
Instance::Instance(Mode mode, Fields &&fields)
|
2018-05-11 14:03:53 +00:00
|
|
|
: QObject()
|
2020-06-17 09:36:25 +00:00
|
|
|
, _private(std::make_unique<Private>(this, mode, std::move(fields))) {
|
|
|
|
_private->start();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
void Instance::resolveProxyDomain(const QString &host) {
|
|
|
|
_private->resolveProxyDomain(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::setGoodProxyDomain(const QString &host, const QString &ip) {
|
|
|
|
_private->setGoodProxyDomain(host, ip);
|
|
|
|
}
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
void Instance::suggestMainDcId(DcId mainDcId) {
|
|
|
|
_private->suggestMainDcId(mainDcId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::setMainDcId(DcId mainDcId) {
|
|
|
|
_private->setMainDcId(mainDcId);
|
|
|
|
}
|
|
|
|
|
|
|
|
DcId Instance::mainDcId() const {
|
|
|
|
return _private->mainDcId();
|
|
|
|
}
|
|
|
|
|
2021-08-30 17:32:30 +00:00
|
|
|
rpl::producer<DcId> Instance::mainDcIdValue() const {
|
|
|
|
return _private->mainDcIdValue();
|
|
|
|
}
|
|
|
|
|
2017-05-30 09:31:32 +00:00
|
|
|
QString Instance::systemLangCode() const {
|
2020-09-30 09:11:44 +00:00
|
|
|
return Lang::GetInstance().systemLangCode();
|
2017-05-30 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
QString Instance::cloudLangCode() const {
|
2020-09-30 09:11:44 +00:00
|
|
|
return Lang::GetInstance().cloudLangCode(Lang::Pack::Current);
|
2017-04-13 17:59:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 11:31:40 +00:00
|
|
|
QString Instance::langPackName() const {
|
2020-09-30 09:11:44 +00:00
|
|
|
return Lang::GetInstance().langPackName();
|
2018-08-20 11:31:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
rpl::producer<> Instance::writeKeysRequests() const {
|
|
|
|
return _private->writeKeysRequests();
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:22:22 +00:00
|
|
|
rpl::producer<> Instance::allKeysDestroyed() const {
|
|
|
|
return _private->allKeysDestroyed();
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
void Instance::requestConfig() {
|
|
|
|
_private->requestConfig();
|
|
|
|
}
|
|
|
|
|
2018-05-02 19:27:03 +00:00
|
|
|
void Instance::setUserPhone(const QString &phone) {
|
|
|
|
_private->setUserPhone(phone);
|
|
|
|
}
|
|
|
|
|
2018-05-24 13:40:19 +00:00
|
|
|
void Instance::badConfigurationError() {
|
|
|
|
_private->badConfigurationError();
|
|
|
|
}
|
|
|
|
|
2019-07-10 15:03:48 +00:00
|
|
|
void Instance::syncHttpUnixtime() {
|
|
|
|
_private->syncHttpUnixtime();
|
|
|
|
}
|
|
|
|
|
2019-12-05 11:46:28 +00:00
|
|
|
void Instance::restartedByTimeout(ShiftedDcId shiftedDcId) {
|
|
|
|
_private->restartedByTimeout(shiftedDcId);
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<ShiftedDcId> Instance::restartsByTimeout() const {
|
|
|
|
return _private->restartsByTimeout();
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:21:19 +00:00
|
|
|
void Instance::requestConfigIfOld() {
|
|
|
|
_private->requestConfigIfOld();
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
void Instance::requestCDNConfig() {
|
|
|
|
_private->requestCDNConfig();
|
|
|
|
}
|
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
void Instance::restart() {
|
|
|
|
_private->restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::restart(ShiftedDcId shiftedDcId) {
|
|
|
|
_private->restart(shiftedDcId);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 Instance::dcstate(ShiftedDcId shiftedDcId) {
|
|
|
|
return _private->dcstate(shiftedDcId);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Instance::dctransport(ShiftedDcId shiftedDcId) {
|
|
|
|
return _private->dctransport(shiftedDcId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::ping() {
|
|
|
|
_private->ping();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::cancel(mtpRequestId requestId) {
|
|
|
|
_private->cancel(requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 Instance::state(mtpRequestId requestId) { // < 0 means waiting for such count of ms
|
|
|
|
return _private->state(requestId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::killSession(ShiftedDcId shiftedDcId) {
|
|
|
|
_private->killSession(shiftedDcId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::stopSession(ShiftedDcId shiftedDcId) {
|
|
|
|
_private->stopSession(shiftedDcId);
|
|
|
|
}
|
|
|
|
|
2017-04-18 15:21:03 +00:00
|
|
|
void Instance::reInitConnection(DcId dcId) {
|
|
|
|
_private->reInitConnection(dcId);
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:33:45 +00:00
|
|
|
void Instance::logout(Fn<void()> done) {
|
|
|
|
_private->logout(std::move(done));
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
void Instance::dcPersistentKeyChanged(
|
|
|
|
DcId dcId,
|
|
|
|
const AuthKeyPtr &persistentKey) {
|
|
|
|
_private->dcPersistentKeyChanged(dcId, persistentKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::dcTemporaryKeyChanged(DcId dcId) {
|
|
|
|
_private->dcTemporaryKeyChanged(dcId);
|
2019-11-18 12:53:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:10:51 +00:00
|
|
|
rpl::producer<DcId> Instance::dcTemporaryKeyChanged() const {
|
|
|
|
return _private->dcTemporaryKeyChanged();
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
AuthKeysList Instance::getKeysForWrite() const {
|
2017-02-24 17:15:41 +00:00
|
|
|
return _private->getKeysForWrite();
|
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
void Instance::addKeysForDestroy(AuthKeysList &&keys) {
|
|
|
|
_private->addKeysForDestroy(std::move(keys));
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
Config &Instance::config() const {
|
|
|
|
return _private->config();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ConfigFields &Instance::configValues() const {
|
|
|
|
return _private->configValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
DcOptions &Instance::dcOptions() const {
|
2017-02-24 17:15:41 +00:00
|
|
|
return _private->dcOptions();
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
Environment Instance::environment() const {
|
|
|
|
return _private->environment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Instance::isTestMode() const {
|
|
|
|
return _private->isTestMode();
|
|
|
|
}
|
|
|
|
|
2019-02-01 07:48:31 +00:00
|
|
|
QString Instance::deviceModel() const {
|
|
|
|
return _private->deviceModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Instance::systemVersion() const {
|
|
|
|
return _private->systemVersion();
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::setUpdatesHandler(Fn<void(const Response&)> handler) {
|
|
|
|
_private->setUpdatesHandler(std::move(handler));
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::setGlobalFailHandler(
|
2021-03-12 12:48:00 +00:00
|
|
|
Fn<void(const Error&, const Response&)> handler) {
|
2021-03-12 10:45:13 +00:00
|
|
|
_private->setGlobalFailHandler(std::move(handler));
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::setStateChangedHandler(
|
|
|
|
Fn<void(ShiftedDcId shiftedDcId, int32 state)> handler) {
|
2017-02-24 17:15:41 +00:00
|
|
|
_private->setStateChangedHandler(std::move(handler));
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::setSessionResetHandler(
|
|
|
|
Fn<void(ShiftedDcId shiftedDcId)> handler) {
|
2017-02-24 17:15:41 +00:00
|
|
|
_private->setSessionResetHandler(std::move(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::clearGlobalHandlers() {
|
|
|
|
_private->clearGlobalHandlers();
|
|
|
|
}
|
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
void Instance::onStateChange(ShiftedDcId shiftedDcId, int32 state) {
|
|
|
|
_private->onStateChange(shiftedDcId, state);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
void Instance::onSessionReset(ShiftedDcId shiftedDcId) {
|
|
|
|
_private->onSessionReset(shiftedDcId);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
bool Instance::hasCallback(mtpRequestId requestId) const {
|
|
|
|
return _private->hasCallback(requestId);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::processCallback(const Response &response) {
|
|
|
|
_private->processCallback(response);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
void Instance::processUpdate(const Response &message) {
|
|
|
|
_private->processUpdate(message);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
bool Instance::rpcErrorOccured(
|
|
|
|
const Response &response,
|
|
|
|
const FailHandler &onFail,
|
2021-03-12 12:48:00 +00:00
|
|
|
const Error &error) {
|
2021-03-12 10:45:13 +00:00
|
|
|
return _private->rpcErrorOccured(response, onFail, error);
|
2017-02-24 17:15:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
bool Instance::isKeysDestroyer() const {
|
|
|
|
return _private->isKeysDestroyer();
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:33:45 +00:00
|
|
|
void Instance::keyWasPossiblyDestroyed(ShiftedDcId shiftedDcId) {
|
|
|
|
_private->keyWasPossiblyDestroyed(shiftedDcId);
|
2017-02-25 16:44:02 +00:00
|
|
|
}
|
2019-11-13 08:31:12 +00:00
|
|
|
|
2019-11-20 13:33:45 +00:00
|
|
|
void Instance::keyDestroyedOnServer(ShiftedDcId shiftedDcId, uint64 keyId) {
|
|
|
|
_private->keyDestroyedOnServer(shiftedDcId, keyId);
|
2019-11-14 13:34:58 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
void Instance::sendRequest(
|
|
|
|
mtpRequestId requestId,
|
2019-11-22 09:15:38 +00:00
|
|
|
SerializedRequest &&request,
|
2021-03-12 10:45:13 +00:00
|
|
|
ResponseHandler &&callbacks,
|
2018-06-02 14:29:21 +00:00
|
|
|
ShiftedDcId shiftedDcId,
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time msCanWait,
|
2018-06-02 14:29:21 +00:00
|
|
|
bool needsLayer,
|
|
|
|
mtpRequestId afterRequestId) {
|
|
|
|
return _private->sendRequest(
|
|
|
|
requestId,
|
|
|
|
std::move(request),
|
|
|
|
std::move(callbacks),
|
|
|
|
shiftedDcId,
|
|
|
|
msCanWait,
|
|
|
|
needsLayer,
|
|
|
|
afterRequestId);
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
void Instance::sendAnything(ShiftedDcId shiftedDcId, crl::time msCanWait) {
|
2019-11-19 10:10:51 +00:00
|
|
|
_private->getSession(shiftedDcId)->sendAnything(msCanWait);
|
2017-12-18 12:40:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
rpl::lifetime &Instance::lifetime() {
|
|
|
|
return _private->lifetime();
|
|
|
|
}
|
|
|
|
|
2017-02-25 17:55:11 +00:00
|
|
|
Instance::~Instance() {
|
|
|
|
_private->prepareToDestroy();
|
|
|
|
}
|
2017-02-24 17:15:41 +00:00
|
|
|
|
|
|
|
} // namespace MTP
|