From 28b543a5c5a8bdfb3657fa947e6df50430ee60b6 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 25 Nov 2010 04:39:07 +0100 Subject: [PATCH] * Use internal PING mechanism in Connection class instead of relying on TCP KEEPALIVE. --- src/msg.h | 1 + src/msgprocessor.cpp | 4 +++- src/network/connection.cpp | 28 ++++++++++++++++++---------- src/network/connection.h | 3 +++ 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/msg.h b/src/msg.h index f0fd5fa33..26b71d7f5 100644 --- a/src/msg.h +++ b/src/msg.h @@ -30,6 +30,7 @@ class Msg public: enum Flag { + PING = 0, RAW = 1, JSON = 2, FRAGMENT = 4, diff --git a/src/msgprocessor.cpp b/src/msgprocessor.cpp index f910b62a6..9f9f78d1c 100644 --- a/src/msgprocessor.cpp +++ b/src/msgprocessor.cpp @@ -60,7 +60,9 @@ void MsgProcessor::handleProcessedMsg( msg_ptr msg ) msg_ptr m = m_msgs.takeFirst(); m_msg_ready.remove( m.data() ); //qDebug() << Q_FUNC_INFO << "totmsgsize:" << m_totmsgsize; - emit ready( m ); + + if ( !msg->is( Msg::PING ) ) + emit ready( m ); } else { diff --git a/src/network/connection.cpp b/src/network/connection.cpp index e5bb70f04..d7ed2b72f 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -8,7 +8,7 @@ #include "servent.h" -#define PROTOVER "2" // must match remote peer, or we can't talk. +#define PROTOVER "3" // must match remote peer, or we can't talk. Connection::Connection( Servent* parent ) @@ -25,6 +25,7 @@ Connection::Connection( Servent* parent ) , m_tx_bytes_requested( 0 ) , m_rx_bytes( 0 ) , m_id( "Connection()" ) + , m_pingtimer( 0 ) , m_statstimer( 0 ) , m_stats_tx_bytes_per_sec( 0 ) , m_stats_rx_bytes_per_sec( 0 ) @@ -57,6 +58,8 @@ Connection::~Connection() { qDebug() << "no valid sock to delete"; } + + delete m_pingtimer; delete m_statstimer; } @@ -208,19 +211,16 @@ Connection::doSetup() //stats timer calculates BW used by this connection m_statstimer = new QTimer; m_statstimer->moveToThread( this->thread() ); - m_statstimer->setInterval(1000); + m_statstimer->setInterval( 1000 ); connect( m_statstimer, SIGNAL( timeout() ), SLOT( calcStats() ) ); m_statstimer->start(); m_statstimer_mark.start(); - m_sock->setSocketOption( QAbstractSocket::KeepAliveOption, 1 ); - int sockID = m_sock.data()->socketDescriptor(); - int idle = 30; - int intvl = 5; - int cnt = 3; - setsockopt( sockID, SOL_TCP, TCP_KEEPIDLE, &idle, sizeof( idle ) ); - setsockopt( sockID, SOL_TCP, TCP_KEEPINTVL, &intvl, sizeof( intvl ) ); - setsockopt( sockID, SOL_TCP, TCP_KEEPCNT, &cnt, sizeof( cnt ) ); + m_pingtimer = new QTimer; + m_pingtimer->moveToThread( this->thread() ); + m_pingtimer->setInterval( 5000 ); + connect( m_pingtimer, SIGNAL( timeout() ), SLOT( onPingTimer() ) ); + m_pingtimer->start(); m_sock->moveToThread( thread() ); @@ -462,3 +462,11 @@ Connection::calcStats() emit statsTick( m_stats_tx_bytes_per_sec, m_stats_rx_bytes_per_sec ); } + + +void +Connection::onPingTimer() +{ + qDebug() << Q_FUNC_INFO; + sendMsg( Msg::factory( QByteArray(), Msg::PING ) ); +} diff --git a/src/network/connection.h b/src/network/connection.h index 79a3259e9..66033c4da 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -99,6 +99,7 @@ private slots: void authCheckTimeout(); void bytesWritten( qint64 ); void calcStats(); + void onPingTimer(); protected: QPointer m_sock; @@ -118,6 +119,8 @@ private: qint64 m_rx_bytes; QString m_id; + QTimer* m_pingtimer; + QTimer* m_statstimer; QTime m_statstimer_mark; qint64 m_stats_tx_bytes_per_sec, m_stats_rx_bytes_per_sec;