diff --git a/core/backends/lan/landevicelink.cpp b/core/backends/lan/landevicelink.cpp index a0b14da56..c172dbb3a 100644 --- a/core/backends/lan/landevicelink.cpp +++ b/core/backends/lan/landevicelink.cpp @@ -29,17 +29,26 @@ LanDeviceLink::LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource) : DeviceLink(deviceId, parent, connectionSource) - , mSocketLineReader(new SocketLineReader(socket)) + , mSocketLineReader(nullptr) { - connect(mSocketLineReader, SIGNAL(readyRead()), - this, SLOT(dataReceived())); + reset(socket, connectionSource); +} +void LanDeviceLink::reset(QSslSocket* socket, DeviceLink::ConnectionStarted connectionSource) +{ //We take ownership of the socket. //When the link provider destroys us, //the socket (and the reader) will be //destroyed as well - connect(socket, SIGNAL(disconnected()), - this, SLOT(deleteLater())); + + if (mSocketLineReader) { + delete mSocketLineReader; + } + + mSocketLineReader = new SocketLineReader(socket); + + connect(socket, SIGNAL(disconnected()), this, SLOT(deleteLater())); + connect(mSocketLineReader, SIGNAL(readyRead()), this, SLOT(dataReceived())); mSocketLineReader->setParent(this); socket->setParent(this); } diff --git a/core/backends/lan/landevicelink.h b/core/backends/lan/landevicelink.h index 48c379eed..fa6667ba0 100644 --- a/core/backends/lan/landevicelink.h +++ b/core/backends/lan/landevicelink.h @@ -37,6 +37,7 @@ class LanDeviceLink public: LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource); + void reset(QSslSocket* socket, ConnectionStarted connectionSource); virtual QString name() Q_DECL_OVERRIDE; bool sendPackage(NetworkPackage& np) override; diff --git a/core/backends/lan/lanlinkprovider.cpp b/core/backends/lan/lanlinkprovider.cpp index 2eef1520e..07b39afb7 100644 --- a/core/backends/lan/lanlinkprovider.cpp +++ b/core/backends/lan/lanlinkprovider.cpp @@ -433,24 +433,22 @@ void LanLinkProvider::configureSocket(QSslSocket* socket) void LanLinkProvider::addLink(const QString& deviceId, QSslSocket* socket, NetworkPackage* receivedPackage, DeviceLink::ConnectionStarted connectionOrigin) { - - LanDeviceLink* deviceLink = new LanDeviceLink(deviceId, this, socket, connectionOrigin); - connect(deviceLink, SIGNAL(destroyed(QObject*)), this, SLOT(deviceLinkDestroyed(QObject*))); - // Socket disconnection will now be handled by LanDeviceLink disconnect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater())); - //We kill any possible link from this same device - QMap< QString, DeviceLink* >::iterator oldLinkIterator = mLinks.find(deviceLink->deviceId()); - if (oldLinkIterator != mLinks.end()) { - DeviceLink* oldLink = oldLinkIterator.value(); - disconnect(oldLink, SIGNAL(destroyed(QObject*)), - this, SLOT(deviceLinkDestroyed(QObject*))); - oldLink->deleteLater(); - mLinks.erase(oldLinkIterator); + DeviceLink* deviceLink; + //Do we have a link for this device already? + QMap< QString, LanDeviceLink* >::iterator linkIterator = mLinks.find(deviceLink->deviceId()); + if (linkIterator != mLinks.end()) { + deviceLink = linkIterator.value(); + deviceLink->reset(socket, connectionOrigin); + } else { + deviceLink = new LanDeviceLink(deviceId, this, socket, connectionOrigin); + connect(deviceLink, SIGNAL(destroyed(QObject*)), this, SLOT(deviceLinkDestroyed(QObject*))); + mLinks[deviceId] = deviceLink; } - mLinks[deviceId] = deviceLink; + refreshPairingHandler(deviceId); Q_EMIT onConnectionReceived(*receivedPackage, deviceLink); diff --git a/core/backends/lan/lanlinkprovider.h b/core/backends/lan/lanlinkprovider.h index 40e6afc83..db82e6334 100644 --- a/core/backends/lan/lanlinkprovider.h +++ b/core/backends/lan/lanlinkprovider.h @@ -74,7 +74,7 @@ private: const static quint16 port = 1714; quint16 mTcpPort; - QMap mLinks; + QMap mLinks; QMap mPairingHandlers; struct PendingConnect {