Commit f63684fa authored by LNJ's avatar LNJ Committed by Jonah Brüchert

Automatically reconnect 5s after disconnection

If the reason for disconnection wasn't user disconnection or authentication
failure, then a 5s timer will be started to reconnect.
parent 44b94dc4
......@@ -37,10 +37,26 @@
// Kaidan
#include "ClientThread.h"
// interval in seconds in which a new connection will be tryed
static const unsigned int RECONNECT_INTERVAL = 5000;
ClientWorker::ClientWorker(gloox::Client* client, ClientThread *controller,
QObject* parent) : QObject(parent), client(client), controller(controller)
{
client->registerConnectionListener(this);
// reconnect timer
reconnectTimer.moveToThread(controller);
reconnectTimer.setInterval(RECONNECT_INTERVAL);
reconnectTimer.setSingleShot(true); // only call once
connect(&reconnectTimer, &QTimer::timeout, this, &ClientWorker::xmppConnect);
connect(this, &ClientWorker::stopReconnectTimerRequested,
&reconnectTimer, &QTimer::stop);
}
ClientWorker::~ClientWorker()
{
emit stopReconnectTimerRequested();
}
void ClientWorker::updateClient()
......@@ -107,6 +123,8 @@ void ClientWorker::onDisconnect(gloox::ConnectionError error)
if (error == gloox::ConnAuthenticationFailed)
// if JID/password is wrong, request new credentials from QML
emit controller->newCredentialsNeeded();
else if (error != gloox::ConnUserDisconnected)
reconnect();
}
}
......@@ -122,3 +140,9 @@ void ClientWorker::stopWorkTimer()
{
controller->workTimer.stop();
}
void ClientWorker::reconnect()
{
qDebug().noquote() << QString("[client] Will do reconnect in %1 ms").arg(RECONNECT_INTERVAL);
reconnectTimer.start();
}
......@@ -33,6 +33,7 @@
// Qt
#include <QObject>
#include <QTimer>
// gloox
#include <gloox/connectionlistener.h>
......@@ -60,6 +61,11 @@ public:
ClientWorker(gloox::Client *client, ClientThread *contoller,
QObject *parent = nullptr);
~ClientWorker();
signals:
void stopReconnectTimerRequested();
public slots:
/**
* Main function that will be executed via a QTimer.
......@@ -103,8 +109,14 @@ private:
*/
virtual bool onTLSConnect(const gloox::CertInfo &info);
/**
* Start timer to reconnect after connection failed
*/
void reconnect();
gloox::Client *client;
ClientThread *controller;
QTimer reconnectTimer;
};
#endif // CLIENTWORKER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment