Handle the readyRead() signal in RemotePeer instead of LegacyPeer
[quassel.git] / src / common / remotepeer.cpp
index d3752f1..f2439fc 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2013 by the Quassel Project                        *
+ *   Copyright (C) 2005-2014 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -31,8 +31,8 @@
 
 using namespace Protocol;
 
-RemotePeer::RemotePeer(QTcpSocket *socket, QObject *parent)
-    : Peer(parent),
+RemotePeer::RemotePeer(::AuthHandler *authHandler, QTcpSocket *socket, QObject *parent)
+    : Peer(authHandler, parent),
     _socket(socket),
     _signalProxy(0),
     _heartBeatTimer(new QTimer(this)),
@@ -40,8 +40,10 @@ RemotePeer::RemotePeer(QTcpSocket *socket, QObject *parent)
     _lag(0)
 {
     socket->setParent(this);
+    connect(socket, SIGNAL(readyRead()), SLOT(onSocketDataAvailable()));
+    connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), SLOT(onSocketStateChanged(QAbstractSocket::SocketState)));
+    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(onSocketError(QAbstractSocket::SocketError)));
     connect(socket, SIGNAL(disconnected()), SIGNAL(disconnected()));
-    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SIGNAL(error(QAbstractSocket::SocketError)));
 
 #ifdef HAVE_SSL
     QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket);
@@ -50,6 +52,25 @@ RemotePeer::RemotePeer(QTcpSocket *socket, QObject *parent)
 #endif
 
     connect(_heartBeatTimer, SIGNAL(timeout()), SLOT(sendHeartBeat()));
+
+    // It's possible that more data has already arrived during the handshake, so readyRead() wouldn't be triggered.
+    // However, we can't call a virtual function from the ctor, so let's do it asynchronously.
+    if (socket->bytesAvailable())
+        QTimer::singleShot(0, this, SLOT(onSocketDataAvailable()));
+}
+
+
+void RemotePeer::onSocketStateChanged(QAbstractSocket::SocketState state)
+{
+    if (state == QAbstractSocket::ClosingState) {
+        emit statusMessage(tr("Disconnecting..."));
+    }
+}
+
+
+void RemotePeer::onSocketError(QAbstractSocket::SocketError error)
+{
+    emit socketError(error, socket()->errorString());
 }
 
 
@@ -169,9 +190,9 @@ void RemotePeer::handle(const HeartBeatReply &heartBeatReply)
 {
     _heartBeatCount = 0;
 #if QT_VERSION >= 0x040900
-    emit lagUpdated(heartBeatReply.timestamp().msecsTo(QDateTime::currentDateTime().toUTC()) / 2);
+    emit lagUpdated(heartBeatReply.timestamp.msecsTo(QDateTime::currentDateTime().toUTC()) / 2);
 #else
-    emit lagUpdated(heartBeatReply.timestamp().time().msecsTo(QDateTime::currentDateTime().toUTC().time()) / 2);
+    emit lagUpdated(heartBeatReply.timestamp.time().msecsTo(QDateTime::currentDateTime().toUTC().time()) / 2);
 #endif
 }