1 /***************************************************************************
2 * Copyright (C) 2005-2016 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
23 #include <QHostAddress>
27 # include <QSslSocket>
29 # include <QTcpSocket>
32 #include "remotepeer.h"
34 using namespace Protocol;
36 const quint32 maxMessageSize = 64 * 1024 * 1024; // This is uncompressed size. 64 MB should be enough for any sort of initData or backlog chunk
38 RemotePeer::RemotePeer(::AuthHandler *authHandler, QTcpSocket *socket, Compressor::CompressionLevel level, QObject *parent)
39 : Peer(authHandler, parent),
41 _compressor(new Compressor(socket, level, this)),
43 _heartBeatTimer(new QTimer(this)),
48 socket->setParent(this);
49 connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), SLOT(onSocketStateChanged(QAbstractSocket::SocketState)));
50 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(onSocketError(QAbstractSocket::SocketError)));
51 connect(socket, SIGNAL(disconnected()), SIGNAL(disconnected()));
54 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket);
56 connect(sslSocket, SIGNAL(encrypted()), SIGNAL(secureStateChanged()));
59 connect(_compressor, SIGNAL(readyRead()), SLOT(onReadyRead()));
60 connect(_compressor, SIGNAL(error(Compressor::Error)), SLOT(onCompressionError(Compressor::Error)));
62 connect(_heartBeatTimer, SIGNAL(timeout()), SLOT(sendHeartBeat()));
66 void RemotePeer::onSocketStateChanged(QAbstractSocket::SocketState state)
68 if (state == QAbstractSocket::ClosingState) {
69 emit statusMessage(tr("Disconnecting..."));
74 void RemotePeer::onSocketError(QAbstractSocket::SocketError error)
76 emit socketError(error, socket()->errorString());
80 void RemotePeer::onCompressionError(Compressor::Error error)
82 close(QString("Compression error %1").arg(error));
86 QString RemotePeer::description() const
89 return socket()->peerAddress().toString();
94 QString RemotePeer::address() const
97 return socket()->peerAddress().toString();
102 quint16 RemotePeer::port() const
105 return socket()->peerPort();
111 ::SignalProxy *RemotePeer::signalProxy() const
117 void RemotePeer::setSignalProxy(::SignalProxy *proxy)
119 if (proxy == _signalProxy)
123 _heartBeatTimer->stop();
124 disconnect(signalProxy(), 0, this, 0);
131 qWarning() << Q_FUNC_INFO << "Setting another SignalProxy not supported, ignoring!";
134 _signalProxy = proxy;
135 connect(proxy, SIGNAL(heartBeatIntervalChanged(int)), SLOT(changeHeartBeatInterval(int)));
136 _heartBeatTimer->setInterval(proxy->heartBeatInterval() * 1000);
137 _heartBeatTimer->start();
142 void RemotePeer::changeHeartBeatInterval(int secs)
145 _heartBeatTimer->stop();
147 _heartBeatTimer->setInterval(secs * 1000);
148 _heartBeatTimer->start();
153 int RemotePeer::lag() const
159 QTcpSocket *RemotePeer::socket() const
165 bool RemotePeer::isSecure() const
171 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
172 if (sslSocket && sslSocket->isEncrypted())
180 bool RemotePeer::isLocal() const
183 if (socket()->peerAddress() == QHostAddress::LocalHost || socket()->peerAddress() == QHostAddress::LocalHostIPv6)
190 bool RemotePeer::isOpen() const
192 return socket() && socket()->state() == QTcpSocket::ConnectedState;
196 void RemotePeer::close(const QString &reason)
198 if (!reason.isEmpty()) {
199 qWarning() << "Disconnecting:" << reason;
202 if (socket() && socket()->state() != QTcpSocket::UnconnectedState) {
203 socket()->disconnectFromHost();
208 void RemotePeer::onReadyRead()
211 while (readMessage(msg))
216 bool RemotePeer::readMessage(QByteArray &msg)
219 if (_compressor->bytesAvailable() < 4)
221 _compressor->read((char*)&_msgSize, 4);
222 _msgSize = qFromBigEndian<quint32>(_msgSize);
224 if (_msgSize > maxMessageSize) {
225 close("Peer tried to send package larger than max package size!");
230 close("Peer tried to send an empty message!");
235 if (_compressor->bytesAvailable() < _msgSize) {
236 emit transferProgress(socket()->bytesAvailable(), _msgSize);
240 emit transferProgress(_msgSize, _msgSize);
242 msg.resize(_msgSize);
243 qint64 bytesRead = _compressor->read(msg.data(), _msgSize);
244 if (bytesRead != _msgSize) {
245 close("Premature end of data stream!");
254 void RemotePeer::writeMessage(const QByteArray &msg)
256 quint32 size = qToBigEndian<quint32>(msg.size());
257 _compressor->write((const char*)&size, 4, Compressor::NoFlush);
258 _compressor->write(msg.constData(), msg.size());
262 void RemotePeer::handle(const HeartBeat &heartBeat)
264 dispatch(HeartBeatReply(heartBeat.timestamp));
268 void RemotePeer::handle(const HeartBeatReply &heartBeatReply)
271 emit lagUpdated(heartBeatReply.timestamp.msecsTo(QDateTime::currentDateTime().toUTC()) / 2);
275 void RemotePeer::sendHeartBeat()
277 if (signalProxy()->maxHeartBeatCount() > 0 && _heartBeatCount >= signalProxy()->maxHeartBeatCount()) {
278 qWarning() << "Disconnecting peer:" << description()
279 << "(didn't receive a heartbeat for over" << _heartBeatCount *_heartBeatTimer->interval() / 1000 << "seconds)";
281 _heartBeatTimer->stop();
285 if (_heartBeatCount > 0) {
286 _lag = _heartBeatCount * _heartBeatTimer->interval();
287 emit lagUpdated(_lag);
290 dispatch(HeartBeat(QDateTime::currentDateTime().toUTC()));