Simplify socketlinereader by using Qt build in parser

This also speeds it a lot making it possible to pass long lines.

REVIEW:116645
This commit is contained in:
Àlex Fiestas 2014-04-14 21:49:47 +02:00
parent 4a74f413ae
commit 0f734de3e9

View file

@ -1,5 +1,6 @@
/** /**
* Copyright 2013 Albert Vaca <albertvaka@gmail.com> * Copyright 2013 Albert Vaca <albertvaka@gmail.com>
* Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as * modify it under the terms of the GNU General Public License as
@ -20,8 +21,6 @@
#include "socketlinereader.h" #include "socketlinereader.h"
#include "../../kdebugnamespace.h"
SocketLineReader::SocketLineReader(QTcpSocket* socket, QObject* parent) SocketLineReader::SocketLineReader(QTcpSocket* socket, QObject* parent)
: QObject(parent) : QObject(parent)
, mSocket(socket) , mSocket(socket)
@ -37,36 +36,24 @@ SocketLineReader::SocketLineReader(QTcpSocket* socket, QObject* parent)
void SocketLineReader::dataReceived() void SocketLineReader::dataReceived()
{ {
int packages = mPackages.size();
QByteArray data = lastChunk + mSocket->readAll(); while(mSocket->canReadLine()) {
const QByteArray line = mSocket->readLine();
int parsedLength = 0; if (line.length() > 1) {
int packageLength = 0; mPackages.enqueue(line);//we don't want single \n
Q_FOREACH(char c, data) {
packageLength++;
if (c == '\n') {
QByteArray package = data.mid(parsedLength,packageLength);
parsedLength += packageLength;
packageLength = 0;
if(package.length() > 1) { //Ignore single newlines
mPackages.enqueue(package);
}
} }
} }
lastChunk = data.mid(parsedLength); //If we still have things to read from the socket, call dataReceived again
//We do this manually because we do not trust readyRead to be emitted again
//So we call this method again just in case.
if (mSocket->bytesAvailable() > 0) { if (mSocket->bytesAvailable() > 0) {
QMetaObject::invokeMethod(this, "dataReceived", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "dataReceived", Qt::QueuedConnection);
return;
}
} else { //If we have any packages, tell it to the world.
if (!mPackages.isEmpty()) {
if (mPackages.length() > 0) {
Q_EMIT readyRead(); Q_EMIT readyRead();
} else {
kDebug(kdeconnect_kded()) << "Received incomplete chunk of data, waiting for more";
}
} }
} }