From cc7d034fa67196ad4950d3785aff64e4c5765855 Mon Sep 17 00:00:00 2001 From: Alexey Andreyev Date: Wed, 30 Jan 2019 19:30:07 +0300 Subject: Connection: infinite sync loop logic by default --- lib/connection.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'lib/connection.cpp') diff --git a/lib/connection.cpp b/lib/connection.cpp index c582cf94..982145f7 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -133,6 +133,8 @@ Connection::Connection(const QUrl& server, QObject* parent) , d(std::make_unique(std::make_unique(server))) { d->q = this; // All d initialization should occur before this line + // sync loop: + connect(this, &Connection::syncDone, this, &Connection::getNewEvents); } Connection::Connection(QObject* parent) @@ -250,7 +252,7 @@ void Connection::Private::connectWithToken(const QString& user, << "by user" << userId << "from device" << deviceId; emit q->stateChanged(); emit q->connected(); - + q->sync(); // initial sync after connection } void Connection::checkAndConnect(const QString& userId, @@ -406,6 +408,15 @@ void Connection::onSyncSuccess(SyncData &&data, bool fromCache) { } } +void Connection::getNewEvents() +{ + // Borrowed the logic from Quiark's code in Tensor + // to cache not too aggressively and not on the first sync. + if (++_saveStateCounter % 17 == 2) + saveState(); + sync(30*1000); +} + void Connection::stopSync() { if (d->syncJob) -- cgit v1.2.3 From 22dd5f1e8988b03a691487cdad164a82a36e7f8c Mon Sep 17 00:00:00 2001 From: Alexey Andreyev Date: Sat, 2 Feb 2019 23:51:20 +0300 Subject: Connection: separated sync loop logic with delay control Signed-off-by: Alexey Andreyev --- lib/connection.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 8 deletions(-) (limited to 'lib/connection.cpp') diff --git a/lib/connection.cpp b/lib/connection.cpp index 982145f7..e7f9e4b2 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -45,6 +45,7 @@ #include #include #include +#include using namespace QMatrixClient; @@ -133,8 +134,6 @@ Connection::Connection(const QUrl& server, QObject* parent) , d(std::make_unique(std::make_unique(server))) { d->q = this; // All d initialization should occur before this line - // sync loop: - connect(this, &Connection::syncDone, this, &Connection::getNewEvents); } Connection::Connection(QObject* parent) @@ -232,6 +231,11 @@ void Connection::doConnectToServer(const QString& user, const QString& password, }); } +void Connection::syncLoopIteration() +{ + sync(_syncLoopTimeout); +} + void Connection::connectWithToken(const QString& userId, const QString& accessToken, const QString& deviceId) @@ -252,7 +256,6 @@ void Connection::Private::connectWithToken(const QString& user, << "by user" << userId << "from device" << deviceId; emit q->stateChanged(); emit q->connected(); - q->sync(); // initial sync after connection } void Connection::checkAndConnect(const QString& userId, @@ -321,6 +324,15 @@ void Connection::sync(int timeout) }); } +void Connection::syncLoop(int timeout) +{ + _syncLoopTimeout = timeout; + connect(this, &Connection::syncDone, this, &Connection::getNewEventsOnSyncDone); + connect(this, &Connection::syncError, this, &Connection::getNewEventsOnSyncError); + _syncLoopElapsedTimer.start(); + sync(_syncLoopTimeout); // initial sync to start the loop +} + void Connection::onSyncSuccess(SyncData &&data, bool fromCache) { d->data->setLastEvent(data.nextBatch()); for (auto&& roomData: data.takeRoomData()) @@ -410,11 +422,33 @@ void Connection::onSyncSuccess(SyncData &&data, bool fromCache) { void Connection::getNewEvents() { - // Borrowed the logic from Quiark's code in Tensor - // to cache not too aggressively and not on the first sync. - if (++_saveStateCounter % 17 == 2) - saveState(); - sync(30*1000); + int delay = minSyncLoopDelayMs() - _syncLoopElapsedTimer.restart(); + if (delay<0) { + delay = 0; + } + QTimer::singleShot(delay, this, &Connection::syncLoopIteration); +} + +void Connection::getNewEventsOnSyncDone() +{ + if (_prevSyncLoopIterationDone) { + _syncLoopAttemptNumber++; + } else { + _syncLoopAttemptNumber = 0; + } + emit syncAttemptNumberChanged(_syncLoopAttemptNumber); + getNewEvents(); +} + +void Connection::getNewEventsOnSyncError() +{ + if (_prevSyncLoopIterationDone) { + _syncLoopAttemptNumber = 0; + } else { + _syncLoopAttemptNumber++; + } + emit syncAttemptNumberChanged(_syncLoopAttemptNumber); + getNewEvents(); } void Connection::stopSync() @@ -436,6 +470,15 @@ PostReceiptJob* Connection::postReceipt(Room* room, RoomEvent* event) const return callApi(room->id(), "m.read", event->id()); } +void Connection::setMinSyncDelayMs(qint64 minSyncDelayMs) +{ + if (_minSyncLoopDelayMs == minSyncDelayMs) + return; + + _minSyncLoopDelayMs = minSyncDelayMs; + emit minSyncDelayMsChanged(_minSyncLoopDelayMs); +} + JoinRoomJob* Connection::joinRoom(const QString& roomAlias, const QStringList& serverNames) { @@ -1100,6 +1143,16 @@ user_factory_t Connection::userFactory() return _userFactory; } +qint64 Connection::minSyncLoopDelayMs() const +{ + return _minSyncLoopDelayMs; +} + +uint Connection::syncLoopAttemptNumber() const +{ + return _syncLoopAttemptNumber; +} + room_factory_t Connection::_roomFactory = defaultRoomFactory<>(); user_factory_t Connection::_userFactory = defaultUserFactory<>(); -- cgit v1.2.3 From bf6cd3d29052f9e79fee29c6a6646d95271a196a Mon Sep 17 00:00:00 2001 From: Alexey Andreyev Date: Mon, 4 Feb 2019 11:58:43 +0300 Subject: Connection: simplified sync loop logic without delays Signed-off-by: Alexey Andreyev --- lib/connection.cpp | 58 +++--------------------------------------------------- 1 file changed, 3 insertions(+), 55 deletions(-) (limited to 'lib/connection.cpp') diff --git a/lib/connection.cpp b/lib/connection.cpp index e7f9e4b2..a9a8bba3 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -45,7 +45,6 @@ #include #include #include -#include using namespace QMatrixClient; @@ -256,6 +255,7 @@ void Connection::Private::connectWithToken(const QString& user, << "by user" << userId << "from device" << deviceId; emit q->stateChanged(); emit q->connected(); + } void Connection::checkAndConnect(const QString& userId, @@ -327,10 +327,8 @@ void Connection::sync(int timeout) void Connection::syncLoop(int timeout) { _syncLoopTimeout = timeout; - connect(this, &Connection::syncDone, this, &Connection::getNewEventsOnSyncDone); - connect(this, &Connection::syncError, this, &Connection::getNewEventsOnSyncError); - _syncLoopElapsedTimer.start(); - sync(_syncLoopTimeout); // initial sync to start the loop + connect(this, &Connection::syncDone, this, &Connection::syncLoopIteration); + syncLoopIteration(); // initial sync to start the loop } void Connection::onSyncSuccess(SyncData &&data, bool fromCache) { @@ -420,37 +418,6 @@ void Connection::onSyncSuccess(SyncData &&data, bool fromCache) { } } -void Connection::getNewEvents() -{ - int delay = minSyncLoopDelayMs() - _syncLoopElapsedTimer.restart(); - if (delay<0) { - delay = 0; - } - QTimer::singleShot(delay, this, &Connection::syncLoopIteration); -} - -void Connection::getNewEventsOnSyncDone() -{ - if (_prevSyncLoopIterationDone) { - _syncLoopAttemptNumber++; - } else { - _syncLoopAttemptNumber = 0; - } - emit syncAttemptNumberChanged(_syncLoopAttemptNumber); - getNewEvents(); -} - -void Connection::getNewEventsOnSyncError() -{ - if (_prevSyncLoopIterationDone) { - _syncLoopAttemptNumber = 0; - } else { - _syncLoopAttemptNumber++; - } - emit syncAttemptNumberChanged(_syncLoopAttemptNumber); - getNewEvents(); -} - void Connection::stopSync() { if (d->syncJob) @@ -470,15 +437,6 @@ PostReceiptJob* Connection::postReceipt(Room* room, RoomEvent* event) const return callApi(room->id(), "m.read", event->id()); } -void Connection::setMinSyncDelayMs(qint64 minSyncDelayMs) -{ - if (_minSyncLoopDelayMs == minSyncDelayMs) - return; - - _minSyncLoopDelayMs = minSyncDelayMs; - emit minSyncDelayMsChanged(_minSyncLoopDelayMs); -} - JoinRoomJob* Connection::joinRoom(const QString& roomAlias, const QStringList& serverNames) { @@ -1143,16 +1101,6 @@ user_factory_t Connection::userFactory() return _userFactory; } -qint64 Connection::minSyncLoopDelayMs() const -{ - return _minSyncLoopDelayMs; -} - -uint Connection::syncLoopAttemptNumber() const -{ - return _syncLoopAttemptNumber; -} - room_factory_t Connection::_roomFactory = defaultRoomFactory<>(); user_factory_t Connection::_userFactory = defaultUserFactory<>(); -- cgit v1.2.3 From 73c836239bfa35713ad76d5e205ce2f2dceffffd Mon Sep 17 00:00:00 2001 From: Alexey Andreyev Date: Sun, 10 Feb 2019 15:08:08 +0300 Subject: Connection: move syncLoopTimeout to Connection::Private Signed-off-by: Alexey Andreyev --- lib/connection.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/connection.cpp') diff --git a/lib/connection.cpp b/lib/connection.cpp index a9a8bba3..63b0a31d 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -90,6 +90,7 @@ class Connection::Private DirectChatUsersMap directChatUsers; std::unordered_map accountData; QString userId; + int syncLoopTimeout = -1; SyncJob* syncJob = nullptr; @@ -232,7 +233,7 @@ void Connection::doConnectToServer(const QString& user, const QString& password, void Connection::syncLoopIteration() { - sync(_syncLoopTimeout); + sync(d->syncLoopTimeout); } void Connection::connectWithToken(const QString& userId, @@ -326,7 +327,7 @@ void Connection::sync(int timeout) void Connection::syncLoop(int timeout) { - _syncLoopTimeout = timeout; + d->syncLoopTimeout = timeout; connect(this, &Connection::syncDone, this, &Connection::syncLoopIteration); syncLoopIteration(); // initial sync to start the loop } -- cgit v1.2.3