Skip to content

Commit

Permalink
Requesting config if can't connect.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed May 5, 2018
1 parent 4a9db99 commit 909acb2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
20 changes: 18 additions & 2 deletions Telegram/SourceFiles/mtproto/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ constexpr auto kRecreateKeyId = AuthKey::KeyId(0xFFFFFFFFFFFFFFFFULL);
constexpr auto kIntSize = static_cast<int>(sizeof(mtpPrime));
constexpr auto kMaxModExpSize = 256;

// If we can't connect for this time we will ask _instance to update config.
constexpr auto kRequestConfigTimeout = TimeMs(8000);

// Don't try to handle messages larger than this size.
constexpr auto kMaxMessageLength = 16 * 1024 * 1024;

Expand Down Expand Up @@ -356,6 +359,7 @@ ConnectionPrivate::ConnectionPrivate(Instance *instance, QThread *thread, Connec
, _state(DisconnectedState)
, _shiftedDcId(shiftedDcId)
, _owner(owner)
, _configWasFineAt(getms(true))
, _waitForReceived(MTPMinReceiveDelay)
, _waitForConnected(MTPMinConnectDelay)
//, sessionDataMutex(QReadWriteLock::Recursive)
Expand Down Expand Up @@ -1031,11 +1035,21 @@ void ConnectionPrivate::connectToServer(bool afterConfig) {
DEBUG_LOG(("MTP Info: DC %1 options for IPv4 over HTTP not found, waiting for config").arg(_shiftedDcId));
if (Global::TryIPv6() && noIPv6) DEBUG_LOG(("MTP Info: DC %1 options for IPv6 over HTTP not found, waiting for config").arg(_shiftedDcId));
connect(_instance, SIGNAL(configLoaded()), this, SLOT(onConfigLoaded()), Qt::UniqueConnection);
InvokeQueued(_instance, [instance = _instance] { instance->requestConfig(); });
InvokeQueued(_instance, [instance = _instance] {
instance->requestConfig();
});
return;
}

if (afterConfig && (_conn4 || _conn6)) return;
if (afterConfig) {
if (_conn4 || _conn6) {
return;
}
} else if (getms(true) - _configWasFineAt > kRequestConfigTimeout) {
InvokeQueued(_instance, [instance = _instance] {
instance->requestConfigIfOld();
});
}

createConn(!noIPv4, !noIPv6);
retryTimer.stop();
Expand Down Expand Up @@ -2340,6 +2354,8 @@ void ConnectionPrivate::updateAuthKey() {
QReadLocker lockFinished(&sessionDataMutex);
if (!sessionData || !_conn) return;

_configWasFineAt = getms(true);

DEBUG_LOG(("AuthKey Info: Connection updating key from Session, dc %1").arg(_shiftedDcId));
uint64 newKeyId = 0;
{
Expand Down
1 change: 1 addition & 0 deletions Telegram/SourceFiles/mtproto/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public slots:
AbstractConnection *_conn = nullptr;
AbstractConnection *_conn4 = nullptr;
AbstractConnection *_conn6 = nullptr;
TimeMs _configWasFineAt = 0;

SingleTimer retryTimer; // exp retry timer
int retryTimeout = 1;
Expand Down
18 changes: 18 additions & 0 deletions Telegram/SourceFiles/mtproto/mtp_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ For license and copyright information please follow this link:
#include "base/timer.h"

namespace MTP {
namespace {

constexpr auto kConfigBecomesOldIn = 2 * 60 * TimeMs(1000);

} // namespace

class Instance::Private : private Sender {
public:
Expand All @@ -41,6 +46,7 @@ class Instance::Private : private Sender {
not_null<DcOptions*> dcOptions();

void requestConfig();
void requestConfigIfOld();
void requestCDNConfig();

void restart();
Expand Down Expand Up @@ -149,6 +155,7 @@ class Instance::Private : private Sender {

std::unique_ptr<internal::ConfigLoader> _configLoader;
mtpRequestId _cdnConfigLoadRequestId = 0;
TimeMs _lastConfigLoadedTime = 0;

std::map<DcId, AuthKeyPtr> _keysForWrite;
mutable QReadWriteLock _keysForWriteLock;
Expand Down Expand Up @@ -284,6 +291,12 @@ void Instance::Private::requestConfig() {
_configLoader->load();
}

void Instance::Private::requestConfigIfOld() {
if (getms(true) - _lastConfigLoadedTime >= kConfigBecomesOldIn) {
requestConfig();
}
}

void Instance::Private::requestCDNConfig() {
if (_cdnConfigLoadRequestId || _mainDcId == Config::kNoneMainDc) {
return;
Expand Down Expand Up @@ -580,6 +593,7 @@ void Instance::Private::configLoadDone(const MTPConfig &result) {
Expects(result.type() == mtpc_config);

_configLoader.reset();
_lastConfigLoadedTime = getms(true);

auto &data = result.c_config();
DEBUG_LOG(("MTP Info: got config, chat_size_max: %1, date: %2, test_mode: %3, this_dc: %4, dc_options.length: %5").arg(data.vchat_size_max.v).arg(data.vdate.v).arg(mtpIsTrue(data.vtest_mode)).arg(data.vthis_dc.v).arg(data.vdc_options.v.size()));
Expand Down Expand Up @@ -1310,6 +1324,10 @@ void Instance::requestConfig() {
_private->requestConfig();
}

void Instance::requestConfigIfOld() {
_private->requestConfigIfOld();
}

void Instance::requestCDNConfig() {
_private->requestCDNConfig();
}
Expand Down
1 change: 1 addition & 0 deletions Telegram/SourceFiles/mtproto/mtp_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class Instance : public QObject {
void scheduleKeyDestroy(ShiftedDcId shiftedDcId);

void requestConfig();
void requestConfigIfOld();
void requestCDNConfig();

~Instance();
Expand Down

0 comments on commit 909acb2

Please sign in to comment.