2019-09-08 22:27:57 +02:00
|
|
|
#include "IrcConnection2.hpp"
|
|
|
|
|
2021-06-06 16:25:13 +02:00
|
|
|
#include "common/QLogging.hpp"
|
2020-01-03 20:55:13 +01:00
|
|
|
#include "common/Version.hpp"
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
namespace chatterino {
|
|
|
|
|
2021-06-06 16:25:13 +02:00
|
|
|
// The minimum interval between attempting to establish a new connection
|
|
|
|
const int RECONNECT_MIN_INTERVAL = 15000;
|
|
|
|
|
2020-01-03 20:55:13 +01:00
|
|
|
namespace {
|
|
|
|
|
2020-01-25 14:33:38 +01:00
|
|
|
const auto payload = QString("chatterino/" CHATTERINO_VERSION);
|
2020-01-03 20:55:13 +01:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
IrcConnection::IrcConnection(QObject *parent)
|
|
|
|
: Communi::IrcConnection(parent)
|
|
|
|
{
|
2021-06-06 16:25:13 +02:00
|
|
|
// Log connection errors for ease-of-debugging
|
|
|
|
QObject::connect(this, &Communi::IrcConnection::socketError, this,
|
|
|
|
[this](QAbstractSocket::SocketError error) {
|
|
|
|
qCDebug(chatterinoIrc) << "Connection error:" << error;
|
|
|
|
});
|
|
|
|
|
|
|
|
QObject::connect(
|
|
|
|
this, &Communi::IrcConnection::socketStateChanged, this,
|
|
|
|
[this](QAbstractSocket::SocketState state) {
|
|
|
|
if (state == QAbstractSocket::UnconnectedState)
|
|
|
|
{
|
|
|
|
this->pingTimer_.stop();
|
|
|
|
|
|
|
|
// The socket will enter unconnected state both in case of
|
|
|
|
// socket error (including failures to connect) and regular
|
|
|
|
// disconnects. We signal that the connection was lost if this
|
|
|
|
// was not the result of us calling `close`.
|
|
|
|
if (!this->expectConnectionLoss_.load())
|
|
|
|
{
|
|
|
|
this->connectionLost.invoke(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Schedule a reconnect that won't violate RECONNECT_MIN_INTERVAL
|
|
|
|
this->smartReconnect.connect([this] {
|
|
|
|
if (this->reconnectTimer_.isActive())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto delta =
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::steady_clock::now() - this->lastConnected_)
|
|
|
|
.count();
|
|
|
|
delta = delta < RECONNECT_MIN_INTERVAL
|
|
|
|
? (RECONNECT_MIN_INTERVAL - delta)
|
|
|
|
: 10;
|
|
|
|
qCDebug(chatterinoIrc) << "Reconnecting in" << delta << "ms";
|
|
|
|
this->reconnectTimer_.start(delta);
|
|
|
|
});
|
|
|
|
|
|
|
|
this->reconnectTimer_.setInterval(RECONNECT_MIN_INTERVAL);
|
|
|
|
this->reconnectTimer_.setSingleShot(true);
|
|
|
|
QObject::connect(&this->reconnectTimer_, &QTimer::timeout, [this] {
|
|
|
|
if (this->isConnected())
|
|
|
|
{
|
|
|
|
// E.g. user manually reconnecting doesn't cancel this path, so
|
|
|
|
// just ignore
|
|
|
|
qCDebug(chatterinoIrc) << "Reconnect: already reconnected";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qCDebug(chatterinoIrc) << "Reconnecting";
|
|
|
|
this->open();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Send ping every x seconds
|
2019-09-08 22:27:57 +02:00
|
|
|
this->pingTimer_.setInterval(5000);
|
|
|
|
this->pingTimer_.start();
|
|
|
|
QObject::connect(&this->pingTimer_, &QTimer::timeout, [this] {
|
|
|
|
if (this->isConnected())
|
|
|
|
{
|
2021-06-06 16:25:13 +02:00
|
|
|
if (this->recentlyReceivedMessage_.load())
|
2020-01-03 20:55:13 +01:00
|
|
|
{
|
2021-06-06 16:25:13 +02:00
|
|
|
// If we're still receiving messages, all is well
|
|
|
|
this->recentlyReceivedMessage_ = false;
|
|
|
|
this->waitingForPong_ = false;
|
2020-01-03 20:55:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-06 16:25:13 +02:00
|
|
|
if (this->waitingForPong_.load())
|
|
|
|
{
|
|
|
|
// The remote server did not send a PONG fast enough; close the
|
|
|
|
// connection
|
|
|
|
this->close();
|
|
|
|
this->connectionLost.invoke(true);
|
|
|
|
}
|
|
|
|
else
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
2020-01-03 20:55:13 +01:00
|
|
|
this->sendRaw("PING " + payload);
|
|
|
|
this->waitingForPong_ = true;
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-08 12:02:19 +01:00
|
|
|
QObject::connect(this, &Communi::IrcConnection::connected, this, [this] {
|
2021-06-06 16:25:13 +02:00
|
|
|
this->pingTimer_.start();
|
2020-11-08 12:02:19 +01:00
|
|
|
});
|
2020-01-03 20:55:13 +01:00
|
|
|
|
|
|
|
QObject::connect(this, &Communi::IrcConnection::pongMessageReceived,
|
|
|
|
[this](Communi::IrcPongMessage *message) {
|
|
|
|
if (message->argument() == payload)
|
|
|
|
{
|
|
|
|
this->waitingForPong_ = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-06 16:25:13 +02:00
|
|
|
QObject::connect(this, &Communi::IrcConnection::messageReceived,
|
|
|
|
[this](Communi::IrcMessage *message) {
|
|
|
|
// This connection is probably still alive
|
|
|
|
this->recentlyReceivedMessage_ = true;
|
|
|
|
});
|
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2021-06-06 16:25:13 +02:00
|
|
|
void IrcConnection::open()
|
|
|
|
{
|
|
|
|
// Accurately track the time a connection was opened
|
|
|
|
this->lastConnected_ = std::chrono::steady_clock::now();
|
|
|
|
this->expectConnectionLoss_ = false;
|
|
|
|
this->waitingForPong_ = false;
|
|
|
|
this->recentlyReceivedMessage_ = false;
|
|
|
|
Communi::IrcConnection::open();
|
|
|
|
}
|
2021-01-09 18:05:02 +01:00
|
|
|
|
2021-06-06 16:25:13 +02:00
|
|
|
void IrcConnection::close()
|
|
|
|
{
|
|
|
|
this->expectConnectionLoss_ = true;
|
|
|
|
Communi::IrcConnection::close();
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|