2014-05-30 12:53:19 +04:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 13:23:14 +03:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2018-01-03 13:23:14 +03:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 12:53:19 +04:00
|
|
|
*/
|
2016-03-23 21:43:12 +03:00
|
|
|
#include "mtproto/dcenter.h"
|
|
|
|
|
|
|
|
#include "mtproto/facade.h"
|
2019-11-18 12:28:14 +03:00
|
|
|
#include "mtproto/mtproto_auth_key.h"
|
2017-02-23 09:57:04 +03:00
|
|
|
#include "mtproto/dc_options.h"
|
2017-02-24 20:15:41 +03:00
|
|
|
#include "mtproto/mtp_instance.h"
|
2017-06-26 20:38:16 +03:00
|
|
|
#include "mtproto/special_config_request.h"
|
2017-03-04 13:23:56 +03:00
|
|
|
#include "storage/localstorage.h"
|
2014-11-22 12:45:04 +03:00
|
|
|
|
2016-03-24 15:57:10 +03:00
|
|
|
namespace MTP {
|
|
|
|
namespace internal {
|
|
|
|
namespace {
|
2017-02-23 09:57:04 +03:00
|
|
|
|
|
|
|
constexpr auto kEnumerateDcTimeout = 8000; // 8 seconds timeout for help_getConfig to work (then move to other dc)
|
2017-06-26 20:38:16 +03:00
|
|
|
constexpr auto kSpecialRequestTimeoutMs = 6000; // 4 seconds timeout for it to work in a specially requested dc.
|
2017-02-23 09:57:04 +03:00
|
|
|
|
2016-03-24 15:57:10 +03:00
|
|
|
} // namespace
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2019-11-15 16:04:32 +03:00
|
|
|
Dcenter::Dcenter(DcId dcId, AuthKeyPtr &&key)
|
|
|
|
: _id(dcId)
|
2019-11-19 13:10:51 +03:00
|
|
|
, _persistentKey(std::move(key)) {
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2019-11-15 16:04:32 +03:00
|
|
|
DcId Dcenter::id() const {
|
|
|
|
return _id;
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2019-11-19 13:10:51 +03:00
|
|
|
AuthKeyPtr Dcenter::getTemporaryKey() const {
|
2019-11-15 16:04:32 +03:00
|
|
|
QReadLocker lock(&_mutex);
|
2019-11-19 13:10:51 +03:00
|
|
|
return _temporaryKey;
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2019-11-19 13:10:51 +03:00
|
|
|
AuthKeyPtr Dcenter::getPersistentKey() const {
|
|
|
|
QReadLocker lock(&_mutex);
|
|
|
|
return _persistentKey;
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2019-11-19 13:10:51 +03:00
|
|
|
bool Dcenter::destroyTemporaryKey(uint64 keyId) {
|
|
|
|
QWriteLocker lock(&_mutex);
|
|
|
|
if (!_temporaryKey || _temporaryKey->keyId() != keyId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_temporaryKey = nullptr;
|
|
|
|
_connectionInited = false;
|
|
|
|
return true;
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2019-11-19 13:10:51 +03:00
|
|
|
bool Dcenter::destroyConfirmedForgottenKey(uint64 keyId) {
|
2019-11-15 16:04:32 +03:00
|
|
|
QWriteLocker lock(&_mutex);
|
2019-11-19 13:10:51 +03:00
|
|
|
if (!_persistentKey || _persistentKey->keyId() != keyId) {
|
2019-11-15 16:04:32 +03:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-19 13:10:51 +03:00
|
|
|
_temporaryKey = nullptr;
|
|
|
|
_persistentKey = nullptr;
|
2019-11-15 16:04:32 +03:00
|
|
|
_connectionInited = false;
|
|
|
|
return true;
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:28:33 +03:00
|
|
|
bool Dcenter::connectionInited() const {
|
2019-11-15 16:04:32 +03:00
|
|
|
QReadLocker lock(&_mutex);
|
2019-11-15 10:28:33 +03:00
|
|
|
return _connectionInited;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dcenter::setConnectionInited(bool connectionInited) {
|
2019-11-15 16:04:32 +03:00
|
|
|
QWriteLocker lock(&_mutex);
|
2019-11-15 10:28:33 +03:00
|
|
|
_connectionInited = connectionInited;
|
2019-11-15 16:04:32 +03:00
|
|
|
}
|
2019-11-15 10:28:33 +03:00
|
|
|
|
2019-11-15 16:04:32 +03:00
|
|
|
bool Dcenter::acquireKeyCreation() {
|
|
|
|
QReadLocker lock(&_mutex);
|
2019-11-19 13:10:51 +03:00
|
|
|
if (_temporaryKey != nullptr) {
|
2019-11-15 16:04:32 +03:00
|
|
|
return false;
|
2019-11-15 10:28:33 +03:00
|
|
|
}
|
2019-11-15 16:04:32 +03:00
|
|
|
auto expected = false;
|
|
|
|
return _creatingKey.compare_exchange_strong(expected, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dcenter::releaseKeyCreationOnFail() {
|
|
|
|
Expects(_creatingKey);
|
2019-11-19 13:10:51 +03:00
|
|
|
Expects(_temporaryKey == nullptr);
|
2019-11-15 16:04:32 +03:00
|
|
|
|
|
|
|
_creatingKey = false;
|
|
|
|
}
|
|
|
|
|
2019-11-19 13:10:51 +03:00
|
|
|
void Dcenter::releaseKeyCreationOnDone(
|
|
|
|
const AuthKeyPtr &temporaryKey,
|
|
|
|
const AuthKeyPtr &persistentKey) {
|
2019-11-15 16:04:32 +03:00
|
|
|
Expects(_creatingKey);
|
2019-11-19 13:10:51 +03:00
|
|
|
Expects(_temporaryKey == nullptr);
|
2019-11-15 16:04:32 +03:00
|
|
|
|
|
|
|
QWriteLocker lock(&_mutex);
|
2019-11-19 13:10:51 +03:00
|
|
|
DEBUG_LOG(("AuthKey Info: Dcenter::releaseKeyCreationOnDone(%1, %2), "
|
|
|
|
"emitting authKeyChanged, dc %3"
|
|
|
|
).arg(temporaryKey ? temporaryKey->keyId() : 0
|
|
|
|
).arg(persistentKey ? persistentKey->keyId() : 0
|
2019-11-18 15:53:37 +03:00
|
|
|
).arg(_id));
|
2019-11-19 13:10:51 +03:00
|
|
|
_temporaryKey = temporaryKey;
|
|
|
|
if (persistentKey) {
|
|
|
|
_persistentKey = persistentKey;
|
|
|
|
}
|
2019-11-15 16:04:32 +03:00
|
|
|
_connectionInited = false;
|
|
|
|
_creatingKey = false;
|
2019-11-15 10:28:33 +03:00
|
|
|
}
|
|
|
|
|
2016-03-24 15:57:10 +03:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace MTP
|