Experimental reconnect support

This commit is contained in:
Rasmus Karlsson 2018-10-13 15:45:13 +02:00
parent c38bb05bb4
commit 230b2006dc
2 changed files with 32 additions and 0 deletions

View file

@ -11,6 +11,10 @@
namespace chatterino { namespace chatterino {
const int RECONNECT_BASE_INTERVAL = 2000;
// 60 falloff counter means it will try to reconnect at most every 60*2 seconds
const int MAX_FALLOFF_COUNTER = 60;
AbstractIrcServer::AbstractIrcServer() AbstractIrcServer::AbstractIrcServer()
{ {
// Initialize the connections // Initialize the connections
@ -38,12 +42,29 @@ AbstractIrcServer::AbstractIrcServer()
QObject::connect(this->readConnection_.get(), QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::disconnected, &Communi::IrcConnection::disconnected,
[this] { this->onDisconnected(); }); [this] { this->onDisconnected(); });
QObject::connect(this->readConnection_.get(),
&Communi::IrcConnection::socketError,
[this] { this->onSocketError(); });
// listen to reconnect request // listen to reconnect request
this->readConnection_->reconnectRequested.connect( this->readConnection_->reconnectRequested.connect(
[this] { this->connect(); }); [this] { this->connect(); });
// this->writeConnection->reconnectRequested.connect([this] { // this->writeConnection->reconnectRequested.connect([this] {
// this->connect(); }); // 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()) {
log("Trying to reconnect... {}", this->falloffCounter_);
this->connect();
}
});
} }
void AbstractIrcServer::connect() void AbstractIrcServer::connect()
@ -215,6 +236,8 @@ void AbstractIrcServer::onConnected()
chan->addMessage(connected); chan->addMessage(connected);
} }
this->falloffCounter_ = 1;
} }
void AbstractIrcServer::onDisconnected() void AbstractIrcServer::onDisconnected()
@ -235,6 +258,11 @@ void AbstractIrcServer::onDisconnected()
} }
} }
void AbstractIrcServer::onSocketError()
{
this->reconnectTimer_.start();
}
std::shared_ptr<Channel> AbstractIrcServer::getCustomChannel( std::shared_ptr<Channel> AbstractIrcServer::getCustomChannel(
const QString &channelName) const QString &channelName)
{ {

View file

@ -54,6 +54,7 @@ protected:
virtual void onConnected(); virtual void onConnected();
virtual void onDisconnected(); virtual void onDisconnected();
virtual void onSocketError();
virtual std::shared_ptr<Channel> getCustomChannel( virtual std::shared_ptr<Channel> getCustomChannel(
const QString &channelName); const QString &channelName);
@ -70,6 +71,9 @@ private:
std::unique_ptr<IrcConnection> writeConnection_ = nullptr; std::unique_ptr<IrcConnection> writeConnection_ = nullptr;
std::unique_ptr<IrcConnection> readConnection_ = nullptr; std::unique_ptr<IrcConnection> readConnection_ = nullptr;
QTimer reconnectTimer_;
int falloffCounter_ = 1;
std::mutex connectionMutex_; std::mutex connectionMutex_;
// bool autoReconnect_ = false; // bool autoReconnect_ = false;