mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-18 13:30:34 +01:00
Update singleApplication.
See : https://github.com/itay-grudev/SingleApplication git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5708 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -91,7 +91,7 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
|
|||||||
|
|
||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
|
|
||||||
// Random sleep here limits the probability of a colision between two racing apps
|
// Random sleep here limits the probability of a collision between two racing apps
|
||||||
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
|
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
|
||||||
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 ) );
|
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 ) );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,7 +101,6 @@ void SingleApplicationPrivate::genBlockServerName()
|
|||||||
// User level block requires a user specific data in the hash
|
// User level block requires a user specific data in the hash
|
||||||
if( options & SingleApplication::Mode::User ) {
|
if( options & SingleApplication::Mode::User ) {
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
// Q_UNUSED(timeout);
|
|
||||||
wchar_t username [ UNLEN + 1 ];
|
wchar_t username [ UNLEN + 1 ];
|
||||||
// Specifies size of the buffer on input
|
// Specifies size of the buffer on input
|
||||||
DWORD usernameLength = UNLEN + 1;
|
DWORD usernameLength = UNLEN + 1;
|
||||||
@@ -110,7 +109,8 @@ void SingleApplicationPrivate::genBlockServerName()
|
|||||||
} else {
|
} else {
|
||||||
appData.addData( QStandardPaths::standardLocations( QStandardPaths::HomeLocation ).join("").toUtf8() );
|
appData.addData( QStandardPaths::standardLocations( QStandardPaths::HomeLocation ).join("").toUtf8() );
|
||||||
}
|
}
|
||||||
#else
|
#endif
|
||||||
|
#ifdef Q_OS_UNIX
|
||||||
QProcess process;
|
QProcess process;
|
||||||
process.start( "whoami" );
|
process.start( "whoami" );
|
||||||
if( process.waitForFinished( 100 ) &&
|
if( process.waitForFinished( 100 ) &&
|
||||||
@@ -207,7 +207,11 @@ void SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType conne
|
|||||||
// Notify the parent that a new instance had been started;
|
// Notify the parent that a new instance had been started;
|
||||||
QByteArray initMsg;
|
QByteArray initMsg;
|
||||||
QDataStream writeStream(&initMsg, QIODevice::WriteOnly);
|
QDataStream writeStream(&initMsg, QIODevice::WriteOnly);
|
||||||
writeStream.setVersion(QDataStream::Qt_5_5);
|
|
||||||
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
||||||
|
writeStream.setVersion(QDataStream::Qt_5_6);
|
||||||
|
#endif
|
||||||
|
|
||||||
writeStream << blockServerName.toLatin1();
|
writeStream << blockServerName.toLatin1();
|
||||||
writeStream << static_cast<quint8>(connectionType);
|
writeStream << static_cast<quint8>(connectionType);
|
||||||
writeStream << instanceNumber;
|
writeStream << instanceNumber;
|
||||||
@@ -217,7 +221,10 @@ void SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType conne
|
|||||||
// 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);
|
||||||
headerStream.setVersion(QDataStream::Qt_5_5);
|
|
||||||
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
||||||
|
headerStream.setVersion(QDataStream::Qt_5_6);
|
||||||
|
#endif
|
||||||
headerStream << static_cast <quint64>( initMsg.length() );
|
headerStream << static_cast <quint64>( initMsg.length() );
|
||||||
|
|
||||||
socket->write( header );
|
socket->write( header );
|
||||||
@@ -252,85 +259,133 @@ qint64 SingleApplicationPrivate::primaryPid()
|
|||||||
*/
|
*/
|
||||||
void SingleApplicationPrivate::slotConnectionEstablished()
|
void SingleApplicationPrivate::slotConnectionEstablished()
|
||||||
{
|
{
|
||||||
Q_Q(SingleApplication);
|
|
||||||
|
|
||||||
QLocalSocket *nextConnSocket = server->nextPendingConnection();
|
QLocalSocket *nextConnSocket = server->nextPendingConnection();
|
||||||
|
connectionMap.insert(nextConnSocket, ConnectionInfo());
|
||||||
|
|
||||||
quint32 instanceId = 0;
|
QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose,
|
||||||
ConnectionType connectionType = InvalidConnection;
|
[nextConnSocket, this]() {
|
||||||
if( nextConnSocket->waitForReadyRead( 100 ) ) {
|
auto &info = connectionMap[nextConnSocket];
|
||||||
// read the fields in same order and format as written
|
Q_EMIT this->slotClientConnectionClosed( nextConnSocket, info.instanceId );
|
||||||
QDataStream headerStream(nextConnSocket);
|
|
||||||
headerStream.setVersion( QDataStream::Qt_5_5 );
|
|
||||||
|
|
||||||
// Read the header to know the message length
|
|
||||||
quint64 msgLen = 0;
|
|
||||||
headerStream >> msgLen;
|
|
||||||
|
|
||||||
if( msgLen >= sizeof( quint16 ) ) {
|
|
||||||
// Read the message body
|
|
||||||
QByteArray msgBytes = nextConnSocket->read(msgLen);
|
|
||||||
QDataStream readStream(msgBytes);
|
|
||||||
readStream.setVersion( QDataStream::Qt_5_5 );
|
|
||||||
|
|
||||||
// server name
|
|
||||||
QByteArray latin1Name;
|
|
||||||
readStream >> latin1Name;
|
|
||||||
|
|
||||||
// connection type
|
|
||||||
quint8 connType = InvalidConnection;
|
|
||||||
readStream >> connType;
|
|
||||||
connectionType = static_cast <ConnectionType>( connType );
|
|
||||||
|
|
||||||
// instance id
|
|
||||||
readStream >> instanceId;
|
|
||||||
|
|
||||||
// checksum
|
|
||||||
quint16 msgChecksum = 0;
|
|
||||||
readStream >> msgChecksum;
|
|
||||||
|
|
||||||
const quint16 actualChecksum = qChecksum(msgBytes.constData(), static_cast<quint32>(msgBytes.length() - sizeof(quint16)));
|
|
||||||
|
|
||||||
if (readStream.status() != QDataStream::Ok || QLatin1String(latin1Name) != blockServerName || msgChecksum != actualChecksum) {
|
|
||||||
connectionType = InvalidConnection;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
);
|
||||||
|
|
||||||
if( connectionType == InvalidConnection ) {
|
QObject::connect(nextConnSocket, &QLocalSocket::disconnected,
|
||||||
nextConnSocket->close();
|
[nextConnSocket, this](){
|
||||||
delete nextConnSocket;
|
connectionMap.remove(nextConnSocket);
|
||||||
|
nextConnSocket->deleteLater();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
QObject::connect(nextConnSocket, &QLocalSocket::readyRead,
|
||||||
|
[nextConnSocket, this]() {
|
||||||
|
auto &info = connectionMap[nextConnSocket];
|
||||||
|
switch(info.stage) {
|
||||||
|
case StageHeader:
|
||||||
|
readInitMessageHeader(nextConnSocket);
|
||||||
|
break;
|
||||||
|
case StageBody:
|
||||||
|
readInitMessageBody(nextConnSocket);
|
||||||
|
break;
|
||||||
|
case StageConnected:
|
||||||
|
Q_EMIT this->slotDataAvailable( nextConnSocket, info.instanceId );
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SingleApplicationPrivate::readInitMessageHeader( QLocalSocket *sock )
|
||||||
|
{
|
||||||
|
if (!connectionMap.contains( sock )) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QObject::connect(
|
if( sock->bytesAvailable() < ( qint64 )sizeof( quint64 ) ) {
|
||||||
nextConnSocket,
|
return;
|
||||||
&QLocalSocket::aboutToClose,
|
}
|
||||||
this,
|
|
||||||
[nextConnSocket, instanceId, this]() {
|
|
||||||
Q_EMIT this->slotClientConnectionClosed( nextConnSocket, instanceId );
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
QObject::connect(
|
QDataStream headerStream( sock );
|
||||||
nextConnSocket,
|
|
||||||
&QLocalSocket::readyRead,
|
|
||||||
this,
|
|
||||||
[nextConnSocket, instanceId, this]() {
|
|
||||||
Q_EMIT this->slotDataAvailable( nextConnSocket, instanceId );
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if( connectionType == NewInstance || (
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
||||||
connectionType == SecondaryInstance &&
|
headerStream.setVersion( QDataStream::Qt_5_6 );
|
||||||
options & SingleApplication::Mode::SecondaryNotification
|
#endif
|
||||||
)
|
|
||||||
) {
|
// Read the header to know the message length
|
||||||
|
quint64 msgLen = 0;
|
||||||
|
headerStream >> msgLen;
|
||||||
|
ConnectionInfo &info = connectionMap[sock];
|
||||||
|
info.stage = StageBody;
|
||||||
|
info.msgLen = msgLen;
|
||||||
|
|
||||||
|
if ( sock->bytesAvailable() >= (qint64) msgLen ) {
|
||||||
|
readInitMessageBody( sock );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
||||||
|
{
|
||||||
|
Q_Q(SingleApplication);
|
||||||
|
|
||||||
|
if (!connectionMap.contains( sock )) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ConnectionInfo &info = connectionMap[sock];
|
||||||
|
if( sock->bytesAvailable() < ( qint64 )info.msgLen ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the message body
|
||||||
|
QByteArray msgBytes = sock->read(info.msgLen);
|
||||||
|
QDataStream readStream(msgBytes);
|
||||||
|
|
||||||
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
||||||
|
readStream.setVersion( QDataStream::Qt_5_6 );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// server name
|
||||||
|
QByteArray latin1Name;
|
||||||
|
readStream >> latin1Name;
|
||||||
|
|
||||||
|
// connection type
|
||||||
|
ConnectionType connectionType = InvalidConnection;
|
||||||
|
quint8 connTypeVal = InvalidConnection;
|
||||||
|
readStream >> connTypeVal;
|
||||||
|
connectionType = static_cast <ConnectionType>( connTypeVal );
|
||||||
|
|
||||||
|
// instance id
|
||||||
|
quint32 instanceId = 0;
|
||||||
|
readStream >> instanceId;
|
||||||
|
|
||||||
|
// checksum
|
||||||
|
quint16 msgChecksum = 0;
|
||||||
|
readStream >> msgChecksum;
|
||||||
|
|
||||||
|
const quint16 actualChecksum = qChecksum( msgBytes.constData(), static_cast<quint32>( msgBytes.length() - sizeof( quint16 ) ) );
|
||||||
|
|
||||||
|
bool isValid = readStream.status() == QDataStream::Ok &&
|
||||||
|
QLatin1String(latin1Name) == blockServerName &&
|
||||||
|
msgChecksum == actualChecksum;
|
||||||
|
|
||||||
|
if( !isValid ) {
|
||||||
|
sock->close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
info.instanceId = instanceId;
|
||||||
|
info.stage = StageConnected;
|
||||||
|
|
||||||
|
if( connectionType == NewInstance ||
|
||||||
|
( connectionType == SecondaryInstance &&
|
||||||
|
options & SingleApplication::Mode::SecondaryNotification ) )
|
||||||
|
{
|
||||||
Q_EMIT q->instanceStarted();
|
Q_EMIT q->instanceStarted();
|
||||||
}
|
}
|
||||||
|
|
||||||
if( nextConnSocket->bytesAvailable() > 0 ) {
|
if (sock->bytesAvailable() > 0) {
|
||||||
Q_EMIT this->slotDataAvailable( nextConnSocket, instanceId );
|
Q_EMIT this->slotDataAvailable( sock, instanceId );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -344,5 +399,4 @@ void SingleApplicationPrivate::slotClientConnectionClosed( QLocalSocket *closedS
|
|||||||
{
|
{
|
||||||
if( closedSocket->bytesAvailable() > 0 )
|
if( closedSocket->bytesAvailable() > 0 )
|
||||||
Q_EMIT slotDataAvailable( closedSocket, instanceId );
|
Q_EMIT slotDataAvailable( closedSocket, instanceId );
|
||||||
closedSocket->deleteLater();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,14 @@ struct InstancesInfo {
|
|||||||
quint16 checksum;
|
quint16 checksum;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ConnectionInfo {
|
||||||
|
explicit ConnectionInfo() :
|
||||||
|
msgLen(0), instanceId(0), stage(0) {}
|
||||||
|
qint64 msgLen;
|
||||||
|
quint32 instanceId;
|
||||||
|
quint8 stage;
|
||||||
|
};
|
||||||
|
|
||||||
class SingleApplicationPrivate : public QObject {
|
class SingleApplicationPrivate : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@@ -53,6 +61,11 @@ public:
|
|||||||
SecondaryInstance = 2,
|
SecondaryInstance = 2,
|
||||||
Reconnect = 3
|
Reconnect = 3
|
||||||
};
|
};
|
||||||
|
enum ConnectionStage : quint8 {
|
||||||
|
StageHeader = 0,
|
||||||
|
StageBody = 1,
|
||||||
|
StageConnected = 2,
|
||||||
|
};
|
||||||
Q_DECLARE_PUBLIC(SingleApplication)
|
Q_DECLARE_PUBLIC(SingleApplication)
|
||||||
|
|
||||||
SingleApplicationPrivate( SingleApplication *q_ptr );
|
SingleApplicationPrivate( SingleApplication *q_ptr );
|
||||||
@@ -65,6 +78,8 @@ public:
|
|||||||
void connectToPrimary(int msecs, ConnectionType connectionType );
|
void connectToPrimary(int msecs, ConnectionType connectionType );
|
||||||
quint16 blockChecksum();
|
quint16 blockChecksum();
|
||||||
qint64 primaryPid();
|
qint64 primaryPid();
|
||||||
|
void readInitMessageHeader(QLocalSocket *socket);
|
||||||
|
void readInitMessageBody(QLocalSocket *socket);
|
||||||
|
|
||||||
SingleApplication *q_ptr;
|
SingleApplication *q_ptr;
|
||||||
QSharedMemory *memory;
|
QSharedMemory *memory;
|
||||||
@@ -73,6 +88,7 @@ public:
|
|||||||
quint32 instanceNumber;
|
quint32 instanceNumber;
|
||||||
QString blockServerName;
|
QString blockServerName;
|
||||||
SingleApplication::Options options;
|
SingleApplication::Options options;
|
||||||
|
QMap<QLocalSocket*, ConnectionInfo> connectionMap;
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
void slotConnectionEstablished();
|
void slotConnectionEstablished();
|
||||||
|
|||||||
Reference in New Issue
Block a user