From 90296a2d854501e2de3eb91fd9f14a4072103efb Mon Sep 17 00:00:00 2001 From: pajlada Date: Fri, 3 Jan 2020 20:55:13 +0100 Subject: [PATCH] Expect a PONG response to our PINGs (#1476) If no PONG was received, force a reconnection Fixes #1164 --- src/providers/irc/IrcConnection2.cpp | 29 +++++++++++++++++++++++++++- src/providers/irc/IrcConnection2.hpp | 3 +++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/providers/irc/IrcConnection2.cpp b/src/providers/irc/IrcConnection2.cpp index 0ba639247..08ff718bc 100644 --- a/src/providers/irc/IrcConnection2.cpp +++ b/src/providers/irc/IrcConnection2.cpp @@ -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; diff --git a/src/providers/irc/IrcConnection2.hpp b/src/providers/irc/IrcConnection2.hpp index 70aa9f670..65f1e1ea0 100644 --- a/src/providers/irc/IrcConnection2.hpp +++ b/src/providers/irc/IrcConnection2.hpp @@ -18,6 +18,9 @@ private: QTimer pingTimer_; QTimer reconnectTimer_; std::atomic 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 waitingForPong_{false}; }; } // namespace chatterino