From 60d30db837930e734683ddb0b69212d8d20c97bf Mon Sep 17 00:00:00 2001 From: Itay Grudev Date: Tue, 24 Jan 2017 16:54:53 +0000 Subject: Refactored slotConnectionEstablished() The code is now shorter and easier to understand. Fixed an uninitialised variable warning as reported in #20. --- singleapplication.cpp | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/singleapplication.cpp b/singleapplication.cpp index 7070973..7d77292 100644 --- a/singleapplication.cpp +++ b/singleapplication.cpp @@ -264,21 +264,23 @@ void SingleApplicationPrivate::slotConnectionEstablished() QLocalSocket *socket = server->nextPendingConnection(); // Verify that the new connection follows the SingleApplication protocol - char connectionType; + char connectionType = '\0'; // Invalid connection quint32 instanceId; QByteArray initMsg, tmp; - bool invalidConnection = false; if( socket->waitForReadyRead( 100 ) ) { tmp = socket->read( blockServerName.length() ); // Verify that the socket data start with blockServerName if( tmp == blockServerName.toLatin1() ) { initMsg = tmp; - tmp = socket->read(1); // Verify that the next charecter is N/S/R (connecion type) // Stands for New Instance/Secondary Instance/Reconnect - if( tmp == "N" || tmp == "S" || tmp == "R" ) { - connectionType = tmp.at(0); - initMsg += tmp; + connectionType = socket->read( 1 )[0]; + switch( connectionType ) { + case 'N': + case 'S': + case 'R': + { + initMsg += connectionType; tmp = socket->read( sizeof(quint32) ); const char * data = tmp.constData(); instanceId = (quint32)*data; @@ -289,20 +291,16 @@ void SingleApplicationPrivate::slotConnectionEstablished() 256 ); tmp = socket->read( checksum.length() ); - if( checksum != tmp ) { - invalidConnection = true; - } - } else { - invalidConnection = true; + if( checksum == tmp ) + break; // Otherwise set to invalid connection (next line) + } + default: + connectionType = '\0'; // Invalid connection } - } else { - invalidConnection = true; } - } else { - invalidConnection = true; } - if( invalidConnection ) { + if( connectionType == '\0' ) { socket->close(); delete socket; return; -- cgit v1.2.1