2016-07-05 01:19:48 +08:00
|
|
|
// The MIT License (MIT)
|
|
|
|
//
|
|
|
|
// Copyright (c) Itay Grudev 2015 - 2016
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
2016-03-05 02:15:13 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
#include <QtCore/QProcess>
|
|
|
|
#include <QtCore/QByteArray>
|
2016-05-05 02:59:07 +08:00
|
|
|
#include <QtCore/QSemaphore>
|
|
|
|
#include <QtCore/QSharedMemory>
|
2016-08-10 09:42:46 +08:00
|
|
|
#include <QtCore/QStandardPaths>
|
|
|
|
#include <QtCore/QCryptographicHash>
|
2016-03-06 07:09:04 +08:00
|
|
|
#include <QtNetwork/QLocalServer>
|
2016-08-10 09:42:46 +08:00
|
|
|
#include <QtNetwork/QLocalSocket>
|
2012-12-23 06:12:38 +08:00
|
|
|
|
2015-06-06 05:26:41 +08:00
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#include <lmcons.h>
|
|
|
|
#endif
|
|
|
|
|
2016-03-05 02:15:13 +08:00
|
|
|
#include "singleapplication.h"
|
2016-08-10 09:42:46 +08:00
|
|
|
#include "singleapplication_p.h"
|
2016-03-05 02:15:13 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
SingleApplicationPrivate::SingleApplicationPrivate( SingleApplication *q_ptr ) : q_ptr( q_ptr ) {
|
|
|
|
server = nullptr;
|
|
|
|
socket = nullptr;
|
|
|
|
}
|
2016-03-05 02:15:13 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
SingleApplicationPrivate::~SingleApplicationPrivate()
|
|
|
|
{
|
|
|
|
cleanUp();
|
|
|
|
}
|
2016-03-05 02:15:13 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
void SingleApplicationPrivate::genBlockServerName( int timeout )
|
|
|
|
{
|
|
|
|
QCryptographicHash appData( QCryptographicHash::Sha256 );
|
|
|
|
appData.addData( "SingleApplication", 17 );
|
|
|
|
appData.addData( SingleApplication::app_t::applicationName().toUtf8() );
|
|
|
|
appData.addData( SingleApplication::app_t::organizationName().toUtf8() );
|
|
|
|
appData.addData( SingleApplication::app_t::organizationDomain().toUtf8() );
|
|
|
|
|
2016-11-27 01:40:47 +08:00
|
|
|
if( ! (options & SingleApplication::Mode::ExcludeAppVersion) ) {
|
|
|
|
appData.addData( SingleApplication::app_t::applicationVersion().toUtf8() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! (options & SingleApplication::Mode::ExcludeAppPath) ) {
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
appData.addData( SingleApplication::app_t::applicationFilePath().toLower().toUtf8() );
|
|
|
|
#else
|
|
|
|
appData.addData( SingleApplication::app_t::applicationFilePath().toUtf8() );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
// User level block requires a user specific data in the hash
|
|
|
|
if( options & SingleApplication::Mode::User ) {
|
|
|
|
#ifdef Q_OS_WIN
|
2016-08-24 19:04:49 +08:00
|
|
|
Q_UNUSED(timeout);
|
2016-08-19 01:28:57 +08:00
|
|
|
wchar_t username [ UNLEN + 1 ];
|
2016-08-10 09:42:46 +08:00
|
|
|
// Specifies size of the buffer on input
|
2016-08-19 01:28:57 +08:00
|
|
|
DWORD usernameLength = UNLEN + 1;
|
2016-08-10 09:42:46 +08:00
|
|
|
if( GetUserName( username, &usernameLength ) ) {
|
2016-11-27 19:09:42 +08:00
|
|
|
appData.addData( QString::fromWCharArray(username).toUtf8() );
|
2016-08-10 09:42:46 +08:00
|
|
|
} else {
|
|
|
|
appData.addData( QStandardPaths::standardLocations( QStandardPaths::HomeLocation ).join("").toUtf8() );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
QString username;
|
|
|
|
QProcess process;
|
|
|
|
process.start( "whoami" );
|
|
|
|
if( process.waitForFinished( timeout ) &&
|
|
|
|
process.exitCode() == QProcess::NormalExit) {
|
|
|
|
appData.addData( process.readLine() );
|
|
|
|
} else {
|
|
|
|
appData.addData( QStandardPaths::standardLocations( QStandardPaths::HomeLocation ).join("").toUtf8() );
|
|
|
|
}
|
|
|
|
#endif
|
2016-05-05 02:59:07 +08:00
|
|
|
}
|
2015-06-06 05:26:41 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with
|
|
|
|
// server naming requirements.
|
|
|
|
blockServerName = appData.result().toBase64().replace("/", "_");
|
|
|
|
}
|
2016-03-22 07:32:20 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
void SingleApplicationPrivate::startPrimary( bool resetMemory )
|
|
|
|
{
|
2016-05-05 02:59:07 +08:00
|
|
|
#ifdef Q_OS_UNIX
|
2016-08-10 09:42:46 +08:00
|
|
|
// Handle any further termination signals to ensure the
|
|
|
|
// QSharedMemory block is deleted even if the process crashes
|
|
|
|
crashHandler();
|
2016-05-05 02:59:07 +08:00
|
|
|
#endif
|
2016-08-10 09:42:46 +08:00
|
|
|
// Successful creation means that no main process exists
|
|
|
|
// So we start a QLocalServer to listen for connections
|
|
|
|
QLocalServer::removeServer( blockServerName );
|
|
|
|
server = new QLocalServer();
|
2016-11-27 01:40:47 +08:00
|
|
|
|
|
|
|
// Restrict access to the socket according to the
|
|
|
|
// SingleApplication::Mode::User flag on User level or no restrictions
|
|
|
|
if( options & SingleApplication::Mode::User ) {
|
|
|
|
server->setSocketOptions( QLocalServer::UserAccessOption );
|
|
|
|
} else {
|
|
|
|
server->setSocketOptions( QLocalServer::WorldAccessOption );
|
|
|
|
}
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
server->listen( blockServerName );
|
|
|
|
QObject::connect(
|
|
|
|
server,
|
|
|
|
&QLocalServer::newConnection,
|
|
|
|
this,
|
|
|
|
&SingleApplicationPrivate::slotConnectionEstablished
|
|
|
|
);
|
|
|
|
|
|
|
|
// Reset the number of connections
|
|
|
|
memory->lock();
|
|
|
|
InstancesInfo* inst = (InstancesInfo*)memory->data();
|
|
|
|
|
|
|
|
if( resetMemory ){
|
|
|
|
inst->primary = true;
|
|
|
|
inst->secondary = 0;
|
|
|
|
} else {
|
|
|
|
inst->primary = true;
|
2016-05-05 02:59:07 +08:00
|
|
|
}
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
memory->unlock();
|
|
|
|
|
|
|
|
instanceNumber = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SingleApplicationPrivate::startSecondary()
|
|
|
|
{
|
2016-05-05 02:59:07 +08:00
|
|
|
#ifdef Q_OS_UNIX
|
2016-08-10 09:42:46 +08:00
|
|
|
// Handle any further termination signals to ensure the
|
|
|
|
// QSharedMemory block is deleted even if the process crashes
|
|
|
|
crashHandler();
|
2016-05-05 02:59:07 +08:00
|
|
|
#endif
|
2016-08-10 09:42:46 +08:00
|
|
|
}
|
2016-05-05 02:59:07 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
void SingleApplicationPrivate::connectToPrimary( int msecs, char connectionType )
|
|
|
|
{
|
|
|
|
// Connect to the Local Server of the Primary Instance if not already
|
|
|
|
// connected.
|
|
|
|
if( socket == nullptr ) {
|
|
|
|
socket = new QLocalSocket();
|
2016-05-05 03:51:06 +08:00
|
|
|
}
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
// If already connected - we are done;
|
|
|
|
if( socket->state() == QLocalSocket::ConnectedState )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If not connect
|
|
|
|
if( socket->state() == QLocalSocket::UnconnectedState ||
|
|
|
|
socket->state() == QLocalSocket::ClosingState ) {
|
|
|
|
socket->connectToServer( blockServerName );
|
|
|
|
}
|
2016-05-05 02:59:07 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
// Wait for being connected
|
|
|
|
if( socket->state() == QLocalSocket::ConnectingState ) {
|
|
|
|
socket->waitForConnected( msecs );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialisation message according to the SingleApplication protocol
|
|
|
|
if( socket->state() == QLocalSocket::ConnectedState ) {
|
2016-05-05 02:59:07 +08:00
|
|
|
// Notify the parent that a new instance had been started;
|
2016-08-10 09:42:46 +08:00
|
|
|
QByteArray initMsg = blockServerName.toLatin1();
|
|
|
|
|
|
|
|
initMsg.append( connectionType );
|
|
|
|
initMsg.append( (const char *)&instanceNumber, sizeof(quint32) );
|
|
|
|
initMsg.append( QByteArray::number( qChecksum( initMsg.constData(), initMsg.length() ), 256) );
|
|
|
|
|
|
|
|
socket->write( initMsg );
|
|
|
|
socket->flush();
|
|
|
|
socket->waitForBytesWritten( msecs );
|
2015-06-06 05:26:41 +08:00
|
|
|
}
|
2016-08-10 09:42:46 +08:00
|
|
|
}
|
2015-06-06 05:26:41 +08:00
|
|
|
|
|
|
|
#ifdef Q_OS_UNIX
|
2016-08-10 09:42:46 +08:00
|
|
|
void SingleApplicationPrivate::crashHandler()
|
2015-06-06 05:26:41 +08:00
|
|
|
{
|
|
|
|
// This guarantees the program will work even with multiple
|
2016-03-05 02:15:13 +08:00
|
|
|
// instances of SingleApplication in different threads.
|
|
|
|
// Which in my opinion is idiotic, but lets handle that too.
|
2015-06-06 05:26:41 +08:00
|
|
|
{
|
|
|
|
sharedMemMutex.lock();
|
2016-05-05 02:59:07 +08:00
|
|
|
sharedMem.append( this );
|
2015-06-06 05:26:41 +08:00
|
|
|
sharedMemMutex.unlock();
|
|
|
|
}
|
2016-05-05 02:59:07 +08:00
|
|
|
|
2015-06-06 05:26:41 +08:00
|
|
|
// Handle any further termination signals to ensure the
|
|
|
|
// QSharedMemory block is deleted even if the process crashes
|
2016-07-10 08:11:55 +08:00
|
|
|
signal( SIGHUP, SingleApplicationPrivate::terminate ); // 1
|
|
|
|
signal( SIGINT, SingleApplicationPrivate::terminate ); // 2
|
|
|
|
signal( SIGQUIT, SingleApplicationPrivate::terminate ); // 3
|
|
|
|
signal( SIGILL, SingleApplicationPrivate::terminate ); // 4
|
|
|
|
signal( SIGABRT, SingleApplicationPrivate::terminate ); // 6
|
|
|
|
signal( SIGFPE, SingleApplicationPrivate::terminate ); // 8
|
|
|
|
signal( SIGBUS, SingleApplicationPrivate::terminate ); // 10
|
|
|
|
signal( SIGSEGV, SingleApplicationPrivate::terminate ); // 11
|
|
|
|
signal( SIGSYS, SingleApplicationPrivate::terminate ); // 12
|
|
|
|
signal( SIGPIPE, SingleApplicationPrivate::terminate ); // 13
|
|
|
|
signal( SIGALRM, SingleApplicationPrivate::terminate ); // 14
|
|
|
|
signal( SIGTERM, SingleApplicationPrivate::terminate ); // 15
|
|
|
|
signal( SIGXCPU, SingleApplicationPrivate::terminate ); // 24
|
|
|
|
signal( SIGXFSZ, SingleApplicationPrivate::terminate ); // 25
|
2015-06-06 05:26:41 +08:00
|
|
|
}
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
void SingleApplicationPrivate::terminate( int signum )
|
2015-06-06 05:26:41 +08:00
|
|
|
{
|
|
|
|
while( ! sharedMem.empty() ) {
|
|
|
|
delete sharedMem.back();
|
|
|
|
sharedMem.pop_back();
|
|
|
|
}
|
2016-05-05 02:59:07 +08:00
|
|
|
::exit( 128 + signum );
|
2015-06-06 05:26:41 +08:00
|
|
|
}
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
QList<SingleApplicationPrivate*> SingleApplicationPrivate::sharedMem;
|
|
|
|
QMutex SingleApplicationPrivate::sharedMemMutex;
|
2015-06-06 05:26:41 +08:00
|
|
|
#endif
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
void SingleApplicationPrivate::cleanUp() {
|
|
|
|
if( socket != nullptr ) {
|
|
|
|
socket->close();
|
|
|
|
delete socket;
|
|
|
|
}
|
|
|
|
memory->lock();
|
|
|
|
InstancesInfo* inst = (InstancesInfo*)memory->data();
|
|
|
|
if( server != nullptr ) {
|
|
|
|
server->close();
|
|
|
|
inst->primary = false;
|
|
|
|
}
|
|
|
|
memory->unlock();
|
|
|
|
delete memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Executed when a connection has been made to the LocalServer
|
|
|
|
*/
|
|
|
|
void SingleApplicationPrivate::slotConnectionEstablished()
|
|
|
|
{
|
|
|
|
Q_Q(SingleApplication);
|
|
|
|
|
|
|
|
QLocalSocket *socket = server->nextPendingConnection();
|
|
|
|
|
|
|
|
// Verify that the new connection follows the SingleApplication protocol
|
2017-01-25 00:54:53 +08:00
|
|
|
char connectionType = '\0'; // Invalid connection
|
2016-08-10 09:42:46 +08:00
|
|
|
quint32 instanceId;
|
|
|
|
QByteArray initMsg, tmp;
|
|
|
|
if( socket->waitForReadyRead( 100 ) ) {
|
|
|
|
tmp = socket->read( blockServerName.length() );
|
|
|
|
// Verify that the socket data start with blockServerName
|
|
|
|
if( tmp == blockServerName.toLatin1() ) {
|
|
|
|
initMsg = tmp;
|
|
|
|
// Verify that the next charecter is N/S/R (connecion type)
|
|
|
|
// Stands for New Instance/Secondary Instance/Reconnect
|
2017-01-25 00:54:53 +08:00
|
|
|
connectionType = socket->read( 1 )[0];
|
|
|
|
switch( connectionType ) {
|
|
|
|
case 'N':
|
|
|
|
case 'S':
|
|
|
|
case 'R':
|
|
|
|
{
|
|
|
|
initMsg += connectionType;
|
2016-08-10 09:42:46 +08:00
|
|
|
tmp = socket->read( sizeof(quint32) );
|
|
|
|
const char * data = tmp.constData();
|
|
|
|
instanceId = (quint32)*data;
|
|
|
|
initMsg += tmp;
|
|
|
|
// Verify the checksum of the initMsg
|
|
|
|
QByteArray checksum = QByteArray::number(
|
|
|
|
qChecksum( initMsg.constData(), initMsg.length() ),
|
|
|
|
256
|
|
|
|
);
|
|
|
|
tmp = socket->read( checksum.length() );
|
2017-01-25 00:54:53 +08:00
|
|
|
if( checksum == tmp )
|
|
|
|
break; // Otherwise set to invalid connection (next line)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
connectionType = '\0'; // Invalid connection
|
2016-08-10 09:42:46 +08:00
|
|
|
}
|
2016-05-05 02:59:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 00:54:53 +08:00
|
|
|
if( connectionType == '\0' ) {
|
2016-08-10 09:42:46 +08:00
|
|
|
socket->close();
|
|
|
|
delete socket;
|
|
|
|
return;
|
|
|
|
}
|
2015-06-06 05:26:41 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
QObject::connect(
|
|
|
|
socket,
|
|
|
|
&QLocalSocket::aboutToClose,
|
|
|
|
this,
|
|
|
|
[socket, instanceId, this]() {
|
|
|
|
Q_EMIT this->slotClientConnectionClosed( socket, instanceId );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
QObject::connect(
|
|
|
|
socket,
|
|
|
|
&QLocalSocket::readyRead,
|
|
|
|
this,
|
|
|
|
[socket, instanceId, this]() {
|
|
|
|
Q_EMIT this->slotDataAvailable( socket, instanceId );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if( connectionType == 'N' || (
|
|
|
|
connectionType == 'S' &&
|
|
|
|
options & SingleApplication::Mode::SecondaryNotification
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
Q_EMIT q->instanceStarted();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( socket->bytesAvailable() > 0 ) {
|
|
|
|
Q_EMIT this->slotDataAvailable( socket, instanceId );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SingleApplicationPrivate::slotDataAvailable( QLocalSocket *socket, quint32 instanceId )
|
|
|
|
{
|
|
|
|
Q_Q(SingleApplication);
|
|
|
|
Q_EMIT q->receivedMessage( instanceId, socket->readAll() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void SingleApplicationPrivate::slotClientConnectionClosed( QLocalSocket *socket, quint32 instanceId )
|
|
|
|
{
|
|
|
|
if( socket->bytesAvailable() > 0 )
|
|
|
|
Q_EMIT slotDataAvailable( socket, instanceId );
|
|
|
|
socket->deleteLater();
|
|
|
|
}
|
2015-06-06 05:26:41 +08:00
|
|
|
|
2012-12-23 06:12:38 +08:00
|
|
|
/**
|
2015-02-27 03:19:38 +08:00
|
|
|
* @brief Constructor. Checks and fires up LocalServer or closes the program
|
2015-02-27 03:00:11 +08:00
|
|
|
* if another instance already exists
|
2012-12-23 06:12:38 +08:00
|
|
|
* @param argc
|
|
|
|
* @param argv
|
2016-08-10 09:42:46 +08:00
|
|
|
* @param {bool} allowSecondaryInstances
|
2012-12-23 06:12:38 +08:00
|
|
|
*/
|
2016-08-10 09:42:46 +08:00
|
|
|
SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSecondary, Options options, int timeout )
|
2016-05-05 04:05:59 +08:00
|
|
|
: app_t( argc, argv ), d_ptr( new SingleApplicationPrivate( this ) )
|
2012-12-23 06:12:38 +08:00
|
|
|
{
|
2016-03-05 02:15:13 +08:00
|
|
|
Q_D(SingleApplication);
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
// Store the current mode of the program
|
|
|
|
d->options = options;
|
2016-05-05 02:59:07 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
// Generating an application ID used for identifying the shared memory
|
|
|
|
// block and QLocalServer
|
|
|
|
d->genBlockServerName( timeout );
|
2012-12-23 06:12:38 +08:00
|
|
|
|
2016-05-05 03:41:23 +08:00
|
|
|
// Guarantee thread safe behaviour with a shared memory block. Also by
|
2016-08-10 09:42:46 +08:00
|
|
|
// explicitly attaching it and then deleting it we make sure that the
|
|
|
|
// memory is deleted even if the process had crashed on Unix.
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
d->memory = new QSharedMemory( d->blockServerName );
|
2016-05-05 03:41:23 +08:00
|
|
|
d->memory->attach();
|
|
|
|
delete d->memory;
|
2015-06-06 05:26:41 +08:00
|
|
|
#endif
|
2016-08-10 09:42:46 +08:00
|
|
|
d->memory = new QSharedMemory( d->blockServerName );
|
|
|
|
|
|
|
|
// Create a shared memory block
|
|
|
|
if( d->memory->create( sizeof( InstancesInfo ) ) ) {
|
2016-05-05 02:59:07 +08:00
|
|
|
d->startPrimary( true );
|
|
|
|
return;
|
2015-02-27 03:00:11 +08:00
|
|
|
} else {
|
2016-05-05 02:59:07 +08:00
|
|
|
// Attempt to attach to the memory segment
|
|
|
|
if( d->memory->attach() ) {
|
|
|
|
d->memory->lock();
|
|
|
|
InstancesInfo* inst = (InstancesInfo*)d->memory->data();
|
|
|
|
|
|
|
|
if( ! inst->primary ) {
|
|
|
|
d->startPrimary( false );
|
|
|
|
d->memory->unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if another instance can be started
|
2016-08-10 09:42:46 +08:00
|
|
|
if( allowSecondary ) {
|
2016-05-05 02:59:07 +08:00
|
|
|
inst->secondary += 1;
|
2016-08-10 09:42:46 +08:00
|
|
|
d->instanceNumber = inst->secondary;
|
2016-05-05 02:59:07 +08:00
|
|
|
d->startSecondary();
|
2016-08-10 09:42:46 +08:00
|
|
|
if( d->options & Mode::SecondaryNotification ) {
|
|
|
|
d->connectToPrimary( timeout, 'S' );
|
|
|
|
}
|
2016-05-05 02:59:07 +08:00
|
|
|
d->memory->unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->memory->unlock();
|
|
|
|
}
|
2015-02-27 03:00:11 +08:00
|
|
|
}
|
2016-05-05 02:59:07 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
d->connectToPrimary( timeout, 'N' );
|
2016-05-05 03:51:06 +08:00
|
|
|
delete d;
|
2016-08-10 09:42:46 +08:00
|
|
|
::exit( EXIT_SUCCESS );
|
2012-12-23 06:12:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-27 03:00:11 +08:00
|
|
|
* @brief Destructor
|
2012-12-23 06:12:38 +08:00
|
|
|
*/
|
2015-02-27 03:00:11 +08:00
|
|
|
SingleApplication::~SingleApplication()
|
2012-12-23 06:12:38 +08:00
|
|
|
{
|
2016-03-05 02:15:13 +08:00
|
|
|
Q_D(SingleApplication);
|
2016-05-05 02:59:07 +08:00
|
|
|
delete d;
|
|
|
|
}
|
2016-03-05 02:15:13 +08:00
|
|
|
|
2016-05-05 02:59:07 +08:00
|
|
|
bool SingleApplication::isPrimary()
|
|
|
|
{
|
|
|
|
Q_D(SingleApplication);
|
2016-08-10 09:42:46 +08:00
|
|
|
return d->server != nullptr;
|
2016-05-05 02:59:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SingleApplication::isSecondary()
|
|
|
|
{
|
|
|
|
Q_D(SingleApplication);
|
2016-08-10 09:42:46 +08:00
|
|
|
return d->server == nullptr;
|
2012-12-23 06:12:38 +08:00
|
|
|
}
|
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
quint32 SingleApplication::instanceId()
|
2012-12-23 06:12:38 +08:00
|
|
|
{
|
2016-03-05 02:15:13 +08:00
|
|
|
Q_D(SingleApplication);
|
2016-08-10 09:42:46 +08:00
|
|
|
return d->instanceNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SingleApplication::sendMessage( QByteArray message, int timeout )
|
|
|
|
{
|
|
|
|
Q_D(SingleApplication);
|
|
|
|
|
|
|
|
// Nobody to connect to
|
|
|
|
if( isPrimary() ) return false;
|
|
|
|
|
|
|
|
// Make sure the socket is connected
|
|
|
|
d->connectToPrimary( timeout, 'R' );
|
2016-03-05 02:15:13 +08:00
|
|
|
|
2016-08-10 09:42:46 +08:00
|
|
|
d->socket->write( message );
|
|
|
|
bool dataWritten = d->socket->flush();
|
|
|
|
d->socket->waitForBytesWritten( timeout );
|
|
|
|
return dataWritten;
|
2012-12-23 06:12:38 +08:00
|
|
|
}
|