From 26b0ec8b98d05b521ec63840a5ea56a2c5c3f4f0 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Wed, 9 Sep 2015 12:30:39 +0200 Subject: [PATCH] Make sure we don't disconnect from discovering remotes Otherwise it's hard to let other devices connect to us, without having both devices in discoverability mode. Reviewed by Albert Vaca --- core/backends/devicelink.cpp | 3 ++- core/backends/devicelink.h | 9 ++++++++- core/backends/lan/landevicelink.cpp | 4 ++-- core/backends/lan/landevicelink.h | 2 +- core/backends/lan/lanlinkprovider.cpp | 6 +++--- core/backends/loopback/loopbackdevicelink.cpp | 2 +- core/daemon.cpp | 6 ++++-- 7 files changed, 21 insertions(+), 11 deletions(-) diff --git a/core/backends/devicelink.cpp b/core/backends/devicelink.cpp index 08a3f8387..bb190fd8d 100644 --- a/core/backends/devicelink.cpp +++ b/core/backends/devicelink.cpp @@ -22,10 +22,11 @@ #include "kdeconnectconfig.h" #include "linkprovider.h" -DeviceLink::DeviceLink(const QString& deviceId, LinkProvider* parent) +DeviceLink::DeviceLink(const QString& deviceId, LinkProvider* parent, ConnectionStarted connectionSource) : QObject(parent) , mPrivateKey(KdeConnectConfig::instance()->privateKey()) , mDeviceId(deviceId) + , mConnectionSource(connectionSource) , mLinkProvider(parent) { Q_ASSERT(!deviceId.isEmpty()); diff --git a/core/backends/devicelink.h b/core/backends/devicelink.h index af17662f0..158db8d29 100644 --- a/core/backends/devicelink.h +++ b/core/backends/devicelink.h @@ -35,7 +35,9 @@ class DeviceLink Q_OBJECT public: - DeviceLink(const QString& deviceId, LinkProvider* parent); + enum ConnectionStarted : bool { Locally, Remotely }; + + DeviceLink(const QString& deviceId, LinkProvider* parent, ConnectionStarted connectionSource); virtual ~DeviceLink() { }; const QString& deviceId() { return mDeviceId; } @@ -44,6 +46,10 @@ public: virtual bool sendPackage(NetworkPackage& np) = 0; virtual bool sendPackageEncrypted(QCA::PublicKey& publicKey, NetworkPackage& np) = 0; + ConnectionStarted connectionSource() const { + return mConnectionSource; + } + Q_SIGNALS: void receivedPackage(const NetworkPackage& np); @@ -52,6 +58,7 @@ protected: private: const QString mDeviceId; + const ConnectionStarted mConnectionSource; LinkProvider* mLinkProvider; }; diff --git a/core/backends/lan/landevicelink.cpp b/core/backends/lan/landevicelink.cpp index 229038209..10273fe50 100644 --- a/core/backends/lan/landevicelink.cpp +++ b/core/backends/lan/landevicelink.cpp @@ -31,8 +31,8 @@ #include "downloadjob.h" #include "socketlinereader.h" -LanDeviceLink::LanDeviceLink(const QString& deviceId, LinkProvider* parent, QTcpSocket* socket) - : DeviceLink(deviceId, parent) +LanDeviceLink::LanDeviceLink(const QString& deviceId, LinkProvider* parent, QTcpSocket* socket, ConnectionStarted connectionSource) + : DeviceLink(deviceId, parent, connectionSource) , mSocketLineReader(new SocketLineReader(socket)) { connect(mSocketLineReader, SIGNAL(readyRead()), diff --git a/core/backends/lan/landevicelink.h b/core/backends/lan/landevicelink.h index b16daa27f..35a83ebc6 100644 --- a/core/backends/lan/landevicelink.h +++ b/core/backends/lan/landevicelink.h @@ -35,7 +35,7 @@ class LanDeviceLink Q_OBJECT public: - LanDeviceLink(const QString& deviceId, LinkProvider* parent, QTcpSocket* socket); + LanDeviceLink(const QString& deviceId, LinkProvider* parent, QTcpSocket* socket, ConnectionStarted connectionSource); bool sendPackage(NetworkPackage& np) override; bool sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np) override; diff --git a/core/backends/lan/lanlinkprovider.cpp b/core/backends/lan/lanlinkprovider.cpp index 85a19040d..8aee7488e 100644 --- a/core/backends/lan/lanlinkprovider.cpp +++ b/core/backends/lan/lanlinkprovider.cpp @@ -106,7 +106,7 @@ void LanLinkProvider::onNetworkChange() //I'm the existing device, a new device is kindly introducing itself. //I will create a TcpSocket and try to connect. This can result in either connected() or connectError(). -void LanLinkProvider::newUdpConnection() +void LanLinkProvider::newUdpConnection() //udpBroadcastReceived { while (mUdpServer->hasPendingDatagrams()) { QByteArray datagram; @@ -174,8 +174,8 @@ void LanLinkProvider::connected() NetworkPackage* receivedPackage = receivedIdentityPackages[socket].np; const QString& deviceId = receivedPackage->get("deviceId"); //qCDebug(KDECONNECT_CORE) << "Connected" << socket->isWritable(); + LanDeviceLink* deviceLink = new LanDeviceLink(deviceId, this, socket, DeviceLink::Remotely); - LanDeviceLink* deviceLink = new LanDeviceLink(deviceId, this, socket); NetworkPackage np2(""); NetworkPackage::createIdentityPackage(&np2); @@ -262,7 +262,7 @@ void LanLinkProvider::dataReceived() socket, SLOT(deleteLater())); const QString& deviceId = np.get("deviceId"); - LanDeviceLink* deviceLink = new LanDeviceLink(deviceId, this, socket); + LanDeviceLink* deviceLink = new LanDeviceLink(deviceId, this, socket, DeviceLink::Locally); connect(deviceLink, SIGNAL(destroyed(QObject*)), this, SLOT(deviceLinkDestroyed(QObject*))); diff --git a/core/backends/loopback/loopbackdevicelink.cpp b/core/backends/loopback/loopbackdevicelink.cpp index ac174d4b1..f97533037 100644 --- a/core/backends/loopback/loopbackdevicelink.cpp +++ b/core/backends/loopback/loopbackdevicelink.cpp @@ -23,7 +23,7 @@ #include "loopbacklinkprovider.h" LoopbackDeviceLink::LoopbackDeviceLink(const QString& deviceId, LoopbackLinkProvider* provider) - : DeviceLink(deviceId, provider) + : DeviceLink(deviceId, provider, Remotely) { } diff --git a/core/daemon.cpp b/core/daemon.cpp index ec971665e..99cff722d 100644 --- a/core/daemon.cpp +++ b/core/daemon.cpp @@ -163,8 +163,10 @@ void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink* Device* device = new Device(this, identityPackage, dl); //qCDebug(KDECONNECT_CORE) << "It is a new device"; - if (d->discoveryMode && !device->isPaired()) { - device->deleteLater(); + //we discard the connections that we created but it's not paired. + //we keep the remotely initiated ones, since the remotes require them + if (!isDiscoveringDevices() && !device->isPaired() && dl->connectionSource() == DeviceLink::ConnectionStarted::Locally) { + dl->deleteLater(); } else { connect(device, SIGNAL(reachableStatusChanged()), this, SLOT(onDeviceStatusChanged())); connect(device, SIGNAL(pairingChanged(bool)), this, SLOT(onDeviceStatusChanged()));