1 parent
6e789fd83f
commit
2aa44fae9e
2 files changed
+58
-31
No files matched your search
+54
-30
@@ -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<qint64>(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<qint64>(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;
|
||||
|
||||
+4
-1
@@ -3,6 +3,7 @@
|
||||
#include "storage/SettingsManager.hpp"
|
||||
#include <QJsonObject>
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
#include <QTimer>
|
||||
#include <QWebSocket>
|
||||
#include <functional>
|
||||
@@ -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<QWebSocket> socket_ = nullptr;
|
||||
QString currentHost_;
|
||||
int currentPort_ = 8765;
|
||||
QString currentNickname_;
|
||||
|
||||
Reference in new issue
Block a user