tdesktop/Telegram/SourceFiles/mtproto/details/mtproto_dcenter.cpp

170 lines
4.6 KiB
C++
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
2019-12-02 13:10:19 +00:00
#include "mtproto/details/mtproto_dcenter.h"
#include "mtproto/facade.h"
2019-11-18 09:28:14 +00:00
#include "mtproto/mtproto_auth_key.h"
#include "mtproto/dc_options.h"
#include "mtproto/mtp_instance.h"
2017-06-26 17:38:16 +00:00
#include "mtproto/special_config_request.h"
namespace MTP {
2019-12-02 13:10:19 +00:00
namespace details {
namespace {
constexpr auto kEnumerateDcTimeout = 8000; // 8 seconds timeout for help_getConfig to work (then move to other dc)
2017-06-26 17:38:16 +00:00
constexpr auto kSpecialRequestTimeoutMs = 6000; // 4 seconds timeout for it to work in a specially requested dc.
2019-11-21 10:37:39 +00:00
int IndexByType(TemporaryKeyType type) {
switch (type) {
case TemporaryKeyType::Regular: return 0;
case TemporaryKeyType::MediaCluster: return 1;
}
Unexpected("Type value in IndexByType.");
}
int IndexByType(CreatingKeyType type) {
switch (type) {
case CreatingKeyType::Persistent:
case CreatingKeyType::TemporaryRegular: return 0;
case CreatingKeyType::TemporaryMediaCluster: return 1;
}
Unexpected("Creating type value in IndexByType.");
}
const char *NameOfType(CreatingKeyType type) {
switch (type) {
case CreatingKeyType::Persistent: return "persistent";
case CreatingKeyType::TemporaryRegular: return "regular";
case CreatingKeyType::TemporaryMediaCluster: return "media";
}
Unexpected("Type value in NameOfType.");
}
} // namespace
2019-12-28 10:52:29 +00:00
TemporaryKeyType TemporaryKeyTypeByDcType(DcType type) {
return (type == DcType::MediaCluster)
? TemporaryKeyType::MediaCluster
: TemporaryKeyType::Regular;
}
Dcenter::Dcenter(DcId dcId, AuthKeyPtr &&key)
: _id(dcId)
, _persistentKey(std::move(key)) {
}
DcId Dcenter::id() const {
return _id;
}
2019-11-21 10:37:39 +00:00
AuthKeyPtr Dcenter::getTemporaryKey(TemporaryKeyType type) const {
QReadLocker lock(&_mutex);
2019-11-21 10:37:39 +00:00
return _temporaryKeys[IndexByType(type)];
}
AuthKeyPtr Dcenter::getPersistentKey() const {
QReadLocker lock(&_mutex);
return _persistentKey;
}
bool Dcenter::destroyTemporaryKey(uint64 keyId) {
QWriteLocker lock(&_mutex);
2019-11-21 10:37:39 +00:00
for (auto &key : _temporaryKeys) {
if (key && key->keyId() == keyId) {
key = nullptr;
_connectionInited = false;
return true;
}
}
2019-11-21 10:37:39 +00:00
return false;
}
bool Dcenter::destroyConfirmedForgottenKey(uint64 keyId) {
QWriteLocker lock(&_mutex);
if (!_persistentKey || _persistentKey->keyId() != keyId) {
return false;
}
2019-11-21 10:37:39 +00:00
for (auto &key : _temporaryKeys) {
key = nullptr;
}
_persistentKey = nullptr;
_connectionInited = false;
return true;
}
2019-11-15 07:28:33 +00:00
bool Dcenter::connectionInited() const {
QReadLocker lock(&_mutex);
2019-11-15 07:28:33 +00:00
return _connectionInited;
}
void Dcenter::setConnectionInited(bool connectionInited) {
QWriteLocker lock(&_mutex);
2019-11-15 07:28:33 +00:00
_connectionInited = connectionInited;
}
2019-11-15 07:28:33 +00:00
2019-12-28 10:52:29 +00:00
CreatingKeyType Dcenter::acquireKeyCreation(DcType type) {
QReadLocker lock(&_mutex);
2019-12-28 10:52:29 +00:00
const auto keyType = TemporaryKeyTypeByDcType(type);
const auto index = IndexByType(keyType);
2019-11-21 10:37:39 +00:00
auto &key = _temporaryKeys[index];
if (key != nullptr) {
return CreatingKeyType::None;
2019-11-15 07:28:33 +00:00
}
auto expected = false;
2019-11-21 10:37:39 +00:00
const auto regular = IndexByType(TemporaryKeyType::Regular);
2019-12-28 10:52:29 +00:00
if (keyType == TemporaryKeyType::MediaCluster && _temporaryKeys[regular]) {
2019-11-21 10:37:39 +00:00
return !_creatingKeys[index].compare_exchange_strong(expected, true)
? CreatingKeyType::None
: CreatingKeyType::TemporaryMediaCluster;
}
return !_creatingKeys[regular].compare_exchange_strong(expected, true)
? CreatingKeyType::None
2019-12-28 10:52:29 +00:00
: (type != DcType::Cdn && !_persistentKey)
2019-11-21 10:37:39 +00:00
? CreatingKeyType::Persistent
: CreatingKeyType::TemporaryRegular;
}
2019-11-21 10:37:39 +00:00
bool Dcenter::releaseKeyCreationOnDone(
CreatingKeyType type,
const AuthKeyPtr &temporaryKey,
2019-11-21 10:37:39 +00:00
const AuthKeyPtr &persistentKeyUsedForBind) {
Expects(_creatingKeys[IndexByType(type)]);
Expects(_temporaryKeys[IndexByType(type)] == nullptr);
Expects(temporaryKey != nullptr);
QWriteLocker lock(&_mutex);
2019-11-21 10:37:39 +00:00
if (type == CreatingKeyType::Persistent) {
_persistentKey = persistentKeyUsedForBind;
} else if (_persistentKey != persistentKeyUsedForBind) {
return false;
}
2019-11-21 10:37:39 +00:00
_temporaryKeys[IndexByType(type)] = temporaryKey;
_creatingKeys[IndexByType(type)] = false;
_connectionInited = false;
2019-11-21 10:37:39 +00:00
DEBUG_LOG(("AuthKey Info: Dcenter::releaseKeyCreationOnDone(%1, %2, %3)."
).arg(NameOfType(type)
).arg(temporaryKey ? temporaryKey->keyId() : 0
).arg(persistentKeyUsedForBind
? persistentKeyUsedForBind->keyId()
: 0));
return true;
}
void Dcenter::releaseKeyCreationOnFail(CreatingKeyType type) {
Expects(_creatingKeys[IndexByType(type)]);
Expects(_temporaryKeys[IndexByType(type)] == nullptr);
_creatingKeys[IndexByType(type)] = false;
2019-11-15 07:28:33 +00:00
}
2019-12-02 13:10:19 +00:00
} // namespace details
} // namespace MTP