[SCM] kdeconnect packaging branch, master, updated. debian/0.9g-1-1183-g9d69498

Maximiliano Curia maxy at moszumanska.debian.org
Fri Oct 14 14:27:08 UTC 2016


Gitweb-URL: http://git.debian.org/?p=pkg-kde/kde-extras/kdeconnect.git;a=commitdiff;h=5bbfcd8

The following commit has been merged in the master branch:
commit 5bbfcd860f4da3e40b3c3c6296edef3ad40ce312
Author: Albert Vaca <albertvaka at gmail.com>
Date:   Wed Oct 30 01:18:25 2013 +0100

    LanDeviceLink now waits to read an entire line before trying to read it
    
    Packages always end in 
, so this way we make sure we have a complete
    package to unserialize. Before this patch, some long packages that were
    divided in chunks weren't received correctly.
---
 kded/backends/lan/CMakeLists.txt                   |  1 +
 kded/backends/lan/landevicelink.cpp                | 37 ++++--------
 kded/backends/lan/landevicelink.h                  |  7 +--
 kded/backends/lan/lanlinkprovider.cpp              | 25 ++++++++
 kded/backends/lan/lanlinkprovider.h                |  2 +
 kded/backends/lan/socketlinereader.cpp             | 68 ++++++++++++++++++++++
 .../lan/{landevicelink.h => socketlinereader.h}    | 35 ++++++-----
 7 files changed, 131 insertions(+), 44 deletions(-)

diff --git a/kded/backends/lan/CMakeLists.txt b/kded/backends/lan/CMakeLists.txt
index 5b5cda3..96b55b9 100644
--- a/kded/backends/lan/CMakeLists.txt
+++ b/kded/backends/lan/CMakeLists.txt
@@ -6,6 +6,7 @@ set(kded_kdeconnect_SRCS
     backends/lan/landevicelink.cpp
     backends/lan/uploadjob.cpp
     backends/lan/downloadjob.cpp
+    backends/lan/socketlinereader.cpp
 
     PARENT_SCOPE
 )
diff --git a/kded/backends/lan/landevicelink.cpp b/kded/backends/lan/landevicelink.cpp
index bf9bd75..b2fef59 100644
--- a/kded/backends/lan/landevicelink.cpp
+++ b/kded/backends/lan/landevicelink.cpp
@@ -28,28 +28,16 @@
 #include "../linkprovider.h"
 #include "uploadjob.h"
 #include "downloadjob.h"
+#include "socketlinereader.h"
 
 LanDeviceLink::LanDeviceLink(const QString& d, LinkProvider* a, QTcpSocket* socket)
     : DeviceLink(d, a)
+    , mSocketLineReader(new SocketLineReader(socket, a))
 {
-    mSocket = socket;
 
-    int fd = socket->socketDescriptor();
-    int enableKeepAlive = 1;
-    setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enableKeepAlive, sizeof(enableKeepAlive));
-
-    int maxIdle = 60; /* seconds */
-    setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &maxIdle, sizeof(maxIdle));
-
-    int count = 3;  // send up to 3 keepalive packets out, then disconnect if no response
-    setsockopt(fd, getprotobyname("TCP")->p_proto, TCP_KEEPCNT, &count, sizeof(count));
-
-    int interval = 5;   // send a keepalive packet out every 2 seconds (after the 5 second idle period)
-    setsockopt(fd, getprotobyname("TCP")->p_proto, TCP_KEEPINTVL, &interval, sizeof(interval));
-
-    connect(mSocket, SIGNAL(disconnected()),
+    connect(mSocketLineReader, SIGNAL(disconnected()),
             this, SLOT(deleteLater()));
-    connect(mSocket, SIGNAL(readyRead()),
+    connect(mSocketLineReader, SIGNAL(readyRead()),
             this, SLOT(dataReceived()));
 }
 
@@ -63,7 +51,7 @@ bool LanDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np
 
     np.encrypt(key);
 
-    int written = mSocket->write(np.serialize());
+    int written = mSocketLineReader->write(np.serialize());
 
     //TODO: Actually detect if a package is received or not, now we keep TCP
     //"ESTABLISHED" connections that look legit (return true when we use them),
@@ -79,7 +67,7 @@ bool LanDeviceLink::sendPackage(NetworkPackage& np)
          np.setPayloadTransferInfo(job->getTransferInfo());
     }
 
