Format singleapplication sources

This commit is contained in:
Jonas Kvinge
2020-10-04 02:18:10 +02:00
parent 51f2383a07
commit 28d0cc8795
8 changed files with 97 additions and 134 deletions

View File

@@ -59,7 +59,7 @@
* @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);
@@ -88,7 +88,7 @@ SingleApplication::SingleApplication(int &argc, char *argv[], bool allowSecondar
} }
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;
@@ -96,7 +96,7 @@ SingleApplication::SingleApplication(int &argc, char *argv[], bool allowSecondar
} }
} }
InstancesInfo* inst = static_cast<InstancesInfo*>(d->memory->data()); InstancesInfo *inst = static_cast<InstancesInfo *>(d->memory->data());
QElapsedTimer time; QElapsedTimer time;
time.start(); time.start();
@@ -118,7 +118,7 @@ SingleApplication::SingleApplication(int &argc, char *argv[], bool allowSecondar
QThread::sleep(QRandomGenerator::global()->bounded(8u, 18u)); QThread::sleep(QRandomGenerator::global()->bounded(8u, 18u));
#else #else
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));
#endif #endif
} }
@@ -148,7 +148,6 @@ SingleApplication::SingleApplication(int &argc, char *argv[], bool allowSecondar
delete d; delete d;
::exit(EXIT_SUCCESS); ::exit(EXIT_SUCCESS);
} }
/** /**
@@ -193,5 +192,4 @@ bool SingleApplication::sendMessage(QByteArray message, int timeout) {
bool dataWritten = d->socket->waitForBytesWritten(timeout); bool dataWritten = d->socket->waitForBytesWritten(timeout);
d->socket->flush(); d->socket->flush();
return dataWritten; return dataWritten;
} }

View File

@@ -51,7 +51,7 @@ class SingleApplication : public QApplication {
typedef QApplication app_t; typedef QApplication app_t;
public: public:
/** /**
* @brief Mode of operation of SingleApplication. * @brief Mode of operation of SingleApplication.
* Whether the block should be user-wide or system-wide and whether the * Whether the block should be user-wide or system-wide and whether the
@@ -63,11 +63,11 @@ public:
* @enum * @enum
*/ */
enum Mode { enum Mode {
User = 1 << 0, User = 1 << 0,
System = 1 << 1, System = 1 << 1,
SecondaryNotification = 1 << 2, SecondaryNotification = 1 << 2,
ExcludeAppVersion = 1 << 3, ExcludeAppVersion = 1 << 3,
ExcludeAppPath = 1 << 4 ExcludeAppPath = 1 << 4
}; };
Q_DECLARE_FLAGS(Options, Mode) Q_DECLARE_FLAGS(Options, Mode)
@@ -91,7 +91,7 @@ public:
* Usually 4*timeout would be the worst case (fail) scenario. * Usually 4*timeout would be the worst case (fail) scenario.
* @see See the corresponding QAPPLICATION_CLASS constructor for reference * @see See the corresponding QAPPLICATION_CLASS constructor for reference
*/ */
explicit SingleApplication( int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 1000 ); explicit SingleApplication(int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 1000);
~SingleApplication() override; ~SingleApplication() override;
/** /**
@@ -125,18 +125,17 @@ public:
* @note sendMessage() will return false if invoked from the primary * @note sendMessage() will return false if invoked from the primary
* instance. * instance.
*/ */
bool sendMessage( QByteArray message, int timeout = 1000 ); bool sendMessage(QByteArray message, int timeout = 1000);
signals: signals:
void instanceStarted(); void instanceStarted();
void receivedMessage( quint32 instanceId, QByteArray message ); void receivedMessage(quint32 instanceId, QByteArray message);
private: private:
SingleApplicationPrivate *d_ptr; SingleApplicationPrivate *d_ptr;
Q_DECLARE_PRIVATE(SingleApplication) Q_DECLARE_PRIVATE(SingleApplication)
}; };
Q_DECLARE_OPERATORS_FOR_FLAGS(SingleApplication::Options) Q_DECLARE_OPERATORS_FOR_FLAGS(SingleApplication::Options)
#endif // SINGLEAPPLICATION_H #endif // SINGLEAPPLICATION_H

View File

