From 2aa44fae9e45d041ca70a078913a0753324a25f5 Mon Sep 17 00:00:00 2001 From: faumaray Date: Sat, 11 Jul 2026 00:45:47 +0300 Subject: [PATCH] making WEB Signed-off-by: faumaray --- src/CoreClient.cpp | 84 +++++++++++++++++++++++++++++----------------- src/CoreClient.hpp | 5 ++- 2 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/CoreClient.cpp b/src/CoreClient.cpp index be020db..4aafcc0 100644 --- a/src/CoreClient.cpp +++ b/src/CoreClient.cpp @@ -105,14 +105,16 @@ namespace Echoion::Desktop stopReconnectTimer(); cancelPendingRequests(); - if (socket_->state() != QAbstractSocket::UnconnectedState) { + if (socket_ && + socket_->state() != QAbstractSocket::UnconnectedState) { socket_->close(); } } bool CoreClient::isConnected() const { - return socket_->state() == QAbstractSocket::ConnectedState; + return socket_ && + socket_->state() == QAbstractSocket::ConnectedState; } void CoreClient::requestAuthToken(const QString& nickname) @@ -304,45 +306,67 @@ namespace Echoion::Desktop pending.retryCount++; - // Retry after delay - QTimer::singleShot( - RETRY_DELAY_MS * pending.retryCount, this, [this, requestId]() { - auto it = pendingRequests_.find(requestId); - if (it == pendingRequests_.end()) { - return; - } + // Owned, cancellable retry timer. A fire-and-forget QTimer::singleShot + // would still fire after a disconnect and resend on a dead/reconnected + // socket; cancelPendingRequests() stops these on teardown. + auto* timer = new QTimer(this); + timer->setSingleShot(true); + pending.retryTimer = timer; + const int delay = RETRY_DELAY_MS * pending.retryCount; + connect(timer, &QTimer::timeout, this, [this, requestId, timer]() { + timer->deleteLater(); - auto& pending = it->second; + auto it = pendingRequests_.find(requestId); + if (it == pendingRequests_.end()) { + return; + } + it->second.retryTimer = nullptr; // spent; don't touch in cancel - quint64 newRequestId = nextRequestId_++; + // The socket may have dropped between scheduling and firing. Never + // resend on a closed or reconnecting socket. + if (!socket_ || + socket_->state() != QAbstractSocket::ConnectedState) { + return; + } - QJsonObject request; - request["id"] = static_cast(newRequestId); - request["method"] = pending.method; - // Same wire format as sendRequest: the server's - // RpcRequest.params is a JSON-encoded STRING; a raw object - // here made every retried request fail to decode. - request["params"] = QString::fromUtf8( - QJsonDocument(pending.params) - .toJson(QJsonDocument::Compact)); + auto& pending = it->second; - if (!authToken_.isEmpty()) { - request["authToken"] = authToken_; - } + quint64 newRequestId = nextRequestId_++; - // Move callback to new request ID - PendingRequest newPending = std::move(pending); - pendingRequests_.erase(it); - pendingRequests_[newRequestId] = std::move(newPending); + QJsonObject request; + request["id"] = static_cast(newRequestId); + request["method"] = pending.method; + // Same wire format as sendRequest: the server's + // RpcRequest.params is a JSON-encoded STRING; a raw object + // here made every retried request fail to decode. + request["params"] = QString::fromUtf8( + QJsonDocument(pending.params).toJson(QJsonDocument::Compact)); - QJsonDocument doc(request); - socket_->sendTextMessage(doc.toJson(QJsonDocument::Compact)); - }); + if (!authToken_.isEmpty()) { + request["authToken"] = authToken_; + } + + // Move callback to new request ID + PendingRequest newPending = std::move(pending); + pendingRequests_.erase(it); + pendingRequests_[newRequestId] = std::move(newPending); + + QJsonDocument doc(request); + socket_->sendTextMessage(doc.toJson(QJsonDocument::Compact)); + }); + timer->start(delay); } void CoreClient::cancelPendingRequests() { for (auto& [id, pending] : pendingRequests_) { + // Stop any scheduled retry so it can't fire after the connection + // is gone and resend on a stale socket. + if (pending.retryTimer) { + pending.retryTimer->stop(); + pending.retryTimer->deleteLater(); + pending.retryTimer = nullptr; + } if (pending.callback) { QJsonObject error; error["success"] = false; diff --git a/src/CoreClient.hpp b/src/CoreClient.hpp index d6c57e9..1ed945f 100644 --- a/src/CoreClient.hpp +++ b/src/CoreClient.hpp @@ -3,6 +3,7 @@ #include "storage/SettingsManager.hpp" #include #include +#include #include #include #include @@ -153,7 +154,9 @@ namespace Echoion::Desktop [[nodiscard]] int methodToInt(const QString& method) const; SettingsManager settingsManager_; - QWebSocket* socket_ = nullptr; + // QPointer so a stray access after teardown is null-checked rather + // than dangling. Qt parent-ownership still frees it. + QPointer socket_ = nullptr; QString currentHost_; int currentPort_ = 8765; QString currentNickname_;