-    int written = mSocket->write(np.serialize());
+    int written = mSocketLineReader->write(np.serialize());
     //TODO: Actually detect if a package is received or not, now we keep TCP
     //"ESTABLISHED" connections that look legit (return true when we use them),
     //but that are actually broken
@@ -89,9 +77,9 @@ bool LanDeviceLink::sendPackage(NetworkPackage& np)
 void LanDeviceLink::dataReceived()
 {
 
-    QByteArray package = mSocket->readLine();
+    QByteArray package = mSocketLineReader->readLine();
 
-    //qDebug() << "LanDeviceLink dataReceived" << data;
+    //qDebug() << "LanDeviceLink dataReceived" << package;
 
     NetworkPackage unserialized(QString::null);
     NetworkPackage::unserialize(package, &unserialized);
@@ -103,7 +91,7 @@ void LanDeviceLink::dataReceived()
 
         if (decrypted.hasPayloadTransferInfo()) {
             qDebug() << "HasPayloadTransferInfo";
-            DownloadJob* job = new DownloadJob(mSocket->peerAddress(), decrypted.payloadTransferInfo());
+            DownloadJob* job = new DownloadJob(mSocketLineReader->peerAddress(), decrypted.payloadTransferInfo());
             job->start();
             decrypted.setPayload(job->getPayload(), decrypted.payloadSize());
         }
@@ -120,13 +108,8 @@ void LanDeviceLink::dataReceived()
 
     }
 
-    if (mSocket->bytesAvailable() > 0) {
+    if (mSocketLineReader->bytesAvailable() > 0) {
         QMetaObject::invokeMethod(this, "dataReceived", Qt::QueuedConnection);
     }
 
 }
-
-void LanDeviceLink::readyRead()
-{
-
-}
diff --git a/kded/backends/lan/landevicelink.h b/kded/backends/lan/landevicelink.h
index f40f259..2bc55f6 100644
--- a/kded/backends/lan/landevicelink.h
+++ b/kded/backends/lan/landevicelink.h
@@ -27,7 +27,7 @@
 
 #include "../devicelink.h"
 
-class AvahiTcpLinkProvider;
+class SocketLineReader;
 
 class LanDeviceLink
     : public DeviceLink
@@ -42,11 +42,10 @@ public:
 
 private Q_SLOTS:
     void dataReceived();
-    void readyRead();
 
 private:
-    QTcpSocket* mSocket;
+    SocketLineReader* mSocketLineReader;
 
 };
 
-#endif // UDPDEVICELINK_H
+#endif
diff --git a/kded/backends/lan/lanlinkprovider.cpp b/kded/backends/lan/lanlinkprovider.cpp
index d1caa1e..32ddb6f 100644
--- a/kded/backends/lan/lanlinkprovider.cpp
+++ b/kded/backends/lan/lanlinkprovider.cpp
@@ -20,12 +20,34 @@
 
 #include "lanlinkprovider.h"
 
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <netdb.h>
+
 #include <QHostInfo>
 #include <QTcpServer>
 #include <QUdpSocket>
 
 #include "landevicelink.h"
 
+void LanLinkProvider::configureSocket(QTcpSocket* socket)
+{
+    int fd = socket->socketDescriptor();
+    int enableKeepAlive = 1;
+    setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enableKeepAlive, sizeof(enableKeepAlive));
+
+    int maxIdle = 60; /* seconds */
+    setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &maxIdle, sizeof(maxIdle));
+
+    int count = 3;  // send up to 3 keepalive packets out, then disconnect if no response
+    setsockopt(fd, getprotobyname("TCP")->p_proto, TCP_KEEPCNT, &count, sizeof(count));
+
+    int interval = 5;   // send a keepalive packet out every 2 seconds (after the 5 second idle period)
+    setsockopt(fd, getprotobyname("TCP")->p_proto, TCP_KEEPINTVL, &interval, sizeof(interval));
+
+}
+
 LanLinkProvider::LanLinkProvider()
 {
 
@@ -134,6 +156,8 @@ void LanLinkProvider::connected()
     disconnect(socket, SIGNAL(connected()), this, SLOT(connected()));
     disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError()));
 
+    configureSocket(socket);
+
     NetworkPackage* np = receivedIdentityPackages[socket].np;
     const QString& id = np->get<QString>("deviceId");
     //qDebug() << "Connected" << socket->isWritable();
