mirror of
https://github.com/itay-grudev/SingleApplication.git
synced 2025-04-20 20:14:45 +08:00
Improved the connectToPrimary() method and fixed issue with instanceStarted() not getting emitted
This commit is contained in:
parent
eca5803665
commit
3e83f5ce13
@ -85,7 +85,7 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
|
|||||||
abortSafely();
|
abortSafely();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
qCritical() << "SingleApplication: Unable create block.";
|
qCritical() << "SingleApplication: Unable to create block.";
|
||||||
abortSafely();
|
abortSafely();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -238,7 +238,8 @@ bool SingleApplication::sendMessage( const QByteArray &message, int timeout )
|
|||||||
if( isPrimary() ) return false;
|
if( isPrimary() ) return false;
|
||||||
|
|
||||||
// Make sure the socket is connected
|
// Make sure the socket is connected
|
||||||
d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect );
|
if( ! d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect ) )
|
||||||
|
return false;
|
||||||
|
|
||||||
d->socket->write( message );
|
d->socket->write( message );
|
||||||
bool dataWritten = d->socket->waitForBytesWritten( timeout );
|
bool dataWritten = d->socket->waitForBytesWritten( timeout );
|
||||||
@ -254,7 +255,7 @@ void SingleApplication::abortSafely()
|
|||||||
{
|
{
|
||||||
Q_D( SingleApplication );
|
Q_D( SingleApplication );
|
||||||
|
|
||||||
qCritical() << d->memory->errorString();
|
qCritical() << "SingleApplication: " << d->memory->error() << d->memory->errorString();
|
||||||
delete d;
|
delete d;
|
||||||
::exit( EXIT_FAILURE );
|
::exit( EXIT_FAILURE );
|
||||||
}
|
}
|
||||||
|
@ -208,59 +208,68 @@ void SingleApplicationPrivate::startSecondary()
|
|||||||
instanceNumber = inst->secondary;
|
instanceNumber = inst->secondary;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType connectionType )
|
bool SingleApplicationPrivate::connectToPrimary( int timeout, ConnectionType connectionType )
|
||||||
{
|
{
|
||||||
|
QElapsedTimer time;
|
||||||
|
time.start();
|
||||||
|
|
||||||
// Connect to the Local Server of the Primary Instance if not already
|
// Connect to the Local Server of the Primary Instance if not already
|
||||||
// connected.
|
// connected.
|
||||||
if( socket == nullptr ){
|
if( socket == nullptr ){
|
||||||
socket = new QLocalSocket();
|
socket = new QLocalSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If already connected - we are done;
|
if( socket->state() == QLocalSocket::ConnectedState ) return true;
|
||||||
if( socket->state() == QLocalSocket::ConnectedState )
|
|
||||||
return;
|
|
||||||
|
|
||||||
// If not connect
|
if( socket->state() != QLocalSocket::ConnectedState ){
|
||||||
if( socket->state() == QLocalSocket::UnconnectedState ||
|
|
||||||
socket->state() == QLocalSocket::ClosingState ){
|
|
||||||
socket->connectToServer( blockServerName );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for being connected
|
while( true ){
|
||||||
if( socket->state() == QLocalSocket::ConnectingState ){
|
randomSleep();
|
||||||
socket->waitForConnected( msecs );
|
|
||||||
|
if( socket->state() != QLocalSocket::ConnectingState )
|
||||||
|
socket->connectToServer( blockServerName );
|
||||||
|
|
||||||
|
if( socket->state() == QLocalSocket::ConnectingState ){
|
||||||
|
socket->waitForConnected( timeout - time.elapsed() );
|
||||||
|
}
|
||||||
|
|
||||||
|
// If connected break out of the loop
|
||||||
|
if( socket->state() == QLocalSocket::ConnectedState ) break;
|
||||||
|
|
||||||
|
// If elapsed time since start is longer than the method timeout return
|
||||||
|
if( time.elapsed() >= timeout ) return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialisation message according to the SingleApplication protocol
|
// Initialisation message according to the SingleApplication protocol
|
||||||
if( socket->state() == QLocalSocket::ConnectedState ){
|
QByteArray initMsg;
|
||||||
// Notify the parent that a new instance had been started;
|
QDataStream writeStream(&initMsg, QIODevice::WriteOnly);
|
||||||
QByteArray initMsg;
|
|
||||||
QDataStream writeStream(&initMsg, QIODevice::WriteOnly);
|
|
||||||
|
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
||||||
writeStream.setVersion(QDataStream::Qt_5_6);
|
writeStream.setVersion(QDataStream::Qt_5_6);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
writeStream << blockServerName.toLatin1();
|
writeStream << blockServerName.toLatin1();
|
||||||
writeStream << static_cast<quint8>(connectionType);
|
writeStream << static_cast<quint8>(connectionType);
|
||||||
writeStream << instanceNumber;
|
writeStream << instanceNumber;
|
||||||
quint16 checksum = qChecksum(initMsg.constData(), static_cast<quint32>(initMsg.length()));
|
quint16 checksum = qChecksum(initMsg.constData(), static_cast<quint32>(initMsg.length()));
|
||||||
writeStream << checksum;
|
writeStream << checksum;
|
||||||
|
|
||||||
// The header indicates the message length that follows
|
// The header indicates the message length that follows
|
||||||
QByteArray header;
|
QByteArray header;
|
||||||
QDataStream headerStream(&header, QIODevice::WriteOnly);
|
QDataStream headerStream(&header, QIODevice::WriteOnly);
|
||||||
|
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
||||||
headerStream.setVersion(QDataStream::Qt_5_6);
|
headerStream.setVersion(QDataStream::Qt_5_6);
|
||||||
#endif
|
#endif
|
||||||
headerStream << static_cast <quint64>( initMsg.length() );
|
headerStream << static_cast <quint64>( initMsg.length() );
|
||||||
|
|
||||||
socket->write( header );
|
socket->write( header );
|
||||||
socket->write( initMsg );
|
socket->write( initMsg );
|
||||||
socket->flush();
|
socket->flush();
|
||||||
socket->waitForBytesWritten( msecs );
|
if( socket->waitForBytesWritten( timeout - time.elapsed() )) return true;
|
||||||
}
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
quint16 SingleApplicationPrivate::blockChecksum()
|
quint16 SingleApplicationPrivate::blockChecksum()
|
||||||
|
@ -75,7 +75,7 @@ public:
|
|||||||
void initializeMemoryBlock();
|
void initializeMemoryBlock();
|
||||||
void startPrimary();
|
void startPrimary();
|
||||||
void startSecondary();
|
void startSecondary();
|
||||||
void connectToPrimary(int msecs, ConnectionType connectionType );
|
bool connectToPrimary(int msecs, ConnectionType connectionType );
|
||||||
quint16 blockChecksum();
|
quint16 blockChecksum();
|
||||||
qint64 primaryPid();
|
qint64 primaryPid();
|
||||||
QString primaryUser();
|
QString primaryUser();
|
||||||
|
Loading…
Reference in New Issue
Block a user