@@ -62,12 +62,11 @@
#endif #endif
SingleApplicationPrivate::SingleApplicationPrivate(SingleApplication *_q_ptr) SingleApplicationPrivate::SingleApplicationPrivate(SingleApplication *_q_ptr)
: q_ptr(_q_ptr), : q_ptr(_q_ptr),
memory(nullptr), memory(nullptr),
socket(nullptr), socket(nullptr),
server(nullptr), server(nullptr),
instanceNumber(-1) instanceNumber(-1) {}
{}
SingleApplicationPrivate::~SingleApplicationPrivate() { SingleApplicationPrivate::~SingleApplicationPrivate() {
@@ -77,7 +76,7 @@ SingleApplicationPrivate::~SingleApplicationPrivate() {
} }
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;
@@ -88,7 +87,6 @@ SingleApplicationPrivate::~SingleApplicationPrivate() {
memory->unlock(); memory->unlock();
delete memory; delete memory;
} }
void SingleApplicationPrivate::genBlockServerName() { void SingleApplicationPrivate::genBlockServerName() {
@@ -115,17 +113,17 @@ void SingleApplicationPrivate::genBlockServerName() {
if (options & SingleApplication::Mode::User) { if (options & SingleApplication::Mode::User) {
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
QByteArray username; QByteArray username;
#if defined(HAVE_GETEUID) && defined(HAVE_GETPWUID) # if defined(HAVE_GETEUID) && defined(HAVE_GETPWUID)
struct passwd *pw = getpwuid(geteuid()); struct passwd *pw = getpwuid(geteuid());
if (pw) { if (pw) {
username = pw->pw_name; username = pw->pw_name;
} }
#endif # endif
if (username.isEmpty()) username = qgetenv("USER"); if (username.isEmpty()) username = qgetenv("USER");
appData.addData(username); appData.addData(username);
#endif #endif
#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)) {
@@ -139,17 +137,15 @@ void SingleApplicationPrivate::genBlockServerName() {
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with server naming requirements. // Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with 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() {
@@ -174,14 +170,13 @@ void SingleApplicationPrivate::startPrimary() {
QObject::connect(server, &QLocalServer::newConnection, this, &SingleApplicationPrivate::slotConnectionEstablished); QObject::connect(server, &QLocalServer::newConnection, this, &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() {}
@@ -239,19 +234,17 @@ void SingleApplicationPrivate::connectToPrimary(const int msecs, const Connectio
socket->flush(); socket->flush();
socket->waitForBytesWritten(msecs); socket->waitForBytesWritten(msecs);
} }
} }
quint16 SingleApplicationPrivate::blockChecksum() { quint16 SingleApplicationPrivate::blockChecksum() {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
quint16 checksum = qChecksum(QByteArray(static_cast<const char*>(memory->constData()), offsetof(InstancesInfo, checksum))); quint16 checksum = qChecksum(QByteArray(static_cast<const char *>(memory->constData()), offsetof(InstancesInfo, checksum)));
#else #else
quint16 checksum = qChecksum(static_cast<const char*>(memory->constData()), offsetof(InstancesInfo, checksum)); quint16 checksum = qChecksum(static_cast<const char *>(memory->constData()), offsetof(InstancesInfo, checksum));
#endif #endif
return checksum; return checksum;
} }
qint64 SingleApplicationPrivate::primaryPid() { qint64 SingleApplicationPrivate::primaryPid() {
@@ -259,12 +252,11 @@ 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;
} }
/** /**
@@ -279,35 +271,31 @@ void SingleApplicationPrivate::slotConnectionEstablished() {
[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) {
@@ -334,7 +322,6 @@ void SingleApplicationPrivate::readInitMessageHeader(QLocalSocket *sock) {
if (sock->bytesAvailable() >= static_cast<qint64>(msgLen)) { if (sock->bytesAvailable() >= static_cast<qint64>(msgLen)) {
readInitMessageBody(sock); readInitMessageBody(sock);
} }
} }
void SingleApplicationPrivate::readInitMessageBody(QLocalSocket *sock) { void SingleApplicationPrivate::readInitMessageBody(QLocalSocket *sock) {
@@ -397,19 +384,16 @@ void SingleApplicationPrivate::readInitMessageBody(QLocalSocket *sock) {
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, const quint32 instanceId) { void SingleApplicationPrivate::slotDataAvailable(QLocalSocket *dataSocket, const 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, const quint32 instanceId) { void SingleApplicationPrivate::slotClientConnectionClosed(QLocalSocket *closedSocket, const quint32 instanceId) {
if (closedSocket->bytesAvailable() > 0) if (closedSocket->bytesAvailable() > 0)
Q_EMIT slotDataAvailable(closedSocket, instanceId); Q_EMIT slotDataAvailable(closedSocket, instanceId);
} }

View File

@@ -94,12 +94,12 @@ class SingleApplicationPrivate : public QObject {
quint32 instanceNumber; quint32 instanceNumber;
QString blockServerName; QString blockServerName;
SingleApplication::Options options; SingleApplication::Options options;
QMap<QLocalSocket*, ConnectionInfo> connectionMap; QMap<QLocalSocket *, ConnectionInfo> connectionMap;
public slots: public slots:
void slotConnectionEstablished(); void slotConnectionEstablished();
void slotDataAvailable(QLocalSocket*, const quint32); void slotDataAvailable(QLocalSocket *, const quint32);
void slotClientConnectionClosed(QLocalSocket*, const quint32); void slotClientConnectionClosed(QLocalSocket *, const quint32);
}; };
#endif // SINGLEAPPLICATION_P_H #endif // SINGLEAPPLICATION_P_H

View File

@@ -59,7 +59,7 @@
* @param {bool} allowSecondaryInstances * @param {bool} allowSecondaryInstances
*/ */
SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], bool allowSecondary, Options options, int timeout) SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], bool allowSecondary, Options options, int timeout)
: app_t(argc, argv), d_ptr(new SingleCoreApplicationPrivate(this)) { : app_t(argc, argv), d_ptr(new SingleCoreApplicationPrivate(this)) {
Q_D(SingleCoreApplication); Q_D(SingleCoreApplication);
@@ -96,7 +96,7 @@ SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], bool allow
} }
} }
InstancesInfo* inst = static_cast<InstancesInfo*>(d->memory->data()); InstancesInfo *inst = static_cast<InstancesInfo *>(d->memory->data());
QElapsedTimer time; QElapsedTimer time;
time.start(); time.start();
@@ -104,7 +104,7 @@ SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], bool allow
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() << "SingleCoreApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure."; qWarning() << "SingleCoreApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure.";
@@ -118,7 +118,7 @@ SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], bool allow
QThread::sleep(QRandomGenerator::global()->bounded(8u, 18u)); QThread::sleep(QRandomGenerator::global()->bounded(8u, 18u));
#else #else
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));
#endif #endif
} }
@@ -134,7 +134,7 @@ SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], bool allow
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, SingleCoreApplicationPrivate::SecondaryInstance); d->connectToPrimary(timeout, SingleCoreApplicationPrivate::SecondaryInstance);
} }
d->memory->unlock(); d->memory->unlock();
@@ -148,7 +148,6 @@ SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], bool allow
delete d; delete d;
::exit(EXIT_SUCCESS); ::exit(EXIT_SUCCESS);
} }
/** /**
@@ -184,7 +183,7 @@ bool SingleCoreApplication::sendMessage(QByteArray message, int timeout) {
Q_D(SingleCoreApplication); Q_D(SingleCoreApplication);
// 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, SingleCoreApplicationPrivate::Reconnect); d->connectToPrimary(timeout, SingleCoreApplicationPrivate::Reconnect);
@@ -193,5 +192,4 @@ bool SingleCoreApplication::sendMessage(QByteArray message, int timeout) {
bool dataWritten = d->socket->waitForBytesWritten(timeout); bool dataWritten = d->socket->waitForBytesWritten(timeout);
d->socket->flush(); d->socket->flush();
return dataWritten; return dataWritten;
} }

View File

@@ -51,7 +51,7 @@ class SingleCoreApplication : public QCoreApplication {
typedef QCoreApplication app_t; typedef QCoreApplication app_t;
public: public:
/** /**
* @brief Mode of operation of SingleCoreApplication. * @brief Mode of operation of SingleCoreApplication.
* Whether the block should be user-wide or system-wide and whether the * Whether the block should be user-wide or system-wide and whether the
@@ -63,11 +63,11 @@ public:
* @enum * @enum
*/ */
enum Mode { enum Mode {
User = 1 << 0, User = 1 << 0,
System = 1 << 1, System = 1 << 1,
SecondaryNotification = 1 << 2, SecondaryNotification = 1 << 2,
ExcludeAppVersion = 1 << 3, ExcludeAppVersion = 1 << 3,
ExcludeAppPath = 1 << 4 ExcludeAppPath = 1 << 4
}; };
Q_DECLARE_FLAGS(Options, Mode) Q_DECLARE_FLAGS(Options, Mode)
@@ -91,7 +91,7 @@ public:
* Usually 4*timeout would be the worst case (fail) scenario. * Usually 4*timeout would be the worst case (fail) scenario.
* @see See the corresponding QAPPLICATION_CLASS constructor for reference * @see See the corresponding QAPPLICATION_CLASS constructor for reference
*/ */
explicit SingleCoreApplication( int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 1000 ); explicit SingleCoreApplication(int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 1000);
~SingleCoreApplication() override; ~SingleCoreApplication() override;
/** /**
@@ -125,17 +125,17 @@ public:
* @note sendMessage() will return false if invoked from the primary * @note sendMessage() will return false if invoked from the primary
* instance. * instance.
*/ */
bool sendMessage( QByteArray message, int timeout = 1000 ); bool sendMessage(QByteArray message, int timeout = 1000);
signals: signals:
void instanceStarted(); void instanceStarted();
void receivedMessage( quint32 instanceId, QByteArray message ); void receivedMessage(quint32 instanceId, QByteArray message);
private: private:
SingleCoreApplicationPrivate *d_ptr; SingleCoreApplicationPrivate *d_ptr;
Q_DECLARE_PRIVATE(SingleCoreApplication) Q_DECLARE_PRIVATE(SingleCoreApplication)
}; };
Q_DECLARE_OPERATORS_FOR_FLAGS(SingleCoreApplication::Options) Q_DECLARE_OPERATORS_FOR_FLAGS(SingleCoreApplication::Options)
#endif // SINGLECOREAPPLICATION_H #endif // SINGLECOREAPPLICATION_H

