No need to use a QBuffer in DownloadJob

Summary:
QSslSocket is already a QIODevice so just use that.

Should fix the issue of transfering *big* files.

Unfortunately this seems to trigger a bug in KIO and CPU usage goes through the roof, so haven't really been able to test it does actually fix things.

Please don't merge/approve yet

Reviewers: apol, albertvaka, kdeconnect

Subscribers: #kde_connect

Differential Revision: https://phabricator.kde.org/D6039
This commit is contained in:
Albert Astals Cid 2017-08-01 19:40:43 +02:00 committed by Aleix Pol
parent 08f31736ad
commit af778b71b1
3 changed files with 30 additions and 18 deletions

View file

@ -31,17 +31,30 @@
#include "lanlinkprovider.h"
#include "core/core_debug.h"
class QQSslSocket : public QSslSocket
{
private:
// Workaround Qt bug https://codereview.qt-project.org/#/c/195723/
qint64 readData(char *data, qint64 maxSize) override {
qint64 res = QSslSocket::readData(data, maxSize);
if (res == 0 && encryptedBytesAvailable() == 0 && state() != ConnectedState) {
return maxSize ? qint64(-1) : qint64(0);
} else {
return res;
}
}
};
DownloadJob::DownloadJob(const QHostAddress &address, const QVariantMap &transferInfo)
: KJob()
, mAddress(address)
, mPort(transferInfo[QStringLiteral("port")].toInt())
, mSocket(new QSslSocket)
, mBuffer(new QBuffer)
, mSocket(new QQSslSocket)
{
LanLinkProvider::configureSslSocket(mSocket.data(), transferInfo.value(QStringLiteral("deviceId")).toString(), true);
connect(mSocket.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketFailed(QAbstractSocket::SocketError)));
// connect(mSocket.data(), &QAbstractSocket::stateChanged, [](QAbstractSocket::SocketState state){ qDebug() << "statechange" << state; });
connect(mSocket.data(), &QAbstractSocket::connected, this, &DownloadJob::socketConnected);
}
DownloadJob::~DownloadJob()
@ -54,26 +67,22 @@ void DownloadJob::start()
//TODO: Timeout?
// Cannot use read only, might be due to ssl handshake, getting QIODevice::ReadOnly error and no connection
mSocket->connectToHostEncrypted(mAddress.toString(), mPort, QIODevice::ReadWrite);
bool b = mBuffer->open(QBuffer::ReadWrite);
Q_ASSERT(b);
}
void DownloadJob::socketFailed(QAbstractSocket::SocketError error)
{
if (error != QAbstractSocket::RemoteHostClosedError) { //remote host closes when finishes
qWarning(KDECONNECT_CORE) << "error..." << mSocket->errorString();
setError(error + 1);
setErrorText(mSocket->errorString());
} else {
auto ba = mSocket->readAll();
mBuffer->write(ba);
mBuffer->seek(0);
}
qWarning() << "error..." << (error == QAbstractSocket::RemoteHostClosedError) << mSocket->errorString();
setError(error + 1);
setErrorText(mSocket->errorString());
emitResult();
}
QSharedPointer<QIODevice> DownloadJob::getPayload()
{
return mBuffer.staticCast<QIODevice>();
return mSocket.staticCast<QIODevice>();
}
void DownloadJob::socketConnected()
{
emitResult();
}

View file

@ -47,11 +47,10 @@ private:
QHostAddress mAddress;
qint16 mPort;
QSharedPointer<QSslSocket> mSocket;
QSharedPointer<QBuffer> mBuffer;
private Q_SLOTS:
void socketFailed(QAbstractSocket::SocketError error);
void socketConnected();
};
#endif // UPLOADJOB_H

View file

@ -79,6 +79,10 @@ void FileTransferJob::doStart()
void FileTransferJob::startTransfer()
{
// Don't put each ready read
if (mTimer.isValid())
return;
setProcessedAmount(Bytes, 0);
mTimer.start();
description(this, i18n("Receiving file over KDE Connect"),