/***************************************************************************
- * Copyright (C) 2005-2013 by the Quassel Project *
+ * Copyright (C) 2005-2018 by the Quassel Project *
* devel@quassel-irc.org *
* *
* This program is free software; you can redistribute it and/or modify *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
+#include <QtEndian>
+
#include <QHostAddress>
#include <QTimer>
#ifdef HAVE_SSL
# include <QSslSocket>
+#else
+# include <QTcpSocket>
#endif
#include "remotepeer.h"
+#include "util.h"
using namespace Protocol;
-RemotePeer::RemotePeer(QTcpSocket *socket, QObject *parent)
- : SignalProxy::AbstractPeer(parent),
+const quint32 maxMessageSize = 64 * 1024 * 1024; // This is uncompressed size. 64 MB should be enough for any sort of initData or backlog chunk
+
+RemotePeer::RemotePeer(::AuthHandler *authHandler, QTcpSocket *socket, Compressor::CompressionLevel level, QObject *parent)
+ : Peer(authHandler, parent),
_socket(socket),
- _signalProxy(0),
+ _compressor(new Compressor(socket, level, this)),
+ _signalProxy(nullptr),
_heartBeatTimer(new QTimer(this)),
_heartBeatCount(0),
- _lag(0)
+ _lag(0),
+ _msgSize(0)
{
socket->setParent(this);
- connect(socket, SIGNAL(disconnected()), SIGNAL(disconnected()));
- connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SIGNAL(error(QAbstractSocket::SocketError)));
+ connect(socket, &QAbstractSocket::stateChanged, this, &RemotePeer::onSocketStateChanged);
+ connect(socket, selectOverload<QAbstractSocket::SocketError>(&QAbstractSocket::error), this, &RemotePeer::onSocketError);
+ connect(socket, &QAbstractSocket::disconnected, this, &Peer::disconnected);
#ifdef HAVE_SSL
- QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket);
- if (sslSocket)
- connect(sslSocket, SIGNAL(encrypted()), SIGNAL(secureStateChanged()));
+ auto *sslSocket = qobject_cast<QSslSocket *>(socket);
+ if (sslSocket) {
+ connect(sslSocket, &QSslSocket::encrypted, this, [this]() { emit secureStateChanged(true); });
+ }
#endif
- connect(_heartBeatTimer, SIGNAL(timeout()), SLOT(sendHeartBeat()));
+ connect(_compressor, &Compressor::readyRead, this, &RemotePeer::onReadyRead);
+ connect(_compressor, &Compressor::error, this, &RemotePeer::onCompressionError);
+
+ connect(_heartBeatTimer, &QTimer::timeout, this, &RemotePeer::sendHeartBeat);
+}
+
+
+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());
+}
+
+
+void RemotePeer::onCompressionError(Compressor::Error error)
+{
+ close(QString("Compression error %1").arg(error));
}
return QString();
}
+QString RemotePeer::address() const
+{
+ if (socket())
+ return socket()->peerAddress().toString();
+
+ return QString();
+}
+
+quint16 RemotePeer::port() const
+{
+ if (socket())
+ return socket()->peerPort();
+
+ return 0;
+}
+
::SignalProxy *RemotePeer::signalProxy() const
{
if (!proxy) {
_heartBeatTimer->stop();
- disconnect(signalProxy(), 0, this, 0);
- _signalProxy = 0;
+ disconnect(signalProxy(), nullptr, this, nullptr);
+ _signalProxy = nullptr;
if (isOpen())
close();
}
return;
}
_signalProxy = proxy;
- connect(proxy, SIGNAL(heartBeatIntervalChanged(int)), SLOT(changeHeartBeatInterval(int)));
+ connect(proxy, &SignalProxy::heartBeatIntervalChanged, this, &RemotePeer::changeHeartBeatInterval);
_heartBeatTimer->setInterval(proxy->heartBeatInterval() * 1000);
_heartBeatTimer->start();
}
if (isLocal())
return true;
#ifdef HAVE_SSL
- QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
+ auto *sslSocket = qobject_cast<QSslSocket *>(socket());
if (sslSocket && sslSocket->isEncrypted())
return true;
#endif
}
+void RemotePeer::onReadyRead()
+{
+ QByteArray msg;
+ while (readMessage(msg)) {
+ if (SignalProxy::current())
+ SignalProxy::current()->setSourcePeer(this);
+
+ processMessage(msg);
+
+ if (SignalProxy::current())
+ SignalProxy::current()->setSourcePeer(nullptr);
+ }
+}
+
+
+bool RemotePeer::readMessage(QByteArray &msg)
+{
+ if (_msgSize == 0) {
+ if (_compressor->bytesAvailable() < 4)
+ return false;
+ _compressor->read((char*)&_msgSize, 4);
+ _msgSize = qFromBigEndian<quint32>(_msgSize);
+
+ if (_msgSize > maxMessageSize) {
+ close("Peer tried to send package larger than max package size!");
+ return false;
+ }
+
+ if (_msgSize == 0) {
+ close("Peer tried to send an empty message!");
+ return false;
+ }
+ }
+
+ if (_compressor->bytesAvailable() < _msgSize) {
+ emit transferProgress(socket()->bytesAvailable(), _msgSize);
+ return false;
+ }
+
+ emit transferProgress(_msgSize, _msgSize);
+
+ msg.resize(_msgSize);
+ qint64 bytesRead = _compressor->read(msg.data(), _msgSize);
+ if (bytesRead != _msgSize) {
+ close("Premature end of data stream!");
+ return false;
+ }
+
+ _msgSize = 0;
+ return true;
+}
+
+
+void RemotePeer::writeMessage(const QByteArray &msg)
+{
+ auto size = qToBigEndian<quint32>(msg.size());
+ _compressor->write((const char*)&size, 4, Compressor::NoFlush);
+ _compressor->write(msg.constData(), msg.size());
+}
+
+
void RemotePeer::handle(const HeartBeat &heartBeat)
{
- dispatch(HeartBeatReply(heartBeat.timestamp()));
+ dispatch(HeartBeatReply(heartBeat.timestamp));
}
void RemotePeer::handle(const HeartBeatReply &heartBeatReply)
{
_heartBeatCount = 0;
-#if QT_VERSION < 0x040700
- emit lagUpdated(heartBeatReply.timestamp().time().msecsTo(QTime::currentTime()) / 2);
-#else
- emit lagUpdated(heartBeatReply.timestamp().msecsTo(QDateTime::currentDateTime()) / 2);
-#endif
+ emit lagUpdated(heartBeatReply.timestamp.msecsTo(QDateTime::currentDateTime().toUTC()) / 2);
}
emit lagUpdated(_lag);
}
- dispatch(HeartBeat(QDateTime::currentDateTime()));
+ dispatch(HeartBeat(QDateTime::currentDateTime().toUTC()));
++_heartBeatCount;
}
-
-