From 49b55768f7a3a02acaad4a837790139a2c5f26b8 Mon Sep 17 00:00:00 2001 From: dsc Date: Fri, 30 Apr 2021 22:20:04 +0200 Subject: [PATCH] Detect wakeup from hibernate, re-establish websocket connection --- src/appcontext.cpp | 18 ++++++++++++++++++ src/appcontext.h | 3 +++ 2 files changed, 21 insertions(+) diff --git a/src/appcontext.cpp b/src/appcontext.cpp index d1e6ce5..415c29f 100644 --- a/src/appcontext.cpp +++ b/src/appcontext.cpp @@ -111,6 +111,24 @@ AppContext::AppContext(QCommandLineParser *cmdargs) { this->storeWallet(); }); + // If system clock skewed for >= 300 seconds, assume a wake-up from hibernate and reload the websocket connection + if(!this->isAndroid) + m_hibernateTimer.start(3 * 1000); + + m_hibernatePreviousTime = std::chrono::steady_clock::now(); + connect(&m_hibernateTimer, &QTimer::timeout, [this]() { + const auto now = std::chrono::steady_clock::now(); + const auto elapsed = now - m_hibernatePreviousTime; + + if(elapsed >= m_hibernateDetectInterval) { + qCritical() << "Clock skew detected, resetting websocket connection"; + this->ws->webSocket.abort(); + this->ws->start(); + } + + m_hibernatePreviousTime = now; + }); + // restore height lookup this->initRestoreHeights(); diff --git a/src/appcontext.h b/src/appcontext.h index 9aec9b6..d3b84a5 100644 --- a/src/appcontext.h +++ b/src/appcontext.h @@ -221,6 +221,9 @@ private: WalletKeysFilesModel *m_walletKeysFilesModel; const int m_donationBoundary = 15; QTimer m_storeTimer; + QTimer m_hibernateTimer; + std::chrono::seconds m_hibernateDetectInterval{300}; + std::chrono::time_point m_hibernatePreviousTime; void setupPathsUnix(); void setupPathsWindows();