mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-22 17:50:52 +01:00
Fix indentation code
This commit is contained in:
@@ -40,103 +40,103 @@
|
|||||||
* @param {bool} allowSecondaryInstances
|
* @param {bool} allowSecondaryInstances
|
||||||
*/
|
*/
|
||||||
SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSecondary, Options options, int timeout )
|
SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSecondary, Options options, int timeout )
|
||||||
: app_t( argc, argv ), d_ptr( new SingleApplicationPrivate( this ) )
|
: app_t( argc, argv ), d_ptr( new SingleApplicationPrivate( this ) )
|
||||||
{
|
{
|
||||||
Q_D(SingleApplication);
|
Q_D(SingleApplication);
|
||||||
|
|
||||||
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
|
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
|
||||||
// On Android and iOS since the library is not supported fallback to
|
// On Android and iOS since the library is not supported fallback to
|
||||||
// standard QApplication behaviour by simply returning at this point.
|
// standard QApplication behaviour by simply returning at this point.
|
||||||
qWarning() << "SingleApplication is not supported on Android and iOS systems.";
|
qWarning() << "SingleApplication is not supported on Android and iOS systems.";
|
||||||
return;
|
return;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Store the current mode of the program
|
// Store the current mode of the program
|
||||||
d->options = options;
|
d->options = options;
|
||||||
|
|
||||||
// Generating an application ID used for identifying the shared memory
|
// Generating an application ID used for identifying the shared memory
|
||||||
// block and QLocalServer
|
// block and QLocalServer
|
||||||
d->genBlockServerName();
|
d->genBlockServerName();
|
||||||
|
|
||||||
#ifdef Q_OS_UNIX
|
#ifdef Q_OS_UNIX
|
||||||
// By explicitly attaching it and then deleting it we make sure that the
|
// By explicitly attaching it and then deleting it we make sure that the
|
||||||
// memory is deleted even after the process has crashed on Unix.
|
// memory is deleted even after the process has crashed on Unix.
|
||||||
d->memory = new QSharedMemory( d->blockServerName );
|
d->memory = new QSharedMemory( d->blockServerName );
|
||||||
d->memory->attach();
|
d->memory->attach();
|
||||||
delete d->memory;
|
delete d->memory;
|
||||||
#endif
|
#endif
|
||||||
// Guarantee thread safe behaviour with a shared memory block.
|
// Guarantee thread safe behaviour with a shared memory block.
|
||||||
d->memory = new QSharedMemory( d->blockServerName );
|
d->memory = new QSharedMemory( d->blockServerName );
|
||||||
|
|
||||||
// Create a shared memory block
|
// Create a shared memory block
|
||||||
if( d->memory->create( sizeof( InstancesInfo ) ) ) {
|
if( d->memory->create( sizeof( InstancesInfo ) ) ) {
|
||||||
// Initialize the shared memory block
|
// Initialize the shared memory block
|
||||||
d->memory->lock();
|
d->memory->lock();
|
||||||
d->initializeMemoryBlock();
|
d->initializeMemoryBlock();
|
||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
} else {
|
} else {
|
||||||
// Attempt to attach to the memory segment
|
// Attempt to attach to the memory segment
|
||||||
if( ! d->memory->attach() ) {
|
if( ! d->memory->attach() ) {
|
||||||
qCritical() << "SingleApplication: Unable to attach to shared memory block.";
|
qCritical() << "SingleApplication: Unable to attach to shared memory block.";
|
||||||
qCritical() << d->memory->errorString();
|
qCritical() << d->memory->errorString();
|
||||||
delete d;
|
delete d;
|
||||||
::exit( EXIT_FAILURE );
|
::exit( EXIT_FAILURE );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
InstancesInfo* inst = static_cast<InstancesInfo*>( d->memory->data() );
|
InstancesInfo* inst = static_cast<InstancesInfo*>( d->memory->data() );
|
||||||
QElapsedTimer time;
|
QElapsedTimer time;
|
||||||
time.start();
|
time.start();
|
||||||
|
|
||||||
// Make sure the shared memory block is initialised and in consistent state
|
// Make sure the shared memory block is initialised and in consistent state
|
||||||
while( true ) {
|
while( true ) {
|
||||||
d->memory->lock();
|
d->memory->lock();
|
||||||
|
|
||||||
if( d->blockChecksum() == inst->checksum ) break;
|
if( d->blockChecksum() == inst->checksum ) break;
|
||||||
|
|
||||||
if( time.elapsed() > 5000 ) {
|
if( time.elapsed() > 5000 ) {
|
||||||
qWarning() << "SingleApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure.";
|
qWarning() << "SingleApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure.";
|
||||||
d->initializeMemoryBlock();
|
d->initializeMemoryBlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
|
|
||||||
// Random sleep here limits the probability of a collision between two racing apps
|
// Random sleep here limits the probability of a collision between two racing apps
|
||||||
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) // ### Qt 6: remove
|
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) // ### Qt 6: remove
|
||||||
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 ) );
|
||||||
#else
|
#else
|
||||||
quint32 value = QRandomGenerator::global()->generate();
|
quint32 value = QRandomGenerator::global()->generate();
|
||||||
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( value ) / RAND_MAX * 10 ) );
|
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( value ) / RAND_MAX * 10 ) );
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if( inst->primary == false) {
|
if( inst->primary == false) {
|
||||||
d->startPrimary();
|
d->startPrimary();
|
||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if another instance can be started
|
// Check if another instance can be started
|
||||||
if( allowSecondary ) {
|
if( allowSecondary ) {
|
||||||
inst->secondary += 1;
|
inst->secondary += 1;
|
||||||
inst->checksum = d->blockChecksum();
|
inst->checksum = d->blockChecksum();
|
||||||
d->instanceNumber = inst->secondary;
|
d->instanceNumber = inst->secondary;
|
||||||
d->startSecondary();
|
d->startSecondary();
|
||||||
if( d->options & Mode::SecondaryNotification ) {
|
if( d->options & Mode::SecondaryNotification ) {
|
||||||
d->connectToPrimary( timeout, SingleApplicationPrivate::SecondaryInstance );
|
d->connectToPrimary( timeout, SingleApplicationPrivate::SecondaryInstance );
|
||||||
}
|
}
|
||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
d->memory->unlock();
|
d->memory->unlock();
|
||||||
|
|
||||||
d->connectToPrimary( timeout, SingleApplicationPrivate::NewInstance );
|
d->connectToPrimary( timeout, SingleApplicationPrivate::NewInstance );
|
||||||
|
|
||||||
delete d;
|
delete d;
|
||||||
|
|
||||||
::exit( EXIT_SUCCESS );
|
::exit( EXIT_SUCCESS );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -144,46 +144,46 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda
|
|||||||
*/
|
*/
|
||||||
SingleApplication::~SingleApplication()
|
SingleApplication::~SingleApplication()
|
||||||
{
|
{
|
||||||
Q_D(SingleApplication);
|
Q_D(SingleApplication);
|
||||||
delete d;
|
delete d;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SingleApplication::isPrimary()
|
bool SingleApplication::isPrimary()
|
||||||
{
|
{
|
||||||
Q_D(SingleApplication);
|
Q_D(SingleApplication);
|
||||||
return d->server != nullptr;
|
return d->server != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SingleApplication::isSecondary()
|
bool SingleApplication::isSecondary()
|
||||||
{
|
{
|
||||||
Q_D(SingleApplication);
|
Q_D(SingleApplication);
|
||||||
return d->server == nullptr;
|
return d->server == nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
quint32 SingleApplication::instanceId()
|
quint32 SingleApplication::instanceId()
|
||||||
{
|
{
|
||||||
Q_D(SingleApplication);
|
Q_D(SingleApplication);
|
||||||
return d->instanceNumber;
|
return d->instanceNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
qint64 SingleApplication::primaryPid()
|
qint64 SingleApplication::primaryPid()
|
||||||
{
|
{
|
||||||
Q_D(SingleApplication);
|
Q_D(SingleApplication);
|
||||||
return d->primaryPid();
|
return d->primaryPid();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SingleApplication::sendMessage( QByteArray message, int timeout )
|
bool SingleApplication::sendMessage( QByteArray message, int timeout )
|
||||||
{
|
{
|
||||||
Q_D(SingleApplication);
|
Q_D(SingleApplication);
|
||||||
|
|
||||||
// Nobody to connect to
|
// Nobody to connect to
|
||||||
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 );
|
d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect );
|
||||||
|
|
||||||
d->socket->write( message );
|
d->socket->write( message );
|
||||||
bool dataWritten = d->socket->waitForBytesWritten( timeout );
|
bool dataWritten = d->socket->waitForBytesWritten( timeout );
|
||||||
d->socket->flush();
|
d->socket->flush();
|
||||||
return dataWritten;
|
return dataWritten;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,133 +49,133 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <lmcons.h>
|
#include <lmcons.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
SingleApplicationPrivate::SingleApplicationPrivate( SingleApplication *q_ptr )
|
SingleApplicationPrivate::SingleApplicationPrivate( SingleApplication *q_ptr )
|
||||||
: q_ptr( q_ptr )
|
: q_ptr( q_ptr )
|
||||||
{
|
{
|
||||||
server = nullptr;
|
server = nullptr;
|
||||||
socket = nullptr;
|
socket = nullptr;
|
||||||
memory = nullptr;
|
memory = nullptr;
|
||||||
instanceNumber = -1;
|
instanceNumber = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
SingleApplicationPrivate::~SingleApplicationPrivate()
|
SingleApplicationPrivate::~SingleApplicationPrivate()
|
||||||
{
|
{
|
||||||
if( socket != nullptr ) {
|
if( socket != nullptr ) {
|
||||||
socket->close();
|
socket->close();
|
||||||
delete socket;
|
delete socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
memory->lock();
|
memory->lock();
|
||||||
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
|
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
|
||||||
if( server != nullptr ) {
|
if( server != nullptr ) {
|
||||||
server->close();
|
server->close();
|
||||||
delete server;
|
delete server;
|
||||||
inst->primary = false;
|
inst->primary = false;
|
||||||
inst->primaryPid = -1;
|
inst->primaryPid = -1;
|
||||||
inst->checksum = blockChecksum();
|
inst->checksum = blockChecksum();
|
||||||
}
|
}
|
||||||
memory->unlock();
|
memory->unlock();
|
||||||
|
|
||||||
delete memory;
|
delete memory;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::genBlockServerName()
|
void SingleApplicationPrivate::genBlockServerName()
|
||||||
{
|
{
|
||||||
QCryptographicHash appData( QCryptographicHash::Sha256 );
|
QCryptographicHash appData( QCryptographicHash::Sha256 );
|
||||||
appData.addData( "SingleApplication", 17 );
|
appData.addData( "SingleApplication", 17 );
|
||||||
appData.addData( SingleApplication::app_t::applicationName().toUtf8() );
|
appData.addData( SingleApplication::app_t::applicationName().toUtf8() );
|
||||||
appData.addData( SingleApplication::app_t::organizationName().toUtf8() );
|
appData.addData( SingleApplication::app_t::organizationName().toUtf8() );
|
||||||
appData.addData( SingleApplication::app_t::organizationDomain().toUtf8() );
|
appData.addData( SingleApplication::app_t::organizationDomain().toUtf8() );
|
||||||
|
|
||||||
if( ! (options & SingleApplication::Mode::ExcludeAppVersion) ) {
|
if( ! (options & SingleApplication::Mode::ExcludeAppVersion) ) {
|
||||||
appData.addData( SingleApplication::app_t::applicationVersion().toUtf8() );
|
appData.addData( SingleApplication::app_t::applicationVersion().toUtf8() );
|
||||||
}
|
}
|
||||||
|
|
||||||
if( ! (options & SingleApplication::Mode::ExcludeAppPath) ) {
|
if( ! (options & SingleApplication::Mode::ExcludeAppPath) ) {
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
appData.addData( SingleApplication::app_t::applicationFilePath().toLower().toUtf8() );
|
appData.addData( SingleApplication::app_t::applicationFilePath().toLower().toUtf8() );
|
||||||
#else
|
#else
|
||||||
appData.addData( SingleApplication::app_t::applicationFilePath().toUtf8() );
|
appData.addData( SingleApplication::app_t::applicationFilePath().toUtf8() );
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
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;
|
||||||
if( GetUserNameW( username, &usernameLength ) ) {
|
if( GetUserNameW( username, &usernameLength ) ) {
|
||||||
appData.addData( QString::fromWCharArray(username).toUtf8() );
|
appData.addData( QString::fromWCharArray(username).toUtf8() );
|
||||||
} else {
|
} else {
|
||||||
appData.addData( qgetenv("USERNAME") );
|
appData.addData( qgetenv("USERNAME") );
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef Q_OS_UNIX
|
#ifdef Q_OS_UNIX
|
||||||
QByteArray username;
|
QByteArray username;
|
||||||
uid_t uid = geteuid();
|
uid_t uid = geteuid();
|
||||||
struct passwd *pw = getpwuid(uid);
|
struct passwd *pw = getpwuid(uid);
|
||||||
if( pw ) {
|
if( pw ) {
|
||||||
username = pw->pw_name;
|
username = pw->pw_name;
|
||||||
}
|
}
|
||||||
if( username.isEmpty() ) {
|
if( username.isEmpty() ) {
|
||||||
username = qgetenv("USER");
|
username = qgetenv("USER");
|
||||||
}
|
}
|
||||||
appData.addData(username);
|
appData.addData(username);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with
|
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with
|
||||||
// server naming requirements.
|
// server naming requirements.
|
||||||
blockServerName = appData.result().toBase64().replace("/", "_");
|
blockServerName = appData.result().toBase64().replace("/", "_");
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::initializeMemoryBlock()
|
void SingleApplicationPrivate::initializeMemoryBlock()
|
||||||
{
|
{
|
||||||
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
|
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
|
||||||
inst->primary = false;
|
inst->primary = false;
|
||||||
inst->secondary = 0;
|
inst->secondary = 0;
|
||||||
inst->primaryPid = -1;
|
inst->primaryPid = -1;
|
||||||
inst->checksum = blockChecksum();
|
inst->checksum = blockChecksum();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::startPrimary()
|
void SingleApplicationPrivate::startPrimary()
|
||||||
{
|
{
|
||||||
Q_Q(SingleApplication);
|
Q_Q(SingleApplication);
|
||||||
|
|
||||||
// Successful creation means that no main process exists
|
// Successful creation means that no main process exists
|
||||||
// So we start a QLocalServer to listen for connections
|
// So we start a QLocalServer to listen for connections
|
||||||
QLocalServer::removeServer( blockServerName );
|
QLocalServer::removeServer( blockServerName );
|
||||||
server = new QLocalServer();
|
server = new QLocalServer();
|
||||||
|
|
||||||
// Restrict access to the socket according to the
|
// Restrict access to the socket according to the
|
||||||
// SingleApplication::Mode::User flag on User level or no restrictions
|
// SingleApplication::Mode::User flag on User level or no restrictions
|
||||||
if( options & SingleApplication::Mode::User ) {
|
if( options & SingleApplication::Mode::User ) {
|
||||||
server->setSocketOptions( QLocalServer::UserAccessOption );
|
server->setSocketOptions( QLocalServer::UserAccessOption );
|
||||||
} else {
|
} else {
|
||||||
server->setSocketOptions( QLocalServer::WorldAccessOption );
|
server->setSocketOptions( QLocalServer::WorldAccessOption );
|
||||||
}
|
}
|
||||||
|
|
||||||
server->listen( blockServerName );
|
server->listen( blockServerName );
|
||||||
QObject::connect(
|
QObject::connect(
|
||||||
server,
|
server,
|
||||||
&QLocalServer::newConnection,
|
&QLocalServer::newConnection,
|
||||||
this,
|
this,
|
||||||
&SingleApplicationPrivate::slotConnectionEstablished
|
&SingleApplicationPrivate::slotConnectionEstablished
|
||||||
);
|
);
|
||||||
|
|
||||||
// Reset the number of connections
|
// Reset the number of connections
|
||||||
InstancesInfo* inst = static_cast <InstancesInfo*>( memory->data() );
|
InstancesInfo* inst = static_cast <InstancesInfo*>( memory->data() );
|
||||||
|
|
||||||
inst->primary = true;
|
inst->primary = true;
|
||||||
inst->primaryPid = q->applicationPid();
|
inst->primaryPid = q->applicationPid();
|
||||||
inst->checksum = blockChecksum();
|
inst->checksum = blockChecksum();
|
||||||
|
|
||||||
instanceNumber = 0;
|
instanceNumber = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::startSecondary()
|
void SingleApplicationPrivate::startSecondary()
|
||||||
@@ -184,77 +184,77 @@ void SingleApplicationPrivate::startSecondary()
|
|||||||
|
|
||||||
void SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType connectionType )
|
void SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType connectionType )
|
||||||
{
|
{
|
||||||
// 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 already connected - we are done;
|
||||||
if( socket->state() == QLocalSocket::ConnectedState )
|
if( socket->state() == QLocalSocket::ConnectedState )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// If not connect
|
// If not connect
|
||||||
if( socket->state() == QLocalSocket::UnconnectedState ||
|
if( socket->state() == QLocalSocket::UnconnectedState ||
|
||||||
socket->state() == QLocalSocket::ClosingState ) {
|
socket->state() == QLocalSocket::ClosingState ) {
|
||||||
socket->connectToServer( blockServerName );
|
socket->connectToServer( blockServerName );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for being connected
|
// Wait for being connected
|
||||||
if( socket->state() == QLocalSocket::ConnectingState ) {
|
if( socket->state() == QLocalSocket::ConnectingState ) {
|
||||||
socket->waitForConnected( msecs );
|
socket->waitForConnected( msecs );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialisation message according to the SingleApplication protocol
|
// Initialisation message according to the SingleApplication protocol
|
||||||
if( socket->state() == QLocalSocket::ConnectedState ) {
|
if( socket->state() == QLocalSocket::ConnectedState ) {
|
||||||
// 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);
|
||||||
|
|
||||||
#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 );
|
socket->waitForBytesWritten( msecs );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
quint16 SingleApplicationPrivate::blockChecksum()
|
quint16 SingleApplicationPrivate::blockChecksum()
|
||||||
{
|
{
|
||||||
return qChecksum(
|
return qChecksum(
|
||||||
static_cast <const char *>( memory->data() ),
|
static_cast <const char *>( memory->data() ),
|
||||||
offsetof( InstancesInfo, checksum )
|
offsetof( InstancesInfo, checksum )
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
qint64 SingleApplicationPrivate::primaryPid()
|
qint64 SingleApplicationPrivate::primaryPid()
|
||||||
{
|
{
|
||||||
qint64 pid;
|
qint64 pid;
|
||||||
|
|
||||||
memory->lock();
|
memory->lock();
|
||||||
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
|
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
|
||||||
pid = inst->primaryPid;
|
pid = inst->primaryPid;
|
||||||
memory->unlock();
|
memory->unlock();
|
||||||
|
|
||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -262,144 +262,144 @@ qint64 SingleApplicationPrivate::primaryPid()
|
|||||||
*/
|
*/
|
||||||
void SingleApplicationPrivate::slotConnectionEstablished()
|
void SingleApplicationPrivate::slotConnectionEstablished()
|
||||||
{
|
{
|
||||||
QLocalSocket *nextConnSocket = server->nextPendingConnection();
|
QLocalSocket *nextConnSocket = server->nextPendingConnection();
|
||||||
connectionMap.insert(nextConnSocket, ConnectionInfo());
|
connectionMap.insert(nextConnSocket, ConnectionInfo());
|
||||||
|
|
||||||
QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose,
|
QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose,
|
||||||
[nextConnSocket, this]() {
|
[nextConnSocket, this]() {
|
||||||
auto &info = connectionMap[nextConnSocket];
|
auto &info = connectionMap[nextConnSocket];
|
||||||
Q_EMIT this->slotClientConnectionClosed( nextConnSocket, info.instanceId );
|
Q_EMIT this->slotClientConnectionClosed( nextConnSocket, info.instanceId );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
QObject::connect(nextConnSocket, &QLocalSocket::disconnected,
|
QObject::connect(nextConnSocket, &QLocalSocket::disconnected,
|
||||||
[nextConnSocket, this](){
|
[nextConnSocket, this](){
|
||||||
connectionMap.remove(nextConnSocket);
|
connectionMap.remove(nextConnSocket);
|
||||||
nextConnSocket->deleteLater();
|
nextConnSocket->deleteLater();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
QObject::connect(nextConnSocket, &QLocalSocket::readyRead,
|
QObject::connect(nextConnSocket, &QLocalSocket::readyRead,
|
||||||
[nextConnSocket, this]() {
|
[nextConnSocket, this]() {
|
||||||
auto &info = connectionMap[nextConnSocket];
|
auto &info = connectionMap[nextConnSocket];
|
||||||
switch(info.stage) {
|
switch(info.stage) {
|
||||||
case StageHeader:
|
case StageHeader:
|
||||||
readInitMessageHeader(nextConnSocket);
|
readInitMessageHeader(nextConnSocket);
|
||||||
break;
|
break;
|
||||||
case StageBody:
|
case StageBody:
|
||||||
readInitMessageBody(nextConnSocket);
|
readInitMessageBody(nextConnSocket);
|
||||||
break;
|
break;
|
||||||
case StageConnected:
|
case StageConnected:
|
||||||
Q_EMIT this->slotDataAvailable( nextConnSocket, info.instanceId );
|
Q_EMIT this->slotDataAvailable( nextConnSocket, info.instanceId );
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::readInitMessageHeader( QLocalSocket *sock )
|
void SingleApplicationPrivate::readInitMessageHeader( QLocalSocket *sock )
|
||||||
{
|
{
|
||||||
if (!connectionMap.contains( sock )) {
|
if (!connectionMap.contains( sock )) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( sock->bytesAvailable() < ( qint64 )sizeof( quint64 ) ) {
|
if( sock->bytesAvailable() < ( qint64 )sizeof( quint64 ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QDataStream headerStream( sock );
|
QDataStream headerStream( sock );
|
||||||
|
|
||||||
#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
|
||||||
|
|
||||||
// Read the header to know the message length
|
// Read the header to know the message length
|
||||||
quint64 msgLen = 0;
|
quint64 msgLen = 0;
|
||||||
headerStream >> msgLen;
|
headerStream >> msgLen;
|
||||||
ConnectionInfo &info = connectionMap[sock];
|
ConnectionInfo &info = connectionMap[sock];
|
||||||
info.stage = StageBody;
|
info.stage = StageBody;
|
||||||
info.msgLen = msgLen;
|
info.msgLen = msgLen;
|
||||||
|
|
||||||
if ( sock->bytesAvailable() >= (qint64) msgLen ) {
|
if ( sock->bytesAvailable() >= (qint64) msgLen ) {
|
||||||
readInitMessageBody( sock );
|
readInitMessageBody( sock );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
||||||
{
|
{
|
||||||
Q_Q(SingleApplication);
|
Q_Q(SingleApplication);
|
||||||
|
|
||||||
if (!connectionMap.contains( sock )) {
|
if (!connectionMap.contains( sock )) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionInfo &info = connectionMap[sock];
|
ConnectionInfo &info = connectionMap[sock];
|
||||||
if( sock->bytesAvailable() < ( qint64 )info.msgLen ) {
|
if( sock->bytesAvailable() < ( qint64 )info.msgLen ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the message body
|
// Read the message body
|
||||||
QByteArray msgBytes = sock->read(info.msgLen);
|
QByteArray msgBytes = sock->read(info.msgLen);
|
||||||
QDataStream readStream(msgBytes);
|
QDataStream readStream(msgBytes);
|
||||||
|
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
||||||
readStream.setVersion( QDataStream::Qt_5_6 );
|
readStream.setVersion( QDataStream::Qt_5_6 );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// server name
|
// server name
|
||||||
QByteArray latin1Name;
|
QByteArray latin1Name;
|
||||||
readStream >> latin1Name;
|
readStream >> latin1Name;
|
||||||
|
|
||||||
// connection type
|
// connection type
|
||||||
ConnectionType connectionType = InvalidConnection;
|
ConnectionType connectionType = InvalidConnection;
|
||||||
quint8 connTypeVal = InvalidConnection;
|
quint8 connTypeVal = InvalidConnection;
|
||||||
readStream >> connTypeVal;
|
readStream >> connTypeVal;
|
||||||
connectionType = static_cast <ConnectionType>( connTypeVal );
|
connectionType = static_cast <ConnectionType>( connTypeVal );
|
||||||
|
|
||||||
// instance id
|
// instance id
|
||||||
quint32 instanceId = 0;
|
quint32 instanceId = 0;
|
||||||
readStream >> instanceId;
|
readStream >> instanceId;
|
||||||
|
|
||||||
// checksum
|
// checksum
|
||||||
quint16 msgChecksum = 0;
|
quint16 msgChecksum = 0;
|
||||||
readStream >> msgChecksum;
|
readStream >> msgChecksum;
|
||||||
|
|
||||||
const quint16 actualChecksum = qChecksum( msgBytes.constData(), static_cast<quint32>( msgBytes.length() - sizeof( quint16 ) ) );
|
const quint16 actualChecksum = qChecksum( msgBytes.constData(), static_cast<quint32>( msgBytes.length() - sizeof( quint16 ) ) );
|
||||||
|
|
||||||
bool isValid = readStream.status() == QDataStream::Ok &&
|
bool isValid = readStream.status() == QDataStream::Ok &&
|
||||||
QLatin1String(latin1Name) == blockServerName &&
|
QLatin1String(latin1Name) == blockServerName &&
|
||||||
msgChecksum == actualChecksum;
|
msgChecksum == actualChecksum;
|
||||||
|
|
||||||
if( !isValid ) {
|
if( !isValid ) {
|
||||||
sock->close();
|
sock->close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
info.instanceId = instanceId;
|
info.instanceId = instanceId;
|
||||||
info.stage = StageConnected;
|
info.stage = StageConnected;
|
||||||
|
|
||||||
if( connectionType == NewInstance ||
|
if( connectionType == NewInstance ||
|
||||||
( connectionType == SecondaryInstance &&
|
( connectionType == SecondaryInstance &&
|
||||||
options & SingleApplication::Mode::SecondaryNotification ) )
|
options & SingleApplication::Mode::SecondaryNotification ) )
|
||||||
{
|
{
|
||||||
Q_EMIT q->instanceStarted();
|
Q_EMIT q->instanceStarted();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sock->bytesAvailable() > 0) {
|
if (sock->bytesAvailable() > 0) {
|
||||||
Q_EMIT this->slotDataAvailable( sock, instanceId );
|
Q_EMIT this->slotDataAvailable( sock, instanceId );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::slotDataAvailable( QLocalSocket *dataSocket, quint32 instanceId )
|
void SingleApplicationPrivate::slotDataAvailable( QLocalSocket *dataSocket, quint32 instanceId )
|
||||||
{
|
{
|
||||||
Q_Q(SingleApplication);
|
Q_Q(SingleApplication);
|
||||||
Q_EMIT q->receivedMessage( instanceId, dataSocket->readAll() );
|
Q_EMIT q->receivedMessage( instanceId, dataSocket->readAll() );
|
||||||
}
|
}
|
||||||
|
|
||||||
void SingleApplicationPrivate::slotClientConnectionClosed( QLocalSocket *closedSocket, quint32 instanceId )
|
void SingleApplicationPrivate::slotClientConnectionClosed( QLocalSocket *closedSocket, quint32 instanceId )
|
||||||
{
|
{
|
||||||
if( closedSocket->bytesAvailable() > 0 )
|
if( closedSocket->bytesAvailable() > 0 )
|
||||||
Q_EMIT slotDataAvailable( closedSocket, instanceId );
|
Q_EMIT slotDataAvailable( closedSocket, instanceId );
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user