diff options
| author | Itay Grudev <itay@grudev.com> | 2017-01-24 16:54:53 +0000 | 
|---|---|---|
| committer | Itay Grudev <itay@grudev.com> | 2017-01-24 16:54:53 +0000 | 
| commit | 60d30db837930e734683ddb0b69212d8d20c97bf (patch) | |
| tree | 53a857c60a4aa1f432931b8adfc081386f9d8b37 | |
| parent | Fixed typos in CHANGELOG.md (diff) | |
| download | singleapplication-60d30db837930e734683ddb0b69212d8d20c97bf.tar.xz | |
Refactored slotConnectionEstablished()
The code is now shorter and easier to understand. Fixed an
uninitialised variable warning as reported in #20.
| -rw-r--r-- | singleapplication.cpp | 30 | 
1 files 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;  | 
