From c9626c140c7ddbf5e7c8789e66cced02bffb11d3 Mon Sep 17 00:00:00 2001 From: John Preston <johnprestonmail@gmail.com> Date: Thu, 16 Apr 2015 17:59:42 +0300 Subject: [PATCH] sending ping_delay_disconnect instead of getDifference by no-updates timeouts --- Telegram/SourceFiles/config.h | 4 ++-- Telegram/SourceFiles/mainwidget.cpp | 11 ++++++++-- Telegram/SourceFiles/mainwidget.h | 1 + Telegram/SourceFiles/mtproto/mtp.cpp | 7 ++++++ Telegram/SourceFiles/mtproto/mtp.h | 1 + .../SourceFiles/mtproto/mtpConnection.cpp | 22 ++++++++++++++----- Telegram/SourceFiles/mtproto/mtpConnection.h | 3 ++- Telegram/SourceFiles/mtproto/mtpSession.cpp | 14 ++++++++++-- Telegram/SourceFiles/mtproto/mtpSession.h | 4 ++++ 9 files changed, 54 insertions(+), 13 deletions(-) diff --git a/Telegram/SourceFiles/config.h b/Telegram/SourceFiles/config.h index 6e26233eab..94c06777f1 100644 --- a/Telegram/SourceFiles/config.h +++ b/Telegram/SourceFiles/config.h @@ -279,8 +279,8 @@ enum { UploadRequestInterval = 500, // one part each half second, if not uploaded faster MaxPhotosInMemory = 50, // try to clear some memory after 50 photos are created - NoUpdatesTimeout = 180 * 1000, // if nothing is received in 3 min we getDifference - NoUpdatesAfterSleepTimeout = 60 * 1000, // if nothing is received in 1 min when was a sleepmode we getDifference + NoUpdatesTimeout = 60 * 1000, // if nothing is received in 1 min we ping + NoUpdatesAfterSleepTimeout = 60 * 1000, // if nothing is received in 1 min when was a sleepmode we ping WaitForSkippedTimeout = 1000, // 1s wait for skipped seq or pts in updates MemoryForImageCache = 64 * 1024 * 1024, // after 64mb of unpacked images we try to clear some memory diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index d7469b104c..0f53a50fe4 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -360,7 +360,7 @@ _failDifferenceTimeout(1), _lastUpdateTime(0), _cachedX(0), _cachedY(0), _backgr connect(&dialogs, SIGNAL(cancelled()), this, SLOT(dialogsCancelled())); connect(&history, SIGNAL(cancelled()), &dialogs, SLOT(activate())); connect(this, SIGNAL(peerPhotoChanged(PeerData*)), this, SIGNAL(dialogsUpdated())); - connect(&noUpdatesTimer, SIGNAL(timeout()), this, SLOT(getDifference())); + connect(&noUpdatesTimer, SIGNAL(timeout()), this, SLOT(mtpPing())); connect(&_onlineTimer, SIGNAL(timeout()), this, SLOT(updateOnline())); connect(&_onlineUpdater, SIGNAL(timeout()), this, SLOT(updateOnlineDisplay())); connect(&_idleFinishTimer, SIGNAL(timeout()), this, SLOT(checkIdleFinish())); @@ -1195,7 +1195,8 @@ void MainWidget::peerUsernameChanged(PeerData *peer) { void MainWidget::checkLastUpdate(bool afterSleep) { uint64 n = getms(true); if (_lastUpdateTime && n > _lastUpdateTime + (afterSleep ? NoUpdatesAfterSleepTimeout : NoUpdatesTimeout)) { - getDifference(); + _lastUpdateTime = n; + MTP::ping(); } } @@ -2398,6 +2399,10 @@ void MainWidget::getDifference() { MTP::send(MTPupdates_GetDifference(MTP_int(updGoodPts), MTP_int(updDate), MTP_int(updQts)), rpcDone(&MainWidget::gotDifference), rpcFail(&MainWidget::failDifference)); } +void MainWidget::mtpPing() { + MTP::ping(); +} + void MainWidget::start(const MTPUser &user) { int32 uid = user.c_userSelf().vid.v; if (MTP::authedId() != uid) { @@ -2805,6 +2810,7 @@ void MainWidget::handleUpdates(const MTPUpdates &updates) { case mtpc_updateShortMessage: { const MTPDupdateShortMessage &d(updates.c_updateShortMessage()); if (!App::userLoaded(d.vuser_id.v) || (d.has_fwd_from_id() && !App::userLoaded(d.vfwd_from_id.v))) { + DEBUG_LOG(("Not loaded for updateShortMessage, good getDifference!")); return getDifference(); } if (!updPtsUpdated(d.vpts.v, d.vpts_count.v)) { @@ -2823,6 +2829,7 @@ void MainWidget::handleUpdates(const MTPUpdates &updates) { case mtpc_updateShortChatMessage: { const MTPDupdateShortChatMessage &d(updates.c_updateShortChatMessage()); if (!App::chatLoaded(d.vchat_id.v) || !App::userLoaded(d.vfrom_id.v) || (d.has_fwd_from_id() && !App::userLoaded(d.vfwd_from_id.v))) { + DEBUG_LOG(("Not loaded for updateShortMessage, good getDifference!")); return getDifference(); } if (!updPtsUpdated(d.vpts.v, d.vpts_count.v)) { diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h index 7a675848d5..f3d8e888b7 100644 --- a/Telegram/SourceFiles/mainwidget.h +++ b/Telegram/SourceFiles/mainwidget.h @@ -367,6 +367,7 @@ public slots: void onParentResize(const QSize &newSize); void getDifference(); + void mtpPing(); void getDifferenceForce(); void updateOnline(bool gotOtherOffline = false); diff --git a/Telegram/SourceFiles/mtproto/mtp.cpp b/Telegram/SourceFiles/mtproto/mtp.cpp index ce7867553e..d2553c4f94 100644 --- a/Telegram/SourceFiles/mtproto/mtp.cpp +++ b/Telegram/SourceFiles/mtproto/mtp.cpp @@ -684,6 +684,13 @@ namespace MTP { _mtp_internal::getSession(dc); } + void ping() { + MTProtoSessionPtr session = _mtp_internal::getSession(0); + if (!session) return; + + return session->ping(); + } + void cancel(mtpRequestId requestId) { mtpMsgId msgId = 0; requestsDelays.remove(requestId); diff --git a/Telegram/SourceFiles/mtproto/mtp.h b/Telegram/SourceFiles/mtproto/mtp.h index 5e4bc6cf74..1224b7ea92 100644 --- a/Telegram/SourceFiles/mtproto/mtp.h +++ b/Telegram/SourceFiles/mtproto/mtp.h @@ -101,6 +101,7 @@ namespace MTP { inline mtpRequestId send(const TRequest &request, RPCDoneHandlerPtr onDone, RPCFailHandlerPtr onFail = RPCFailHandlerPtr(), int32 dc = 0, uint64 msCanWait = 0, mtpRequestId after = 0) { return send(request, RPCResponseHandler(onDone, onFail), dc, msCanWait, after); } + void ping(); void cancel(mtpRequestId req); void killSession(int32 dc); void stopSession(int32 dc); diff --git a/Telegram/SourceFiles/mtproto/mtpConnection.cpp b/Telegram/SourceFiles/mtproto/mtpConnection.cpp index b615890e2e..c4622735ac 100644 --- a/Telegram/SourceFiles/mtproto/mtpConnection.cpp +++ b/Telegram/SourceFiles/mtproto/mtpConnection.cpp @@ -1096,7 +1096,7 @@ MTProtoConnectionPrivate::MTProtoConnectionPrivate(QThread *thread, MTProtoConne , firstSentAt(-1) , _pingId(0) , _pingIdToSend(0) - , _pingSent(0) + , _pingSendAt(0) , _pingMsgId(0) , restarted(false) , keyId(0) @@ -1137,6 +1137,7 @@ MTProtoConnectionPrivate::MTProtoConnectionPrivate(QThread *thread, MTProtoConne 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(sessionData->owner(), SIGNAL(needToPing()), this, SLOT(onPingSendForce()), Qt::QueuedConnection); connect(this, SIGNAL(sessionResetDone()), sessionData->owner(), SLOT(onResetDone()), Qt::QueuedConnection); static bool _registered = false; @@ -1425,7 +1426,7 @@ void MTProtoConnectionPrivate::tryToSend() { bool prependOnly = (state != MTProtoConnection::Connected); mtpRequest pingRequest; if (dc < _mtp_internal::dcShift) { // main session - if (!prependOnly && !_pingIdToSend && !_pingId && _pingSent + (MTPPingSendAfterAuto * 1000ULL) <= getms(true)) { + if (!prependOnly && !_pingIdToSend && !_pingId && _pingSendAt <= getms(true)) { _pingIdToSend = MTP::nonce<mtpPingId>(); } } @@ -1444,7 +1445,8 @@ void MTProtoConnectionPrivate::tryToSend() { DEBUG_LOG(("MTP Info: sending ping_delay_disconnect, ping_id: %1").arg(_pingIdToSend)); } - _pingSent = pingRequest->msDate = getms(true); // > 0 - can send without container + pingRequest->msDate = getms(true); // > 0 - can send without container + _pingSendAt = pingRequest->msDate + (MTPPingSendAfterAuto * 1000ULL); pingRequest->requestId = 0; // dont add to haveSent / wereAcked maps if (dc < _mtp_internal::dcShift && !prependOnly) { // main session @@ -1733,7 +1735,7 @@ void MTProtoConnectionPrivate::socketStart(bool afterConfig) { return; } setState(MTProtoConnection::Connecting); - _pingId = _pingMsgId = _pingIdToSend = _pingSent = 0; + _pingId = _pingMsgId = _pingIdToSend = _pingSendAt = 0; _pingSender.stop(); const mtpDcOption *dcOption = 0; @@ -1840,17 +1842,25 @@ void MTProtoConnectionPrivate::onOldConnection() { void MTProtoConnectionPrivate::onPingSender() { if (_pingId) { - if (_pingSent + (MTPPingSendAfter - 1) * 1000 < getms(true)) { + if (_pingSendAt + (MTPPingSendAfter - MTPPingSendAfterAuto - 1) * 1000ULL < getms(true)) { LOG(("Could not send ping for MTPPingSendAfter seconds, restarting..")); return restart(); } else { - _pingSender.start(_pingSent + (MTPPingSendAfter * 1000) - getms(true)); + _pingSender.start(_pingSendAt + (MTPPingSendAfter - MTPPingSendAfterAuto) * 1000ULL - getms(true)); } } else { emit needToSendAsync(); } } +void MTProtoConnectionPrivate::onPingSendForce() { + if (!_pingId) { + _pingSendAt = 0; + DEBUG_LOG(("Will send ping!")); + tryToSend(); + } +} + void MTProtoConnectionPrivate::onBadConnection() { if (cConnectionType() != dbictAuto && cConnectionType() != dbictTcpProxy) { return; diff --git a/Telegram/SourceFiles/mtproto/mtpConnection.h b/Telegram/SourceFiles/mtproto/mtpConnection.h index 9f0b63341a..ba97afea53 100644 --- a/Telegram/SourceFiles/mtproto/mtpConnection.h +++ b/Telegram/SourceFiles/mtproto/mtpConnection.h @@ -341,6 +341,7 @@ public slots: void restart(bool maybeBadKey = false); void onPingSender(); + void onPingSendForce(); void onBadConnection(); void onOldConnection(); void onSentSome(uint64 size); @@ -416,7 +417,7 @@ private: void requestsAcked(const QVector<MTPlong> &ids, bool byResponse = false); mtpPingId _pingId, _pingIdToSend; - uint64 _pingSent; + uint64 _pingSendAt; mtpMsgId _pingMsgId; SingleTimer _pingSender; diff --git a/Telegram/SourceFiles/mtproto/mtpSession.cpp b/Telegram/SourceFiles/mtproto/mtpSession.cpp index 4d599c9139..d0654dff4d 100644 --- a/Telegram/SourceFiles/mtproto/mtpSession.cpp +++ b/Telegram/SourceFiles/mtproto/mtpSession.cpp @@ -63,7 +63,7 @@ void MTPSessionData::clear() { } -MTProtoSession::MTProtoSession() : data(this), dcId(0), dc(0), msSendCall(0), msWait(0) { +MTProtoSession::MTProtoSession() : data(this), dcId(0), dc(0), msSendCall(0), msWait(0), _ping(false) { } void MTProtoSession::start(int32 dcenter) { @@ -171,7 +171,12 @@ void MTProtoSession::needToResumeAndSend() { } } } - emit needToSend(); + if (_ping) { + _ping = false; + emit needToPing(); + } else { + emit needToSend(); + } } void MTProtoSession::sendPong(quint64 msgId, quint64 pingId) { @@ -272,6 +277,11 @@ void MTProtoSession::cancel(mtpRequestId requestId, mtpMsgId msgId) { } } +void MTProtoSession::ping() { + _ping = true; + sendAnything(0); +} + int32 MTProtoSession::requestState(mtpRequestId requestId) const { MTProtoConnections::const_iterator j = connections.cbegin(), e = connections.cend(); int32 result = MTP::RequestSent; diff --git a/Telegram/SourceFiles/mtproto/mtpSession.h b/Telegram/SourceFiles/mtproto/mtpSession.h index e1b98a3abc..7e0145d714 100644 --- a/Telegram/SourceFiles/mtproto/mtpSession.h +++ b/Telegram/SourceFiles/mtproto/mtpSession.h @@ -236,6 +236,7 @@ public: template <typename TRequest> mtpRequestId send(const TRequest &request, RPCResponseHandler callbacks = RPCResponseHandler(), uint64 msCanWait = 0, bool needsLayer = false, bool toMainDC = false, mtpRequestId after = 0); // send mtp request + void ping(); void cancel(mtpRequestId requestId, mtpMsgId msgId); int32 requestState(mtpRequestId requestId) const; int32 getState() const; @@ -247,6 +248,7 @@ signals: void authKeyCreated(); void needToSend(); + void needToPing(); void needToRestart(); public slots: @@ -281,6 +283,8 @@ private: uint64 msSendCall, msWait; + bool _ping; + QTimer timeouter; SingleTimer sender;