added a shitty reconnect implementation

This commit is contained in:
fourtf 2018-06-04 21:37:19 +02:00
parent 8de0a59533
commit d9cb8093cb
3 changed files with 41 additions and 0 deletions

View file

@ -33,6 +33,10 @@ AbstractIrcServer::AbstractIrcServer()
[this] { this->onConnected(); }); [this] { this->onConnected(); });
QObject::connect(this->readConnection.get(), &Communi::IrcConnection::disconnected, QObject::connect(this->readConnection.get(), &Communi::IrcConnection::disconnected,
[this] { this->onDisconnected(); }); [this] { this->onDisconnected(); });
// listen to reconnect request
this->readConnection->reconnectRequested.connect([this] { this->connect(); });
// this->writeConnection->reconnectRequested.connect([this] { this->connect(); });
} }
IrcConnection *AbstractIrcServer::getReadConnection() const IrcConnection *AbstractIrcServer::getReadConnection() const

View file

@ -7,6 +7,33 @@ namespace irc {
IrcConnection::IrcConnection(QObject *parent) IrcConnection::IrcConnection(QObject *parent)
: Communi::IrcConnection(parent) : Communi::IrcConnection(parent)
{ {
this->pingTimer_.setInterval(10000);
this->pingTimer_.start();
QObject::connect(&this->pingTimer_, &QTimer::timeout, [this] {
if (!this->recentlyReceivedMessage_.load()) {
this->sendRaw("PING");
this->reconnectTimer_.start();
}
this->recentlyReceivedMessage_ = false;
});
this->reconnectTimer_.setInterval(10000);
this->reconnectTimer_.setSingleShot(true);
QObject::connect(&this->reconnectTimer_, &QTimer::timeout,
[this] { reconnectRequested.invoke(); });
QObject::connect(this, &Communi::IrcConnection::messageReceived,
[this](Communi::IrcMessage *message) {
if (message->command() == "PONG") {
qDebug() << "PONG";
}
this->recentlyReceivedMessage_ = true;
if (this->reconnectTimer_.isActive()) {
this->reconnectTimer_.stop();
qDebug() << "reconnect stopped";
}
});
} }
} // namespace irc } // namespace irc

View file

@ -1,6 +1,9 @@
#pragma once #pragma once
#include <pajlada/signals/signal.hpp>
#include <IrcConnection> #include <IrcConnection>
#include <QTimer>
namespace chatterino { namespace chatterino {
namespace providers { namespace providers {
@ -10,6 +13,13 @@ class IrcConnection : public Communi::IrcConnection
{ {
public: public:
IrcConnection(QObject *parent = nullptr); IrcConnection(QObject *parent = nullptr);
pajlada::Signals::NoArgSignal reconnectRequested;
private:
QTimer pingTimer_;
QTimer reconnectTimer_;
std::atomic<bool> recentlyReceivedMessage_{true};
}; };
} // namespace irc } // namespace irc