diff --git a/Telegram/SourceFiles/mtproto/mtp.cpp b/Telegram/SourceFiles/mtproto/mtp.cpp index 0d1122de4..912714ad5 100644 --- a/Telegram/SourceFiles/mtproto/mtp.cpp +++ b/Telegram/SourceFiles/mtproto/mtp.cpp @@ -677,8 +677,16 @@ namespace MTP { void killSession(int32 dc) { Sessions::iterator i = sessions.find(dc); if (i != sessions.end()) { + bool wasMain = (i.value() == mainSession); + i.value()->stop(); sessions.erase(i); + + if (wasMain) { + mainSession = MTProtoSessionPtr(new MTProtoSession()); + mainSession->start(mtpMainDC()); + sessions[mainSession->getDC()] = mainSession; + } } } diff --git a/Telegram/SourceFiles/mtproto/mtpConnection.cpp b/Telegram/SourceFiles/mtproto/mtpConnection.cpp index 5ed9f7419..9193aed81 100644 --- a/Telegram/SourceFiles/mtproto/mtpConnection.cpp +++ b/Telegram/SourceFiles/mtproto/mtpConnection.cpp @@ -298,16 +298,16 @@ int32 MTProtoConnection::start(MTPSessionData *sessionData, int32 dc) { dc = data->getDC(); if (!dc) { + delete data; + delete thread; + data = 0; + thread = 0; return 0; } thread->start(); return dc; } -void MTProtoConnection::restart() { - emit data->needToRestart(); -} - void MTProtoConnection::stop() { if (data) data->stop(); if (thread) thread->quit(); @@ -318,6 +318,7 @@ void MTProtoConnection::stopped() { if (data) data->deleteLater(); thread = 0; data = 0; + delete this; } int32 MTProtoConnection::state() const { @@ -1107,20 +1108,20 @@ MTProtoConnectionPrivate::MTProtoConnectionPrivate(QThread *thread, MTProtoConne connect(thread, SIGNAL(started()), this, SLOT(socketStart())); connect(thread, SIGNAL(finished()), this, SLOT(doFinish())); - connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); connect(&retryTimer, SIGNAL(timeout()), this, SLOT(retryByTimer())); connect(&connCheckTimer, SIGNAL(timeout()), this, SLOT(onBadConnection())); connect(&oldConnectionTimer, SIGNAL(timeout()), this, SLOT(onOldConnection())); - connect(sessionData->owner(), SIGNAL(authKeyCreated()), this, SLOT(updateAuthKey())); + connect(sessionData->owner(), SIGNAL(authKeyCreated()), this, SLOT(updateAuthKey()), Qt::QueuedConnection); - connect(this, SIGNAL(needToRestart()), this, SLOT(restartNow())); - connect(this, SIGNAL(needToReceive()), sessionData->owner(), SLOT(tryToReceive())); - connect(this, SIGNAL(stateChanged(qint32)), sessionData->owner(), SLOT(onConnectionStateChange(qint32))); - connect(sessionData->owner(), SIGNAL(needToSend()), this, SLOT(tryToSend())); - connect(this, SIGNAL(needToSendAsync()), sessionData->owner(), SIGNAL(needToSend())); - connect(this, SIGNAL(sessionResetDone()), sessionData->owner(), SLOT(onResetDone())); - connect(this, SIGNAL(sendAnythingAsync(quint64)), sessionData->owner(), SLOT(sendAnything(quint64))); + connect(sessionData->owner(), SIGNAL(needToRestart()), this, SLOT(restartNow()), Qt::QueuedConnection); + connect(this, SIGNAL(needToReceive()), sessionData->owner(), SLOT(tryToReceive()), Qt::QueuedConnection); + connect(this, SIGNAL(stateChanged(qint32)), sessionData->owner(), SLOT(onConnectionStateChange(qint32)), Qt::QueuedConnection); + connect(sessionData->owner(), SIGNAL(needToSend()), this, SLOT(tryToSend()), Qt::QueuedConnection); + connect(this, SIGNAL(needToSendAsync()), sessionData->owner(), SIGNAL(needToSend()), Qt::QueuedConnection); + connect(this, SIGNAL(sendHttpWait()), sessionData->owner(), SLOT(sendHttpWait()), Qt::QueuedConnection); + connect(this, SIGNAL(sessionResetDone()), sessionData->owner(), SLOT(onResetDone()), Qt::QueuedConnection); + connect(this, SIGNAL(sendAnythingAsync(quint64)), sessionData->owner(), SLOT(sendAnything(quint64)), Qt::QueuedConnection); } void MTProtoConnectionPrivate::onConfigLoaded() { @@ -1986,7 +1987,7 @@ void MTProtoConnectionPrivate::handleReceived() { } } if (conn->needHttpWait()) { - sessionData->owner()->send(MTPHttpWait(MTP_http_wait(MTP_int(100), MTP_int(30), MTP_int(25000)))); + emit sendHttpWait(); } } @@ -3399,5 +3400,4 @@ void MTProtoConnectionPrivate::stop() { } MTProtoConnection::~MTProtoConnection() { - stopped(); } diff --git a/Telegram/SourceFiles/mtproto/mtpConnection.h b/Telegram/SourceFiles/mtproto/mtpConnection.h index 4d0e99426..1dd9be144 100644 --- a/Telegram/SourceFiles/mtproto/mtpConnection.h +++ b/Telegram/SourceFiles/mtproto/mtpConnection.h @@ -71,7 +71,6 @@ public: MTProtoConnection(); int32 start(MTPSessionData *data, int32 dc = 0); // return dc - void restart(); void stop(); void stopped(); ~MTProtoConnection(); @@ -312,6 +311,8 @@ signals: void needToSendAsync(); void sendAnythingAsync(quint64); + void sendHttpWait(); + public slots: void retryByTimer(); diff --git a/Telegram/SourceFiles/mtproto/mtpDC.cpp b/Telegram/SourceFiles/mtproto/mtpDC.cpp index 2d19de2aa..1d2dde7fc 100644 --- a/Telegram/SourceFiles/mtproto/mtpDC.cpp +++ b/Telegram/SourceFiles/mtproto/mtpDC.cpp @@ -460,7 +460,7 @@ void MTProtoConfigLoader::load() { void MTProtoConfigLoader::done() { _enumDCTimer.stop(); if (_enumRequest) MTP::cancel(_enumRequest); - if (_enumCurrent) MTP::killSession(_enumCurrent); + if (_enumCurrent) MTP::killSession(MTP::cfg + _enumCurrent); emit loaded(); } diff --git a/Telegram/SourceFiles/mtproto/mtpSession.cpp b/Telegram/SourceFiles/mtproto/mtpSession.cpp index b58fe7652..0c7c955de 100644 --- a/Telegram/SourceFiles/mtproto/mtpSession.cpp +++ b/Telegram/SourceFiles/mtproto/mtpSession.cpp @@ -120,9 +120,7 @@ void MTProtoSession::start(int32 dcenter, uint32 connects) { } void MTProtoSession::restart() { - for (MTProtoConnections::const_iterator i = connections.cbegin(), e = connections.cend(); i != e; ++i) { - (*i)->restart(); - } + emit needToRestart(); } void MTProtoSession::stop() { @@ -158,6 +156,10 @@ void MTProtoSession::sendAnything(quint64 msCanWait) { } } +void MTProtoSession::sendHttpWait() { + send(MTPHttpWait(MTP_http_wait(MTP_int(100), MTP_int(30), MTP_int(25000))), RPCResponseHandler(), 50); +} + void MTProtoSession::checkRequestsByTimer() { QVector resendingIds; QVector removingIds; // remove very old (10 minutes) containers and resend requests diff --git a/Telegram/SourceFiles/mtproto/mtpSession.h b/Telegram/SourceFiles/mtproto/mtpSession.h index 372d8a642..04281e839 100644 --- a/Telegram/SourceFiles/mtproto/mtpSession.h +++ b/Telegram/SourceFiles/mtproto/mtpSession.h @@ -250,8 +250,8 @@ public: signals: void authKeyCreated(); - void needToSend(); + void needToRestart(); public slots: @@ -265,6 +265,8 @@ public slots: void sendAnything(quint64 msCanWait); + void sendHttpWait(); + private: typedef QList MTProtoConnections;