From d9cb8093cbe6318322d3cbfc975fefc173b64afc Mon Sep 17 00:00:00 2001 From: fourtf Date: Mon, 4 Jun 2018 21:37:19 +0200 Subject: [PATCH] added a shitty reconnect implementation --- src/providers/irc/abstractircserver.cpp | 4 ++++ src/providers/irc/ircconnection2.cpp | 27 +++++++++++++++++++++++++ src/providers/irc/ircconnection2.hpp | 10 +++++++++ 3 files changed, 41 insertions(+) diff --git a/src/providers/irc/abstractircserver.cpp b/src/providers/irc/abstractircserver.cpp index f83ec7435..aa4ac746d 100644 --- a/src/providers/irc/abstractircserver.cpp +++ b/src/providers/irc/abstractircserver.cpp @@ -33,6 +33,10 @@ AbstractIrcServer::AbstractIrcServer() [this] { this->onConnected(); }); QObject::connect(this->readConnection.get(), &Communi::IrcConnection::disconnected, [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 diff --git a/src/providers/irc/ircconnection2.cpp b/src/providers/irc/ircconnection2.cpp index c2cb7aa63..5d3aefcb5 100644 --- a/src/providers/irc/ircconnection2.cpp +++ b/src/providers/irc/ircconnection2.cpp @@ -7,6 +7,33 @@ namespace irc { IrcConnection::IrcConnection(QObject *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 diff --git a/src/providers/irc/ircconnection2.hpp b/src/providers/irc/ircconnection2.hpp index 85c9bfe7c..c9a468b75 100644 --- a/src/providers/irc/ircconnection2.hpp +++ b/src/providers/irc/ircconnection2.hpp @@ -1,6 +1,9 @@ #pragma once +#include + #include +#include namespace chatterino { namespace providers { @@ -10,6 +13,13 @@ class IrcConnection : public Communi::IrcConnection { public: IrcConnection(QObject *parent = nullptr); + + pajlada::Signals::NoArgSignal reconnectRequested; + +private: + QTimer pingTimer_; + QTimer reconnectTimer_; + std::atomic recentlyReceivedMessage_{true}; }; } // namespace irc