| 222 | } |
| 223 | |
| 224 | bool SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType connectionType ) |
| 225 | { |
| 226 | QElapsedTimer time; |
| 227 | time.start(); |
| 228 | |
| 229 | // Connect to the Local Server of the Primary Instance if not already |
| 230 | // connected. |
| 231 | if( socket == nullptr ){ |
| 232 | socket = new QLocalSocket(); |
| 233 | } |
| 234 | |
| 235 | if( socket->state() == QLocalSocket::ConnectedState ) return true; |
| 236 | |
| 237 | if( socket->state() != QLocalSocket::ConnectedState ){ |
| 238 | |
| 239 | while( true ){ |
| 240 | randomSleep(); |
| 241 | |
| 242 | if( socket->state() != QLocalSocket::ConnectingState ) |
| 243 | socket->connectToServer( blockServerName ); |
| 244 | |
| 245 | if( socket->state() == QLocalSocket::ConnectingState ){ |
| 246 | socket->waitForConnected( static_cast<int>(msecs - time.elapsed()) ); |
| 247 | } |
| 248 | |
| 249 | // If connected break out of the loop |
| 250 | if( socket->state() == QLocalSocket::ConnectedState ) break; |
| 251 | |
| 252 | // If elapsed time since start is longer than the method timeout return |
| 253 | if( time.elapsed() >= msecs ) return false; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Initialisation message according to the SingleApplication protocol |
| 258 | QByteArray initMsg; |
| 259 | QDataStream writeStream(&initMsg, QIODevice::WriteOnly); |
| 260 | |
| 261 | #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) |
| 262 | writeStream.setVersion(QDataStream::Qt_5_6); |
| 263 | #endif |
| 264 | |
| 265 | writeStream << blockServerName.toLatin1(); |
| 266 | writeStream << static_cast<quint8>(connectionType); |
| 267 | writeStream << instanceNumber; |
| 268 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 269 | quint16 checksum = qChecksum(QByteArray(initMsg, static_cast<quint32>(initMsg.length()))); |
| 270 | #else |
| 271 | quint16 checksum = qChecksum(initMsg.constData(), static_cast<quint32>(initMsg.length())); |
| 272 | #endif |
| 273 | writeStream << checksum; |
| 274 | |
| 275 | return writeConfirmedMessage( static_cast<int>(msecs - time.elapsed()), initMsg ); |
| 276 | } |
| 277 | |
| 278 | void SingleApplicationPrivate::writeAck( QLocalSocket *sock ) { |
| 279 | sock->putChar('\n'); |
no test coverage detected