2019-09-08 22:27:57 +02:00
# include "IrcConnection2.hpp"
2020-01-03 20:55:13 +01:00
# include "common/Version.hpp"
2019-09-08 22:27:57 +02:00
namespace chatterino {
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 )
{
// send ping every x seconds
this - > pingTimer_ . setInterval ( 5000 ) ;
this - > pingTimer_ . start ( ) ;
QObject : : connect ( & this - > pingTimer_ , & QTimer : : timeout , [ this ] {
if ( this - > isConnected ( ) )
{
2020-01-03 20:55:13 +01:00
if ( this - > waitingForPong_ . load ( ) )
{
// Not sending another ping as we haven't received the matching pong yet
return ;
}
2019-09-08 22:27:57 +02:00
if ( ! this - > recentlyReceivedMessage_ . load ( ) )
{
2020-01-03 20:55:13 +01:00
this - > sendRaw ( " PING " + payload ) ;
2019-09-08 22:27:57 +02:00
this - > reconnectTimer_ . start ( ) ;
2020-01-03 20:55:13 +01:00
this - > waitingForPong_ = true ;
2019-09-08 22:27:57 +02:00
}
this - > recentlyReceivedMessage_ = false ;
}
} ) ;
// reconnect after x seconds without receiving a message
this - > reconnectTimer_ . setInterval ( 5000 ) ;
this - > reconnectTimer_ . setSingleShot ( true ) ;
QObject : : connect ( & this - > reconnectTimer_ , & QTimer : : timeout , [ this ] {
if ( this - > isConnected ( ) )
{
reconnectRequested . invoke ( ) ;
}
} ) ;
2020-11-08 12:02:19 +01:00
QObject : : connect ( this , & Communi : : IrcConnection : : connected , this , [ this ] {
this - > waitingForPong_ = false ;
} ) ;
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-01-09 18:05:02 +01:00
QObject : : connect (
this , & Communi : : IrcConnection : : messageReceived ,
[ this ] ( Communi : : IrcMessage * message ) {
this - > recentlyReceivedMessage_ = true ;
2019-09-08 22:27:57 +02:00
2021-01-09 18:05:02 +01:00
if ( this - > reconnectTimer_ . isActive ( ) )
{
this - > reconnectTimer_ . stop ( ) ;
// The reconnect timer had started, that means we were waiting for a pong.
// Since we received a message, this means that any pong response is meaningless, so we can just stop waiting for the pong and send another ping soon:tm:
this - > waitingForPong_ = false ;
}
} ) ;
2019-09-08 22:27:57 +02:00
}
} // namespace chatterino