Coding style and return early refactoring

This commit is contained in:
Àlex Fiestas 2014-04-14 21:45:41 +02:00
parent e3e0eac779
commit 3735ca2cd1
2 changed files with 89 additions and 84 deletions

View file

@ -68,10 +68,13 @@ LanLinkProvider::LanLinkProvider()
void LanLinkProvider::onStart() void LanLinkProvider::onStart()
{ {
mUdpServer->bind(QHostAddress::Broadcast, port, QUdpSocket::ShareAddress); bool buildSucceed = mUdpServer->bind(QHostAddress::Broadcast, port, QUdpSocket::ShareAddress)
Q_ASSERT(buildSucceed);
tcpPort = port; mTcpPort = port;
while (!mTcpServer->listen(QHostAddress::Any, tcpPort)) tcpPort++; while (!mTcpServer->listen(QHostAddress::Any, mTcpPort)) {
mTcpPort++;
}
onNetworkChange(QNetworkSession::Connected); onNetworkChange(QNetworkSession::Connected);
} }
@ -87,12 +90,14 @@ void LanLinkProvider::onNetworkChange(QNetworkSession::State state)
{ {
Q_UNUSED(state); Q_UNUSED(state);
if (!mTcpServer->isListening()) return; if (!mTcpServer->isListening()) {
return;
}
NetworkPackage np(""); NetworkPackage np("");
NetworkPackage::createIdentityPackage(&np); NetworkPackage::createIdentityPackage(&np);
np.set("tcpPort",tcpPort); np.set("tcpPort", mTcpPort);
QUdpSocket().writeDatagram(np.serialize(),QHostAddress("255.255.255.255"), port); mUdpSocket.writeDatagram(np.serialize(), QHostAddress("255.255.255.255"), port);
} }
//I'm the existing device, a new device is kindly introducing itself (I will create a TcpSocket) //I'm the existing device, a new device is kindly introducing itself (I will create a TcpSocket)
@ -109,40 +114,39 @@ void LanLinkProvider::newUdpConnection()
NetworkPackage* np = new NetworkPackage(""); NetworkPackage* np = new NetworkPackage("");
bool success = NetworkPackage::unserialize(datagram,np); bool success = NetworkPackage::unserialize(datagram,np);
if (success && np->type() == PACKAGE_TYPE_IDENTITY) { mUdpServer->readDatagram(datagram.data(), datagram.size(), &sender);
NetworkPackage np2(""); NetworkPackage* receivedPackage = new NetworkPackage("");
NetworkPackage::createIdentityPackage(&np2); bool success = NetworkPackage::unserialize(datagram, receivedPackage);
if (np->get<QString>("deviceId") == np2.get<QString>("deviceId")) {
//kDebug(kdeconnect_kded()) << "Ignoring my own broadcast";
return;
}
int tcpPort = np->get<int>("tcpPort",port);
//kDebug(kdeconnect_kded()) << "Received Udp presentation from" << sender << "asking for a tcp connection on port " << tcpPort;
QTcpSocket* socket = new QTcpSocket(this);
receivedIdentityPackages[socket].np = np;
receivedIdentityPackages[socket].sender = sender;
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError()));
socket->connectToHost(sender, tcpPort);
} else {
delete np;
if (!success || receivedPackage->type() != PACKAGE_TYPE_IDENTITY) {
delete receivedPackage;
} }
}
NetworkPackage np2("");
NetworkPackage::createIdentityPackage(&np2);
if (receivedPackage->get<QString>("deviceId") == np2.get<QString>("deviceId")) {
//kDebug(kdeconnect_kded()) << "Ignoring my own broadcast";
return;
}
int tcpPort = receivedPackage->get<int>("tcpPort", port);
//kDebug(kdeconnect_kded()) << "Received Udp presentation from" << sender << "asking for a tcp connection on port " << tcpPort;
QTcpSocket* socket = new QTcpSocket(this);
receivedIdentityPackages[socket].np = receivedPackage;
receivedIdentityPackages[socket].sender = sender;
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError()));
socket->connectToHost(sender, tcpPort);
}
} }
void LanLinkProvider::connectError() void LanLinkProvider::connectError()
{ {
QTcpSocket* socket = (QTcpSocket*)sender(); QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError())); disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError()));
disconnect(socket, SIGNAL(connected()), this, SLOT(connected())); disconnect(socket, SIGNAL(connected()), this, SLOT(connected()));
@ -150,63 +154,64 @@ void LanLinkProvider::connectError()
kDebug(kdeconnect_kded()) << "Fallback (1), try reverse connection"; kDebug(kdeconnect_kded()) << "Fallback (1), try reverse connection";
NetworkPackage np(""); NetworkPackage np("");
NetworkPackage::createIdentityPackage(&np); NetworkPackage::createIdentityPackage(&np);
np.set("tcpPort",tcpPort); np.set("tcpPort", mTcpPort);
QUdpSocket().writeDatagram(np.serialize(), receivedIdentityPackages[socket].sender, port); mUdpSocket.writeDatagram(np.serialize(), receivedIdentityPackages[socket].sender, port);
} }
void LanLinkProvider::connected() void LanLinkProvider::connected()
{ {
QTcpSocket* socket = (QTcpSocket*)sender(); QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
disconnect(socket, SIGNAL(connected()), this, SLOT(connected())); disconnect(socket, SIGNAL(connected()), this, SLOT(connected()));
disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError())); disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError()));
configureSocket(socket); configureSocket(socket);
NetworkPackage* np = receivedIdentityPackages[socket].np; NetworkPackage* receivedPackage = receivedIdentityPackages[socket].np;
const QString& id = np->get<QString>("deviceId"); const QString& deviceId = receivedPackage->get<QString>("deviceId");
//kDebug(kdeconnect_kded()) << "Connected" << socket->isWritable(); //kDebug(kdeconnect_kded()) << "Connected" << socket->isWritable();
LanDeviceLink* dl = new LanDeviceLink(id, this, socket); LanDeviceLink* deviceLink = new LanDeviceLink(deviceId, this, socket);
NetworkPackage np2(""); NetworkPackage np2("");
NetworkPackage::createIdentityPackage(&np2); NetworkPackage::createIdentityPackage(&np2);
bool success = dl->sendPackage(np2); bool success = deviceLink->sendPackage(np2);
//TODO: Use reverse connection too to preffer connecting a unstable device (a phone) to a stable device (a computer) //TODO: Use reverse connection too to preffer connecting a unstable device (a phone) to a stable device (a computer)
if (success) { if (success) {
//kDebug(kdeconnect_kded()) << "Handshaking done (i'm the existing device)"; //kDebug(kdeconnect_kded()) << "Handshaking done (i'm the existing device)";
connect(dl, SIGNAL(destroyed(QObject*)), connect(deviceLink, SIGNAL(destroyed(QObject*)),
this, SLOT(deviceLinkDestroyed(QObject*))); this, SLOT(deviceLinkDestroyed(QObject*)));
Q_EMIT onConnectionReceived(*np, dl); Q_EMIT onConnectionReceived(*receivedPackage, deviceLink);
QMap< QString, DeviceLink* >::iterator oldLinkIterator = links.find(id); //We kill any possible link from this same device
if (oldLinkIterator != links.end()) { QMap< QString, DeviceLink* >::iterator oldLinkIterator = mLinks.find(deviceId);
if (oldLinkIterator != mLinks.end()) {
DeviceLink* oldLink = oldLinkIterator.value(); DeviceLink* oldLink = oldLinkIterator.value();
disconnect(oldLink, SIGNAL(destroyed(QObject*)), disconnect(oldLink, SIGNAL(destroyed(QObject*)),
this, SLOT(deviceLinkDestroyed(QObject*))); this, SLOT(deviceLinkDestroyed(QObject*)));
oldLink->deleteLater(); oldLink->deleteLater();
links.erase(oldLinkIterator); mLinks.erase(oldLinkIterator);
} }
links[id] = dl; mLinks[deviceId] = deviceLink;
} else { } else {
//I think this will never happen //I think this will never happen
kDebug(kdeconnect_kded()) << "Fallback (2), try reverse connection"; kDebug(kdeconnect_kded()) << "Fallback (2), try reverse connection";
QUdpSocket().writeDatagram(np2.serialize(), receivedIdentityPackages[socket].sender, port); mUdpSocket.writeDatagram(np2.serialize(), receivedIdentityPackages[socket].sender, port);
delete dl; delete deviceLink;
} }
receivedIdentityPackages.remove(socket); receivedIdentityPackages.remove(socket);
delete np; delete receivedPackage;
} }
@ -232,57 +237,56 @@ void LanLinkProvider::newConnection()
//I'm the new device and this is the answer to my UDP introduction (data received) //I'm the new device and this is the answer to my UDP introduction (data received)
void LanLinkProvider::dataReceived() void LanLinkProvider::dataReceived()
{ {
QTcpSocket* socket = (QTcpSocket*) QObject::sender(); QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
configureSocket(socket); configureSocket(socket);
QByteArray data = socket->readLine(); const QByteArray data = socket->readLine();
//kDebug(kdeconnect_kded()) << "LanLinkProvider received reply:" << data; //kDebug(kdeconnect_kded()) << "LanLinkProvider received reply:" << data;
NetworkPackage np(""); NetworkPackage np("");
bool success = NetworkPackage::unserialize(data,&np); bool success = NetworkPackage::unserialize(data, &np);
if (success && np.type() == PACKAGE_TYPE_IDENTITY) { if (!success || np.type() != PACKAGE_TYPE_IDENTITY) {
const QString& id = np.get<QString>("deviceId");
LanDeviceLink* dl = new LanDeviceLink(id, this, socket);
//kDebug(kdeconnect_kded()) << "Handshaking done (i'm the new device)";
connect(dl, SIGNAL(destroyed(QObject*)),
this, SLOT(deviceLinkDestroyed(QObject*)));
Q_EMIT onConnectionReceived(np, dl);
QMap< QString, DeviceLink* >::iterator oldLinkIterator = links.find(id);
if (oldLinkIterator != links.end()) {
DeviceLink* oldLink = oldLinkIterator.value();
disconnect(oldLink, SIGNAL(destroyed(QObject*)),
this, SLOT(deviceLinkDestroyed(QObject*)));
oldLink->deleteLater();
links.erase(oldLinkIterator);
}
links[id] = dl;
disconnect(socket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
} else {
kDebug(kdeconnect_kded()) << "LanLinkProvider/newConnection: Not an identification package (wuh?)"; kDebug(kdeconnect_kded()) << "LanLinkProvider/newConnection: Not an identification package (wuh?)";
return;
} }
const QString& deviceId = np.get<QString>("deviceId");
LanDeviceLink* deviceLink = new LanDeviceLink(deviceId, this, socket);
//kDebug(kdeconnect_kded()) << "Handshaking done (i'm the new device)";
connect(deviceLink, SIGNAL(destroyed(QObject*)),
this, SLOT(deviceLinkDestroyed(QObject*)));
Q_EMIT onConnectionReceived(np, deviceLink);
QMap< QString, DeviceLink* >::iterator oldLinkIterator = mLinks.find(deviceId);
if (oldLinkIterator != mLinks.end()) {
DeviceLink* oldLink = oldLinkIterator.value();
disconnect(oldLink, SIGNAL(destroyed(QObject*)),
this, SLOT(deviceLinkDestroyed(QObject*)));
oldLink->deleteLater();
mLinks.erase(oldLinkIterator);
}
mLinks[deviceId] = deviceLink;
disconnect(socket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
} }
void LanLinkProvider::deviceLinkDestroyed(QObject* uncastedDeviceLink) void LanLinkProvider::deviceLinkDestroyed(QObject* uncastedDeviceLink)
{ {
//kDebug(kdeconnect_kded()) << "deviceLinkDestroyed"; //kDebug(kdeconnect_kded()) << "deviceLinkDestroyed";
DeviceLink* deviceLink = (DeviceLink*)uncastedDeviceLink; DeviceLink* deviceLink = qobject_cast<DeviceLink*>(uncastedDeviceLink);
const QString& id = deviceLink->deviceId(); Q_ASSERT(deviceLink);
QMap< QString, DeviceLink* >::iterator oldLinkIterator = links.find(id); const QString& id = deviceLink->deviceId();
if (oldLinkIterator != links.end() && oldLinkIterator.value() == deviceLink) { QMap< QString, DeviceLink* >::iterator oldLinkIterator = mLinks.find(id);
links.erase(oldLinkIterator); if (oldLinkIterator != mLinks.end() && oldLinkIterator.value() == deviceLink) {
mLinks.erase(oldLinkIterator);
} }
} }

View file

@ -58,10 +58,11 @@ private:
QTcpServer* mTcpServer; QTcpServer* mTcpServer;
QUdpSocket* mUdpServer; QUdpSocket* mUdpServer;
QUdpSocket mUdpSocket;
const static quint16 port = 1714; const static quint16 port = 1714;
quint16 tcpPort; quint16 mTcpPort;
QMap<QString, DeviceLink*> links; QMap<QString, DeviceLink*> mLinks;
struct PendingConnect { struct PendingConnect {
NetworkPackage* np; NetworkPackage* np;