Expect a PONG response to our PINGs (#1476)

If no PONG was received, force a reconnection

Fixes #1164
This commit is contained in:
pajlada 2020-01-03 20:55:13 +01:00 committed by GitHub
parent 3c8992cac1
commit 90296a2d85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View file

@ -1,7 +1,16 @@
#include "IrcConnection2.hpp"
#include "common/Version.hpp"
namespace chatterino {
namespace {
const auto payload =
QString("chatterino/%1").arg(Version::instance().version());
} // namespace
IrcConnection::IrcConnection(QObject *parent)
: Communi::IrcConnection(parent)
{
@ -11,10 +20,17 @@ IrcConnection::IrcConnection(QObject *parent)
QObject::connect(&this->pingTimer_, &QTimer::timeout, [this] {
if (this->isConnected())
{
if (this->waitingForPong_.load())
{
// Not sending another ping as we haven't received the matching pong yet
return;
}
if (!this->recentlyReceivedMessage_.load())
{
this->sendRaw("PING chatterino/ping");
this->sendRaw("PING " + payload);
this->reconnectTimer_.start();
this->waitingForPong_ = true;
}
this->recentlyReceivedMessage_ = false;
}
@ -30,6 +46,17 @@ IrcConnection::IrcConnection(QObject *parent)
}
});
QObject::connect(this, &Communi::IrcConnection::connected, this,
[this] { this->waitingForPong_ = false; });
QObject::connect(this, &Communi::IrcConnection::pongMessageReceived,
[this](Communi::IrcPongMessage *message) {
if (message->argument() == payload)
{
this->waitingForPong_ = false;
}
});
QObject::connect(this, &Communi::IrcConnection::messageReceived,
[this](Communi::IrcMessage *) {
this->recentlyReceivedMessage_ = true;

View file

@ -18,6 +18,9 @@ private:
QTimer pingTimer_;
QTimer reconnectTimer_;
std::atomic<bool> recentlyReceivedMessage_{true};
// waitingForPong_ is set to true when we send a PING message, and back to false when we receive the matching PONG response
std::atomic<bool> waitingForPong_{false};
};
} // namespace chatterino