UI: Try to reconnect to service by timer

This commit is contained in:
Nodir Temirkhodjaev 2023-02-05 13:05:16 +03:00
parent 8d888159d6
commit 8ee654fc41
2 changed files with 53 additions and 7 deletions

View File

@ -3,6 +3,7 @@
#include <QDataStream> #include <QDataStream>
#include <QLocalSocket> #include <QLocalSocket>
#include <QLoggingCategory> #include <QLoggingCategory>
#include <QTimer>
namespace { namespace {
@ -86,6 +87,7 @@ ControlWorker::ControlWorker(QLocalSocket *socket, QObject *parent) :
m_isServiceClient(false), m_isServiceClient(false),
m_isClientValidated(false), m_isClientValidated(false),
m_isTryReconnect(false), m_isTryReconnect(false),
m_isReconnecting(false),
m_id(nextWorkerId()), m_id(nextWorkerId()),
m_socket(socket) m_socket(socket)
{ {
@ -110,7 +112,7 @@ void ControlWorker::setupForAsync()
qCWarning(LC) << "Client error:" << id() << socketError << errorString(); qCWarning(LC) << "Client error:" << id() << socketError << errorString();
close(); close();
}); });
connect(socket(), &QLocalSocket::connected, this, &ControlWorker::connected); connect(socket(), &QLocalSocket::connected, this, &ControlWorker::onConnected);
connect(socket(), &QLocalSocket::disconnected, this, &ControlWorker::onDisconnected); connect(socket(), &QLocalSocket::disconnected, this, &ControlWorker::onDisconnected);
connect(socket(), &QLocalSocket::readyRead, this, &ControlWorker::processRequest); connect(socket(), &QLocalSocket::readyRead, this, &ControlWorker::processRequest);
} }
@ -134,18 +136,23 @@ bool ControlWorker::connectToServer()
bool ControlWorker::reconnectToServer() bool ControlWorker::reconnectToServer()
{ {
if (m_isReconnecting)
return false;
m_isReconnecting = true;
close(); close();
if (isTryReconnect() && isConnected()) bool connectedToServer;
return true;
int reconnectCount = 2; int reconnectCount = 2;
do { do {
if (connectToServer()) connectedToServer = connectToServer();
return true; } while (!connectedToServer && --reconnectCount > 0);
} while (--reconnectCount > 0);
return false; m_isReconnecting = false;
return connectedToServer;
} }
void ControlWorker::close() void ControlWorker::close()
@ -154,12 +161,43 @@ void ControlWorker::close()
socket()->close(); socket()->close();
} }
void ControlWorker::onConnected()
{
stopReconnectTimer();
emit connected();
}
void ControlWorker::onDisconnected() void ControlWorker::onDisconnected()
{ {
if (isTryReconnect() && reconnectToServer()) if (isTryReconnect() && reconnectToServer())
return; return;
emit disconnected(); emit disconnected();
startReconnectTimer();
}
void ControlWorker::startReconnectTimer()
{
if (!isTryReconnect())
return;
if (!m_reconnectTimer) {
m_reconnectTimer = new QTimer(this);
m_reconnectTimer->setInterval(1000);
connect(m_reconnectTimer, &QTimer::timeout, this, &ControlWorker::reconnectToServer);
}
m_reconnectTimer->start();
}
void ControlWorker::stopReconnectTimer()
{
if (m_reconnectTimer) {
m_reconnectTimer->stop();
}
} }
QByteArray ControlWorker::buildCommandData(Control::Command command, const QVariantList &args) QByteArray ControlWorker::buildCommandData(Control::Command command, const QVariantList &args)

View File

@ -7,6 +7,7 @@
#include "control.h" #include "control.h"
QT_FORWARD_DECLARE_CLASS(QLocalSocket) QT_FORWARD_DECLARE_CLASS(QLocalSocket)
QT_FORWARD_DECLARE_CLASS(QTimer)
class ControlWorker : public QObject class ControlWorker : public QObject
{ {
@ -58,7 +59,12 @@ public slots:
void close(); void close();
private slots: private slots:
void onConnected();
void onDisconnected(); void onDisconnected();
void startReconnectTimer();
void stopReconnectTimer();
void processRequest(); void processRequest();
private: private:
@ -97,6 +103,7 @@ private:
bool m_isServiceClient : 1; bool m_isServiceClient : 1;
bool m_isClientValidated : 1; bool m_isClientValidated : 1;
bool m_isTryReconnect : 1; bool m_isTryReconnect : 1;
bool m_isReconnecting : 1;
const quint32 m_id = 0; const quint32 m_id = 0;
@ -105,6 +112,7 @@ private:
QString m_serverName; QString m_serverName;
QLocalSocket *m_socket = nullptr; QLocalSocket *m_socket = nullptr;
QTimer *m_reconnectTimer = nullptr;
}; };
#endif // CONTROLWORKER_H #endif // CONTROLWORKER_H