tdesktop/Telegram/SourceFiles/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
*/
#include "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"
2017-03-04 10:23:56 +00:00
#include "storage/localstorage.h"
namespace MTP {
namespace internal {
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
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-11-21 10:37:39 +00:00
CreatingKeyType Dcenter::acquireKeyCreation(TemporaryKeyType type) {
QReadLocker lock(&_mutex);
2019-11-21 10:37:39 +00:00
if (type == TemporaryKeyType::MediaCluster) {
int a = 0;
}
const auto index = IndexByType(type);
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);
if (type == TemporaryKeyType::MediaCluster && _temporaryKeys[regular]) {
return !_creatingKeys[index].compare_exchange_strong(expected, true)
? CreatingKeyType::None
: CreatingKeyType::TemporaryMediaCluster;
}
return !_creatingKeys[regular].compare_exchange_strong(expected, true)
? CreatingKeyType::None
: !_persistentKey
? 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) {
return false;
}
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
}
} // namespace internal
} // namespace MTP