2018-07-27 09:29:55 +08:00
|
|
|
// The MIT License (MIT)
|
|
|
|
//
|
2020-04-22 07:58:15 +08:00
|
|
|
// Copyright (c) Itay Grudev 2015 - 2020
|
2018-07-27 09:29:55 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
//
|
|
|
|
// W A R N I N G !!!
|
|
|
|
// -----------------
|
|
|
|
//
|
|
|
|
// This file is not part of the SingleApplication API. It is used purely as an
|
|
|
|
// implementation detail. This header file may change from version to
|
|
|
|
// version without notice, or may even be removed.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
#include <QtCore/QDir>
|
2020-09-09 09:23:42 +08:00
|
|
|
#include <QtCore/QThread>
|
2018-07-27 09:29:55 +08:00
|
|
|
#include <QtCore/QByteArray>
|
|
|
|
#include <QtCore/QDataStream>
|
2020-09-09 09:23:42 +08:00
|
|
|
#include <QtCore/QElapsedTimer>
|
2018-07-27 09:29:55 +08:00
|
|
|
#include <QtCore/QCryptographicHash>
|
|
|
|
#include <QtNetwork/QLocalServer>
|
|
|
|
#include <QtNetwork/QLocalSocket>
|
|
|
|
|
2020-09-09 09:23:42 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
|
|
|
|
#include <QtCore/QRandomGenerator>
|
|
|
|
#else
|
|
|
|
#include <QtCore/QDateTime>
|
|
|
|
#endif
|
|
|
|
|
2018-07-27 09:29:55 +08:00
|
|
|
#include "singleapplication.h"
|
|
|
|
#include "singleapplication_p.h"
|
|
|
|
|
2019-09-23 01:39:45 +08:00
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
|
2018-07-27 09:29:55 +08:00
|
|
|
#ifdef Q_OS_WIN
|
2020-10-06 18:56:20 +08:00
|
|
|
#ifndef NOMINMAX
|
|
|
|
#define NOMINMAX 1
|
|
|
|
#endif
|
2018-07-27 09:29:55 +08:00
|
|
|
#include <windows.h>
|
|
|
|
#include <lmcons.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SingleApplicationPrivate::SingleApplicationPrivate( SingleApplication *q_ptr )
|
|
|
|
: q_ptr( q_ptr )
|
|
|
|
{
|
|
|
|
server = nullptr;
|
|
|
|
socket = nullptr;
|
2019-01-22 20:21:25 +08:00
|
|
|
memory = nullptr;
|
2020-10-11 01:31:47 +08:00
|
|
|
instanceNumber = 0;
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SingleApplicationPrivate::~SingleApplicationPrivate()
|
|
|
|
{
|
2020-09-09 07:28:02 +08:00
|
|
|
if( socket != nullptr ){
|
2018-07-27 09:29:55 +08:00
|
|
|
socket->close();
|
|
|
|
delete socket;
|
|
|
|
}
|
|
|
|
|
2020-09-09 07:28:02 +08:00
|
|
|
if( memory != nullptr ){
|
2020-04-21 00:33:30 +08:00
|
|
|
memory->lock();
|
2020-05-17 22:50:27 +08:00
|
|
|
auto *inst = static_cast<InstancesInfo*>(memory->data());
|
2020-09-09 07:28:02 +08:00
|
|
|
if( server != nullptr ){
|
2020-04-21 00:33:30 +08:00
|
|
|
server->close();
|
|
|
|
delete server;
|
|
|
|
inst->primary = false;
|
|
|
|
inst->primaryPid = -1;
|
|
|
|
inst->primaryUser[0] = '\0';
|
|
|
|
inst->checksum = blockChecksum();
|
|
|
|
}
|
|
|
|
memory->unlock();
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2020-04-21 00:33:30 +08:00
|
|
|
delete memory;
|
|
|
|
}
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
2020-03-27 14:52:48 +08:00
|
|
|
QString SingleApplicationPrivate::getUsername()
|
|
|
|
{
|
2020-03-03 09:22:54 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
wchar_t username[UNLEN + 1];
|
|
|
|
// Specifies size of the buffer on input
|
|
|
|
DWORD usernameLength = UNLEN + 1;
|
|
|
|
if( GetUserNameW( username, &usernameLength ) )
|
2020-03-27 14:52:48 +08:00
|
|
|
return QString::fromWCharArray( username );
|
2020-03-27 15:19:41 +08:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
|
|
|
|
return QString::fromLocal8Bit( qgetenv( "USERNAME" ) );
|
|
|
|
#else
|
2020-03-27 14:52:48 +08:00
|
|
|
return qEnvironmentVariable( "USERNAME" );
|
2020-03-03 09:22:54 +08:00
|
|
|
#endif
|
2020-03-27 15:19:41 +08:00
|
|
|
#endif
|
2020-03-03 09:22:54 +08:00
|
|
|
#ifdef Q_OS_UNIX
|
2020-03-27 14:52:48 +08:00
|
|
|
QString username;
|
2020-03-03 09:22:54 +08:00
|
|
|
uid_t uid = geteuid();
|
|
|
|
struct passwd *pw = getpwuid( uid );
|
|
|
|
if( pw )
|
2020-03-27 14:52:48 +08:00
|
|
|
username = QString::fromLocal8Bit( pw->pw_name );
|
2020-09-09 07:28:02 +08:00
|
|
|
if ( username.isEmpty() ){
|
2020-03-27 15:19:41 +08:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
|
|
|
|
username = QString::fromLocal8Bit( qgetenv( "USER" ) );
|
|
|
|
#else
|
2020-03-27 14:52:48 +08:00
|
|
|
username = qEnvironmentVariable( "USER" );
|
2020-03-27 15:19:41 +08:00
|
|
|
#endif
|
|
|
|
}
|
2020-03-03 09:22:54 +08:00
|
|
|
return username;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-07-27 09:29:55 +08:00
|
|
|
void SingleApplicationPrivate::genBlockServerName()
|
|
|
|
{
|
|
|
|
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() );
|
|
|
|
|
2020-12-11 08:18:00 +08:00
|
|
|
if ( ! appDataList.isEmpty() )
|
|
|
|
appData.addData( appDataList.join( "" ).toUtf8() );
|
|
|
|
|
2020-09-09 07:28:02 +08:00
|
|
|
if( ! (options & SingleApplication::Mode::ExcludeAppVersion) ){
|
2018-07-27 09:29:55 +08:00
|
|
|
appData.addData( SingleApplication::app_t::applicationVersion().toUtf8() );
|
|
|
|
}
|
|
|
|
|
2020-09-09 07:28:02 +08:00
|
|
|
if( ! (options & SingleApplication::Mode::ExcludeAppPath) ){
|
2018-07-27 09:29:55 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
appData.addData( SingleApplication::app_t::applicationFilePath().toLower().toUtf8() );
|
|
|
|
#else
|
2021-09-20 20:56:31 +08:00
|
|
|
const QByteArray appImagePath = qgetenv( "APPIMAGE" );
|
2021-09-20 20:41:02 +08:00
|
|
|
if ( appImagePath.isEmpty() ) {
|
|
|
|
// Not running as AppImage: use path to executable file
|
|
|
|
appData.addData( SingleApplication::app_t::applicationFilePath().toUtf8() );
|
|
|
|
} else {
|
|
|
|
// Running as AppImage: Use absolute path to AppImage file
|
|
|
|
appData.addData( appImagePath.toUtf8() );
|
|
|
|
}
|
2018-07-27 09:29:55 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// User level block requires a user specific data in the hash
|
2020-09-09 07:28:02 +08:00
|
|
|
if( options & SingleApplication::Mode::User ){
|
2020-03-27 14:52:48 +08:00
|
|
|
appData.addData( getUsername().toUtf8() );
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with
|
|
|
|
// server naming requirements.
|
|
|
|
blockServerName = appData.result().toBase64().replace("/", "_");
|
|
|
|
}
|
|
|
|
|
2020-10-16 02:41:00 +08:00
|
|
|
void SingleApplicationPrivate::initializeMemoryBlock() const
|
2018-07-27 09:29:55 +08:00
|
|
|
{
|
2020-05-17 22:50:27 +08:00
|
|
|
auto *inst = static_cast<InstancesInfo*>( memory->data() );
|
2018-07-27 09:29:55 +08:00
|
|
|
inst->primary = false;
|
|
|
|
inst->secondary = 0;
|
|
|
|
inst->primaryPid = -1;
|
2020-03-03 09:22:54 +08:00
|
|
|
inst->primaryUser[0] = '\0';
|
2018-07-27 09:29:55 +08:00
|
|
|
inst->checksum = blockChecksum();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SingleApplicationPrivate::startPrimary()
|
|
|
|
{
|
2020-09-09 07:28:02 +08:00
|
|
|
// Reset the number of connections
|
|
|
|
auto *inst = static_cast <InstancesInfo*>( memory->data() );
|
|
|
|
|
|
|
|
inst->primary = true;
|
2020-10-16 02:41:00 +08:00
|
|
|
inst->primaryPid = QCoreApplication::applicationPid();
|
2020-09-09 07:28:02 +08:00
|
|
|
qstrncpy( inst->primaryUser, getUsername().toUtf8().data(), sizeof(inst->primaryUser) );
|
|
|
|
inst->checksum = blockChecksum();
|
|
|
|
instanceNumber = 0;
|
2018-07-27 09:29:55 +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();
|
|
|
|
|
|
|
|
// Restrict access to the socket according to the
|
|
|
|
// SingleApplication::Mode::User flag on User level or no restrictions
|
2020-09-09 07:28:02 +08:00
|
|
|
if( options & SingleApplication::Mode::User ){
|
2020-12-11 08:18:00 +08:00
|
|
|
server->setSocketOptions( QLocalServer::UserAccessOption );
|
2018-07-27 09:29:55 +08:00
|
|
|
} else {
|
2020-12-11 08:18:00 +08:00
|
|
|
server->setSocketOptions( QLocalServer::WorldAccessOption );
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
server->listen( blockServerName );
|
|
|
|
QObject::connect(
|
|
|
|
server,
|
|
|
|
&QLocalServer::newConnection,
|
|
|
|
this,
|
|
|
|
&SingleApplicationPrivate::slotConnectionEstablished
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SingleApplicationPrivate::startSecondary()
|
|
|
|
{
|
2020-09-09 07:28:02 +08:00
|
|
|
auto *inst = static_cast <InstancesInfo*>( memory->data() );
|
|
|
|
|
|
|
|
inst->secondary += 1;
|
|
|
|
inst->checksum = blockChecksum();
|
|
|
|
instanceNumber = inst->secondary;
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
2020-10-16 02:41:00 +08:00
|
|
|
bool SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType connectionType )
|
2018-07-27 09:29:55 +08:00
|
|
|
{
|
2020-09-09 09:28:07 +08:00
|
|
|
QElapsedTimer time;
|
|
|
|
time.start();
|
|
|
|
|
2018-07-27 09:29:55 +08:00
|
|
|
// Connect to the Local Server of the Primary Instance if not already
|
|
|
|
// connected.
|
2020-09-09 07:28:02 +08:00
|
|
|
if( socket == nullptr ){
|
2018-07-27 09:29:55 +08:00
|
|
|
socket = new QLocalSocket();
|
|
|
|
}
|
|
|
|
|
2020-09-09 09:28:07 +08:00
|
|
|
if( socket->state() == QLocalSocket::ConnectedState ) return true;
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2020-09-09 09:28:07 +08:00
|
|
|
if( socket->state() != QLocalSocket::ConnectedState ){
|
|
|
|
|
|
|
|
while( true ){
|
2020-12-11 08:18:00 +08:00
|
|
|
randomSleep();
|
2020-09-09 09:28:07 +08:00
|
|
|
|
|
|
|
if( socket->state() != QLocalSocket::ConnectingState )
|
|
|
|
socket->connectToServer( blockServerName );
|
|
|
|
|
|
|
|
if( socket->state() == QLocalSocket::ConnectingState ){
|
2020-10-16 02:41:00 +08:00
|
|
|
socket->waitForConnected( static_cast<int>(msecs - time.elapsed()) );
|
2020-09-09 09:28:07 +08:00
|
|
|
}
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2020-09-09 09:28:07 +08:00
|
|
|
// 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
|
2020-10-16 02:41:00 +08:00
|
|
|
if( time.elapsed() >= msecs ) return false;
|
2020-09-09 09:28:07 +08:00
|
|
|
}
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialisation message according to the SingleApplication protocol
|
2020-09-09 09:28:07 +08:00
|
|
|
QByteArray initMsg;
|
|
|
|
QDataStream writeStream(&initMsg, QIODevice::WriteOnly);
|
2018-12-15 02:41:55 +08:00
|
|
|
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
2020-09-09 09:28:07 +08:00
|
|
|
writeStream.setVersion(QDataStream::Qt_5_6);
|
2018-12-15 02:41:55 +08:00
|
|
|
#endif
|
|
|
|
|
2020-09-09 09:28:07 +08:00
|
|
|
writeStream << blockServerName.toLatin1();
|
|
|
|
writeStream << static_cast<quint8>(connectionType);
|
|
|
|
writeStream << instanceNumber;
|
2020-10-18 06:58:52 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
quint16 checksum = qChecksum(QByteArray(initMsg, static_cast<quint32>(initMsg.length())));
|
|
|
|
#else
|
2020-09-09 09:28:07 +08:00
|
|
|
quint16 checksum = qChecksum(initMsg.constData(), static_cast<quint32>(initMsg.length()));
|
2020-10-18 06:58:52 +08:00
|
|
|
#endif
|
2020-09-09 09:28:07 +08:00
|
|
|
writeStream << checksum;
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2021-05-31 22:14:39 +08:00
|
|
|
return writeConfirmedMessage( static_cast<int>(msecs - time.elapsed()), initMsg );
|
|
|
|
}
|
|
|
|
|
|
|
|
void SingleApplicationPrivate::writeAck( QLocalSocket *sock ) {
|
|
|
|
sock->putChar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SingleApplicationPrivate::writeConfirmedMessage (int msecs, const QByteArray &msg)
|
|
|
|
{
|
|
|
|
QElapsedTimer time;
|
|
|
|
time.start();
|
|
|
|
|
|
|
|
// Frame 1: The header indicates the message length that follows
|
2020-09-09 09:28:07 +08:00
|
|
|
QByteArray header;
|
|
|
|
QDataStream headerStream(&header, QIODevice::WriteOnly);
|
2018-12-15 02:41:55 +08:00
|
|
|
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
2020-09-09 09:28:07 +08:00
|
|
|
headerStream.setVersion(QDataStream::Qt_5_6);
|
2018-12-15 02:41:55 +08:00
|
|
|
#endif
|
2021-05-31 22:14:39 +08:00
|
|
|
headerStream << static_cast <quint64>( msg.length() );
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2021-05-31 22:14:39 +08:00
|
|
|
if( ! writeConfirmedFrame( static_cast<int>(msecs - time.elapsed()), header ))
|
2021-05-31 21:23:18 +08:00
|
|
|
return false;
|
|
|
|
|
2021-05-31 22:14:39 +08:00
|
|
|
// Frame 2: The message
|
|
|
|
return writeConfirmedFrame( static_cast<int>(msecs - time.elapsed()), msg );
|
2021-05-31 21:23:18 +08:00
|
|
|
}
|
|
|
|
|
2021-05-31 22:14:39 +08:00
|
|
|
bool SingleApplicationPrivate::writeConfirmedFrame( int msecs, const QByteArray &msg )
|
2021-05-31 21:23:18 +08:00
|
|
|
{
|
|
|
|
socket->write( msg );
|
2020-09-09 09:28:07 +08:00
|
|
|
socket->flush();
|
2021-05-31 21:23:18 +08:00
|
|
|
|
|
|
|
bool result = socket->waitForReadyRead( msecs ); // await ack byte
|
|
|
|
if (result) {
|
|
|
|
socket->read( 1 );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
2020-10-16 02:41:00 +08:00
|
|
|
quint16 SingleApplicationPrivate::blockChecksum() const
|
2018-07-27 09:29:55 +08:00
|
|
|
{
|
2020-10-18 06:58:52 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
quint16 checksum = qChecksum(QByteArray(static_cast<const char*>(memory->constData()), offsetof(InstancesInfo, checksum)));
|
|
|
|
#else
|
|
|
|
quint16 checksum = qChecksum(static_cast<const char*>(memory->constData()), offsetof(InstancesInfo, checksum));
|
|
|
|
#endif
|
|
|
|
return checksum;
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
2020-10-16 02:41:00 +08:00
|
|
|
qint64 SingleApplicationPrivate::primaryPid() const
|
2018-07-27 09:29:55 +08:00
|
|
|
{
|
|
|
|
qint64 pid;
|
|
|
|
|
|
|
|
memory->lock();
|
2020-05-17 22:50:27 +08:00
|
|
|
auto *inst = static_cast<InstancesInfo*>( memory->data() );
|
2018-07-27 09:29:55 +08:00
|
|
|
pid = inst->primaryPid;
|
|
|
|
memory->unlock();
|
|
|
|
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2020-10-16 02:41:00 +08:00
|
|
|
QString SingleApplicationPrivate::primaryUser() const
|
2020-03-03 09:22:54 +08:00
|
|
|
{
|
|
|
|
QByteArray username;
|
|
|
|
|
|
|
|
memory->lock();
|
2020-05-17 22:50:27 +08:00
|
|
|
auto *inst = static_cast<InstancesInfo*>( memory->data() );
|
2020-03-03 09:22:54 +08:00
|
|
|
username = inst->primaryUser;
|
|
|
|
memory->unlock();
|
|
|
|
|
|
|
|
return QString::fromUtf8( username );
|
|
|
|
}
|
|
|
|
|
2018-07-27 09:29:55 +08:00
|
|
|
/**
|
|
|
|
* @brief Executed when a connection has been made to the LocalServer
|
|
|
|
*/
|
|
|
|
void SingleApplicationPrivate::slotConnectionEstablished()
|
|
|
|
{
|
|
|
|
QLocalSocket *nextConnSocket = server->nextPendingConnection();
|
2018-09-11 05:35:38 +08:00
|
|
|
connectionMap.insert(nextConnSocket, ConnectionInfo());
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2021-03-26 07:16:25 +08:00
|
|
|
QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose, this,
|
2020-09-09 07:28:02 +08:00
|
|
|
[nextConnSocket, this](){
|
2018-09-11 05:35:38 +08:00
|
|
|
auto &info = connectionMap[nextConnSocket];
|
2021-03-26 07:16:25 +08:00
|
|
|
this->slotClientConnectionClosed( nextConnSocket, info.instanceId );
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
2018-09-11 05:35:38 +08:00
|
|
|
);
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2020-12-23 09:49:50 +08:00
|
|
|
QObject::connect(nextConnSocket, &QLocalSocket::disconnected, nextConnSocket, &QLocalSocket::deleteLater);
|
|
|
|
|
2021-03-26 07:16:25 +08:00
|
|
|
QObject::connect(nextConnSocket, &QLocalSocket::destroyed, this,
|
2020-12-24 04:42:56 +08:00
|
|
|
[nextConnSocket, this](){
|
2018-09-11 05:35:38 +08:00
|
|
|
connectionMap.remove(nextConnSocket);
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-03-26 07:16:25 +08:00
|
|
|
QObject::connect(nextConnSocket, &QLocalSocket::readyRead, this,
|
2020-09-09 07:28:02 +08:00
|
|
|
[nextConnSocket, this](){
|
2018-09-11 05:35:38 +08:00
|
|
|
auto &info = connectionMap[nextConnSocket];
|
2020-09-09 07:28:02 +08:00
|
|
|
switch(info.stage){
|
2021-05-31 22:14:39 +08:00
|
|
|
case StageInitHeader:
|
|
|
|
readMessageHeader( nextConnSocket, StageInitBody );
|
2018-09-11 05:35:38 +08:00
|
|
|
break;
|
2021-05-31 22:14:39 +08:00
|
|
|
case StageInitBody:
|
2018-09-11 05:35:38 +08:00
|
|
|
readInitMessageBody(nextConnSocket);
|
|
|
|
break;
|
2021-05-31 22:14:39 +08:00
|
|
|
case StageConnectedHeader:
|
|
|
|
readMessageHeader( nextConnSocket, StageConnectedBody );
|
|
|
|
break;
|
|
|
|
case StageConnectedBody:
|
2021-03-26 07:16:25 +08:00
|
|
|
this->slotDataAvailable( nextConnSocket, info.instanceId );
|
2018-09-11 05:35:38 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
);
|
2018-09-11 05:35:38 +08:00
|
|
|
}
|
|
|
|
|
2021-05-31 22:14:39 +08:00
|
|
|
void SingleApplicationPrivate::readMessageHeader( QLocalSocket *sock, SingleApplicationPrivate::ConnectionStage nextStage )
|
2018-09-11 05:35:38 +08:00
|
|
|
{
|
2020-09-09 07:28:02 +08:00
|
|
|
if (!connectionMap.contains( sock )){
|
2018-09-11 05:35:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-09 07:28:02 +08:00
|
|
|
if( sock->bytesAvailable() < ( qint64 )sizeof( quint64 ) ){
|
2018-09-11 05:35:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDataStream headerStream( sock );
|
2018-12-15 02:41:55 +08:00
|
|
|
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
2018-09-11 05:35:38 +08:00
|
|
|
headerStream.setVersion( QDataStream::Qt_5_6 );
|
2018-12-15 02:41:55 +08:00
|
|
|
#endif
|
2018-09-11 05:35:38 +08:00
|
|
|
|
|
|
|
// Read the header to know the message length
|
|
|
|
quint64 msgLen = 0;
|
|
|
|
headerStream >> msgLen;
|
|
|
|
ConnectionInfo &info = connectionMap[sock];
|
2021-05-31 22:14:39 +08:00
|
|
|
info.stage = nextStage;
|
2018-09-11 05:35:38 +08:00
|
|
|
info.msgLen = msgLen;
|
|
|
|
|
2021-05-31 21:23:18 +08:00
|
|
|
writeAck( sock );
|
2018-09-11 05:35:38 +08:00
|
|
|
}
|
|
|
|
|
2021-05-31 22:14:39 +08:00
|
|
|
bool SingleApplicationPrivate::isFrameComplete( QLocalSocket *sock )
|
2018-09-11 05:35:38 +08:00
|
|
|
{
|
2020-09-09 07:28:02 +08:00
|
|
|
if (!connectionMap.contains( sock )){
|
2021-05-31 22:14:39 +08:00
|
|
|
return false;
|
2018-09-11 05:35:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionInfo &info = connectionMap[sock];
|
2020-09-09 07:28:02 +08:00
|
|
|
if( sock->bytesAvailable() < ( qint64 )info.msgLen ){
|
2021-05-31 22:14:39 +08:00
|
|
|
return false;
|
2018-09-11 05:35:38 +08:00
|
|
|
}
|
|
|
|
|
2021-05-31 22:14:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
|
|
|
{
|
|
|
|
Q_Q(SingleApplication);
|
|
|
|
|
|
|
|
if( !isFrameComplete( sock ) )
|
|
|
|
return;
|
|
|
|
|
2018-09-11 05:35:38 +08:00
|
|
|
// Read the message body
|
2021-05-31 22:14:39 +08:00
|
|
|
QByteArray msgBytes = sock->readAll();
|
2018-09-11 05:35:38 +08:00
|
|
|
QDataStream readStream(msgBytes);
|
2018-12-15 02:41:55 +08:00
|
|
|
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
2018-09-11 05:35:38 +08:00
|
|
|
readStream.setVersion( QDataStream::Qt_5_6 );
|
2018-12-15 02:41:55 +08:00
|
|
|
#endif
|
2018-09-11 05:35:38 +08:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2020-10-18 06:58:52 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
const quint16 actualChecksum = qChecksum(QByteArray(msgBytes, static_cast<quint32>(msgBytes.length() - sizeof(quint16))));
|
|
|
|
#else
|
|
|
|
const quint16 actualChecksum = qChecksum(msgBytes.constData(), static_cast<quint32>(msgBytes.length() - sizeof(quint16)));
|
|
|
|
#endif
|
2018-09-11 05:35:38 +08:00
|
|
|
|
|
|
|
bool isValid = readStream.status() == QDataStream::Ok &&
|
|
|
|
QLatin1String(latin1Name) == blockServerName &&
|
|
|
|
msgChecksum == actualChecksum;
|
|
|
|
|
2020-09-09 07:28:02 +08:00
|
|
|
if( !isValid ){
|
2018-09-11 05:35:38 +08:00
|
|
|
sock->close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-31 22:14:39 +08:00
|
|
|
ConnectionInfo &info = connectionMap[sock];
|
2018-09-11 05:35:38 +08:00
|
|
|
info.instanceId = instanceId;
|
2021-05-31 22:14:39 +08:00
|
|
|
info.stage = StageConnectedHeader;
|
2018-07-27 09:29:55 +08:00
|
|
|
|
2018-09-11 05:35:38 +08:00
|
|
|
if( connectionType == NewInstance ||
|
|
|
|
( connectionType == SecondaryInstance &&
|
|
|
|
options & SingleApplication::Mode::SecondaryNotification ) )
|
|
|
|
{
|
2018-07-27 09:29:55 +08:00
|
|
|
Q_EMIT q->instanceStarted();
|
|
|
|
}
|
|
|
|
|
2021-05-31 21:23:18 +08:00
|
|
|
writeAck( sock );
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SingleApplicationPrivate::slotDataAvailable( QLocalSocket *dataSocket, quint32 instanceId )
|
|
|
|
{
|
|
|
|
Q_Q(SingleApplication);
|
2021-05-31 22:14:39 +08:00
|
|
|
|
|
|
|
if ( !isFrameComplete( dataSocket ) )
|
|
|
|
return;
|
|
|
|
|
2018-07-27 09:29:55 +08:00
|
|
|
Q_EMIT q->receivedMessage( instanceId, dataSocket->readAll() );
|
2021-05-31 21:23:18 +08:00
|
|
|
|
|
|
|
writeAck( dataSocket );
|
2021-05-31 22:14:39 +08:00
|
|
|
|
|
|
|
ConnectionInfo &info = connectionMap[dataSocket];
|
|
|
|
info.stage = StageConnectedHeader;
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SingleApplicationPrivate::slotClientConnectionClosed( QLocalSocket *closedSocket, quint32 instanceId )
|
|
|
|
{
|
|
|
|
if( closedSocket->bytesAvailable() > 0 )
|
2021-03-26 07:16:25 +08:00
|
|
|
slotDataAvailable( closedSocket, instanceId );
|
2018-07-27 09:29:55 +08:00
|
|
|
}
|
2020-09-09 09:23:42 +08:00
|
|
|
|
|
|
|
void SingleApplicationPrivate::randomSleep()
|
|
|
|
{
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK( 5, 10, 0 )
|
|
|
|
QThread::msleep( QRandomGenerator::global()->bounded( 8u, 18u ));
|
|
|
|
#else
|
|
|
|
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
|
2021-04-10 19:37:33 +08:00
|
|
|
QThread::msleep( qrand() % 11 + 8);
|
2020-09-09 09:23:42 +08:00
|
|
|
#endif
|
|
|
|
}
|
2020-12-11 08:18:00 +08:00
|
|
|
|
|
|
|
void SingleApplicationPrivate::addAppData(const QString &data)
|
|
|
|
{
|
|
|
|
appDataList.push_back(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList SingleApplicationPrivate::appData() const
|
|
|
|
{
|
|
|
|
return appDataList;
|
|
|
|
}
|