1 /***************************************************************************
2 * Copyright (C) 2005-2014 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, QObject *parent)
39 : Peer(authHandler, parent),
42 _heartBeatTimer(new QTimer(this)),
47 socket->setParent(this);
48 connect(socket, SIGNAL(readyRead()), SLOT(onReadyRead()));
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(_heartBeatTimer, SIGNAL(timeout()), SLOT(sendHeartBeat()));
61 // It's possible that more data has already arrived during the handshake, so readyRead() wouldn't be triggered.
62 // However, we can't call a virtual function from the ctor, so let's do it asynchronously.
63 if (socket->bytesAvailable())
64 QTimer::singleShot(0, this, SLOT(onReadyRead()));
68 void RemotePeer::onSocketStateChanged(QAbstractSocket::SocketState state)
70 if (state == QAbstractSocket::ClosingState) {
71 emit statusMessage(tr("Disconnecting..."));
76 void RemotePeer::onSocketError(QAbstractSocket::SocketError error)
78 emit socketError(error, socket()->errorString());
82 QString RemotePeer::description() const
85 return socket()->peerAddress().toString();
91 ::SignalProxy *RemotePeer::signalProxy() const
97 void RemotePeer::setSignalProxy(::SignalProxy *proxy)
99 if (proxy == _signalProxy)
103 _heartBeatTimer->stop();
104 disconnect(signalProxy(), 0, this, 0);
111 qWarning() << Q_FUNC_INFO << "Setting another SignalProxy not supported, ignoring!";
114 _signalProxy = proxy;
115 connect(proxy, SIGNAL(heartBeatIntervalChanged(int)), SLOT(changeHeartBeatInterval(int)));
116 _heartBeatTimer->setInterval(proxy->heartBeatInterval() * 1000);
117 _heartBeatTimer->start();
122 void RemotePeer::changeHeartBeatInterval(int secs)
125 _heartBeatTimer->stop();
127 _heartBeatTimer->setInterval(secs * 1000);
128 _heartBeatTimer->start();
133 int RemotePeer::lag() const
139 QTcpSocket *RemotePeer::socket() const
145 bool RemotePeer::isSecure() const
151 QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket());
152 if (sslSocket && sslSocket->isEncrypted())
160 bool RemotePeer::isLocal() const
163 if (socket()->peerAddress() == QHostAddress::LocalHost || socket()->peerAddress() == QHostAddress::LocalHostIPv6)
170 bool RemotePeer::isOpen() const
172 return socket() && socket()->state() == QTcpSocket::ConnectedState;
176 void RemotePeer::close(const QString &reason)
178 if (!reason.isEmpty()) {
179 qWarning() << "Disconnecting:" << reason;
182 if (socket() && socket()->state() != QTcpSocket::UnconnectedState) {
183 socket()->disconnectFromHost();
188 void RemotePeer::onReadyRead()
190 // don't try to read more data if we're already closing
191 if (socket()->state() != QAbstractSocket::ConnectedState)
195 while (readMessage(msg))
200 bool RemotePeer::readMessage(QByteArray &msg)
203 if (socket()->bytesAvailable() < 4)
205 socket()->read((char*)&_msgSize, 4);
206 _msgSize = qFromBigEndian<quint32>(_msgSize);
208 if (_msgSize > maxMessageSize) {
209 close("Peer tried to send package larger than max package size!");
214 close("Peer tried to send an empty message!");
219 if (socket()->bytesAvailable() < _msgSize) {
220 emit transferProgress(socket()->bytesAvailable(), _msgSize);
224 emit transferProgress(_msgSize, _msgSize);
226 msg.resize(_msgSize);
227 qint64 bytesRead = socket()->read(msg.data(), _msgSize);
228 if (bytesRead != _msgSize) {
229 close("Premature end of data stream!");
238 void RemotePeer::writeMessage(const QByteArray &msg)
240 quint32 size = qToBigEndian<quint32>(msg.size());
241 socket()->write((const char*)&size, 4);
242 socket()->write(msg.constData(), msg.size());
246 void RemotePeer::handle(const HeartBeat &heartBeat)
248 dispatch(HeartBeatReply(heartBeat.timestamp));
252 void RemotePeer::handle(const HeartBeatReply &heartBeatReply)
255 #if QT_VERSION >= 0x040700
256 emit lagUpdated(heartBeatReply.timestamp.msecsTo(QDateTime::currentDateTime().toUTC()) / 2);
258 emit lagUpdated(heartBeatReply.timestamp.time().msecsTo(QDateTime::currentDateTime().toUTC().time()) / 2);
263 void RemotePeer::sendHeartBeat()
265 if (signalProxy()->maxHeartBeatCount() > 0 && _heartBeatCount >= signalProxy()->maxHeartBeatCount()) {
266 qWarning() << "Disconnecting peer:" << description()
267 << "(didn't receive a heartbeat for over" << _heartBeatCount *_heartBeatTimer->interval() / 1000 << "seconds)";
269 _heartBeatTimer->stop();
273 if (_heartBeatCount > 0) {
274 _lag = _heartBeatCount * _heartBeatTimer->interval();
275 emit lagUpdated(_lag);
278 dispatch(HeartBeat(QDateTime::currentDateTime().toUTC()));