From ec7995dc427f82414c505363869e3289499b9762 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 25 Nov 2010 05:04:26 +0100 Subject: [PATCH] * Move PING command to ControlConnection. --- src/msg.h | 5 ++--- src/msgprocessor.cpp | 4 +--- src/network/connection.cpp | 20 ++------------------ src/network/connection.h | 3 --- src/network/controlconnection.cpp | 28 ++++++++++++++++++++++++++++ src/network/controlconnection.h | 3 +++ 6 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/msg.h b/src/msg.h index 26b71d7f5..6a9a51dfc 100644 --- a/src/msg.h +++ b/src/msg.h @@ -30,14 +30,13 @@ class Msg public: enum Flag { - PING = 0, RAW = 1, JSON = 2, FRAGMENT = 4, COMPRESSED = 8, DBOP = 16, - UNUSED_FLAG_6 = 32, - UNUSED_FLAG_7 = 64, + PING = 32, + PONG = 64, SETUP = 128 // used to handshake/auth the connection prior to handing over to Connection subclass }; diff --git a/src/msgprocessor.cpp b/src/msgprocessor.cpp index 9f9f78d1c..f910b62a6 100644 --- a/src/msgprocessor.cpp +++ b/src/msgprocessor.cpp @@ -60,9 +60,7 @@ 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; - - if ( !msg->is( Msg::PING ) ) - emit ready( m ); + emit ready( m ); } else { diff --git a/src/network/connection.cpp b/src/network/connection.cpp index d7ed2b72f..ddbc86c94 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -25,7 +25,6 @@ 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 ) @@ -59,7 +58,6 @@ Connection::~Connection() qDebug() << "no valid sock to delete"; } - delete m_pingtimer; delete m_statstimer; } @@ -216,12 +214,6 @@ Connection::doSetup() m_statstimer->start(); m_statstimer_mark.start(); - 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() ); qsrand( QTime( 0, 0, 0 ).secsTo( QTime::currentTime() ) ); @@ -407,7 +399,7 @@ Connection::sendMsg( msg_ptr msg ) if( m_do_shutdown ) { qDebug() << Q_FUNC_INFO << "SHUTTING DOWN, NOT SENDING msg flags:" - << (int)msg->flags() << "length:" << msg->length(); + << (int)msg->flags() << "length:" << msg->length() << id(); return; } @@ -426,7 +418,7 @@ Connection::sendMsg_now( msg_ptr msg ) if( m_sock.isNull() || !m_sock->isOpen() || !m_sock->isWritable() ) { qDebug() << "***** Socket problem, whilst in sendMsg(). Cleaning up. *****"; - shutdown( true ); + shutdown( false ); return; } @@ -462,11 +454,3 @@ 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 66033c4da..79a3259e9 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -99,7 +99,6 @@ private slots: void authCheckTimeout(); void bytesWritten( qint64 ); void calcStats(); - void onPingTimer(); protected: QPointer m_sock; @@ -119,8 +118,6 @@ 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; diff --git a/src/network/controlconnection.cpp b/src/network/controlconnection.cpp index e851bfa9d..9206f335c 100644 --- a/src/network/controlconnection.cpp +++ b/src/network/controlconnection.cpp @@ -14,6 +14,7 @@ ControlConnection::ControlConnection( Servent* parent ) : Connection( parent ) , m_dbsyncconn( 0 ) , m_registered( false ) + , m_pingtimer( 0 ) { qDebug() << "CTOR controlconnection"; setId("ControlConnection()"); @@ -29,6 +30,8 @@ ControlConnection::ControlConnection( Servent* parent ) ControlConnection::~ControlConnection() { qDebug() << "DTOR controlconnection"; + + delete m_pingtimer; m_servent->unregisterControlConnection(this); if( m_dbsyncconn ) m_dbsyncconn->deleteLater(); } @@ -71,6 +74,11 @@ ControlConnection::setup() SLOT( registerSource() ), Qt::QueuedConnection ); m_source->doDBSync(); + + m_pingtimer = new QTimer; + m_pingtimer->setInterval( 5000 ); + connect( m_pingtimer, SIGNAL( timeout() ), SLOT( onPingTimer() ) ); + m_pingtimer->start(); } @@ -169,6 +177,17 @@ ControlConnection::handleMsg( msg_ptr msg ) qDebug() << id() << "got msg:" << QString::fromAscii( msg->payload() ); } + qDebug() << msg->flags(); + if ( msg->is( Msg::PING ) ) + { + qDebug() << "Received Connection PING, sending PONG."; + sendMsg( Msg::factory( QByteArray(), Msg::PONG ) ); + } + else if ( msg->is( Msg::PONG ) ) + { + qDebug() << "Received Connection PONG, nice."; + } + // All control connection msgs are JSON if( !msg->is( Msg::JSON ) ) { @@ -207,3 +226,12 @@ ControlConnection::handleMsg( msg_ptr msg ) qDebug() << id() << "Invalid msg:" << QString::fromAscii(msg->payload()); } + + + +void +ControlConnection::onPingTimer() +{ + qDebug() << Q_FUNC_INFO; + sendMsg( Msg::factory( QByteArray(), Msg::PING ) ); +} diff --git a/src/network/controlconnection.h b/src/network/controlconnection.h index 2280cbce2..c9813c680 100644 --- a/src/network/controlconnection.h +++ b/src/network/controlconnection.h @@ -37,6 +37,7 @@ signals: private slots: void dbSyncConnFinished( QObject* c ); void registerSource(); + void onPingTimer(); private: void setupDbSyncConnection( bool ondemand = false ); @@ -46,6 +47,8 @@ private: QString m_dbconnkey; bool m_registered; + + QTimer* m_pingtimer; }; #endif // CONTROLCONNECTION_H