@@ -202,6 +226,7 @@ void LanLinkProvider::newConnection()
 void LanLinkProvider::dataReceived()
 {
     QTcpSocket* socket = (QTcpSocket*) QObject::sender();
+    configureSocket(socket);
 
     QByteArray data = socket->readLine();
 
diff --git a/kded/backends/lan/lanlinkprovider.h b/kded/backends/lan/lanlinkprovider.h
index 1c52ade..85938b2 100644
--- a/kded/backends/lan/lanlinkprovider.h
+++ b/kded/backends/lan/lanlinkprovider.h
@@ -54,6 +54,8 @@ private Q_SLOTS:
     void deviceLinkDestroyed(QObject*);
 
 private:
+    static void configureSocket(QTcpSocket* socket);
+
     QTcpServer* mTcpServer;
     QUdpSocket* mUdpServer;
     const static quint16 port = 1714;
diff --git a/kded/backends/lan/socketlinereader.cpp b/kded/backends/lan/socketlinereader.cpp
new file mode 100644
index 0000000..3d0542f
--- /dev/null
+++ b/kded/backends/lan/socketlinereader.cpp
@@ -0,0 +1,68 @@
+/**
+ * Copyright 2013 Albert Vaca <albertvaka at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License or (at your option) version 3 or any later version
+ * accepted by the membership of KDE e.V. (or its successor approved
+ * by the membership of KDE e.V.), which shall act as a proxy
+ * defined in Section 14 of version 3 of the license.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "socketlinereader.h"
+
+
+SocketLineReader::SocketLineReader(QTcpSocket* socket, QObject* parent)
+    : QObject(parent)
+    , mSocket(socket)
+{
+
+    connect(mSocket, SIGNAL(disconnected()),
+            this, SIGNAL(disconnected()));
+
+    connect(mSocket, SIGNAL(readyRead()),
+            this, SLOT(dataReceived()));
+
+}
+
+void SocketLineReader::dataReceived()
+{
+
+    QByteArray data = lastChunk + mSocket->readAll();
+
+    int parsedLength = 0;
+    int packageLength = 0;
+    Q_FOREACH(char c, data) {
+        packageLength++;
+        if (c == '
') {
+            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 (mPackages.length() > 0) {
+        Q_EMIT readyRead();
+    } else {
+        qDebug() << "Received incomplete chunk of data, waiting for more";
+    }
+
+    if (mSocket->bytesAvailable() > 0) {
+        QMetaObject::invokeMethod(this, "dataReceived", Qt::QueuedConnection);
+    }
+
+}
diff --git a/kded/backends/lan/landevicelink.h b/kded/backends/lan/socketlinereader.h
similarity index 60%
copy from kded/backends/lan/landevicelink.h
copy to kded/backends/lan/socketlinereader.h
index f40f259..d295607 100644
--- a/kded/backends/lan/landevicelink.h
+++ b/kded/backends/lan/socketlinereader.h
@@ -18,35 +18,44 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef LANDEVICELINK_H
-#define LANDEVICELINK_H
+#ifndef SOCKETLINEREADER_H
+#define SOCKETLINEREADER_H
 
 #include <QObject>
 #include <QString>
+#include <QQueue>
 #include <QTcpSocket>
+#include <QHostAddress>
 
-#include "../devicelink.h"
-
-class AvahiTcpLinkProvider;
-
-class LanDeviceLink
-    : public DeviceLink
+/*
+ * Encapsulates a QTcpSocket and implements the same methods of its API that are
+ * used by LanDeviceLink, but readyRead is emitted only when a newline is found.
+ */
+class SocketLineReader
+    : public QObject
 {
     Q_OBJECT
 
 public:
-    LanDeviceLink(const QString& d, LinkProvider* a, QTcpSocket* socket);
+    SocketLineReader(QTcpSocket* socket, QObject* parent = 0);
 
-    bool sendPackage(NetworkPackage& np);
-    bool sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np);
+    QByteArray readLine() { return mPackages.dequeue(); }
+    qint64 write(const QByteArray& data) { return mSocket->write(data); }
+    QHostAddress peerAddress() { return mSocket->peerAddress(); }
+    qint64 bytesAvailable() { return mPackages.size(); }
+
+Q_SIGNALS:
+    void disconnected();
+    void readyRead();
 
 private Q_SLOTS:
     void dataReceived();
-    void readyRead();
 
 private:
+    QByteArray lastChunk;
     QTcpSocket* mSocket;
+    QQueue<QByteArray> mPackages;
 
 };
 
-#endif // UDPDEVICELINK_H
+#endif

-- 
kdeconnect packaging



More information about the pkg-kde-commits mailing list