mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Expect a PONG response to our PINGs (#1476)
If no PONG was received, force a reconnection Fixes #1164
This commit is contained in:
parent
3c8992cac1
commit
90296a2d85
2 changed files with 31 additions and 1 deletions
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue