Update SingleApplication and pugixml to latest upstream release

This commit is contained in:
Laurent Trinques
2022-04-27 13:52:41 +02:00
parent 1c99cb5c2d
commit 1e255af3be
10 changed files with 14753 additions and 14647 deletions

View File

@@ -3,6 +3,31 @@ Changelog
If by accident I have forgotten to credit someone in the CHANGELOG, email me and I will fix it.
__3.3.4__
---------
* Fix compilation under Qt 6.2+ and stricter Qt compile settings. - _Christoph Cullmann_
__3.3.3__
---------
* Support for Qt 6.3+ - Fixed deprecated `QCryptographicHash::addData()` that will only support `QByteArrayView` going further. - _Moody Liu_
__3.3.2__
---------
* Fixed crash caused by sending a `writeAck` on a removed connection. - _Nicolas Werner_
__3.3.1__
---------
* Added support for _AppImage_ dynamic executable paths. - _Michael Klein_
__3.3.0__
---------
* Fixed message fragmentation issue causing crashes and incorrectly / inconsistently received messages. - _Nils Jeisecke_
__3.2.0__
---------

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.7.0)
cmake_minimum_required(VERSION 3.12.0)
project(SingleApplication LANGUAGES CXX)
@@ -38,3 +38,13 @@ endif()
target_compile_definitions(${PROJECT_NAME} PUBLIC QAPPLICATION_CLASS=${QAPPLICATION_CLASS})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_definitions(${PROJECT_NAME} PRIVATE
QT_NO_CAST_TO_ASCII
QT_NO_CAST_FROM_ASCII
QT_NO_URL_CAST_FROM_STRING
QT_NO_CAST_FROM_BYTEARRAY
QT_USE_QSTRINGBUILDER
QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
QT_NO_KEYWORDS
QT_NO_FOREACH
)

View File