View File

@@ -62,12 +62,11 @@
#endif #endif
SingleCoreApplicationPrivate::SingleCoreApplicationPrivate(SingleCoreApplication *_q_ptr) SingleCoreApplicationPrivate::SingleCoreApplicationPrivate(SingleCoreApplication *_q_ptr)
: q_ptr(_q_ptr), : q_ptr(_q_ptr),
memory(nullptr), memory(nullptr),
socket(nullptr), socket(nullptr),
server(nullptr), server(nullptr),
instanceNumber(-1) instanceNumber(-1) {}
{}
SingleCoreApplicationPrivate::~SingleCoreApplicationPrivate() { SingleCoreApplicationPrivate::~SingleCoreApplicationPrivate() {
@@ -77,7 +76,7 @@ SingleCoreApplicationPrivate::~SingleCoreApplicationPrivate() {
} }
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;
@@ -88,7 +87,6 @@ SingleCoreApplicationPrivate::~SingleCoreApplicationPrivate() {
memory->unlock(); memory->unlock();
delete memory; delete memory;
} }
void SingleCoreApplicationPrivate::genBlockServerName() { void SingleCoreApplicationPrivate::genBlockServerName() {
@@ -115,17 +113,17 @@ void SingleCoreApplicationPrivate::genBlockServerName() {
if (options & SingleCoreApplication::Mode::User) { if (options & SingleCoreApplication::Mode::User) {
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
QByteArray username; QByteArray username;
#if defined(HAVE_GETEUID) && defined(HAVE_GETPWUID) # if defined(HAVE_GETEUID) && defined(HAVE_GETPWUID)
struct passwd *pw = getpwuid(geteuid()); struct passwd *pw = getpwuid(geteuid());
if (pw) { if (pw) {
username = pw->pw_name; username = pw->pw_name;
} }
#endif # endif
if (username.isEmpty()) username = qgetenv("USER"); if (username.isEmpty()) username = qgetenv("USER");
appData.addData(username); appData.addData(username);
#endif #endif
#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)) {
@@ -139,17 +137,15 @@ void SingleCoreApplicationPrivate::genBlockServerName() {
// Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with server naming requirements. // Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with server naming requirements.
blockServerName = appData.result().toBase64().replace("/", "_"); blockServerName = appData.result().toBase64().replace("/", "_");
} }
void SingleCoreApplicationPrivate::initializeMemoryBlock() { void SingleCoreApplicationPrivate::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 SingleCoreApplicationPrivate::startPrimary() { void SingleCoreApplicationPrivate::startPrimary() {
@@ -174,14 +170,13 @@ void SingleCoreApplicationPrivate::startPrimary() {
QObject::connect(server, &QLocalServer::newConnection, this, &SingleCoreApplicationPrivate::slotConnectionEstablished); QObject::connect(server, &QLocalServer::newConnection, this, &SingleCoreApplicationPrivate::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 SingleCoreApplicationPrivate::startSecondary() {} void SingleCoreApplicationPrivate::startSecondary() {}
@@ -239,19 +234,17 @@ void SingleCoreApplicationPrivate::connectToPrimary(const int msecs, const Conne
socket->flush(); socket->flush();
socket->waitForBytesWritten(msecs); socket->waitForBytesWritten(msecs);
} }
} }
quint16 SingleCoreApplicationPrivate::blockChecksum() { quint16 SingleCoreApplicationPrivate::blockChecksum() {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
quint16 checksum = qChecksum(QByteArray(static_cast<const char*>(memory->constData()), offsetof(InstancesInfo, checksum))); quint16 checksum = qChecksum(QByteArray(static_cast<const char *>(memory->constData()), offsetof(InstancesInfo, checksum)));
#else #else
quint16 checksum = qChecksum(static_cast<const char*>(memory->constData()), offsetof(InstancesInfo, checksum)); quint16 checksum = qChecksum(static_cast<const char *>(memory->constData()), offsetof(InstancesInfo, checksum));
#endif #endif
return checksum; return checksum;
} }
qint64 SingleCoreApplicationPrivate::primaryPid() { qint64 SingleCoreApplicationPrivate::primaryPid() {
@@ -259,12 +252,11 @@ qint64 SingleCoreApplicationPrivate::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;
} }
/** /**
@@ -279,35 +271,31 @@ void SingleCoreApplicationPrivate::slotConnectionEstablished() {
[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 SingleCoreApplicationPrivate::readInitMessageHeader(QLocalSocket *sock) { void SingleCoreApplicationPrivate::readInitMessageHeader(QLocalSocket *sock) {
@@ -334,7 +322,6 @@ void SingleCoreApplicationPrivate::readInitMessageHeader(QLocalSocket *sock) {
if (sock->bytesAvailable() >= static_cast<qint64>(msgLen)) { if (sock->bytesAvailable() >= static_cast<qint64>(msgLen)) {
readInitMessageBody(sock); readInitMessageBody(sock);
} }
} }
void SingleCoreApplicationPrivate::readInitMessageBody(QLocalSocket *sock) { void SingleCoreApplicationPrivate::readInitMessageBody(QLocalSocket *sock) {
@@ -397,19 +384,16 @@ void SingleCoreApplicationPrivate::readInitMessageBody(QLocalSocket *sock) {
if (sock->bytesAvailable() > 0) { if (sock->bytesAvailable() > 0) {
Q_EMIT this->slotDataAvailable(sock, instanceId); Q_EMIT this->slotDataAvailable(sock, instanceId);
} }
} }
void SingleCoreApplicationPrivate::slotDataAvailable(QLocalSocket *dataSocket, const quint32 instanceId) { void SingleCoreApplicationPrivate::slotDataAvailable(QLocalSocket *dataSocket, const quint32 instanceId) {
Q_Q(SingleCoreApplication); Q_Q(SingleCoreApplication);
Q_EMIT q->receivedMessage(instanceId, dataSocket->readAll()); Q_EMIT q->receivedMessage(instanceId, dataSocket->readAll());
} }
void SingleCoreApplicationPrivate::slotClientConnectionClosed(QLocalSocket *closedSocket, const quint32 instanceId) { void SingleCoreApplicationPrivate::slotClientConnectionClosed(QLocalSocket *closedSocket, const quint32 instanceId) {
if (closedSocket->bytesAvailable() > 0) if (closedSocket->bytesAvailable() > 0)
Q_EMIT slotDataAvailable(closedSocket, instanceId); Q_EMIT slotDataAvailable(closedSocket, instanceId);
} }

View File

@@ -94,12 +94,12 @@ class SingleCoreApplicationPrivate : public QObject {
quint32 instanceNumber; quint32 instanceNumber;
QString blockServerName; QString blockServerName;
SingleCoreApplication::Options options; SingleCoreApplication::Options options;
QMap<QLocalSocket*, ConnectionInfo> connectionMap; QMap<QLocalSocket *, ConnectionInfo> connectionMap;
public slots: public slots:
void slotConnectionEstablished(); void slotConnectionEstablished();
void slotDataAvailable(QLocalSocket*, const quint32); void slotDataAvailable(QLocalSocket *, const quint32);
void slotClientConnectionClosed(QLocalSocket*, const quint32); void slotClientConnectionClosed(QLocalSocket *, const quint32);
}; };
#endif // SINGLECOREAPPLICATION_P_H #endif // SINGLECOREAPPLICATION_P_H