1 /***************************************************************************
2 * Copyright (C) 2005-2013 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 "legacypeer.h"
25 using namespace Protocol;
27 LegacyPeer::LegacyPeer(QTcpSocket *socket, QObject *parent)
28 : RemotePeer(socket, parent),
30 _useCompression(false)
32 _stream.setDevice(socket);
33 _stream.setVersion(QDataStream::Qt_4_2);
35 connect(socket, SIGNAL(readyRead()), SLOT(socketDataAvailable()));
39 void LegacyPeer::setSignalProxy(::SignalProxy *proxy)
41 RemotePeer::setSignalProxy(proxy);
44 // enable compression now if requested - the initial handshake is uncompressed in the legacy protocol!
45 _useCompression = socket()->property("UseCompression").toBool();
51 void LegacyPeer::socketDataAvailable()
54 while (readSocketData(item)) {
55 // if no sigproxy is set, we're in handshake mode and let the data be handled elsewhere
57 emit dataReceived(item);
59 handlePackedFunc(item);
64 bool LegacyPeer::readSocketData(QVariant &item)
66 if (_blockSize == 0) {
67 if (socket()->bytesAvailable() < 4)
69 _stream >> _blockSize;
72 if (_blockSize > 1 << 22) {
73 close("Peer tried to send package larger than max package size!");
77 if (_blockSize == 0) {
78 close("Peer tried to send 0 byte package!");
82 if (socket()->bytesAvailable() < _blockSize) {
83 emit transferProgress(socket()->bytesAvailable(), _blockSize);
87 emit transferProgress(_blockSize, _blockSize);
91 if (_useCompression) {
95 int nbytes = rawItem.size();
97 const char *data = rawItem.constData();
98 if (nbytes < 4 || (data[0] != 0 || data[1] != 0 || data[2] != 0 || data[3] != 0)) {
99 close("Peer sent corrupted compressed data!");
104 rawItem = qUncompress(rawItem);
106 QDataStream itemStream(&rawItem, QIODevice::ReadOnly);
107 itemStream.setVersion(QDataStream::Qt_4_2);
114 if (!item.isValid()) {
115 close("Peer sent corrupt data: unable to load QVariant!");
123 void LegacyPeer::writeSocketData(const QVariant &item)
125 if (!socket()->isOpen()) {
126 qWarning() << Q_FUNC_INFO << "Can't write to a closed socket!";
131 QDataStream out(&block, QIODevice::WriteOnly);
132 out.setVersion(QDataStream::Qt_4_2);
134 if (_useCompression) {
136 QDataStream itemStream(&rawItem, QIODevice::WriteOnly);
137 itemStream.setVersion(QDataStream::Qt_4_2);
140 rawItem = qCompress(rawItem);
148 _stream << block; // also writes the length as part of the serialization format
152 void LegacyPeer::handlePackedFunc(const QVariant &packedFunc)
154 QVariantList params(packedFunc.toList());
156 if (params.isEmpty()) {
157 qWarning() << Q_FUNC_INFO << "Received incompatible data:" << packedFunc;
161 RequestType requestType = (RequestType)params.takeFirst().value<int>();
162 switch (requestType) {
164 if (params.count() < 3) {
165 qWarning() << Q_FUNC_INFO << "Received invalid sync call:" << params;
168 QByteArray className = params.takeFirst().toByteArray();
169 QString objectName = params.takeFirst().toString();
170 QByteArray slotName = params.takeFirst().toByteArray();
171 handle(Protocol::SyncMessage(className, objectName, slotName, params));
175 if (params.empty()) {
176 qWarning() << Q_FUNC_INFO << "Received empty RPC call!";
179 QByteArray slotName = params.takeFirst().toByteArray();
180 handle(Protocol::RpcCall(slotName, params));
184 if (params.count() != 2) {
185 qWarning() << Q_FUNC_INFO << "Received invalid InitRequest:" << params;
188 QByteArray className = params[0].toByteArray();
189 QString objectName = params[1].toString();
190 handle(Protocol::InitRequest(className, objectName));
194 if (params.count() != 3) {
195 qWarning() << Q_FUNC_INFO << "Received invalid InitData:" << params;
198 QByteArray className = params[0].toByteArray();
199 QString objectName = params[1].toString();
200 QVariantMap initData = params[2].toMap();
201 handle(Protocol::InitData(className, objectName, initData));
205 if (params.count() != 1) {
206 qWarning() << Q_FUNC_INFO << "Received invalid HeartBeat:" << params;
209 // The legacy protocol would only send a QTime, no QDateTime
210 // so we assume it's sent today, which works in exactly the same cases as it did in the old implementation
211 QDateTime dateTime = QDateTime::currentDateTimeUtc();
212 dateTime.setTime(params[0].toTime());
213 handle(Protocol::HeartBeat(dateTime));
216 case HeartBeatReply: {
217 if (params.count() != 1) {
218 qWarning() << Q_FUNC_INFO << "Received invalid HeartBeat:" << params;
221 // The legacy protocol would only send a QTime, no QDateTime
222 // so we assume it's sent today, which works in exactly the same cases as it did in the old implementation
223 QDateTime dateTime = QDateTime::currentDateTimeUtc();
224 dateTime.setTime(params[0].toTime());
225 handle(Protocol::HeartBeatReply(dateTime));
233 void LegacyPeer::dispatch(const Protocol::SyncMessage &msg)
235 dispatchPackedFunc(QVariantList() << (qint16)Sync << msg.className() << msg.objectName() << msg.slotName() << msg.params());
239 void LegacyPeer::dispatch(const Protocol::RpcCall &msg)
241 dispatchPackedFunc(QVariantList() << (qint16)RpcCall << msg.slotName() << msg.params());
245 void LegacyPeer::dispatch(const Protocol::InitRequest &msg)
247 dispatchPackedFunc(QVariantList() << (qint16)InitRequest << msg.className() << msg.objectName());
251 void LegacyPeer::dispatch(const Protocol::InitData &msg)
253 dispatchPackedFunc(QVariantList() << (qint16)InitData << msg.className() << msg.objectName() << msg.initData());
257 void LegacyPeer::dispatch(const Protocol::HeartBeat &msg)
259 dispatchPackedFunc(QVariantList() << (qint16)HeartBeat << msg.timestamp().time());
263 void LegacyPeer::dispatch(const Protocol::HeartBeatReply &msg)
265 dispatchPackedFunc(QVariantList() << (qint16)HeartBeatReply << msg.timestamp().time());
269 void LegacyPeer::dispatchPackedFunc(const QVariantList &packedFunc)
271 writeSocketData(QVariant(packedFunc));