@@ -35,7 +35,7 @@ To include the library files I would recommend that you add it as a git
submodule to your project. Here is how:
```bash
git submodule add git@github.com:itay-grudev/SingleApplication.git singleapplication
git submodule add https://github.com/itay-grudev/SingleApplication.git singleapplication
```
**Qmake:**
@@ -182,7 +182,8 @@ bool SingleApplication::sendMessage( QByteArray message, int timeout = 100 )
```
Sends `message` to the Primary Instance. Uses `timeout` as a the maximum timeout
in milliseconds for blocking functions
in milliseconds for blocking functions. Returns `true` if the message has been sent
successfully. If the message can't be sent or the function timeouts - returns `false`.
---

View File

@@ -2,7 +2,6 @@
int main(int argc, char *argv[])
{
// Allow secondary instances
SingleApplication app( argc, argv );
qWarning() << "Started a new instance";

View File

@@ -248,10 +248,7 @@ bool SingleApplication::sendMessage( const QByteArray &message, int timeout )
if( ! d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect ) )
return false;
d->socket->write( message );
bool dataWritten = d->socket->waitForBytesWritten( timeout );
d->socket->flush();
return dataWritten;
return d->writeConfirmedMessage( timeout, message );
}
/**

View File

@@ -131,21 +131,34 @@ QString SingleApplicationPrivate::getUsername()
void SingleApplicationPrivate::genBlockServerName()
{
QCryptographicHash appData( QCryptographicHash::Sha256 );
#if QT_VERSION < QT_VERSION_CHECK(6, 3, 0)
appData.addData( "SingleApplication", 17 );
#else
appData.addData( QByteArrayView{"SingleApplication"} );
#endif
appData.addData( SingleApplication::app_t::applicationName().toUtf8() );
appData.addData( SingleApplication::app_t::organizationName().toUtf8() );
appData.addData( SingleApplication::app_t::organizationDomain().toUtf8() );
if ( ! appDataList.isEmpty() )
appData.addData( appDataList.join( "" ).toUtf8() );
appData.addData( appDataList.join(QString()).toUtf8() );
if( ! (options & SingleApplication::Mode::ExcludeAppVersion) ){
appData.addData( SingleApplication::app_t::applicationVersion().toUtf8() );
}
if( ! (options & SingleApplication::Mode::ExcludeAppPath) ){
#ifdef Q_OS_WIN
#if defined(Q_OS_WIN)
appData.addData( SingleApplication::app_t::applicationFilePath().toLower().toUtf8() );
#elif defined(Q_OS_LINUX)
// If the application is running as an AppImage then the APPIMAGE env var should be used
// instead of applicationPath() as each instance is launched with its own executable path
const QByteArray appImagePath = qgetenv( "APPIMAGE" );
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 );
};
#else
appData.addData( SingleApplication::app_t::applicationFilePath().toUtf8() );
#endif
@@ -158,7 +171,7 @@ void SingleApplicationPrivate::genBlockServerName()
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with
// server naming requirements.
blockServerName = appData.result().toBase64().replace("/", "_");
blockServerName = QString::fromUtf8(appData.result().toBase64().replace("/", "_"));
}
void SingleApplicationPrivate::initializeMemoryBlock() const
@@ -257,26 +270,52 @@ bool SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType conne
writeStream << static_cast<quint8>(connectionType);
writeStream << instanceNumber;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
quint16 checksum = qChecksum(QByteArray(initMsg, static_cast<quint32>(initMsg.length())));
quint16 checksum = qChecksum(QByteArray(initMsg.constData(), static_cast<quint32>(initMsg.length())));
#else
quint16 checksum = qChecksum(initMsg.constData(), static_cast<quint32>(initMsg.length()));
#endif
writeStream << checksum;
// The header indicates the message length that follows
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
QByteArray header;
QDataStream headerStream(&header, QIODevice::WriteOnly);
#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>( msg.length() );
socket->write( header );
socket->write( initMsg );
bool result = socket->waitForBytesWritten( static_cast<int>(msecs - time.elapsed()) );
if( ! writeConfirmedFrame( static_cast<int>(msecs - time.elapsed()), header ))
return false;
// Frame 2: The message
return writeConfirmedFrame( static_cast<int>(msecs - time.elapsed()), msg );
}
bool SingleApplicationPrivate::writeConfirmedFrame( int msecs, const QByteArray &msg )
{
socket->write( msg );
socket->flush();
return result;
bool result = socket->waitForReadyRead( msecs ); // await ack byte
if (result) {
socket->read( 1 );
return true;
}
return false;
}
quint16 SingleApplicationPrivate::blockChecksum() const
@@ -321,33 +360,36 @@ void SingleApplicationPrivate::slotConnectionEstablished()
QLocalSocket *nextConnSocket = server->nextPendingConnection();
connectionMap.insert(nextConnSocket, ConnectionInfo());
QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose,
QObject::connect(nextConnSocket, &QLocalSocket::aboutToClose, this,
[nextConnSocket, this](){
auto &info = connectionMap[nextConnSocket];
Q_EMIT this->slotClientConnectionClosed( nextConnSocket, info.instanceId );
this->slotClientConnectionClosed( nextConnSocket, info.instanceId );
}
);
QObject::connect(nextConnSocket, &QLocalSocket::disconnected, nextConnSocket, &QLocalSocket::deleteLater);
QObject::connect(nextConnSocket, &QLocalSocket::destroyed,
QObject::connect(nextConnSocket, &QLocalSocket::destroyed, this,
[nextConnSocket, this](){
connectionMap.remove(nextConnSocket);
}
);
QObject::connect(nextConnSocket, &QLocalSocket::readyRead,
QObject::connect(nextConnSocket, &QLocalSocket::readyRead, this,
[nextConnSocket, this](){
auto &info = connectionMap[nextConnSocket];
switch(info.stage){
case StageHeader:
readInitMessageHeader(nextConnSocket);
case StageInitHeader:
readMessageHeader( nextConnSocket, StageInitBody );
break;
case StageBody:
case StageInitBody:
readInitMessageBody(nextConnSocket);
break;
case StageConnected:
Q_EMIT this->slotDataAvailable( nextConnSocket, info.instanceId );
case StageConnectedHeader:
readMessageHeader( nextConnSocket, StageConnectedBody );
break;
case StageConnectedBody:
this->slotDataAvailable( nextConnSocket, info.instanceId );
break;
default:
break;
@@ -356,7 +398,7 @@ void SingleApplicationPrivate::slotConnectionEstablished()
);
}
void SingleApplicationPrivate::readInitMessageHeader( QLocalSocket *sock )
void SingleApplicationPrivate::readMessageHeader( QLocalSocket *sock, SingleApplicationPrivate::ConnectionStage nextStage )
{
if (!connectionMap.contains( sock )){
return;
@@ -376,29 +418,35 @@ void SingleApplicationPrivate::readInitMessageHeader( QLocalSocket *sock )
quint64 msgLen = 0;
headerStream >> msgLen;
ConnectionInfo &info = connectionMap[sock];
info.stage = StageBody;
info.stage = nextStage;
info.msgLen = msgLen;
if ( sock->bytesAvailable() >= (qint64) msgLen ){
readInitMessageBody( sock );
writeAck( sock );
}
bool SingleApplicationPrivate::isFrameComplete( QLocalSocket *sock )
{
if (!connectionMap.contains( sock )){
return false;
}
ConnectionInfo &info = connectionMap[sock];
if( sock->bytesAvailable() < ( qint64 )info.msgLen ){
return false;
}
return true;
}
void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
{
Q_Q(SingleApplication);
if (!connectionMap.contains( sock )){
if( !isFrameComplete( sock ) )
return;
}
ConnectionInfo &info = connectionMap[sock];
if( sock->bytesAvailable() < ( qint64 )info.msgLen ){
return;
}
// Read the message body
QByteArray msgBytes = sock->read(info.msgLen);
QByteArray msgBytes = sock->readAll();
QDataStream readStream(msgBytes);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
@@ -424,7 +472,7 @@ void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
readStream >> msgChecksum;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
const quint16 actualChecksum = qChecksum(QByteArray(msgBytes, static_cast<quint32>(msgBytes.length() - sizeof(quint16))));
const quint16 actualChecksum = qChecksum(QByteArray(msgBytes.constData(), static_cast<quint32>(msgBytes.length() - sizeof(quint16))));
#else
const quint16 actualChecksum = qChecksum(msgBytes.constData(), static_cast<quint32>(msgBytes.length() - sizeof(quint16)));
#endif
@@ -438,8 +486,9 @@ void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
return;
}
ConnectionInfo &info = connectionMap[sock];
info.instanceId = instanceId;
info.stage = StageConnected;
info.stage = StageConnectedHeader;
if( connectionType == NewInstance ||
( connectionType == SecondaryInstance &&
@@ -448,21 +497,30 @@ void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
Q_EMIT q->instanceStarted();
}
if (sock->bytesAvailable() > 0){
Q_EMIT this->slotDataAvailable( sock, instanceId );
}
writeAck( sock );
}
void SingleApplicationPrivate::slotDataAvailable( QLocalSocket *dataSocket, quint32 instanceId )
{
Q_Q(SingleApplication);
Q_EMIT q->receivedMessage( instanceId, dataSocket->readAll() );
if ( !isFrameComplete( dataSocket ) )
return;
const QByteArray message = dataSocket->readAll();
writeAck( dataSocket );
ConnectionInfo &info = connectionMap[dataSocket];
info.stage = StageConnectedHeader;
Q_EMIT q->receivedMessage( instanceId, message);
}
void SingleApplicationPrivate::slotClientConnectionClosed( QLocalSocket *closedSocket, quint32 instanceId )
{
if( closedSocket->bytesAvailable() > 0 )
Q_EMIT slotDataAvailable( closedSocket, instanceId );
slotDataAvailable( closedSocket, instanceId );
}
void SingleApplicationPrivate::randomSleep()
@@ -471,7 +529,7 @@ void SingleApplicationPrivate::randomSleep()
QThread::msleep( QRandomGenerator::global()->bounded( 8u, 18u ));
#else
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
QThread::msleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 ));
QThread::msleep( qrand() % 11 + 8);
#endif
}

View File

@@ -61,9 +61,10 @@ public:
Reconnect = 3
};
enum ConnectionStage : quint8 {
StageHeader = 0,
StageBody = 1,
StageConnected = 2,
StageInitHeader = 0,
StageInitBody = 1,
StageConnectedHeader = 2,
StageConnectedBody = 3,
};
Q_DECLARE_PUBLIC(SingleApplication)
@@ -79,8 +80,12 @@ public:
quint16 blockChecksum() const;
qint64 primaryPid() const;
QString primaryUser() const;
void readInitMessageHeader(QLocalSocket *socket);
bool isFrameComplete(QLocalSocket *sock);
void readMessageHeader(QLocalSocket *socket, ConnectionStage nextStage);
void readInitMessageBody(QLocalSocket *socket);
void writeAck(QLocalSocket *sock);
bool writeConfirmedFrame(int msecs, const QByteArray &msg);
bool writeConfirmedMessage(int msecs, const QByteArray &msg);
static void randomSleep();
void addAppData(const QString &data);
QStringList appData() const;

View File

@@ -1,7 +1,7 @@
/**
* pugixml parser - version 1.11
* pugixml parser - version 1.12
* --------------------------------------------------------
* Copyright (C) 2006-2020, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
* Copyright (C) 2006-2022, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
* Report bugs and download new versions at https://pugixml.org/
*
* This library is distributed under the MIT License. See notice at the end
@@ -52,7 +52,7 @@
#endif
/**
* Copyright (c) 2006-2020 Arseny Kapoulkine
* Copyright (c) 2006-2022 Arseny Kapoulkine
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation

View File

@@ -1,7 +1,7 @@
/**
* pugixml parser - version 1.11
* pugixml parser - version 1.12
* --------------------------------------------------------
* Copyright (C) 2006-2020, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
* Copyright (C) 2006-2022, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
* Report bugs and download new versions at https://pugixml.org/
*
* This library is distributed under the MIT License. See notice at the end
@@ -132,8 +132,10 @@ using std::memset;
#endif
// In some environments MSVC is a compiler but the CRT lacks certain MSVC-specific features
#if defined(_MSC_VER) && !defined(__S3E__)
#if defined(_MSC_VER) && !defined(__S3E__) && !defined(_WIN32_WCE)
# define PUGI__MSVC_CRT_VERSION _MSC_VER
#elif defined(_WIN32_WCE)
# define PUGI__MSVC_CRT_VERSION 1310 // MSVC7.1
#endif
// Not all platforms have snprintf; we define a wrapper that uses snprintf if possible. This only works with buffers with a known size.
@@ -526,7 +528,8 @@ PUGI__NS_BEGIN
xml_memory_page* page = xml_memory_page::construct(memory);
assert(page);
page->allocator = _root->allocator;
assert(this == _root->allocator);
page->allocator = this;
return page;
}
@@ -4732,7 +4735,7 @@ PUGI__NS_BEGIN
// we need to get length of entire file to load it in memory; the only (relatively) sane way to do it is via seek/tell trick
PUGI__FN xml_parse_status get_file_size(FILE* file, size_t& out_result)
{
#if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400 && !defined(_WIN32_WCE)
#if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400
// there are 64-bit versions of fseek/ftell, let's use them
typedef __int64 length_type;
@@ -6735,7 +6738,7 @@ namespace pugi
return const_cast<xml_node*>(&_wrap); // BCC5 workaround
}
PUGI__FN const xml_node_iterator& xml_node_iterator::operator++()
PUGI__FN xml_node_iterator& xml_node_iterator::operator++()
{
assert(_wrap._root);
_wrap._root = _wrap._root->next_sibling;
@@ -6749,7 +6752,7 @@ namespace pugi
return temp;
}
PUGI__FN const xml_node_iterator& xml_node_iterator::operator--()
PUGI__FN xml_node_iterator& xml_node_iterator::operator--()
{
_wrap = _wrap._root ? _wrap.previous_sibling() : _parent.last_child();
return *this;
@@ -6796,7 +6799,7 @@ namespace pugi
return const_cast<xml_attribute*>(&_wrap); // BCC5 workaround
}
PUGI__FN const xml_attribute_iterator& xml_attribute_iterator::operator++()
PUGI__FN xml_attribute_iterator& xml_attribute_iterator::operator++()
{
assert(_wrap._attr);
_wrap._attr = _wrap._attr->next_attribute;
@@ -6810,7 +6813,7 @@ namespace pugi
return temp;
}
PUGI__FN const xml_attribute_iterator& xml_attribute_iterator::operator--()
PUGI__FN xml_attribute_iterator& xml_attribute_iterator::operator--()
{
_wrap = _wrap._attr ? _wrap.previous_attribute() : _parent.last_attribute();
return *this;
@@ -6857,7 +6860,7 @@ namespace pugi
return const_cast<xml_node*>(&_wrap); // BCC5 workaround
}
PUGI__FN const xml_named_node_iterator& xml_named_node_iterator::operator++()
PUGI__FN xml_named_node_iterator& xml_named_node_iterator::operator++()
{
assert(_wrap._root);
_wrap = _wrap.next_sibling(_name);
@@ -6871,7 +6874,7 @@ namespace pugi
return temp;
}
PUGI__FN const xml_named_node_iterator& xml_named_node_iterator::operator--()
PUGI__FN xml_named_node_iterator& xml_named_node_iterator::operator--()
{
if (_wrap._root)
_wrap = _wrap.previous_sibling(_name);
@@ -7091,8 +7094,12 @@ namespace pugi
#endif
// move allocation state
// note that other->_root may point to the embedded document page, in which case we should keep original (empty) state
if (other->_root != PUGI__GETPAGE(other))
{
doc->_root = other->_root;
doc->_busy_size = other->_busy_size;
}
// move buffer state
doc->buffer = other->buffer;
@@ -8265,7 +8272,7 @@ PUGI__NS_BEGIN
}
// gets mantissa digits in the form of 0.xxxxx with 0. implied and the exponent
#if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400 && !defined(_WIN32_WCE)
#if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400
PUGI__FN void convert_number_to_mantissa_exponent(double value, char (&buffer)[32], char** out_mantissa, int* out_exponent)
{
// get base values
@@ -11822,15 +11829,17 @@ PUGI__NS_BEGIN
lexeme_t l = _lexer.current();
_lexer.next();
if (++_depth > xpath_ast_depth_limit)
return error_rec();
if (l == lex_double_slash)
{
n = alloc_node(ast_step, n, axis_descendant_or_self, nodetest_type_node, 0);
if (!n) return 0;
++_depth;
}
if (++_depth > xpath_ast_depth_limit)
return error_rec();
n = parse_step(n);
if (!n) return 0;
}
@@ -12995,7 +13004,7 @@ namespace pugi
#endif
/**
* Copyright (c) 2006-2020 Arseny Kapoulkine
* Copyright (c) 2006-2022 Arseny Kapoulkine
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation

View File

@@ -1,7 +1,7 @@
/**
* pugixml parser - version 1.11
* pugixml parser - version 1.12
* --------------------------------------------------------
* Copyright (C) 2006-2020, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
* Copyright (C) 2006-2022, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
* Report bugs and download new versions at https://pugixml.org/
*
* This library is distributed under the MIT License. See notice at the end
@@ -11,10 +11,10 @@
* Copyright (C) 2003, by Kristen Wegner (kristen@tima.net)
*/
#ifndef PUGIXML_VERSION
// Define version macro; evaluates to major * 1000 + minor * 10 + patch so that it's safe to use in less-than comparisons
// Note: pugixml used major * 100 + minor * 10 + patch format up until 1.9 (which had version identifier 190); starting from pugixml 1.10, the minor version number is two digits
# define PUGIXML_VERSION 1110
#ifndef PUGIXML_VERSION
# define PUGIXML_VERSION 1120 // 1.12
#endif
// Include user configuration file (this can define various configuration macros)
@@ -312,6 +312,8 @@ namespace pugi
It begin() const { return _begin; }
It end() const { return _end; }
bool empty() const { return _begin == _end; }
private:
It _begin, _end;
};
@@ -851,10 +853,10 @@ namespace pugi
xml_node& operator*() const;
xml_node* operator->() const;
const xml_node_iterator& operator++();
xml_node_iterator& operator++();
xml_node_iterator operator++(int);
const xml_node_iterator& operator--();
xml_node_iterator& operator--();
xml_node_iterator operator--(int);
};
@@ -893,10 +895,10 @@ namespace pugi
xml_attribute& operator*() const;
xml_attribute* operator->() const;
const xml_attribute_iterator& operator++();
xml_attribute_iterator& operator++();
xml_attribute_iterator operator++(int);
const xml_attribute_iterator& operator--();
xml_attribute_iterator& operator--();
xml_attribute_iterator operator--(int);
};
@@ -929,10 +931,10 @@ namespace pugi
xml_node& operator*() const;
xml_node* operator->() const;
const xml_named_node_iterator& operator++();
xml_named_node_iterator& operator++();
xml_named_node_iterator operator++(int);
const xml_named_node_iterator& operator--();
xml_named_node_iterator& operator--();
xml_named_node_iterator operator--(int);
private:
@@ -1474,7 +1476,7 @@ namespace std
#endif
/**
* Copyright (c) 2006-2020 Arseny Kapoulkine
* Copyright (c) 2006-2022 Arseny Kapoulkine
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation