Emit connectionLost *after* connectionReceived

To prevent destroying and recreating the same device again
This commit is contained in:
Albert Vaca 2013-08-16 05:35:00 +02:00
parent f194ab9a52
commit 046d01c16d

View file

@ -88,14 +88,6 @@ void BroadcastTcpLinkProvider::newUdpConnection()
return; return;
} }
const QString& id = np->get<QString>("deviceId");
if (links.contains(id)) {
//Delete old link if we already know it, probably it is down if this happens.
qDebug() << "Destroying old link";
delete links[id];
links.remove(id);
}
int tcpPort = np->get<int>("tcpPort",port); int tcpPort = np->get<int>("tcpPort",port);
qDebug() << "Received Udp presentation from" << sender << "asking for a tcp connection on port " << tcpPort; qDebug() << "Received Udp presentation from" << sender << "asking for a tcp connection on port " << tcpPort;
@ -107,6 +99,7 @@ void BroadcastTcpLinkProvider::newUdpConnection()
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError())); connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError()));
socket->connectToHost(sender, tcpPort); socket->connectToHost(sender, tcpPort);
} else { } else {
delete np; delete np;
@ -147,6 +140,9 @@ void BroadcastTcpLinkProvider::connected()
connect(dl,SIGNAL(destroyed(QObject*)),this,SLOT(deviceLinkDestroyed(QObject*))); connect(dl,SIGNAL(destroyed(QObject*)),this,SLOT(deviceLinkDestroyed(QObject*)));
QMap< QString, DeviceLink* >::iterator oldLinkIterator = links.find(id);
DeviceLink* oldLink = (oldLinkIterator == links.end())? 0 : oldLinkIterator.value();
links[id] = dl; links[id] = dl;
NetworkPackage np2(""); NetworkPackage np2("");
@ -165,6 +161,11 @@ void BroadcastTcpLinkProvider::connected()
delete dl; delete dl;
} }
if (oldLink) {
Q_EMIT onConnectionLost(oldLink);
delete oldLink;
}
receivedIdentityPackages.remove(socket); receivedIdentityPackages.remove(socket);
delete np; delete np;
@ -208,11 +209,9 @@ void BroadcastTcpLinkProvider::dataReceived()
connect(dl,SIGNAL(destroyed(QObject*)),this,SLOT(deviceLinkDestroyed(QObject*))); connect(dl,SIGNAL(destroyed(QObject*)),this,SLOT(deviceLinkDestroyed(QObject*)));
if (links.contains(id)) { QMap< QString, DeviceLink* >::iterator oldLinkIterator = links.find(id);
//Delete old link if we already know it, probably it is down if this happens. DeviceLink* oldLink = (oldLinkIterator == links.end())? 0 : oldLinkIterator.value();
qDebug() << "Destroying old link";
delete links[id];
}
links[id] = dl; links[id] = dl;
//qDebug() << "BroadcastTcpLinkProvider creating link to device" << id << "(" << socket->peerAddress() << ")"; //qDebug() << "BroadcastTcpLinkProvider creating link to device" << id << "(" << socket->peerAddress() << ")";
@ -221,6 +220,11 @@ void BroadcastTcpLinkProvider::dataReceived()
Q_EMIT onConnectionReceived(np, dl); Q_EMIT onConnectionReceived(np, dl);
if (oldLink) {
Q_EMIT onConnectionLost(oldLink);
delete oldLink;
}
disconnect(socket,SIGNAL(readyRead()),this,SLOT(dataReceived())); disconnect(socket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
} else { } else {