Fix write connection reconnection issues (#2850)

Co-authored-by: Felanbird <41973452+Felanbird@users.noreply.github.com>
Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
Ben de Graaff
2021-06-06 16:25:13 +02:00
committed by GitHub
parent 0b4c521c9b
commit 8639f450f2
5 changed files with 127 additions and 64 deletions
+15 -32
View File
@@ -18,7 +18,7 @@ const int MAX_FALLOFF_COUNTER = 60;
AbstractIrcServer::AbstractIrcServer()
{
// Initialize the connections
// XXX: don't create write connection if there is not separate write connection.
// XXX: don't create write connection if there is no separate write connection.
this->writeConnection_.reset(new IrcConnection);
this->writeConnection_->moveToThread(
QCoreApplication::instance()->thread());
@@ -32,6 +32,11 @@ AbstractIrcServer::AbstractIrcServer()
&Communi::IrcConnection::connected, this, [this] {
this->onWriteConnected(this->writeConnection_.get());
});
this->writeConnection_->connectionLost.connect([this](bool timeout) {
qCDebug(chatterinoIrc)
<< "Write connection reconnect requested. Timeout:" << timeout;
this->writeConnection_->smartReconnect.invoke();
});
// Listen to read connection message signals
this->readConnection_.reset(new IrcConnection);
@@ -55,34 +60,17 @@ AbstractIrcServer::AbstractIrcServer()
&Communi::IrcConnection::disconnected, this, [this] {
this->onDisconnected();
});
QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::socketError, this, [this] {
this->onSocketError();
});
// listen to reconnect request
this->readConnection_->reconnectRequested.connect([this] {
this->addGlobalSystemMessage(
"Server connection timed out, reconnecting");
this->connect();
});
// this->writeConnection->reconnectRequested.connect([this] {
// this->connect(); });
this->reconnectTimer_.setInterval(RECONNECT_BASE_INTERVAL);
this->reconnectTimer_.setSingleShot(true);
QObject::connect(&this->reconnectTimer_, &QTimer::timeout, [this] {
this->reconnectTimer_.setInterval(RECONNECT_BASE_INTERVAL *
this->falloffCounter_);
this->falloffCounter_ =
std::min(MAX_FALLOFF_COUNTER, this->falloffCounter_ + 1);
if (!this->readConnection_->isConnected())
this->readConnection_->connectionLost.connect([this](bool timeout) {
qCDebug(chatterinoIrc)
<< "Read connection reconnect requested. Timeout:" << timeout;
if (timeout)
{
qCDebug(chatterinoIrc)
<< "Trying to reconnect..." << this->falloffCounter_;
this->connect();
// Show additional message since this is going to interrupt a
// connection that is still "connected"
this->addGlobalSystemMessage(
"Server connection timed out, reconnecting");
}
this->readConnection_->smartReconnect.invoke();
});
}
@@ -370,11 +358,6 @@ void AbstractIrcServer::onDisconnected()
}
}
void AbstractIrcServer::onSocketError()
{
this->reconnectTimer_.start();
}
std::shared_ptr<Channel> AbstractIrcServer::getCustomChannel(
const QString &channelName)
{